o
    h4                     @   s2  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 ddlm	Z	 ddl
mZmZmZmZ ejdkr:ddl
mZ nddlmZ ddlmZ dd	lmZ ed
ZedZedZejZejddG dd dZededefddZde dee ef fddZ!de dede fddZ"de dee ee f fddZ#de de de fddZ$de dee ee f fd d!Z%de dee ee f fd"d#Z&de d$ee de fd%d&Z'd'ej(deej) fd(d)Z*d*eej( dee  fd+d,Z+ejddG d-d. d.Z,G d/d0 d0ej-Z.ejddG d1d2 d2Z/G d3d4 d4ej-Z0dS )5z)Functions to process IPython magics with.    N)	lru_cache)	find_spec)DictListOptionalTuple)   
   )	TypeGuard)out)NothingChanged)zget_ipython().run_cell_magiczget_ipython().systemzget_ipython().getoutputzget_ipython().run_line_magic)	ENDMARKERNLNEWLINECOMMENTDEDENTUNIMPORTANT_WS
ESCAPED_NL)captureprunpypypythonpython3timetimeitT)frozenc                   @   s   e Zd ZU eed< eed< dS )ReplacementmasksrcN)__name__
__module____qualname__str__annotations__ r$   r$   O/var/www/html/ai/venv/lib/python3.10/site-packages/black/handle_ipynb_magics.pyr   5      
 r   warnreturnc                 C   s0   t dd uot dd u}|s| rd}t| |S )Ntokenize_rtIPythonz}Skipping .ipynb files as Jupyter dependencies are not installed.
You can fix this by running ``pip install "black[jupyter]"``)r   r   )r'   	installedmsgr$   r$   r%   "jupyter_dependencies_are_installed;   s   r-   r   c                 C   st   ddl m}m}m} || }d}||D ]\}}|jtv rq|jdkr-|jdkr-||= d} |s4| dfS ||dfS )a  Remove trailing semicolon from Jupyter notebook cell.

    For example,

        fig, ax = plt.subplots()
        ax.plot(x_data, y_data);  # plot data

    would become

        fig, ax = plt.subplots()
        ax.plot(x_data, y_data)  # plot data

    Mirrors the logic in `quiet` from `IPython.core.displayhook`, but uses
    ``tokenize_rt`` so that round-tripping works fine.
    r   reversed_enumeratesrc_to_tokenstokens_to_srcFOP;T)r)   r/   r0   r1   nameTOKENS_TO_IGNOREr   )r   r/   r0   r1   tokenstrailing_semicolonidxtokenr$   r$   r%   remove_trailing_semicolonI   s   
r:   has_trailing_semicolonc                 C   sp   |s| S ddl m}m}m} || }||D ]\}}|jtv r q|j|jd d||<  ntddt	||S )zPut trailing semicolon back if cell originally had it.

    Mirrors the logic in `quiet` from `IPython.core.displayhook`, but uses
    ``tokenize_rt`` so that round-tripping works fine.
    r   r.   r3   )r   z{INTERNAL ERROR: Was not able to reinstate trailing semicolon. Please report a bug on https://github.com/psf/black/issues.  N)
r)   r/   r0   r1   r4   r5   _replacer   AssertionErrorr"   )r   r;   r/   r0   r1   r6   r8   r9   r$   r$   r%   put_trailing_semicolon_backi   s   
r>   c                 C   s   g }z
t |  W | |fS  ty   Y nw ddlm} | }|| }t|\}}||7 }||}t|\}}t|	 t| 	 krGt
||7 }||fS )zMask IPython magics so content becomes parseable Python code.

    For example,

        %matplotlib inline
        'foo'

    becomes

        "25716f358c32750e"
        'foo'

    The replacements are returned, along with the transformed code.
    r   )TransformerManager)astparseSyntaxErrorIPython.core.inputtransformer2r?   transform_cellreplace_cell_magicsreplace_magicslen
splitlinesr   )r   replacementsr?   transformer_managertransformedcell_magic_replacementsmagic_replacementsr$   r$   r%   	mask_cell   s$   

