o
    ĎiK                     @   s  d Z ddlZddlZddlZddlZddlZddlmZmZ ddl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e de de d e fd!d"Z!d#efd$d%Z"d#efd&d'Z#d#efd(d)Z$d#efd*d+Z%d#efd,d-Z&d#efd.d/Z'd#efd0d1Z(ee"ee#ee$ee$ee%ee&ee'ee(iZ)ejd2e fd3d4Z*e+d5Z,e+d6ej-Z.e/ Z0e/ Z1d7d8 Z2d9d: Z3d;d< Z4ejd2e fd=d>Z5ejdTd@e6fdAdBZ7dUdCdDZ8ejdEdF Z9ejdGdH Z:ejdIdJ Z;dKZ<e+dL=e<ej-Z>dMZ?ejdNdO Z@e+dP=e<ej-ZAdQZBejdRdS ZCdS )Va,  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)
untokenize
TokenError)StringIO)LineInfo)	tokenutil!z!!???%z%%,;/c                   @   s@   e Zd ZdZd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   t jdtdd d S )Nz`InputTransformer` has been deprecated since IPython 7.0 and emit a warnig since IPython 8.31, it will be removed in the future   
stacklevel)warningswarnDeprecationWarningself r   Y/home/jeff/fluffinator/venv/lib/python3.10/site-packages/IPython/core/inputtransformer.py__init__/   s
   
zInputTransformer.__init__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.
        Nr   r   liner   r   r   push8   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   resetD   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_factoryR   s   z2InputTransformer.wrap.<locals>.transformer_factory)	functoolswraps)r"   r#   r$   r   r!   r   wrapM   s   zInputTransformer.wrapN)__name__
__module____qualname____doc__r   abcabstractmethodr   r   classmethodr'   r   r   r   r   r   ,   s    	

r   )	metaclassc                       8   e Zd ZdZ fddZdd Zdd Zdd	 Z  ZS )
StatelessInputTransformerzDWrapper for a stateless input transformer implemented as a function.c                    s$   t    tjdtdd || _d S )Nz`StatelessInputTransformer` has been deprecated since IPython 7.0 and emit a warnig since IPython 8.31, it will be removed in the futurer   r   )superr   r   r   r   r#   r   r#   	__class__r   r   r   Z   s   

z"StatelessInputTransformer.__init__c                 C      d | jS )Nz%StatelessInputTransformer(func={0!r}))formatr#   r   r   r   r   __repr__e      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   h   s   
zStatelessInputTransformer.pushc                 C   r   )z!No-op - exists for compatibility.Nr   r   r   r   r   r   m   s   zStatelessInputTransformer.reset	r(   r)   r*   r+   r   r8   r   r   __classcell__r   r   r4   r   r1   X   s    r1   c                       r0   )
CoroutineInputTransformerz<Wrapper for an input transformer implemented as a coroutine.c                    s8   t    tjdtdd |di || _t| j d S )N`CoroutineInputTransformer` has been deprecated since IPython 7.0 and emit a warnig since IPython 8.31, it will be removed in the futurer   r   r   )r2   r   r   r   r   coronext)r   r>   r    r4   r   r   r   s   s   
z"CoroutineInputTransformer.__init__c                 C   r6   )Nz%CoroutineInputTransformer(coro={0!r}))r7   r>   r   r   r   r   r8      r9   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.
        r>   sendr   r   r   r   r      s   zCoroutineInputTransformer.pushc                 C   s   | j dS )zrReturn, transformed any lines that the transformer has
        accumulated, and reset its internal state.
        Nr@   r   r   r   r   r      s   zCoroutineInputTransformer.resetr:   r   r   r4   r   r<   q   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 )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(   t jdtdd || _g | _|   d S )Nr=   r   r   )r   r   r   r#   bufreset_tokenizerr3   r   r   r   r      s   zTokenInputTransformer.__init__c                 C   s   t | j}t|j| _d S r   )iterrC   r   generate_tokens_catch_errors__next__	tokenizer)r   itr   r   r   rD      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)rC   appendallr   rH   tokenizeNEWLINENL
ERRORTOKENr   rD   output)r   r   tokens
stop_at_NLintoktr   r   r   r      s&   



