a
    ÂóFbnG  ã                   @   sB  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„ dejdZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZejdd„ ƒZdLdd„Zdd„ Zd d!„ Z d"d#„ Z!d$d%„ Z"d&d'„ Z#d(d)„ Z$d*d+„ Z%eeee ee!ee!ee"ee#ee$ee%iZ&ejd,d-„ ƒZ'e (d.¡Z)e (d/ej*¡Z+e,ƒ Z-e,ƒ Z.d0d1„ Z/d2d3„ Z0d4d5„ Z1ejd6d7„ ƒZ2ejdMd9d:„ƒZ3dNd;d<„Z4ejd=d>„ ƒZ5ejd?d@„ ƒZ6ejdAdB„ ƒZ7dCZ8e (dD 9e8¡ej*¡Z:dEZ;ejdFdG„ ƒZ<e (dH 9e8¡ej*¡Z=dIZ>ejdJdK„ ƒZ?dS )Oa,  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                   @   s8   e Zd ZdZejdd„ ƒZejdd„ ƒZedd„ ƒZ	dS )	ÚInputTransformerz6Abstract base class for line-based input transformers.c                 C   s   dS )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Úliner   r   ú<lib/python3.9/site-packages/IPython/core/inputtransformer.pyÚpush-   s    
zInputTransformer.pushc                 C   s   dS )zReturn, transformed any lines that the transformer has accumulated,
        and reset its internal state.

        Must be overridden by subclasses.
        Nr   ©r   r   r   r   Úreset9   s    zInputTransformer.resetc                    s   t  ˆ¡‡ ‡fdd„ƒ}|S )z‡Can be used by subclasses as a decorator, to return a factory that
        will allow instantiation with the decorated object.
        c                     s   ˆ ˆfi | ¤ŽS ©Nr   )Úkwargs©ÚclsÚfuncr   r   Útransformer_factoryG   s    z2InputTransformer.wrap.<locals>.transformer_factory)Ú	functoolsÚwraps)r   r   r   r   r   r   ÚwrapB   s    zInputTransformer.wrapN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚabcÚabstractmethodr   r   Úclassmethodr   r   r   r   r   r   *   s   

