
    fF                        d Z ddlZddlZddlZddlZddlmZmZmZ ddlm	Z	 ddl
mZ dZdZdZd	Zd
ZdZdZdZdZeeeeeeeeeg	Z G d dej,                        Z G d de      Z G d de      Z G d de      Z G d de      Zej8                  d        Zd Zd Zd Z d Z!d Z"d  Z#d! Z$d" Z%eeee ee!ee!ee"ee#ee$ee%iZ&ej8                  d#        Z' ejP                  d$      Z) ejP                  d%ejT                        Z+ e,       Z- e,       Z.d& Z/d' Z0d( Z1ej8                  d)        Z2ej8                  d6d*       Z3d7d+Z4ej8                  d,        Z5ej8                  d-        Z6ej8                  d.        Z7d/Z8 ejP                  d0js                  e8      ejT                        Z:d1Z;ej8                  d2        Z< ejP                  d3js                  e8      ejT                        Z=d4Z>ej8                  d5        Z?y)8a,  DEPRECATED: Input transformer classes to support IPython special syntax.

This module was deprecated in IPython 7.0, in favour of inputtransformer2.

This includes the machinery to recognise and transform ``%magic`` commands,
``!system`` commands, ``help?`` querying, prompt stripping, and so forth.
    N)generate_tokens
untokenize
TokenError)StringIO)LineInfo!z!!???%z%%,;/c                   h    e Zd ZdZej
                  d        Zej
                  d        Zed        Z	y)InputTransformerz6Abstract base class for line-based input transformers.c                      y)a1  Send a line of input to the transformer, returning the transformed
        input or None if the transformer is waiting for more input.

        Must be overridden by subclasses.

        Implementations may raise ``SyntaxError`` if the input is invalid. No
        other exceptions may be raised.
        N selflines     X/var/www/cvtools/html/venv/lib/python3.12/site-packages/IPython/core/inputtransformer.pypushzInputTransformer.push-   s     	    c                      y)zReturn, transformed any lines that the transformer has accumulated,
        and reset its internal state.

        Must be overridden by subclasses.
        Nr   r   s    r   resetzInputTransformer.reset9   s     	r   c                 F     t        j                         fd       }|S )zCan be used by subclasses as a decorator, to return a factory that
        will allow instantiation with the decorated object.
        c                       fi | S Nr   )kwargsclsfuncs    r   transformer_factoryz2InputTransformer.wrap.<locals>.transformer_factoryG   s    t&v&&r   )	functoolswraps)r    r!   r"   s   `` r   wrapzInputTransformer.wrapB   s(    
 
		' 
	' #"r   N)
__name__
__module____qualname____doc__abcabstractmethodr   r   classmethodr%   r   r   r   r   r   *   sK    @	 	 	  # #r   r   )	metaclassc                   (    e Zd ZdZd Zd Zd Zd Zy)StatelessInputTransformerzDWrapper for a stateless input transformer implemented as a function.c                     || _         y r   r!   r   r!   s     r   __init__z"StatelessInputTransformer.__init__O   s	    	r   c                 8    dj                  | j                        S )Nz%StatelessInputTransformer(func={0!r}))formatr!   r   s    r   __repr__z"StatelessInputTransformer.__repr__R       6==diiHHr   c                 $    | j                  |      S )zQSend a line of input to the transformer, returning the
        transformed input.r1   r   s     r   r   zStatelessInputTransformer.pushU   s     yyr   c                      y)z!No-op - exists for compatibility.Nr   r   s    r   r   zStatelessInputTransformer.resetZ   s    r   Nr&   r'   r(   r)   r3   r6   r   r   r   r   r   r/   r/   M   s    NI
r   r/   c                   (    e Zd ZdZd Zd Zd Zd Zy)CoroutineInputTransformerz<Wrapper for an input transformer implemented as a coroutine.c                 H     |di || _         t        | j                          y )Nr   )coronext)r   r>   r   s      r   r3   z"CoroutineInputTransformer.__init__`   s    N6N	TYYr   c                 8    dj                  | j                        S )Nz%CoroutineInputTransformer(coro={0!r}))r5   r>   r   s    r   r6   z"CoroutineInputTransformer.__repr__e   r7   r   c                 8    | j                   j                  |      S )zSend a line of input to the transformer, returning the
        transformed input or None if the transformer is waiting for more
        input.
        r>   sendr   s     r   r   zCoroutineInputTransformer.pushh   s    
 yy~~d##r   c                 8    | j                   j                  d      S )zrReturn, transformed any lines that the transformer has
        accumulated, and reset its internal state.
        NrB   r   s    r   r   zCoroutineInputTransformer.reseto   s     yy~~d##r   Nr:   r   r   r   r<   r<   ^   s    F
