U
    -e                     @   s   d Z ddlmZ ddlZddlZddlmZmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZ ddlmZmZ dd	lmZ ejd
fddZe
G dd de	ZdS )zExtra magics for terminal use.    )errorN)TryNext
UsageError)Magicsmagics_class
line_magic)ClipboardEmpty)skip_doctest)SListstrip_email_quotes)	py3compatFc                 c   s`   |st d|   d}nd}z ||}|| kr2W dS |V  W q tk
rX   t d Y dS X qdS )zH Yield pasted lines until the user enters the given sentinel value.
    zAPasting code; enter '%s' alone on the line to stop or use Ctrl-D.: Nz<EOF>)printEOFError)sentinelZl_inputquietpromptl r   X/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/IPython/terminal/magics.pyget_pasted_lines   s    
r   c                       s|   e Zd Z fddZdddZdd Zdd	d
ZedddZe	edddZ
edddZejdkrtedd Z  ZS )TerminalMagicsc                    s   t t| | d S )N)superr   __init__)selfshell	__class__r   r   r   )   s    zTerminalMagics.__init__Fc                 C   sf   |r&t | | jj|< td|  n<| |}|| jjd< d| j_z| j|| W 5 d| j_X dS )zM Execute a block, or store it in a variable, per the user's request.
        zBlock assigned to '%s'pasted_blockTFN)r
   
splitlinesr   user_nsr   preclean_inputZusing_paste_magicsrun_cell)r   blocknamestore_historybr   r   r   store_or_execute,   s    
zTerminalMagics.store_or_executec                 C   s4   |  }|r&|d  s&|dd  }qtd|S )Nr      
)r    stripr   join)r   r$   linesr   r   r   r"   <   s    zTerminalMagics.preclean_inputr   c                 C   s`   | j j|}|dkrtdt|ts0tdtd|ddd t|f  | j 	| dS )z, Rerun a previously pasted command.
        Nz"No previous pasted block availablez6Variable 'pasted_block' is not a string, can't executezRe-executing '%s...' (%d chars)r*   r)   r   )
r   r!   getr   
isinstancestrr   splitlenr#   )r   r%   r'   r   r   r   rerun_pastedB   s    
 zTerminalMagics.rerun_pastedr   c                 C   s$   | j   tdddg| j j  dS )z%Toggle autoindent on/off (deprecated)zAutomatic indentation is:ZOFFONN)r   Zset_autoindentr   
autoindent)r   parameter_sr   r   r   r5   Q   s    
zTerminalMagics.autoindentc                 C   sb   | j |ddd\}}d|kr(|   dS d|k}|dd}d	t||d
}| j||dd dS )u  Paste & execute a pre-formatted code block from clipboard.

        You must terminate the block with '--' (two minus-signs) or Ctrl-D
        alone on the line. You can also provide your own sentinel with '%paste
        -s %%' ('%%' is the new sentinel for this operation).

        The block is dedented prior to execution to enable execution of method
        definitions. '>' and '+' characters at the beginning of a line are
        ignored, to allow pasting directly from e-mails, diff files and
        doctests (the '...' continuation prompt is also stripped).  The
        executed block is also assigned to variable named 'pasted_block' for
        later editing with '%edit pasted_block'.

        You can also pass a variable name as an argument, e.g. '%cpaste foo'.
        This assigns the pasted block to variable 'foo' as string, without
        dedenting or executing it (preceding >>> and + is still stripped)

        '%cpaste -r' re-executes the block previously entered by cpaste.
        '%cpaste -q' suppresses any additional output messages.

        Do not be alarmed by garbled output on Windows (it's a readline bug).
        Just press enter and type -- (and press enter again) and the block
        will be what was just pasted.

        Shell escapes are not supported (yet).

        See Also
        --------
        paste : automatically pull code from clipboard.

        Examples
        --------
        ::

          In [8]: %cpaste
          Pasting code; enter '--' alone on the line to stop.
          :>>> a = ["world!", "Hello"]
          :>>> print(" ".join(sorted(a)))
          :--
          Hello world!

        ::
          In [8]: %cpaste
          Pasting code; enter '--' alone on the line to stop.
          :>>> %alias_magic t timeit
          :>>> %t -n1 pass
          :--
          Created `%t` as an alias for `%timeit`.
          Created `%%t` as an alias for `%%timeit`.
          354 ns ± 224 ns per loop (mean ± std. dev. of 7 runs, 1 loop each)
        zrqs:stringmoderNqsz--r*   )r   Tr&   )parse_optionsr3   r.   r,   r   r(   )r   r6   optsr%   r   r   r$   r   r   r   cpasteW   s    6zTerminalMagics.cpastec              
   C   s  | j |ddd\}}d|kr(|   dS z| jj }W nv tk
r } z.t|d}|rft|d  ntd W Y dS d}~X Y n, tk
r } zt	d	|W 5 d}~X Y nX d
|krt
j| j| |dst
jd t
jd | j||dd dS )a  Paste & execute a pre-formatted code block from clipboard.

        The text is pulled directly from the clipboard without user
        intervention and printed back on the screen before execution (unless
        the -q flag is given to force quiet mode).

        The block is dedented prior to execution to enable execution of method
        definitions. '>' and '+' characters at the beginning of a line are
        ignored, to allow pasting directly from e-mails, diff files and
        doctests (the '...' continuation prompt is also stripped).  The
        executed block is also assigned to variable named 'pasted_block' for
        later editing with '%edit pasted_block'.

        You can also pass a variable name as an argument, e.g. '%paste foo'.
        This assigns the pasted block to variable 'foo' as string, without
        executing it (preceding >>> and + is still stripped).

        Options:

          -r: re-executes the block previously entered by cpaste.

          -q: quiet mode: do not echo the pasted text back to the terminal.

        IPython statements (magics, shell escapes) are not supported (yet).

        See Also
        --------
        cpaste : manually paste code into terminal until you mark its end.
        Zrqr7   r8   r:   Nargsr   z&Could not get text from the clipboard.z!The clipboard appears to be emptyr;   r*   z## -- End pasted text --
Tr=   )r>   r3   r   hooksZclipboard_getr   getattrr   r   r   sysstdoutwriteZ
pycolorizeendswithr(   )r   r6   r?   r%   r$   Zclipboard_excmessageer   r   r   paste   s(    

zTerminalMagics.pastewin32c                 C   s   t d dS )zClear screen.
            clsN)ossystem)r   r<   r   r   r   rL      s    zTerminalMagics.cls)F)r   )r   )r   )r   )__name__
__module____qualname__r   r(   r"   r3   r   r5   r	   r@   rJ   rD   platformrL   __classcell__r   r   r   r   r   '   s   

?8
r   )__doc__loggingr   rM   rD   ZIPython.core.errorr   r   ZIPython.core.magicr   r   r   ZIPython.lib.clipboardr   ZIPython.testing.skipdoctestr	   ZIPython.utils.textr
   r   ZIPython.utilsr   inputr   r   r   r   r   r   <module>   s   