o
    û›h  ã                   @   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rW dS |V  W n ty-   t dƒ Y dS w q)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.ú:Ú TNz<EOF>)ÚprintÚEOFError)ÚsentinelZl_inputÚquietÚpromptÚl© r   úMc:\Users\shubh\Videos\agent\nova\Lib\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r>edd„ ƒZ‡  ZS ‡  ZS )ÚTerminalMagicsc                    s   t t| ƒ |¡ d S )N)Úsuperr   Ú__init__)ÚselfÚshell©Ú	__class__r   r   r   )   s   zTerminalMagics.__init__Fc                 C   sn   |rt | ¡ ƒ| jj|< td| ƒ dS |  |¡}|| jjd< d| j_z| j ||¡ W d| j_dS d| j_w )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   sB   |  ¡ }|r|d  ¡ s|dd … }|r|d  ¡ rtd |¡ƒS )Nr   é   Ú
)r    Ústripr   Újoin)r   r$   Úlinesr   r   r   r"   <   s
   ÿzTerminalMagics.preclean_inputr   c                 C   s`   | j j |¡}|du rtdƒ‚t|tƒst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|v r|  ¡  dS d|v }| 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|v r|  ¡  dS z| jj ¡ }W n5 tyA } zt|dƒ}|r2t|d ƒ ntdƒ W Y d}~dS d}~w tyQ } zt	d	ƒ|‚d}~ww d
|vrqt
j | j |¡¡ | d¡skt
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   )Ú__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    