r   )Ú	metaclassc                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚStatelessInputTransformerzDWrapper for a stateless input transformer implemented as a function.c                 C   s
   || _ d S r   ©r   ©r   r   r   r   r   Ú__init__O   s    z"StatelessInputTransformer.__init__c                 C   s   d  | j¡S )Nz%StatelessInputTransformer(func={0!r}))Úformatr   r   r   r   r   Ú__repr__R   s    z"StatelessInputTransformer.__repr__c                 C   s
   |   |¡S )zQSend a line of input to the transformer, returning the
        transformed input.r)   r   r   r   r   r   U   s    zStatelessInputTransformer.pushc                 C   s   dS )z!No-op - exists for compatibility.Nr   r   r   r   r   r   Z   s    zStatelessInputTransformer.resetN©r    r!   r"   r#   r+   r-   r   r   r   r   r   r   r(   M   s
   r(   c                   @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚCoroutineInputTransformerz<Wrapper for an input transformer implemented as a coroutine.c                 K   s   |f i |¤Ž| _ t| j ƒ d S r   )ÚcoroÚnext)r   r0   r   r   r   r   r+   `   s    z"CoroutineInputTransformer.__init__c                 C   s   d  | j¡S )Nz%CoroutineInputTransformer(coro={0!r}))r,   r0   r   r   r   r   r-   e   s    z"CoroutineInputTransformer.__repr__c                 C   s   | j  |¡S )z—Send a line of input to the transformer, returning the
        transformed input or None if the transformer is waiting for more
        input.
        ©r0   Úsendr   r   r   r   r   h   s    zCoroutineInputTransformer.pushc                 C   s   | j  d¡S )zrReturn, transformed any lines that the transformer has
        accumulated, and reset its internal state.
        Nr2   r   r   r   r   r   o   s    zCoroutineInputTransformer.resetNr.   r   r   r   r   r/   ^   s
   r/   c                   @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚTokenInputTransformerzÆWrapper 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                 C   s   || _ g | _|  ¡  d S r   )r   ÚbufÚreset_tokenizerr*   r   r   r   r+   {   s    zTokenInputTransformer.__init__c                 C   s   t | jƒ}t|jƒ| _d S r   )Úiterr5   r   Ú__next__Ú	tokenizer)r   Úitr   r   r   r6   €   s    
z%TokenInputTransformer.reset_tokenizerc                 C   sª   | j  |d ¡ tdd„ | j D ƒƒr,|  ¡ S g }d}zN| jD ]B}| |¡ |d }|tjksj|rp|tjkrp q€q<|tjkr<d}q<W n t	yž   |  
¡  Y d S 0 |  |¡S )NÚ
c                 s   s   | ]}|  ¡ V  qd S r   )Úisspace)Ú.0Úlr   r   r   Ú	<genexpr>†   ó    z-TokenInputTransformer.push.<locals>.<genexpr>Fr   T)r5   ÚappendÚallr   r9   ÚtokenizeÚNEWLINEÚNLÚ
ERRORTOKENr   r6   Úoutput)r   r   ÚtokensZ
stop_at_NLZintokÚtr   r   r   r   „   s"    



zTokenInputTransformer.pushc                 C   s&   | j  ¡  |  ¡  t|  |¡ƒ d¡S )Nr;   )r5   Úclearr6   r   r   Úrstrip©r   rH   r   r   r   rG   ›   s    
zTokenInputTransformer.outputc                 C   s0   d  | j¡}| j ¡  |  ¡  |r,| d¡S d S )NÚ r;   )Újoinr5   rJ   r6   rK   )r   r>   r   r   r   r       s
    
zTokenInputTransformer.resetN)	r    r!   r"   r#   r+   r6   r   rG   r   r   r   r   r   r4   u   s   r4   c                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )Úassemble_python_linesc                    s   t t| ƒ d ¡ d S r   )ÚsuperrO   r+   r   ©Ú	__class__r   r   r+   ¨   s    zassemble_python_lines.__init__c                 C   s   |   ¡ S r   )r   rL   r   r   r   rG   «   s    zassemble_python_lines.output)r    r!   r"   r+   rG   Ú__classcell__r   r   rQ   r   rO   §   s   rO   c                  c   sn   d} | V } | r|   ¡ rqg }| dur^|  d¡rPt| ƒsP| | dd… ¡ dV } q| | ¡ q^qd |¡} qdS )z4Join lines following explicit line continuations (\)rM   Nú\éÿÿÿÿ)r<   ÚendswithÚhas_commentrA   rN   )r   Úpartsr   r   r   Úassemble_logical_lines®   s    
rY   c           	      C   sn   |dkrdnd| v rdnd}d  || g¡}| d¡\}}}| t¡}|du rZd|||f S d	||||f S dS )
zUPrepares a pinfo(2)/psearch call from a target name and the escape
    (i.e. ? or ??)r	   Zpinfo2Ú*ZpsearchZpinfoú Nú&%sget_ipython().run_line_magic(%r, %r)zG%sget_ipython().set_next_input(%r);get_ipython().run_line_magic(%r, %r))rN   Ú	partitionÚlstripÚ	ESC_MAGIC)	ÚtargetÚescÚlspaceÚ
next_inputÚmethodÚargÚt_magic_nameÚ_Út_magic_arg_sr   r   r   Ú_make_help_callÄ   s    þ

ÿri   c                 C   s   | j  ¡  t¡}d| j|f S )zTranslate lines escaped with: !z%sget_ipython().system(%r))r   r^   Ú	ESC_SHELLÚpre©Ú	line_infoÚcmdr   r   r   Ú
_tr_systemÕ   s    ro   c                 C   s    | j  ¡ dd… }d| j|f S )z Translate lines escaped with: !!é   Nz%sget_ipython().getoutput(%r))r   r^   rk   rl   r   r   r   Ú_tr_system2Ú   s    rq   c                 C   s$   | j dd… sdS t| j| j| jƒS )z"Translate lines escaped with: ?/??é   Nzget_ipython().show_usage())r   ri   Úifunra   rk   ©rm   r   r   r   Ú_tr_helpß   s    ru   c                 C   sV   d}| j  t¡r| j S d | j| jg¡ ¡ }| d¡\}}}| t	¡}|| j
||f S )zTranslate lines escaped with: %r\   r[   )r   Ú
startswithÚ
ESC_MAGIC2rN   rs   Úthe_restÚstripr]   r^   r_   rk   )rm   Útplrn   rf   rg   rh   r   r   r   Ú	_tr_magicç   s    
r{   c                 C   s   d| j | jd | j ¡ ¡f S )zTranslate lines escaped with: ,ú
%s%s("%s")z", "©rk   rs   rN   rx   Úsplitrt   r   r   r   Ú	_tr_quoteò   s    
ÿr   c                 C   s   d| j | j| jf S )zTranslate lines escaped with: ;r|   )rk   rs   rx   rt   r   r   r   Ú
_tr_quote2÷   s    
ÿr€   c                 C   s   d| j | jd | j ¡ ¡f S )zTranslate lines escaped with: /z%s%s(%s)z, r}   rt   r   r   r   Ú	_tr_parenü   s    
ÿr   c                 C   s4   | r|   ¡ r| S t| ƒ}|jtvr&| S t|j |ƒS )zMTransform escaped commands - %magic, !system, ?help + various autocalls.
    )r<   r   ra   Útr)r   Zlineinfr   r   r   Úescaped_commands
  s    
rƒ   z\s*a  (%{0,2}
                              (?!\d)[\w*]+            # Variable name
                              (\.(?!\d)[\w*]+)*       # .etc.etc
                              )
                              (\?\??)$                # ? or ??
                              c              
   C   s~   t | ƒj}tƒ }z t|ƒD ]}| |d ¡ qW nH tyx } z0d|jd v rZ| t¡ n
| t¡ W Y d}~n
d}~0 0 |S )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„   ÚtoktypesrI   Úer   r   r   Ú_line_tokens$  s    
 rŒ   c                 C   s   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.
    )rC   ÚCOMMENTrŒ   )Úsrcr   r   r   rW   3  s    rW   c                 C   s   t | ƒ}tj|v p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Œ   rC   r   rˆ   )rŽ   rŠ   r   r   r   Úends_in_comment_or_stringD  s    r   c                 C   sp   t  | ¡}|du st| ƒr| S | d¡}| d¡}t | ¡ d¡}|  ¡ | d¡kr^|  d¡nd}t||||ƒS )z$Translate lines with ?/?? at the endNrr   é   r   r   )	Ú_help_end_reÚsearchr   ÚgroupÚ_initial_space_reÚmatchry   rK   ri   )r   Úmr`   ra   rb   rc   r   r   r   Úhelp_endV  s    


 r—   Fc                 c   s¸   d}t  d¡}d}|V }|s$|V }q| t¡s@|dur|V }q.q| |¡rLq|}g }dV }|dur„| ¡ dksr| s„| |¡ dV }qZ| d¡\}}}| t¡}|||d 	|¡f }qdS )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+\?rM   Nr[   r;   )
