
    \jf                     B   d 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	 ddl
mZ ddlmZmZmZmZmZmZmZ ddlmZ dd	lmZ d
dlmZmZmZ d
dlmZmZmZmZ d
dl m!Z!m"Z"m#Z# d
dlm$Z$m%Z% d
dl&m'Z( d
dl&m)Z* d
dl+m,Z,m-Z-m.Z.  G d de      Z/de-dddfde0dee0   de.dee   de1dee1ef   dede0fdZ2dde-ddfde0dee1ef   dee0   de.dee   de1dede1fdZ3de-ddddfd ed!edee0   de.dee   de1dee1ef   d"e1dede1fd#Z4dde-ddfd edee1ef   dee0   de.dee   de1dede1fd$Z5de-dddfd%ee0ef   dee1ef   de.dee   de1dee0   dede1fd&Z6d'e%defd(Z7ejp                  dee   fd)       Z9ejp                  d%ee0ef   d'e%dee   fd*       Z:de-ddddddfd%ee0ef   dee0   de.dee   de1d+e1dee1ef   d,e1d-ee   dede1fd.Z;e-dddfde0de.dee   d/ee1e/f   d0e1dedeejx                     fd1Z=e-ddddfd ede.dee   d/ee1e/f   d0e1d2eee0      dedeejx                     fd3Z>e-dddfd%ee0ef   de.dee   d/ee1e/f   d0e1dedeejx                     fd4Z?e-dddfd5eee0ef      de.dee   d/ee1e/f   d0e1dedeejx                     fd6Z@de-fd7ee   de.dede.fd8ZAy)9)	ImportKeycheck_code_string
check_filecheck_streamfind_imports_in_codefind_imports_in_filefind_imports_in_pathsfind_imports_in_streamplace_moduleplace_module_with_reasonsort_code_string	sort_filesort_stream    N)EnumStringIO)chain)Path)AnyIteratorOptionalSetTextIOUnioncast)warn)core   )filesidentifyio)ExistingSyntaxErrorsFileSkipCommentFileSkipSettingIntroducedSyntaxErrors)$ask_whether_to_apply_changes_to_filecreate_terminal_printershow_unified_diff)EmptyFile)module)module_with_reason)CYTHON_EXTENSIONSDEFAULT_CONFIGConfigc                        e Zd ZdZdZdZdZdZy)r   a  Defines how to key an individual import, generally for deduping.

    Import keys are defined from less to more specific:

    from x.y import z as a
    ______| |        |    |
       |    |        |    |
    PACKAGE |        |    |
    ________|        |    |
          |          |    |
        MODULE       |    |
    _________________|    |
              |           |
           ATTRIBUTE      |
    ______________________|
                  |
                ALIAS
    r            N)__name__
__module____qualname____doc__PACKAGEMODULE	ATTRIBUTEALIAS     3/root/env/lib/python3.12/site-packages/isort/api.pyr   r   +   s    & GFIEr=   r   Fcode	extensionconfig	file_pathdisregard_skip	show_diffconfig_kwargsreturnc           	          t        |       }t               }t        d||d|}t        |||||||       |j                  d       |j	                         S )a  Sorts any imports within the provided code string, returning a new string with them sorted.

    - **code**: The string of code with imports that need to be sorted.
    - **extension**: The file extension that contains imports. Defaults to filename extension or py.
    - **config**: The config object to use when sorting imports.
    - **file_path**: The disk location where the code string was pulled from.
    - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
    - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
    TextIO stream is provided results will be written to it, otherwise no diff will be computed.
    - ****config_kwargs**: Any config modifications.
    pathrA   )r@   rA   rB   rC   rD   r   r<   )r   _configr   seekread)	r?   r@   rA   rB   rC   rD   rE   input_streamoutput_streams	            r>   r   r   E   sd    ( D>LJMD)FDmDF% qr=   c                 R    t        d||d|}t        t        |       |||||      S )a2  Checks the order, format, and categorization of imports within the provided code string.
    Returns `True` if everything is correct, otherwise `False`.

    - **code**: The string of code with imports that need to be sorted.
    - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
    TextIO stream is provided results will be written to it, otherwise no diff will be computed.
    - **extension**: The file extension that contains imports. Defaults to filename extension or py.
    - **config**: The config object to use when sorting imports.
    - **file_path**: The disk location where the code string was pulled from.
    - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
    - ****config_kwargs**: Any config modifications.
    rH   rD   r@   rA   rB   rC   r<   )rJ   r   r   )r?   rD   r@   rA   rB   rC   rE   s          r>   r   r   i   s;    * D)FDmDF% r=   TrM   rN   raise_on_skipc                    |xs# |xr |j                   j                  d      xs d}|rt               }	t        | j                               }
