o
    ˷eF                     @   s@  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d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dLd9d:Z3dMd;d<Z4ejd=d> Z5ejd?d@ Z6ejdAdB Z7dCZ8e(dD9e8ej*Z:dEZ;ejdFdG Z<e(dH9e8ej*Z=dIZ>ejdJdK Z?dS )Na,  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      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   T/var/www/ideatree/venv/lib/python3.10/site-packages/IPython/core/inputtransformer.pypush-   s   
zInputTransformer.pushc                 C   r   )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 )zCan 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                   @   0   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      d | jS )Nz%StatelessInputTransformer(func={0!r}))formatr   r   r   r   r   __repr__R      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   r   )z!No-op - exists for compatibility.Nr   r   r   r   r   r   Z   s   zStatelessInputTransformer.resetNr!   r"   r#   r$   r-   r0   r   r   r   r   r   r   r*   M   s    r*   c                   @   r)   )CoroutineInputTransformerz<Wrapper for an input transformer implemented as a coroutine.c                 K   s   |di || _ t| j  d S )Nr   )coronext)r   r4   r   r   r   r   r-   `      z"CoroutineInputTransformer.__init__c                 C   r.   )Nz%CoroutineInputTransformer(coro={0!r}))r/   r4   r   r   r   r   r0   e   r1   z"CoroutineInputTransformer.__repr__c                 C   s   | j |S )zSend a line of input to the transformer, returning the
        transformed input or None if the transformer is waiting for more
        input.
        r4   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.
        Nr7   r   r   r   r   r   o   s   zCoroutineInputTransformer.resetNr2   r   r   r   r   r3   ^   s    r3   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 )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                 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   )iterr:   r   __next__	tokenizer)r   itr   r   r   r;      s   
z%TokenInputTransformer.reset_tokenizerc                 C   s   | j |d  tdd | j D r|  S g }d}z&| jD ] }|| |d }|tjks5|r7|tjkr7 n|tjkr>d}qW n t	yN   | 
  Y d S w | |S )N
c                 s   s    | ]}|  V  qd S r   )isspace).0lr   r   r   	<genexpr>   s    z-TokenInputTransformer.push.<locals>.<genexpr>Fr   T)r:   appendallr   r>   tokenizeNEWLINENL
ERRORTOKENr   r;   output)r   r   tokens
stop_at_NLintoktr   r   r   r      s&   



zTokenInputTransformer.pushc                 C   s&   | j   |   t| |dS )Nr@   )r:   clearr;   r   r   rstripr   rL   r   r   r   rK      s   
zTokenInputTransformer.outputc                 C   s0   d | j}| j  |   |r|dS d S )N r@   )joinr:   rP   r;   rQ   )r   rC   r   r   r   r      s   

zTokenInputTransformer.resetN)	r!   r"   r#   r$   r-   r;   r   rK   r   r   r   r   r   r9   u   s    r9   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   )superrU   r-   r   	__class__r   r   r-      s   zassemble_python_lines.__init__c                 C   s   |   S r   )r   rR   r   r   r   rK      s   zassemble_python_lines.output)r!   r"   r#   r-   rK   __classcell__r   r   rW   r   rU      s    rU   c                  c   st    d} 	 | V } | r|   rqg }| dur4| dr*t| s*|| dd  dV } n||  n| dusd|} q)z4Join lines following explicit line continuations (\)rS   TN\)rA   endswithhas_commentrE   rT   )r   partsr   r   r   assemble_logical_lines   s    

	r_   c                 C   sR   |dkrdnd| v rdnd}d || g}|d\}}}|t}d|||f S )zUPrepares a pinfo(2)/psearch call from a target name and the escape
    (i.e. ? or ??)r	   pinfo2*psearchpinfo &%sget_ipython().run_line_magic(%r, %r))rT   	partitionlstrip	ESC_MAGIC)targetesclspacemethodargt_magic_name_t_magic_arg_sr   r   r   _make_help_call   s   