rN   magicc                 C   s   |sJ t t|d d d}t|}d}|| v r1t|}|d7 }|dkr-td| d|| v st|d t|k r@| d}d| dS )	aD  Return randomly generated token to mask IPython magic with.

    For example, if 'magic' was `%matplotlib inline`, then a possible
    token to mask it with would be `"43fdd17f7e5ddc83"`. The token
    will be the same length as the magic, and we make sure that it was
    not already present anywhere else in the cell.
          r   d   zINTERNAL ERROR: Black was not able to replace IPython magic. Please report a bug on https://github.com/psf/black/issues.  The magic might be helpful: N.")maxrG   	TOKEN_HEXr=   )r   rO   nbytesr9   counterr$   r$   r%   	get_token   s&   	
rY   c                 C   sj   g }t | }t }|| |jdu r| |fS |jj}t| |}|t||d | d|jj	 |fS )aN  Replace cell magic with token.

    Note that 'src' will already have been processed by IPython's
    TransformerManager().transform_cell.

    Example,

        get_ipython().run_cell_magic('t', '-n1', 'ls =!ls\n')

    becomes

        "a794."
        ls =!ls

    The replacement, along with the transformed code, is returned.
    Nr   r   
)
r@   rA   CellMagicFindervisit
cell_magicheaderrY   appendr   body)r   rI   treecell_magic_finderr_   r   r$   r$   r%   rE      s   



rE   c           
      C   s   g }t  }|t|  g }t|  ddD ]B\}}||jv rT|j| }t|dkr3td| d|d j	|d j
}}t| |}	|t|	|d |d| |	 }|| qd||fS )	ao  Replace magics within body of cell.

    Note that 'src' will already have been processed by IPython's
    TransformerManager().transform_cell.

    Example, this

        get_ipython().run_line_magic('matplotlib', 'inline')
        'foo'

    becomes

        "5e67db56d490fd39"
        'foo'

    The replacement, along with the transformed code, are returned.
    rQ   )startz#Expecting one magic per line, got: z<
Please report a bug on https://github.com/psf/black/issues.r   rZ   Nr[   )MagicFinderr]   r@   rA   	enumeraterH   magicsrG   r=   
col_offsetrO   rY   r`   r   join)
r   rI   magic_findernew_srcsilineoffsets_and_magicsrh   rO   r   r$   r$   r%   rF      s&   



rF   rI   c                 C   s   |D ]
}|  |j|j} q| S )zRemove replacements from cell.

    For example

        "9b20"
        foo = bar

    becomes

        %%time
        foo = bar
    )replacer   r   )r   rI   replacementr$   r$   r%   unmask_cell  s   rq   nodec                 C   s8   t | tjot | jtjot | jjtjo| jjjdkS )zCheck if attribute is IPython magic.

    Note that the source of the abstract syntax tree
    will already have been processed by IPython's
    TransformerManager().transform_cell.
    get_ipython)
isinstancer@   	AttributevalueCallfuncNameid)rr   r$   r$   r%   _is_ipython_magic  s   r{   argsc                 C   s.   g }| D ]}t |tjsJ ||j q|S N)rt   r@   Strr`   s)r|   str_argsargr$   r$   r%   _get_str_args,  s
   r   c                   @   s<   e Zd ZU eed< ee ed< eed< edefddZdS )	CellMagicr4   paramsra   r(   c                 C   s&   | j rd| j d| j  S d| j S )Nz%% )r   r4   selfr$   r$   r%   r_   :  s   zCellMagic.headerN)r   r    r!   r"   r#   r   propertyr_   r$   r$   r$   r%   r   4  s   
 r   c                   @   s<   e Zd ZdZd
dee ddfddZdejddfdd	Z	dS )r\   ag  Find cell magics.

    Note that the source of the abstract syntax tree
    will already have been processed by IPython's
    TransformerManager().transform_cell.

    For example,

        %%time

        foo()

    would have been transformed to

        get_ipython().run_cell_magic('time', '', 'foo()\n')

    and we look for instances of the latter.
    Nr^   r(   c                 C   s
   || _ d S r}   )r^   )r   r^   r$   r$   r%   __init__U  s   