t	        d|
|	|||||d|}|	j                  d       |
j                  d       t        |
j                         |	j                         ||du r|n||j                         |S t        d||d|}t        |xs d      }|s|r|j                  |      rt        |      |}|j                  rE	 | j                         }t        ||d	dd
       t              } |j'                         s
t               }	 t)        j*                  | ||||      }|j                  rd|j                  d       	 t        |j                         |d	dd
       |j                  d       ||k7  r|j1                  |j                                |S # t        $ r0 |t        vrt!        |      |j"                  rt%        | d       Y w xY w# t,        $ r t-        |      w xY w# t        $ r0 |t        vrt/        |      |j"                  rt%        | d       Y w xY w)a  Sorts any imports within the provided code stream, outputs to the provided output stream.
     Returns `True` if anything is modified from the original input stream, otherwise `False`.

    - **input_stream**: The stream of code with imports that need to be sorted.
    - **output_stream**: The stream where sorted imports should be written to.
    - **extension**: The file extension that contains imports. Defaults to filename extension or py.
    - **config**: The config object to use when sorting imports.
    - **file_path**: The disk location where the code string was pulled from.
    - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
    - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
    TextIO stream is provided results will be written to it, otherwise no diff will be computed.
    - ****config_kwargs**: Any config modifications.
    .py)rM   rN   r@   rA   rB   rC   rQ   r   T
file_inputfile_outputrB   outputcolor_outputrH   zPassed in contentexecr   z< Python AST errors found but ignored due to Cython extension)r@   rA   rQ   r<   )suffixlstripr   rL   r   rK   r(   rY   rJ   str
is_skippedr$   atomiccompileSyntaxErrorr-   r"   verboser   readabler   processr#   r%   write)rM   rN   r@   rA   rB   rC   rD   rQ   rE   _output_stream_input_streamchangedcontent_source_internal_outputfile_contents                  r>   r   r      s   0 QiHI,<,<,C,CC,HQTI! !2!2!45 	
&()'	
 	
 	A1$))+&++-$-$5=9,,	
 D)FDmDF9&9:NiF,=,=i,Hn--$}}		',,.LL.&!Q?  -%%''z	.,,'
 }}a 		$))+^VQJ!!!$ }, 0 5 5 78NM  	 11*>::~~%&&bc		(  .n--.  	 11,^<<~~%&&bc		s0   G H 	.H1 6HHH.16I*)I*c                 z   t        d||d|}|rt        | j                               } t        | t        ||||      }t        |j                  |j                  |j                        }|s1|j                  r$|j                  s|j                  |xs d d       y|j                  |xs d d       |rt               }	| j                  d       | j                         }
t        t        |
      |	||||       |	j                  d       t        |
|	j                         ||du rd	n||j                  
       y)aE  Checks any imports within the provided code stream, returning `False` if any unsorted or
    incorrectly imports are found or `True` if no problems are identified.

    - **input_stream**: The stream of code with imports that need to be sorted.
    - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
    TextIO stream is provided results will be written to it, otherwise no diff will be computed.
    - **extension**: The file extension that contains imports. Defaults to filename extension or py.
    - **config**: The config object to use when sorting imports.
    - **file_path**: The disk location where the code string was pulled from.
    - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
    - ****config_kwargs**: Any config modifications.
    rH   )rM   rN   r@   rA   rB   rC   )colorerrorsuccess z Everything Looks Good!Tz1 Imports are incorrectly sorted and/or formatted.r   NrU   Fr<   )rJ   r   rL   r   r)   r'   rY   format_errorformat_successrb   only_modifiedro   rn   rK   r(   )rM   rD   r@   rA   rB   rC   rE   rh   printerrN   file_contentss              r>   r   r      sA   * D)FDmDF 1 1 34!%G &!!)<)<fF[F[G >>&"6"6OOyB//FGHMMY_"%%VWX 
