ZL i<tdZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl m Z ejdk\rddl mZnddlmZddlmZdd lmZdd lmZed Zed Zed Zej4dGddZededefdZdededdfdZdede eeffdZ!dededefdZ"dede ee#effdZ$de%defdZ&dededefdZ'dede ee#effd Z(dede ee#effd!Z)ded"e#edefd#Z*dedefd$Z+d%ejXdeejZfd&Z.d'e#ejXde#efd(Z/ej4dGd)d*Z0Gd+d,ejbZ2ej4dGd-d.Z3Gd/d0ejbZ4y)1z)Functions to process IPython magics with.N) lru_cache) find_spec)Optional) ) TypeGuard)Mode)out)NothingChanged)zget_ipython().run_cell_magiczget_ipython().systemzget_ipython().getoutputzget_ipython().run_line_magic) ENDMARKERNLNEWLINECOMMENTDEDENTUNIMPORTANT_WS ESCAPED_NL)captureprunpypypythonpython3timetimeitT)frozenc"eZdZUeed<eed<y) ReplacementmasksrcN)__name__ __module__ __qualname__str__annotations___/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/black/handle_ipynb_magics.pyrr0s I Hr%rwarnreturnc`tdduxr tddu}|s|r d}t||S)N tokenize_rtIPythonz}Skipping .ipynb files as Jupyter dependencies are not installed. You can fix this by running ``pip install "black[jupyter]"``)rr )r' installedmsgs r&"jupyter_dependencies_are_installedr.6sF - ,Q91ET1Q  K  C r%rmodectfdtDrtt}|j dr2|j ddddt |jzvrtyy)aCheck that cell does not already contain TransformerManager transformations, or non-Python cell magics, which might cause tokenizer_rt to break because of indentations. If a cell contains ``!ls``, then it'll be transformed to ``get_ipython().system('ls')``. However, if the cell originally contained ``get_ipython().system('ls')``, then it would get transformed in the same way: >>> TransformerManager().transform_cell("get_ipython().system('ls')") "get_ipython().system('ls')\n" >>> TransformerManager().transform_cell("!ls") "get_ipython().system('ls')\n" Due to the impossibility of safely roundtripping in such situations, cells containing transformed magics will be ignored. c3&K|]}|v ywNr$).0transformed_magicrs r& z validate_cell..Us P(9  # Ps%%)maxsplitrN)anyTRANSFORMED_MAGICSr _get_code_start startswithsplitPYTHON_CELL_MAGICSpython_cell_magics)rr/lines` r& validate_cellrBDso" P=O PP 3 D t A q!!"%!D$;$;; < <r%cddlm}m}m}||}d}||D]<\}}|jt vr|jdk(r|j dk(r||=d}n|s|dfS||dfS)aRemove 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. rreversed_enumerate src_to_tokens tokens_to_srcFOP;T)r*rErFrGnameTOKENS_TO_IGNOREr)rrErFrGtokenstrailing_semicolonidxtokens r&remove_trailing_semicolonrP`s ML 3 F(0 U ::) )  :: %))s"2s !%   Ez  $ &&r%has_trailing_semicolonc|s|Sddlm}m}m}||}||D];\}}|jt vr|j |jdz||<n tddt||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. rrDrI)rz{INTERNAL ERROR: Was not able to reinstate trailing semicolon. Please report a bug on https://github.com/psf/black/issues. N) r*rErFrGrJrK_replacerAssertionErrorr")rrQrErFrGrLrNrOs r&put_trailing_semicolon_backrUs " LL 3 F(0  U ::) ) nnSn9s    L   }V$ %%r%cg} tj|||fS#t$rYnwxYwddlm}|}|j |}t |\}}||z }|j |}t|\}}t|jjt|jjk7rt||z }||fS)zMask IPython magics so content becomes parseable Python code. For example, %matplotlib inline 'foo' becomes b"25716f358c32750" 'foo' The replacements are returned, along with the transformed code. r)TransformerManager) astparse SyntaxErrorIPython.core.inputtransformer2rWtransform_cellreplace_cell_magicsreplace_magicslenstrip splitlinesr )r replacementsrWtransformer_manager transformedcell_magic_replacementsmagic_replacementss r& mask_cellrgs')L! # L     B,.&44S9K+>{+K(K(++L%44[AK&4[&A#K# ;    ) ) +,CIIK4J4J4L0MM&&L  $$s  ))n_charsc|dkDsJt|dzdz d}tj|}t|dz|kDr|dd}d|dS) zBCreate a randomly generated token that is n_chars characters long.rr9r7rNzb"")maxsecrets token_hexr_)rhn_bytesrOs r& create_tokenrps^ Q;;'Q,"A&G   g &E 5zA~cr wa=r%magicc|sJt|}t|}d}||vr)t|}|dz }|dkDrtd|d||vr)|S)aDReturn 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. rr7dzINTERNAL 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)r_rprT)rrqrhrOcounters r& get_tokenrusu L5%jG  !EG 3,W%1  S= //4g7   3, Lr%c<g}tj|}t}|j||j||fS|jj }t ||}|jt|||d|jj|fS)aNReplace 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. rr ) rXrYCellMagicFindervisit cell_magicheaderruappendrbody)rrbtreecell_magic_finderr|rs r&r]r]s"')L 99S>D')D!##+L   ) ) 0 0F S& !D 6:;V2'22778 9< GGr%cg}t}|jtj|g}t |j ddD]\}}||j vrz|j |}t|dk7rtd|d|dj|dj}}t||} |jt| ||d|| z}|j|dj||fS) aoReplace 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. rxr7)startz#Expecting one magic per line, got: z< Please report a bug on https://github.com/psf/black/issues.rrwN) MagicFinderrzrXrY enumerater>magicsr_rT col_offsetrqrur}rjoin) rrb magic_findernew_srcsirAoffsets_and_magicsrrqrs r&r^r^s$L=Lsyy~&HSYYt_A64 ## #!-!4!4Q!7 %&!+$9:L9MNRR #1%00"1%++JS%(D    5 A B $t+D 99X  ,,r%rbc`|D](}|j|j|j}*|S)zRemove replacements from cell. For example "9b20" foo = bar becomes %%time foo = bar )replacerr)rrb replacements r& unmask_cellr.s2$= kk+**KOO<= Jr%ctjd|D]9}|jdj}|s%|j dr7|cSy)a0Provides the first line where the code starts. Iterates over lines of code until it finds the first line that doesn't contain only empty spaces and comments. It removes any empty spaces at the start of the line and returns it. If such line doesn't exist, it returns an empty string. z.+r#)refinditergrouplstripr=)rmatchrAs r&r<r<@sLT3'{{1~$$& ,K r%nodec,t|tjxryt|jtjxrSt|jj tj xr#|jj jdk(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) isinstancerX AttributevalueCallfuncNameid)rs r&_is_ipython_magicrOsf 4' 0 tzz388 , 0 tzz 1 0 JJOO  - / r%argscg}|D]S}t|tjrt|jtsJ|j |jU|Sr2)rrXConstantrr"r})rstr_argsargs r& _get_str_argsr^sNH##s||,CIIs1KKK "# Or%cHeZdZUeed<eeed<eed<edefdZy) CellMagicrJparamsr~r(cp|jrd|jd|jSd|jS)Nr6 )rrJselfs r&r|zCellMagic.headerls5 ;; {!DKK=1 1DII;r%N)rr r!r"r#rpropertyr|r$r%r&rrfs/ I SM I    r%rcLeZdZdZddeeddfdZdejddfdZ y) ryahFind 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\n 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||_yr2)r{)rr{s r&__init__zCellMagicFinder.__init__s $r%rcht|jtjr}t |jj r^|jj j dk(r;t|jj}t|d|d|d|_ |j|y)z)Find cell magic, extract header and body.run_cell_magicrr7r9)rJrr~N) rrrXrrrattrrrrr{ generic_visit)rrrs r& visit_ExprzCellMagicFinder.visit_Exprsz tzz388 ,!$**//2 $$(88 1D'T!WT!W4PQ7SDO 4 r%r2) rr r!__doc__rrrrXExprrr$r%r&ryryts6$%8I#6%$% !sxx !D !r%ryc"eZdZUeed<eed<y)OffsetAndMagicrrqN)rr r!intr#r"r$r%r&rrs O Jr%rc`eZdZdZddZdej ddfdZdejddfdZ y) raVisit 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@tjt|_yr2) collections defaultdictlistrrs r&rzMagicFinder.__init__s7B7N7Nt7T r%rct|jtjr%t |jj rt |jj}|jj jdk(r d|d}nj|jj jdk(rd|d}|dr:|d|dz }n.td|jj jd d |j|jjjt|jj||j|y ) 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!rrun_line_magic%r7rzUnexpected IPython magic zC found. Please report a bug on https://github.com/psf/black/issues.N)rrrXrrrrrrrTrlinenor}rrrrrrrs r& visit_AssignzMagicFinder.visit_Assigns djj#(( +0A$**//0R 1Dzz##{2$q'm%%)99$q'm7QtAwi=(C$/ 0D0D/GHRR KK )) * 1 1tzz44c:  4 r%ct|jtjrKt |jj r+t |jj}|jj jdk(r;|ddk(r d|d}n|ddk(r d|d}nwd|d}|drj|d |dz }n^|jj jd k(r d |d}n2|jj jd k(r d |d}nt|j|jjjt|jj||j|y)aLook 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. rrpinfo?r7pinfo2z??rrsystemrrz!!N)rrrXrrrrrrr rrr}rrrrs r&rzMagicFinder.visit_ExprsD& djj#(( +0A$**//0R 1Dzz##'777g%d1gY-C!W(tAwi.Cd1gY-CAw47)},%%1$q'm%%447)n$$ KK )) * 1 1tzz44c:  4 r%)r(N) rr r!rrrXAssignrrrr$r%r&rrs<$U!!!B'!sxx'!D'!r%r)5rrXr dataclassesrrmsys functoolsrimportlib.utilrtypingr version_infortyping_extensions black.moder black.outputr black.reportr frozensetr;rKr? dataclassrboolr.r"rBtuplerPrUrrgrrprur]r^rr<exprrrrr NodeVisitorryrrr$r%r&rs/   $w +'   d#  $       s$48'3'5d+;'@&S&$&3&0'%3'%5d;&7!78'%T # # 3ss0HSHU3[0A+A%BH>&-&-c4 +<&< =&-RS[(9c$    CHH 3==)A SXX49d#   $  !coo!Dd#$^!#//^!r%