ÚreÚcompilerv   rw   r•   ry   rA   r]   r^   rN   )Zend_on_blank_linerz   Zcellmagic_help_rer   ÚfirstÚbodyZ
magic_namerg   r   r   r   Ú	cellmagicf  s2    



ÿÿ

rœ   c                 c   s¾   |du r| }d}|V }|du r q|j d|dd\}}|rX|sX| |¡rX|dur|V }qFq|V }|du rhq| j d|dd\}}|V }|sŠ|r¨|dur¸| jd|ddV }qŠq|dur|V }q¨qdS )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.
    NrM   rr   )Úcount)Úsubnr•   Úsub)Ú	prompt_reÚ
initial_reÚ
turnoff_rer   ÚoutZn1Zn2r   r   r   Ú_strip_promptsŽ  s,    
r¤   c                  C   s*   t  d¡} t  d¡}t  d¡}t| ||ƒS )z:Strip the >>>/... prompts of the Python interactive shell.z^(>>>|\.\.\.)( |$)z	^>>>( |$)z^[%!]©r˜   r™   r¤   )r    r¡   r¢   r   r   r   Úclassic_promptÉ  s    


r¦   c                  C   s    t  d¡} t  d¡}t| |dS )z%Strip IPython's In [1]:/...: prompts.z^(In \[\d+\]: |\s*\.{3,}: ?)z^%%)r¢   r¥   )r    r¢   r   r   r   Ú
ipy_promptÓ  s    

r§   c                  c   sx   t  d¡} d}|V }|du rq|  |¡}|rb| d¡}|durr| |¡rX|t|ƒd… }|V }q6q|dur|V }qbqdS )z©Remove 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]+rM   Nr   )r˜   r™   r•   r“   rv   Úlen)Zspace_rer   r–   Zspacer   r   r   Úleading_indentÝ  s    




r©   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                 C   s&   t  | ¡}|du r| S t| dd¡ S )z<Transform assignment from system commands (e.g. files = !ls)NÚlhsrn   )Úassign_system_rer•   Úassign_system_templater“   )r   r–   r   r   r   Úassign_from_system  s    
r­   z{}%\s*(?P<cmd>.*)z)%s = get_ipython().run_line_magic(%r, %r)c                 C   sN   t  | ¡}|du r| S | dd¡\}}| d¡\}}}| t¡}t|||f S )z;Transform assignment from magic commands (e.g. a = %who_ls)Nrª   rn   r[   )Úassign_magic_rer•   r“   r]   r^   r_   Úassign_magic_template)r   r–   Zm_lhsZm_cmdrf   rg   rh   r   r   r   Úassign_from_magic  s    

r°   )N)F)NN)@r#   r$   r   r˜   rC   r   r   r   Úior   ZIPython.core.splitinputr   rj   Z
ESC_SH_CAPZESC_HELPZ	ESC_HELP2r_   rw   Z	ESC_QUOTEZ
ESC_QUOTE2Z	ESC_PARENZESC_SEQUENCESÚABCMetar   r(   r/   r4   rO   r   rY   ri   ro   rq   ru   r{   r   r€   r   r‚   rƒ   r™   r”   ÚVERBOSEr‘   Úobjectrˆ   r‰   rŒ   rW   r   r—   rœ   r¤   r¦   r§   r©   Z_assign_patr,   r«   r¬   r­   r®   r¯   r°   r   r   r   r   Ú<module>   s˜   þ#2

ù	

ú	
'
;
	
	
ÿ