I$$r   r<   c                   .    e Zd ZdZd Zd Zd Zd Zd Zy)TokenInputTransformerzWrapper for a token-based input transformer.
    
    func should accept a list of tokens (5-tuples, see tokenize docs), and
    return an iterable which can be passed to tokenize.untokenize().
    c                 @    || _         g | _        | j                          y r   )r!   bufreset_tokenizerr2   s     r   r3   zTokenInputTransformer.__init__{   s    	r   c                 b    t        | j                        }t        |j                        | _        y r   )iterrH   r   __next__	tokenizer)r   its     r   rI   z%TokenInputTransformer.reset_tokenizer   s    $((^(5r   c                    | j                   j                  |dz          t        d | j                   D              r| j                         S g }d}	 | j                  D ]X  }|j                  |       |d   }|t
        j                  k(  s|r|t
        j                  k(  r n|t
        j                  k(  sWd}Z | j                  |      S # t        $ r | j                          Y y w xY w)N
c              3   <   K   | ]  }|j                           y wr   )isspace).0ls     r   	<genexpr>z-TokenInputTransformer.push.<locals>.<genexpr>   s     -qqyy{-s   Fr   T)rH   appendallr   rM   tokenizeNEWLINENL
ERRORTOKENr   rI   output)r   r   tokens
stop_at_NLintokts         r   r   zTokenInputTransformer.push   s    t$-DHH--::<
	 &e$!H(((ZA<L(---!%J& {{6""  	  "	s   A"C 3C C$#C$c                     | j                   j                          | j                          t        | j	                  |            j                  d      S )NrP   )rH   clearrI   r   r!   rstripr   r]   s     r   r\   zTokenInputTransformer.output   s;    $))F+,33D99r   c                     dj                  | j                        }| j                  j                          | j                          |r|j	                  d      S y )N rP   )joinrH   rb   rI   rc   )r   rT   s     r   r   zTokenInputTransformer.reset   sD    GGDHH88D>! r   N)	r&   r'   r(   r)   r3   rI   r   r\   r   r   r   r   rF   rF   u   s     

6#.:
"r   rF   c                   $     e Zd Z fdZd Z xZS )assemble_python_linesc                 ,    t         t        |   d        y r   )superri   r3   )r   	__class__s    r   r3   zassemble_python_lines.__init__   s    #T3D9r   c                 "    | j                         S r   )r   rd   s     r   r\   zassemble_python_lines.output   s    zz|r   )r&   r'   r(   r3   r\   __classcell__)rl   s   @r   ri   ri      s    :r   ri   c               #      K   d} 	 | } | r| j                         rg }| J| j                  d      r$t        |       s|j                  | dd        d} n|j                  |        n| Jdj	                  |      } ww)z4Join lines following explicit line continuations (\)rf   N\)rR   endswithhas_commentrV   rg   )r   partss     r   assemble_logical_linesru      s      D

t||~}}T"K,=T#2Y'"
T"  wwu~ s   A(A=+A=c                     |dk(  rdnd| v rdnd}dj                  || g      }|j                  d      \  }}}|j                  t              }|d|d|d	S )
zUPrepares a pinfo(2)/psearch call from a target name and the escape
    (i.e. ? or ??)r
   pinfo2*psearchpinfo zget_ipython().run_line_magic(, ))rg   	partitionlstrip	ESC_MAGIC)targetesclspacemethodargt_magic_name_t_magic_arg_ss           r   _make_help_callr      sk     +h"%-Y  ((FF#
$C%(]]3%7"L!]&&y1L r   c                     | j                   j                         j                  t              }| j                  d|dS )zTranslate lines escaped with: !zget_ipython().system(r}   )r   r   	ESC_SHELLpre	line_infocmds     r   
_tr_systemr      s.    
..


!
(
(
3C+4==#>>r   c                 `    | j                   j                         dd }| j                  d|dS )z Translate lines escaped with: !!   Nzget_ipython().getoutput(r}   )r   r   r   r   s     r   _tr_system2r      s*    
..


!!"
%C.7mmSAAr   c                 x    | j                   dd syt        | j                  | j                  | j                        S )z"Translate lines escaped with: ?/??   Nzget_ipython().show_usage())r   r   ifunr   r   r   s    r   _tr_helpr      s0     >>!"+9>>9==)--HHr   c                 >   d}| j                   j                  t              r| j                   S dj                  | j                  | j
                  g      j                         }|j                  d      \  }}}|j                  t              }|| j                  ||fz  S )zTranslate lines escaped with: %z&%sget_ipython().run_line_magic(%r, %r)r{   )r   
