
    1yif                        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 ddl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e.de.dz  de,dedz  de/de/ez  dede.fdZ0dde+ddfde.de/ez  de.dz  de,dedz  de/dede/fdZ1de+dddd fd!ed"ede.dz  de,dedz  de/de/ez  d#e/dede/fd$Z2dde+ddfd!ede/ez  de.dz  de,dedz  de/dede/fd%Z3de+dd dfd&e.ez  de/ez  de,dedz  de/de.dz  dede/fd'Z4d(e#defd)Z5ejl                  dee   fd*       Z7ejl                  d&e.ez  d(e#dee   fd+       Z8de+dd ddddfd&e.ez  de.dz  de,dedz  de/d,e/de/ez  d-e/d.edz  dede/fd/Z9e+dddfde.de,dedz  d0e/e-z  d1e/dedeejt                     fd2Z;e+ddddfd!ede,dedz  d0e/e-z  d1e/d3e<e.   dz  dedeejt                     fd4Z=e+dddfd&e.ez  de,dedz  d0e/e-z  d1e/dedeejt                     fd5Z>e+dddfd6ee.ez     de,dedz  d0e/e-z  d1e/dedeejt                     fd7Z?de+fd8edz  de,dede,fd9Z@y):)	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)Iterator)EnumStringIO)chain)Path)AnyTextIO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     K/var/www/html/content_weaver/venv/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.
    pathr>   )r=   r>   r?   r@   rA   r   r9   )r   _configr   seekread)	r<   r=   r>   r?   r@   rA   rB   input_streamoutput_streams	            r;   r   r   F   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.
    rE   rA   r=   r>   r?   r@   r9   )rG   r   r   )r<   rA   r=   r>   r?   r@   rB   s          r;   r   r   j   s;    * D)FDmDF% r:   TrJ   rK   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                  rF	 | j                         }t        ||d	dd
       t              } |j'                         s
t               }	 t)        j*                  | ||||      }|j                  re|j                  d       	 t        |j                         |d	dd
       |j                  d       ||k7  r|j1                  |j                                |S # t        $ r2 |t        vrt!        |      |j"                  rt%        | dd       Y w xY w# t,        $ r t-        |      w xY w# t        $ r2 |t        vrt/        |      |j"                  rt%        | d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)rJ   rK   r=   r>   r?   r@   rN   r   T