!$))+!-0')	
 	1$%**,$,4),,	
 r=   filenamec           
      p   |}d|v rR|j                  dd      }|r>|j                  |       }	|j                  rt        |	d    d|         t	        di |	d   }t
        j                  j                  |       5 }
t        |
j                  f||||xs |
j                  |d|cddd       S # 1 sw Y   yxY w)a)  Checks any imports within the provided file, returning `False` if any unsorted or
    incorrectly imports are found or `True` if no problems are identified.

    - **filename**: The name or Path of the file to check.
    - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
    TextIO stream is provided results will be written to it, otherwise no diff will be computed.
    - **config**: The config object to use when sorting imports.
    - **file_path**: The disk location where the code string was pulled from.
    - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
    - **extension**: The file extension that contains imports. Defaults to filename extension or py.
    - ****config_kwargs**: Any config modifications.
    config_trieNr    used for file r   rP   r<   )popsearchrb   printr/   r!   r*   rL   r   streamrI   )rv   rD   rA   rB   rC   r@   rE   file_configrx   config_infosource_files              r>   r   r   1  s    * !K%#''t<%,,X6K~~Q(zBC 2;q>2K	h	 	
;
3;#3#3)
 
	
 	
 	
s   8*B,,B5r   c                 f    | j                   j                  | j                   j                  dz         S )Nz.isorted)rI   with_suffixr[   )r   s    r>   	_tmp_filer   ]  s*    ''(8(8(?(?*(LMMr=   c               #   (   K   t        d        y w)N)newliner   r<   r=   r>    _in_memory_output_stream_contextr   a  s     
4
  s   c              #      K   t        |      }|j                  d|j                  d      5 }t        j                  | |       | d d d        y # 1 sw Y   y xY ww)Nzw+rp   )encodingr   )r   openr   shutilcopymode)rv   r   tmp_filerN   s       r>   _file_output_stream_contextr   f  sR     %H	tk&:&:B	G =(+  s   *AA	AAAask_to_applywrite_to_stdoutrX   c	                    |}
d|	v rR|	j                  dd      }|r>|j                  |       }|j                  rt        |d    d|         t	        di |d   }
t
        j                  j                  |       5 }|xs |j                  }t        d||