rq   c                 C   s   | j  t}d| j|f S )zTranslate lines escaped with: !z%sget_ipython().system(%r))r   rg   	ESC_SHELLpre	line_infocmdr   r   r   
_tr_system   r6   rw   c                 C   s    | j  dd }d| j|f S )z Translate lines escaped with: !!   Nz%sget_ipython().getoutput(%r))r   rg   rs   rt   r   r   r   _tr_system2   s   ry   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   rq   ifunrj   rs   ru   r   r   r   _tr_help   s   r}   c                 C   sV   d}| j tr| j S d| j| jg }|d\}}}|t	}|| j
||f S )zTranslate lines escaped with: %re   rd   )r   
startswith
ESC_MAGIC2rT   r{   the_reststriprf   rg   rh   rs   )ru   tplrv   rn   ro   rp   r   r   r   	_tr_magic   s   
r   c                 C      d| j | jd| j f S )zTranslate lines escaped with: ,
%s%s("%s")z", "rs   r{   rT   r   splitr|   r   r   r   	_tr_quote      
r   c                 C   s   d| j | j| jf S )zTranslate lines escaped with: ;r   )rs   r{   r   r|   r   r   r   
_tr_quote2   s   
r   c                 C   r   )zTranslate lines escaped with: /z%s%s(%s)z, r   r|   r   r   r   	_tr_paren   r   r   c                 C   s4   | r|   r| S t| }|jtvr| S t|j |S )zMTransform escaped commands - %magic, !system, ?help + various autocalls.
    )rA   r   rj   tr)r   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 |S  tyE } z d|jd v r.|t n|t W Y d}~|S W Y d}~|S d}~w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   toktypesrO   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.
    )rG   COMMENTr   )srcr   r   r   r]   4  s   r]   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   rG   r   r   )r   r   r   r   r   ends_in_comment_or_stringE  s   r   c                 C   sN   t | }|du st| r| S |d}|d}t| d}t|||S )z$Translate lines with ?/?? at the endNrz      r   )_help_end_researchr   group_initial_space_rematchrq   )r   mri   rj   rk   r   r   r   help_endW  s   


r   Fc                 c   s    d}t d}d}	 |V }|s|V }|r|ts&|dur%|V }|dusq
||r,q
|}g }dV }|durS| dks?| sS|| dV }|durS| dks?| r?|d\}}}|t}|||d	|f }q)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+\?rS   TNrd   r@   )
recompiler~   r   r   r   rE   rf   rg   rT   )end_on_blank_liner   cellmagic_help_rer   firstbody
magic_namero   r   r   r   	cellmagicd  s:   




r   c                 c   s    |du r| }d}	 |V }|du rq	|j d|dd\}}|r1|s1||r1|dur0|V }|dus)q	|V }|du r9q	| j d|dd\}}|V }|sJ|r\|dur[| jd|ddV }|dusNn|durg|V }|dus`q
)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.
    NrS   Trz   )count)subnr   sub)	prompt_re
initial_re
turnoff_rer   outn1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   s    t d} d}	 |V }|du rq| |}|r6|d}|dur5||r.|t|d }|V }|dus!n|durA|V }|dus:q	)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]+rS   TNr   )r   r   r   r   r~   len)space_rer   r   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lhsrv   )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   rv   rd   )assign_magic_rer   r   rf   rg   rh   assign_magic_template)r   r   m_lhsm_cmdrn   ro   rp   r   r   r   assign_from_magic  s   

r   )F)NN)@r$   r%   r   r   rG   r   r   r   ior   IPython.core.splitinputr   rr   
ESC_SH_CAPESC_HELP	ESC_HELP2rh   r   	ESC_QUOTE
ESC_QUOTE2	ESC_PARENESC_SEQUENCESABCMetar   r*   r3   r9   rU   r    r_   rq   rw   ry   r}   r   r   r   r   r   r   r   r   VERBOSEr   objectr   r   r   r]   r   r   r   r   r   r   r   _assign_patr/   r   r   r   r   r   r   r   r   r   r   <module>   s    #2
	

	

';
	
	