zCellMagicFinder.__init__rr   c                 C   s^   t |jtjr(t|jjr(|jjjdkr(t|jj}t	|d |d |d d| _
| | dS )z)Find cell magic, extract header and body.run_cell_magicr   rQ   rP   )r4   r   ra   N)rt   rv   r@   rw   r{   rx   attrr   r|   r   r^   generic_visit)r   rr   r|   r$   r$   r%   
visit_ExprX  s   
zCellMagicFinder.visit_Exprr}   )
r   r    r!   __doc__r   r   r   r@   Exprr   r$   r$   r$   r%   r\   B  s    r\   c                   @   s   e Zd ZU eed< eed< dS )OffsetAndMagicrh   rO   N)r   r    r!   intr#   r"   r$   r$   r$   r%   r   d  r&   r   c                   @   sB   e Zd ZdZdddZdejddfddZdejddfd	d
Z	dS )re   a  Visit cell to look for get_ipython calls.

    Note that the source of the abstract syntax tree
    will already have been processed by IPython's
    TransformerManager().transform_cell.

    For example,

        %matplotlib inline

    would have been transformed to

        get_ipython().run_line_magic('matplotlib', 'inline')

    and we look for instances of the latter (and likewise for other
    types of magics).
    r(   Nc                 C   s   t t| _d S r}   )collectionsdefaultdictlistrg   r   r$   r$   r%   r     s   zMagicFinder.__init__rr   c                 C   s   t |jtjrYt|jjrYt|jj}|jjjdkr"d|d  }n(|jjjdkr>d|d  }|d r=|d|d  7 }nt	d|jjjd	d
| j
|jj t|jj| | | d
S )a{  Look for system assign magics.

        For example,

            black_version = !black --version
            env = %env var

        would have been (respectively) transformed to

            black_version = get_ipython().getoutput('black --version')
            env = get_ipython().run_line_magic('env', 'var')

        and we look for instances of any of the latter.
        	getoutput!r   run_line_magic%rQ   r   zUnexpected IPython magic zC found. Please report a bug on https://github.com/psf/black/issues.N)rt   rv   r@   rw   r{   rx   r   r|   r   r=   rg   linenor`   r   rh   r   r   rr   r|   r   r$   r$   r%   visit_Assign  s$   zMagicFinder.visit_Assignc                 C   s  t |jtjrzt|jjrzt|jj}|jjjdkrK|d dkr(d|d  }nC|d dkr6d|d  }n5d|d  }|d rJ|d	|d  7 }n |jjjd
krZd|d  }n|jjjdkrid|d  }nt	| j
|jj t|jj| | | dS )a  Look for magics in body of cell.

        For examples,

            !ls
            !!ls
            ?ls
            ??ls

        would (respectively) get transformed to

            get_ipython().system('ls')
            get_ipython().getoutput('ls')
            get_ipython().run_line_magic('pinfo', 'ls')
            get_ipython().run_line_magic('pinfo2', 'ls')

        and we look for instances of any of the latter.
        r   r   pinfo?rQ   pinfo2z??r   r   systemr   r   z!!N)rt   rv   r@   rw   r{   rx   r   r|   r   r   rg   r   r`   r   rh   r   r   r$   r$   r%   r     s(   zMagicFinder.visit_Expr)r(   N)
r   r    r!   r   r   r@   Assignr   r   r   r$   r$   r$   r%   re   l  s
    
!re   )1r   r@   r   dataclassessecretssys	functoolsr   importlib.utilr   typingr   r   r   r   version_infor
   typing_extensionsblack.outputr   black.reportr   	frozensetTRANSFORMED_MAGICSr5   PYTHON_CELL_MAGICS	token_hexrV   	dataclassr   boolr-   r"   r:   r>   rN   rY   rE   rF   rq   exprru   r{   r   r   NodeVisitorr\   r   re   r$   r$   r$   r%   <module>   sV    

 ()

"