d|	}d}	 |r+t        |j                  t        j                  ||||      }n|	 |j                  rt               }nt!        | |      }|5 }t        |j                  |||||      }|j#                  d       |r@|s|r|j                  j#                  d       t%        |j                  j                         |j                         ||d	u rdnt'        t(        |      |j*                  
       |s |rXt-        t/        |j                              s:	 ddd       	 |j                  st1        |      }|j3                          ddd       y|j                  j7                          |j                  rK|j#                  d       |j                  j9                  d      5 }t;        j<                  ||       ddd       ddd       |rV|j                  s&t1        |      }|j?                  |j                         |j@                  st        d|j                          	 |j                  st1        |      }|j3                          nt        |j                  |||||      }|rt|rr|j                  j#                  d       |j#                  d       t%        |j                  j                         |j                         ||d	u rdn||j*                  
       |j                  j7                          |cddd       S # t4        $ r Y w xY w# 1 sw Y   YxY w# 1 sw Y   ^xY w# t4        $ r Y @w xY w# 	 |j                  st1        |      }|j3                          w w # t4        $ r Y w w xY wxY w# tB        $ r tE        | d       Y tF        $ r tE        | d       Y w xY w# 1 sw Y   yxY w)a:  Sorts and formats any groups of imports imports within the provided file or Path.
     Returns `True` if the file has been changed, otherwise `False`.

    - **filename**: The name or Path of the file to format.
    - **extension**: The file extension that contains imports. Defaults to filename extension or py.
    - **config**: The config object to use when sorting imports.
    - **file_path**: The disk location where the code string was pulled from.
    - **disregard_skip**: set to `True` if you want to ignore a skip set in config for this file.
    - **ask_to_apply**: If `True`, prompt before applying any changes.
    - **show_diff**: If `True` the changes that need to be done will be printed to stdout, if a
    TextIO stream is provided results will be written to it, otherwise no diff will be computed.
    - **write_to_stdout**: If `True`, write to stdout instead of the input file.
    - **output**: If a TextIO is provided, results will be written there rather than replacing
    the original file content.
    - ****config_kwargs**: Any config modifications.
    rx   Nr   ry   r   rH   F)rM   rN   rA   rB   rC   r@   TrU   wzFixing z- unable to sort due to existing syntax errorsz5 unable to sort as isort introduces new syntax errorsr<   )$rz   r{   rb   r|   r/   r!   r*   rL   rI   rJ   r   r}   sysstdoutoverwrite_in_placer   r   rK   r(   r   r   rY   r&   r]   r   unlinkFileNotFoundErrorcloser   r   copyfileobjreplacequietr"   r   r%   )rv   r@   rA   rB   rC   r   rD   r   rX   rE   r~   rx   r   r   actual_file_pathrh   output_stream_contextrN   r   fss                       r>   r   r   n  sM   8 !K%#''t<%,,X6K~~Q(zBC 2;q>2K	h	 `;$8(8(8T.{TmTZ	]%!,!3!3"%**!.#1' >5!!444T4V14O (+51 3 !Nm&1-8-?-?.;'-*:/=*3'G *..q1&#,$/$6$6$;$;A$>$53>3E3E3J3J3L4A4F4F4H2B+4+< 04-1&)-D5;5H5H%& (1(40T,/0@0@,A1* 059!NR!#)#<#<+4[+A ( 1G` `h !, 2 2 8 8 :#)#<#<$1$6$6q$9)4)9)9)>)>s)C %Nr(.(:(:="(M%NA!ND ##)#<#<+4[+A ( 0 01A1A B#)<< %0@0@/A&B C!#)#<#<+4[+A ( 1 *%0%7%7&,%"2'5"+G 9#**//2A)'2'9'9'>'>'@(.&6+4+<4))/)<)<  &&,,. A` `H  1 ! !%N %NA!N !NZ  1 ! !	!#)#<#<+4[+A ( 1 $=  1 ! !. $ 	U$%%RST% 	]$%%Z[\	]{` `s   8!Q
0P%O0CN<6O?'N/AN<N/N< A O'O	(B,PQ
	N,(P+N,,P/N94N<<O	O		OPOPP'PP	P	PP	PPQ,Q
.QQ
QQ

Quniquetop_onlyc           	   +   T   K   t        dt        |       ||||d|E d{    y7 w)a  Finds and returns all imports within the provided code string.

    - **code**: The string of code with imports that need to be sorted.
    - **config**: The config object to use when sorting imports.
    - **file_path**: The disk location where the code string was pulled from.
    - **unique**: If True, only the first instance of an import is returned.
    - **top_only**: If True, only return imports that occur before the first function or class.
    - ****config_kwargs**: Any config modifications.
    rM   rA   rB   r   r   Nr<   )r	   r   )r?   rA   rB   r   r   rE   s         r>   r   r     s<     " & d^   s   (&(_seenc              +   "  K   t        dd|i|}t        j                  | |||      }|s
|E d{    |
t               n|}|D ]  }	|dt        j
                  fv r|	j                         }
n|t        j                  k(  r|	j                   d|	j                   }
nQ|t        j                  k(  r|	j                  }
n1|t        j                  k(  r|	j                  j                  d      d   }