startswith
ESC_MAGIC2rg   r   the_reststripr~   r   r   r   )r   tplr   r   r   r   s         r   	_tr_magicr      s    
2C~~  ,~~
((INNI$6$67
8
>
>
@C%(]]3%7"L!]&&y1L)--}===r   c                     | j                   | j                  ddj                  | j                  j	                               dS )zTranslate lines escaped with: ,("z", "")r   r   rg   r   splitr   s    r   	_tr_quoter      s6    $==)..Y%7%7%=%=%?@C Cr   c                 R    | j                   | j                  d| j                  dS )zTranslate lines escaped with: ;r   r   )r   r   r   r   s    r   
_tr_quote2r      s"    $==)..$--/ /r   c                     | j                   | j                  ddj                  | j                  j	                               dS )zTranslate lines escaped with: /(r|   r}   r   r   s    r   	_tr_parenr      s6    "	9#5#5#;#;#=>@ @r   c                     | r| j                         r| S t        |       }|j                  t        vr| S t        |j                     |      S )zMTransform escaped commands - %magic, !system, ?help + various autocalls.
    )rR   r   r   tr)r   lineinfs     r   escaped_commandsr     sA     4<<>tnG{{"gkk?7##r   z\s*a  (%{0,2}
                              (?!\d)[\w*]+            # Variable name
                              (\.(?!\d)[\w*]+)*       # .etc.etc
                              )
                              (\?\??)$                # ? or ??
                              c                 6   t        |       j                  }t               }	 t        |      D ]  }|j	                  |d           	 |S # t
        $ rG}d|j                  d   v r|j	                  t               n|j	                  t               Y d}~|S d}~ww xY w)z5Helper for has_comment and ends_in_comment_or_string.r   zmulti-line stringN)	r   readlinesetr   addr   args_MULTILINE_STRING_MULTILINE_STRUCTURE)r   r   toktypesr`   es        r   _line_tokensr   %  s    ~&&HuH/ * 	ALL1	 O  /!&&)+LL*+LL-.O/s   $A 	B<BBc                 8    t         j                  t        |       v S )a=  Indicate whether an input line has (i.e. ends in, or is) a comment.

    This uses tokenize, so it can distinguish comments from # inside strings.

    Parameters
    ----------
    src : string
        A single line input string.

    Returns
    -------
    comment : bool
        True if source has a comment.
    )rX   COMMENTr   )srcs    r   rs   rs   4  s     S 112r   c                 P    t        |       }t        j                  |v xs t        |v S )a  Indicates whether or not an input line ends in a comment or within
    a multiline string.

    Parameters
    ----------
    src : string
        A single line input string.

    Returns
    -------
    comment : bool
        True if source ends in a comment or multiline string.
    )r   rX   r   r   )r   r   s     r   ends_in_comment_or_stringr   E  s*     C H(L.?8.KLr   c                     t         j                  |       }|t        |       r| S |j                  d      }|j                  d      }t        j                  |       j                  d      }t        |||      S )z$Translate lines with ?/?? at the endr      r   )_help_end_researchr   group_initial_space_rematchr   )r   mr   r   r   s        r   help_endr   W  sj     	D!Ay-d3WWQZF
''!*C$$T*003F63//r   c              #     K   d}t        j                  d      }d}	 |}|s|}|s|j                  t              s
||}|-|j	                  |      r?|}g }d}|C|j                         dk7  s| s.|j                  |       d}||j                         dk7  r+| s.|j                  d      \  }}}|j                  t              }|||dj                  |      fz  }ͭw)zCaptures & transforms cell magics.

    After a cell magic is started, this stores up any lines it gets until it is
    reset (sent None).
    z(get_ipython().run_cell_magic(%r, %r, %r)z%%\w+\?rf   Nr{   rP   )
recompiler   r   r   r   rV   r~   r   rg   )end_on_blank_liner   cellmagic_help_rer   firstbody
magic_namer   s           r   	cellmagicr   d  s     5C

:.D

JD  z*""
 """4(
"&**,""4=NKKJD "&**,""4=N
  %s3
Au&&z2
j%D)9::7 s   'C*C*AC*%C*(AC*c              #   *  K   || }d}	 |}||j                  d|d      \  }}|r|s|j                  |      r
||}|>|}|E| j                  d|d      \  }}|}|s|r|#| j                  d|d      }|n	||}|w)a&  Remove matching input prompts from a block of input.

    Parameters
    ----------
    prompt_re : regular expression
        A regular expression matching any input prompt (including continuation)
    initial_re : regular expression, optional
        A regular expression matching only the initial prompt, but not continuation.
        If no initial expression is given, prompt_re will be used everywhere.
        Used mainly for plain Python prompts, where the continuation prompt
        ``...`` is a valid Python expression in Python 3, so shouldn't be stripped.

    Notes
    -----
    If `initial_re` and `prompt_re differ`,
    only `initial_re` will be tested against the first line.
    If any prompt is found on the first two lines,
    prompts will be stripped from the rest of the block.
    rf   r   )count)subnr   sub)	prompt_re
initial_re
turnoff_rer   outn1n2s          r   _strip_promptsr     s     ( 
D

 <//"d!/4Rb% &"&JD &	< ..T.3R	 "'mmBAm>> "
 ""
 "C s   ABAB	BBc                      t        j                  d      } t        j                  d      }t        j                  d      }t        | ||      S )z:Strip the >>>/... prompts of the Python interactive shell.z^(>>>|\.\.\.)( |$)z	^>>>( |$)z^[%!]r   r   r   )r   r   r   s      r   classic_promptr     s>     

01IL)JH%J)Z<<r   c                  p    t        j                  d      } t        j                  d      }t        | |      S )z%Strip IPython's In [1]:/...: prompts.z^(In \[\d+\]: |\s*\.{3,}: ?)z^%%)r   r   )r   r   s     r   
ipy_promptr     s.     

:;IF#J)
;;r   c               #      K   t        j                  d      } d}	 |}|| j                  |      }|r:|j                  d      }|0|j	                  |      r|t        |      d }|}|&n	||}|^w)zRemove leading indentation.

    If the first line starts with a spaces or tabs, the same whitespace will be
    removed from each following line until it is reset.
    z^[ \t]+rf   Nr   )r   r   r   r   r   len)space_rer   r   spaces       r   leading_indentr     s      zz*%HD

<NN4 GGAJE"??5)E
,D"
 " ""
 " s   A+A9.	A98A9z(?P<lhs>(\s*)
    ([\w\.]+)                # Initial identifier
    (\s*,\s*
        \*?[\w\.]+)*         # Further identifiers for unpacking
    \s*?,?                   # Trailing comma
    )
    \s*=\s*
z{}!\s*(?P<cmd>.*)z %s = get_ipython().getoutput(%r)c                 f    t         j                  |       }|| S t        |j                  dd      z  S )z<Transform assignment from system commands (e.g. files = !ls)lhsr   )assign_system_rer   assign_system_templater   )r   r   s     r   assign_from_systemr     s4     	t$Ay!AGGE5$999r   z{}%\s*(?P<cmd>.*)z)%s = get_ipython().run_line_magic(%r, %r)c                     t         j                  |       }|| S |j                  dd      \  }}|j                  d      \  }}}|j	                  t
              }t        |||fz  S )z;Transform assignment from magic commands (e.g. a = %who_ls)r   r   r{   )assign_magic_rer   r   r~   r   r   assign_magic_template)r   r   m_lhsm_cmdr   r   r   s          r   assign_from_magicr     si     	d#Ay775%(LE5%*__S%9"L!]&&y1L E<#GGGr   )F)NN)@r)   r*   r#   r   rX   r   r   r   ior   IPython.core.splitinputr   r   
ESC_SH_CAPESC_HELP	ESC_HELP2r   r   	ESC_QUOTE
ESC_QUOTE2	ESC_PARENESC_SEQUENCESABCMetar   r/   r<   rF   ri   r%   ru   r   r   r   r   r   r   r   r   r   r   r   r   VERBOSEr   objectr   r   r   rs   r   r   r   r   r   r   r   _assign_patr5   r   r   r   r   r   r   r   r   r   <module>r      s     	  < <  , 	
		
	
	JIzJ	4
!# !#F 0 "$ 0 $.0", 0"d1    *$?
B
I	>C
/
@
 JKHHIIJI  	$  	$ BJJv& rzz " !jj* H x 3"M$ 	0  	0 $;  $;N9$v =  = <  < $  $8  2::299+F

S ; :  : "**188ErzzRC 	H  	Hr   