file_inputfile_outputr?   outputcolor_outputrE   zPassed in contentexec)flagsdont_inheritz< Python AST errors found but ignored due to Cython extensionr.   
stacklevel)r=   r>   rN   r9   )suffixlstripr   rI   r   rH   r%   rV   rG   str
is_skippedr!   atomiccompileSyntaxErrorr*   r   verboser   readabler   processr    r"   write)rJ   rK   r=   r>   r?   r@   rA   rN   rB   _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.&PTU  -%%''z	.,,'
 }}a 
	$))+^V1[_`!!!$ }, 0 5 5 78NQ  	 11*>::~~%&&bc 		*  .n--.  	 11,^<<~~%&&bc 		s0    G H 
/H5 8HHH258I0/I0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.
    rE   )rJ   rK   r=   r>   r?   r@   )colorerrorsuccess z Everything Looks Good!Tz1 Imports are incorrectly sorted and/or formatted.r   NrR   Fr9   )rG   r   rI   r   r&   r$   rV   format_errorformat_successrc   only_modifiedrp   ro   rH   r%   )rJ   rA   r=   r>   r?   r@   rB   ri   printerrK   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   rM   r9   )popsearchrc   printr,   r   r'   rI   r   streamrF   )rw   rA   r>   r?   r@   r=   rB   file_configry   config_infosource_files              r;   r   r   4  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)rF   with_suffixr\   )r   s    r;   	_tmp_filer   `  s*    ''(8(8(?(?*(LMMr:   c               #   (   K   t        d        y w)N)newliner   r9   r:   r;    _in_memory_output_stream_contextr   d  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+rq   )encodingr   )r   openr   shutilcopymode)rw   r   tmp_filerK   s       r;   _file_output_stream_contextr   i  sR     %H	tk&:&:B	G =(+  s   *AA	AAAask_to_applywrite_to_stdoutrU   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       |rA|s|r|j                  j#                  d       t%        |j                  j                         |j                         ||d	u rdnt'        t(        |      |j*                  
       |s |rYt-        t/        |j                              s;	 ddd       |j                  st1        |      }|j3                  d	       ddd       y|j                  j5                          |j                  rK|j#                  d       |j                  j7                  d      5 }t9        j:                  ||       ddd       ddd       |rV|j                  s&t1        |      }|j=                  |j                         |j>                  st        d|j                          |j                  st1        |      }|j3                  d	       nt        |j                  |||||      }|rt|rr|j                  j#                  d       |j#                  d       t%        |j                  j                         |j                         ||d	u rdn||j*                  
       |j                  j5                          |cddd       S # 1 sw Y   JxY w# 1 sw Y   OxY w# |j                  st1        |      }|j3                  d	       w w xY w# t@        $ r tC        | dd       Y otD        $ r tC        | dd       Y w xY w# 1 sw Y   yxY w)a2  Sorts and formats any groups of 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.
    ry   Nr   rz   r   rE   F)rJ   rK   r>   r?   r@   r=   TrR   )
missing_okwzFixing z- unable to sort due to existing syntax errorsr.   rZ   z5 unable to sort as isort introduces new syntax errorsr9   )#r{   r|   rc   r}   r,   r   r'   rI   rF   rG   r   r~   sysstdoutoverwrite_in_placer   r   rH   r%   r   r   rV   r#   r^   r   unlinkcloser   r   copyfileobjreplacequietr   r   r"   )rw   r=   r>   r?   r@   r   rA   r   rU   rB   r   ry   r   r   actual_file_pathri   output_stream_contextrK   r   fss                       r;   r   r   t  s   8 !K%#''t<%,,X6K~~Q(zBC 2;q>2K	h	 `;$8(8(8T.{TmTZ	%!,!3!3"%**!.#1' >2=!444T4V14O (+51 3 !Nm&1-8-?-?.;'-*:/=*3'G *..q1&#,$/$6$6$;$;A$>$53>3E3E3J3J3L4A4F4F4H2B4=4ED4PVXaKb5;5H5H%& (1(40T,/0@0@,A1* 059!NR  &88'0'=H$OOtO<E` `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%88'0'=H$OOtO<)%0%7%7&,%"2'5"+G 9#**//2A)'2'9'9'>'>'@(.&6+4+<4))/)<)<  &&,,. A` `n%N %NA!N !NR  &88'0'=H$OOtO<  90 $ 	c$%%RS`ab% 	#$$YZ	u` `s   8!P#0O*%N;0CN.6N;>)O*0AN.N!N.!A N;CO*P#!N+&N..N8	3N;;,O''O**P P#P P#P  P##P,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.
    rJ   r>   r?   r   r   Nr9   )r	   r   )r<   r>   r?   r   r   rB   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.
    r>   )r>   r?   r   NTrP   r   r9   )rG   r   importssetr   r8   	statementr7   r(   	attributer6   r5   splitadd)rJ   r>   r?   r   r   r   rB   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# t        $ r}t        d|  d| d       Y d}~yd}~ww 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   NzUnable to parse file z due to r.   rZ   r9   )r   r'   rI   r	   r~   rF   OSErrorr   )rw   r>   r?   r   r   rB   r   ro   s           r;   r   r   C  s     $NWW\\(# 	{- (//#7{'7'7!    	 		 	  N$XJhug>1MMNsc   BA, -A AA A, BA  A)%A, (B)A, ,	B5BBBB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.
    r>   Nc              3   >   K   | ]  }t        |         yw))r   r>   r   r   N)r   ).0	file_namer>   r   r   r   s     r;   	<genexpr>z(find_imports_in_paths.<locals>.<genexpr>x  s1      

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

s   r9   )rG   r   r   r   findmapr^   )r   r>   r?   r   r   rB   r   s    ` `` @r;   r   r   c  s\     $ 4F4m4F%+CED

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

  s   AA%A#A%rF   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!r9   )r+   
ValueErrorr,   )rF   r>   rB   s      r;   rG   rG     sZ     . =0=0)-o&'8 
 (-(Mr:   )A__all__
contextlibr   r   collections.abcr   enumr   r   r   	itertoolsr   pathlibr   typingr   r   r   warningsr   isortr   rq   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   r   rG   r9   r:   r;   <module>r      s      
 $     $ $   ! !  e d  ) A ? ? 8 !#! $! 
! Tz!  !  d{	! 
 !  f}!  !  	! L  % #! 
f} Tz 	
 d{   
F !#! $ddd Tzd 	d
 d{d d f}d d d 
dR  % #! @@f}@ Tz@ 	@
 d{@ @ @ 
@J  %#! )
Dj)
f})
 )
 d{	)

 )
 Tz)
 )
 
)
XN4 ND N !(6*: ! ! #* 4 HU[L\   !#!$! GDjGTzG G d{	G
 G G f}G G TMG G 
GX $!$
 d{ 9	
   hoo: $!$!'$'$'$ d{'$ 9	'$
 '$ s8d?'$ '$ hoo'$X $!$NDjNN d{N 9	N
 N N hooND $!$C$J d{ 9	
   hoo> ~
+&,PSr:   