s|
|vs|j                  |
       |	  y7 ܭw)af  Finds and returns all imports within the provided code stream.

    - **input_stream**: The stream of code with imports that need to be sorted.
    - **config**: The config object to use when sorting imports.
    - **file_path**: The disk location where the code string was pulled from.
    - **unique**: If True, only the first instance of an import is returned.
    - **top_only**: If True, only return imports that occur before the first function or class.
    - **_seen**: An optional set of imports already seen. Generally meant only for internal use.
    - ****config_kwargs**: Any config modifications.
    rA   )rA   rB   r   NTrS   r   r<   )rJ   r    importssetr   r;   	statementr:   r+   	attributer9   r8   splitadd)rM   rA   rB   r   r   r   rE   identified_importsseenidentified_importkeys              r>   r	   r	     s    & 4F4m4F!))Vy8 %%%#mSUD/ $dIOO,,#--/Cy***&--.a0A0K0K/LMCy'''#**Cy(((#**005a8C3d?HHSM##$ 	&s   .DDB>D0D5Dc           
   +      K   t         j                  j                  |       5 }t        d|j                  ||xs |j
                  ||d|E d{    ddd       y7 # 1 sw Y   yxY ww)ac  Finds and returns all imports within the provided source file.

    - **filename**: The name or Path of the file to look for imports in.
    - **extension**: The file extension that contains imports. Defaults to filename extension or py.
    - **config**: The config object to use when sorting imports.
    - **file_path**: The disk location where the code string was pulled from.
    - **unique**: If True, only the first instance of an import is returned.
    - **top_only**: If True, only return imports that occur before the first function or class.
    - ****config_kwargs**: Any config modifications.
    r   Nr<   )r!   r*   rL   r	   r}   rI   )rv   rA   rB   r   r   rE   r   s          r>   r   r   =  sz     $ 
h	 
;) 
$++3;#3#3
 
 	
 	

 
	

 
s.    A+-AAA	A+AA($A+pathsc           	   +      K   t        ddi|r
t               ndt        fdt        j                  t        t        |       g g       D         E d{    y7 w)aj  Finds and returns all imports within the provided source paths.

    - **paths**: A collection of paths to recursively look for imports within.
    - **extension**: The file extension that contains imports. Defaults to filename extension or py.
    - **config**: The config object to use when sorting imports.
    - **file_path**: The disk location where the code string was pulled from.
    - **unique**: If True, only the first instance of an import is returned.
    - **top_only**: If True, only return imports that occur before the first function or class.
    - ****config_kwargs**: Any config modifications.
    rA   Nc              3   >   K   | ]  }t        |         yw))r   rA   r   r   N)r   ).0	file_namerA   r   r   r   s     r>   	<genexpr>z(find_imports_in_paths.<locals>.<genexpr>o  s1      

  !&(RV 

s   r<   )rJ   r   r   r   findmapr]   )r   rA   rB   r   r   rE   r   s    ` `` @r>   r   r   Z  s\     $ 4F4m4F(.suDD

 #ZZCRH	

  s   AA%A#A%rI   c                 t    | r|t         u rd|vr	d|vr| |d<   |r|t         urt        d      t        di |}|S )Nsettings_pathsettings_fileziYou can either specify custom configuration options using kwargs or passing in a Config object. Not Both!r<   )r.   
ValueErrorr/   )rI   rA   rE   s      r>   rJ   rJ   x  sZ     . =0=0)-o&'8 
 (-(Mr=   )B__all__
contextlibr   r   enumr   r!   r   	itertoolsr   pathlibr   typingr   r   r   r   r   r   r   warningsr   isortr   rp   r   r    
exceptionsr"   r#   r$   r%   formatr&   r'   r(   r)   r*   placer+   r
   r,   r   settingsr-   r.   r/   r   r]   boolr   r   r   r   r   r   contextmanagerr   r   r   Importr   r	   r   r   rJ   r<   r=   r>   <module>r      s      
     D D D   ! !  e d  ) A ? ? 8  $# $ %*! 
! }!  !  ~	! 
 !  T6\"!  !  	! L &+## $ 
T6\" } 	
 ~   
F  $# $ %*bbb }b 	b
 ~b b T6\"b b b 
bN &+## $ @@T6\"@ }@ 	@
 ~@ @ @ 
@J &+# $#)
CI)
T6\")
 )
 ~	)

 )
 })
 )
 
)
XN4 ND N !(6*: ! ! %T	*:  RZ[aRb    $# $%*!#GCIG}G G ~	G
 G G T6\"G G VG G 
GX $ $%*
 ~ $	/"	
   hoo: $ $%* $'$'$'$ ~'$ $	/"	'$
 '$ CH'$ '$ hoo'$X $ $%*
CI

 ~
 $	/"	

 
 
 hoo
> $ $%*E#t)$% ~ $	/"	
   hoo>  .
4.)/SVr=   