zTokenInputTransformer.pushc                 C   s&   | j   |   t| |dS )NrJ   )rC   clearrD   r   r#   rstripr   rV   r   r   r   rU      s   
zTokenInputTransformer.outputc                 C   s0   d | j}| j  |   |r|dS d S )N rJ   )joinrC   rZ   rD   r[   )r   rM   r   r   r   r      s   

zTokenInputTransformer.resetN)	r(   r)   r*   r+   r   rD   r   rU   r   r   r   r   r   rB      s    rB   c                       s$   e Zd Z fddZdd Z  ZS )assemble_python_linesc                    s   t  d  d S r   )r2   r   r   r4   r   r   r      s   zassemble_python_lines.__init__c                 C   s   |   S r   )r   r\   r   r   r   rU      s   zassemble_python_lines.output)r(   r)   r*   r   rU   r;   r   r   r4   r   r_      s    r_   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 (\)r]   TN\)rK   endswithhas_commentrO   r^   )r   partsr   r   r   assemble_logical_lines   s    

	re   targetesclspacereturnc                 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))r^   	partitionlstrip	ESC_MAGIC)rf   rg   rh   methodargt_magic_name_t_magic_arg_sr   r   r   _make_help_call   s   
rx   	line_infoc                 C   s   | j  t}d| j|f S )zTranslate lines escaped with: !z%sget_ipython().system(%r))r   rq   	ESC_SHELLprery   cmdr   r   r   
_tr_system   s   r~   c                 C   s    | j  dd }d| j|f S )z Translate lines escaped with: !!r   Nz%sget_ipython().getoutput(%r))r   rq   r{   r|   r   r   r   _tr_system2   s   r   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   rx   ifunrg   r{   ry   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: %ro   rn   )r   
startswith
ESC_MAGIC2r^   r   the_reststriprp   rq   rr   r{   )ry   tplr}   ru   rv   rw   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", "r{   r   r^   r   splitr   r   r   r   	_tr_quote     
r   c                 C   s   d| j | j| jf S )zTranslate lines escaped with: ;r   )r{   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   r   c                 C   s4   | r|   r| S t| }|jtvr| S t|j |S )zHTransform escaped commands - %magic, !system, ?help + various autocalls.)rK   r   rg   tr)r   lineinfr   r   r   escaped_commands3  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yF } 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   rF   addr   args_MULTILINE_STRING_MULTILINE_STRUCTURE)r   r   toktypesrY   er   r   r   _line_tokensL  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.
    )rQ   COMMENTr   )srcr   r   r   rc   [  s   rc   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   rQ   r   r   )r   r   r   r   r   ends_in_comment_or_stringl  s   r   c                 C   s^   t | }|du st| r| S |d}|d}t| }|dus$J |d}t|||S )z$Translate lines with ?/?? at the endNr      r   )_help_end_researchr   group_initial_space_rematchrx   )r   mrf   rg   r   rh   r   r   r   help_end~  s   




r   Fend_on_blank_linec                 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+\?r]   TNrn   rJ   )
recompiler   r   r   r   rO   rp   rq   r^   )r   r   cellmagic_help_rer   firstbody
magic_namerv   r   r   r   	cellmagic  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.
    Nr]   Tr   )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]+r]   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lhsr}   )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   r}   rn   )assign_magic_rer   r   rp   rq   rr   assign_magic_template)r   r   m_lhsm_cmdru   rv   rw   r   r   r   assign_from_magic7  s   

r   )F)NN)Dr+   r,   r%   r   rQ   r   r   r   ior   IPython.core.splitinputr   IPython.utilsr   rz   
ESC_SH_CAPESC_HELP	ESC_HELP2rr   r   	ESC_QUOTE
ESC_QUOTE2	ESC_PARENESC_SEQUENCESABCMetar   r1   r<   rB   r_   r'   re   strrx   r~   r   r   r   r   r   r   r   r   r   r   VERBOSEr   objectr   r   r   rc   r   r   boolr   r   r   r   r   _assign_patr7   r   r   r   r   r   r   r   r   r   r   <module>   s    ,9
		

	
';
	
	


