o
    b3                     @   s  d Z ddlmZ ddlZddlZddlmZmZmZm	Z	 ddl
Z
ddlZddlZejdkr4d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ddG dd dZe d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&ejddG d*d+ d+Z'ejG d,d- d-ej(Z)ejddG d.d/ d/Z*ejG d0d1 d1ej(Z+dS )2z)Functions to process IPython magics with.    )	lru_cacheN)DictListTupleOptional)   
   )	TypeGuard)NothingChanged)out)zget_ipython().run_cell_magiczget_ipython().systemzget_ipython().getoutputzget_ipython().run_line_magic)	ENDMARKERNLNEWLINECOMMENTDEDENTZUNIMPORTANT_WSZ
ESCAPED_NL)z%%bashz%%htmlz%%javascriptz%%jsz%%latexz
%%markdownz%%perlz%%rubyz%%scriptz%%shz%%svgz%%writefileT)frozenc                   @      e Zd ZU eed< eed< dS )ReplacementmasksrcN__name__
__module____qualname__str__annotations__ r   r   X/var/www/secure340b-portal/env/lib/python3.10/site-packages/black/handle_ipynb_magics.pyr   :      
 r   verbosequietreturnc                 C   s@   zdd l }dd l}W dS  ty   | s|sd}t| Y dS w )Nr   z{Skipping .ipynb files as Jupyter dependencies are not installed.
You can fix this by running ``pip install black[jupyter]``FT)IPythontokenize_rtModuleNotFoundErrorr   )r   r    r"   r#   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   src_to_tokenstokens_to_srcreversed_enumerateFOP;T)r#   r(   r)   r*   nameTOKENS_TO_IGNOREr   )r   r(   r)   r*   tokensZtrailing_semicolonidxtokenr   r   r   remove_trailing_semicolonQ   s   
r2   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'   r,   )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(   r)   r*   r-   r.   _replacer   AssertionErrorr   )r   r3   r(   r)   r*   r/   r0   r1   r   r   r   put_trailing_semicolon_backu   s   
r6   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SyntaxErrorZIPython.core.inputtransformer2r7   Ztransform_cellreplace_cell_magicsreplace_magicslen
splitlinesr
   )r   replacementsr7   Ztransformer_managerZtransformedZcell_magic_replacementsZmagic_replacementsr   r   r   	mask_cell   s$   

r@   magicc                 C   s   |sJ t t|d d d}t|}d}|| v r3t|}|d7 }|dkr/td| d|| v st|d t|k rB| 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.")maxr=   secretsZ	token_hexr5   )r   rA   nbytesr1   counterr   r   r   	get_token   s&   

	
rK   c                 C   s   g }t | }t }|| |jdu r| |fS |jj d tv r$tt	| |jj}|
t||jj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.
    Nr   r   r   
)r8   r9   CellMagicFindervisit
cell_magicheadersplitNON_PYTHON_CELL_MAGICSr
   rK   appendr   body)r   r?   treeZcell_magic_finderr   r   r   r   r;      s   


r;   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.
    rC   )startz#Expecting one magic per line, got: z<
Please report a bug on https://github.com/psf/black/issues.r   rL   NrM   )MagicFinderrO   r8   r9   	enumerater>   magicsr=   r5   
col_offsetrA   rK   rT   r   join)
r   r?   Zmagic_finderZnew_srcsilineZoffsets_and_magicsr[   rA   r   r   r   r   r<      s&   



r<   r?   c                 C   s   |D ]
}|  |j|j} q| S )zRemove replacements from cell.

    For example

        "9b20"
        foo = bar

    becomes

        %%time
        foo = bar
    )replacer   r   )r   r?   replacementr   r   r   unmask_cell  s   ra   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)
isinstancer8   	AttributevalueCallfuncNameid)rb   r   r   r   _is_ipython_magic*  s   rk   c                   @   r   )	CellMagicrQ   rU   Nr   r   r   r   r   rl   9  r   rl   c                   @   s6   e Zd ZU dZdZee ed< dej	ddfddZ
dS )rN   a^  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.
    NrP   rb   r!   c                 C   s   t |jtjrHt|jjrH|jjjdkrHg }|jjD ]}t |tjs$J |	|j
 qd|d  }|d r?|d|d  7 }t||d d| _| | dS )	z)Find cell magic, extract header and body.Zrun_cell_magicz%%r   rC    rB   )rQ   rU   N)rd   rf   r8   rg   rk   rh   attrargsStrrT   srl   rP   generic_visit)selfrb   ro   argrQ   r   r   r   
visit_ExprT  s   
zCellMagicFinder.visit_Expr)r   r   r   __doc__rP   r   rl   r   r8   Exprru   r   r   r   r   rN   ?  s   
 rN   c                   @   s   e Zd ZU eed< eed< dS )OffsetAndMagicr[   rA   N)r   r   r   intr   r   r   r   r   r   rx   f  r   rx   c                   @   s^   e Zd ZU dZejdd dZeee	e
 f ed< dejddfd	d
ZdejddfddZdS )rX   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).
    c                   C   s
   t tS )N)collectionsdefaultdictlistr   r   r   r   <lambda>  s   
 zMagicFinder.<lambda>)default_factoryrZ   rb   r!   Nc                 C   s   t |jtjrEt|jjrE|jjjdkrEg }|jjD ]}t |tjs$J |	|j
 q|s/J d|d  }| j|jj 	t|jj| | | dS )a  Look for system assign magics.

        For example,

            black_version = !black --version

        would have been transformed to

            black_version = get_ipython().getoutput('black --version')

        and we look for instances of the latter.
        	getoutput!r   N)rd   rf   r8   rg   rk   rh   rn   ro   rp   rT   rq   rZ   linenorx   r[   rr   rs   rb   ro   rt   r   r   r   r   visit_Assign  s    
zMagicFinder.visit_Assignc                 C   s8  t |jtjrt|jjrg }|jjD ]}t |tjsJ ||j	 q|s(J |jjj
dkrf|d dkr=d|d  }nI|d dkrKd|d  }n;d|d  }|d re|d	us\J |d
|d  7 }n |jjj
dkrud|d  }n|jjj
dkrd|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.
        Zrun_line_magicr   Zpinfo?rC   Zpinfo2z??%Nrm   systemr   r   z!!)rd   rf   r8   rg   rk   rh   ro   rp   rT   rq   rn   r
   rZ   r   rx   r[   rr   r   r   r   r   ru     s2   zMagicFinder.visit_Expr)r   r   r   rv   dataclassesfieldrZ   r   ry   r   rx   r   r8   Assignr   rw   ru   r   r   r   r   rX   l  s   
 rX   ),rv   	functoolsr   r   r8   typingr   r   r   r   rH   sysrz   version_infor	   Ztyping_extensionsZblack.reportr
   Zblack.outputr   	frozensetZTRANSFORMED_MAGICSr.   rS   	dataclassr   boolr&   r   r2   r6   r@   rK   r;   r<   ra   exprre   rk   rl   NodeVisitorrN   rx   rX   r   r   r   r   <module>   sT    

$( )

&