
    JG_>                        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
 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 dd
lmZmZmZ ddlmZ ddlmZ ddlmZ ddlm Z m!Z! de dddfde"dee"   de!dee   de#de	e#ef   fdZ$dde ddfde"de	e#ef   dee"   de!dee   de#de#fdZ%de dddfdededee"   de!dee   de#de	e#ef   de#fdZ&dde ddfdede	e#ef   dee"   de!dee   de#de#fdZ'de dddfde	e"ef   de	e#ef   de!dee   de#dee"   de#fdZ(de dddddfde	e"ef   dee"   de!dee   de#d e#de	e#ef   d!e#de#fd"Z)de fd#ee   de!de!fd$Z*y)%    N)StringIO)Path)OptionalTextIOUnioncast)warn)core   )io)ExistingSyntaxErrorsFileSkipCommentFileSkipSettingIntroducedSyntaxErrors)$ask_whether_to_apply_changes_to_filecreate_terminal_printershow_unified_diff)Empty)module)module_with_reason)DEFAULT_CONFIGConfigFcode	extensionconfig	file_pathdisregard_skip	show_diffc           	          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   r   r    )r   _configsort_streamseekread)	r   r   r   r   r   r   config_kwargsinput_streamoutput_streams	            +/usr/lib/python3/dist-packages/isort/api.pysort_code_stringr+      sd    ( D>LJMD)FDmDF% q    returnc                 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.
    r    r   r   r   r   r   r"   )r#   check_streamr   )r   r   r   r   r   r   r'   s          r*   check_code_stringr1   <   s;    * D)FDmDF% r,   r(   r)   c           
      b   |rt               }t        | j                               }	t        d|	|||||d|}
|j                  d       |	j                  d       t	        |	j                         |j                         ||du r|nt        t        |      |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$                  | ||xs# |xr |j&                  j)                  d	      xs d
|      }
|j                  rf|j                  d       	 t        |j                         |ddd       |j                  d       ||k7  r|j-                  |j                                |
S |
S # t        $ r t        |      w xY w# t*        $ r t+        |      w xY w# t        $ r t/        |      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.
    r(   r)   r   r   r   r   r   T
file_inputfile_outputr   outputcolor_outputr    zPassed in contentexecr   .py)r   r   r"   )r   r&   r$   r%   r   r   r   r8   r#   str
is_skippedr   atomiccompileSyntaxErrorr   readabler
   processsuffixlstripr   writer   )r(   r)   r   r   r   r   r   r'   _output_stream_input_streamchangedcontent_source_internal_outputfile_contents                 r*   r$   r$   \   sC   . ! !2!2!45 
&()
 
 	A1$))+&++-$-$5=4	;R,,	
 D)FDmDF9&9:N**95!.11$}}	7',,.LL.&!Q?#L1L %%''z.,,WI$N)2B2B2I2I#2NWSW	
 }}a 	9$))+^VQJ!!!$=0##$4$9$9$;< N7N5  	7&~66	7  .n--.  	9(88	9s%   2*G) 7>H AH )G>HH.c           	      j   t        d||d|}|rt        | j                               } t        | t        ||||      }t        |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t        t        |      |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.
    r    r3   )color z Everything Looks Good!Tz1 Imports are incorrectly sorted and/or formatted.r   Nr4   Fr"   )r#   r   r&   r$   r   r   r8   verboseonly_modifiedsuccesserrorr%   r   r   r   )r(   r   r   r   r   r   r'   rH   printerr)   file_contentss              r*   r0   r0      s3   * D)FDmDF 1 1 34!%G &F,?,?@G>>&"6"6OOyB//FGHb))Z[\$JMa (--/M%m4+##- q!()..0#(D0td696M#00 r,   Tfilenamec           
          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.
    r/   N)r   Filer&   r0   streamr!   )rU   r   r   r   r   r   r'   source_files           r*   
check_filerZ      se    * 
h	 	
;
3;#3#3)
 
	
 	
 	
s   *AAask_to_applywrite_to_stdoutc                    t         j                  j                  |       5 }	|xs |	j                  }
t	        d|
|d|}d}	 |r+t        |	j                  t        j                  ||
||      }n|	j                  j                  |	j                  j                  dz         }	 |j                  d|	j                  d      5 }t        j                  | |       t        |	j                  |||
||      }ddd       |r7|s|r|	j                  j                  d	       |j                  |	j                  d      5 }t!        |	j                  j                         |j                         |
|d
u rdnt#        t$        |      |j&                         |s |rAt)        t+        |	j                              s#	 ddd       	 |j-                          ddd       yddd       |	j                  j1                          |j3                  |	j                         |j4                  st7        d|	j                          	 |j-                          |cddd       S # 1 sw Y   `xY w# t.        $ r Y w xY w# 1 sw Y   xY w# t.        $ r Y >w xY w# 	 |j-                          w # t.        $ r Y w w xY wxY w# t8        $ r t;        |
 d       Y t<        $ r t;        |
 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.
    - ****config_kwargs**: Any config modifications.
    r    F)r(   r)   r   r   r   r   z.isortedwrN   )encodingnewlineNr   Tr4   zFixing z- unable to sort due to existing syntax errorsz5 unable to sort as isort introduces new syntax errorsr"   )r   rW   r&   r!   r#   r$   rX   sysstdoutwith_suffixrC   openr_   shutilcopymoder%   r   r   r   r8   r   r<   unlinkFileNotFoundErrorclosereplacequietprintr   r	   r   )rU   r   r   r   r   r[   r   r\   r'   rY   actual_file_pathrH   tmp_filer)   tmp_outs                  r*   	sort_filerp     s   2 
h	 ?;$8(8(8O.vOO9	]%!,!3!3"%**!.#1' '++778H8H8O8OR\8\])!k&:&:B '  &(;"-)4););*7#)&6+9&/#	 $'..33A6!))4)=)=r "/ " 1!( 1/:/A/A/F/F/H07.>3<3D4$vW`Ja171D1D!" $-$0,P(+K,<,<(=-& ,1!1, )o? ?@1" $**002 (()9)9:%||!GK,<,<+=">? ) ? ?" N - 11 10 -  ), # 	U$%%RST% 	]$%%Z[\	]y? ?s   !K!AJ,"J 2I2AJ9A8I,1J:IA!J5I8K!I	J	I)&J,(I))J,,I5	1J8	JJ,JJ,J)	JJ)	J&	#J)%J&	&J))J,,KK!KK!KK!!K*r!   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   )r!   r   r'   s      r*   r#   r#   q  sZ     n$}4}4-1M/*'8 
 (-(Mr,   )+re   ra   r   r   pathlibr   typingr   r   r   r   warningsr	   isortr
   rN   
exceptionsr   r   r   r   formatr   r   r   r   placer   place_moduler   place_module_with_reasonsettingsr   r   r<   boolr+   r1   r$   r0   rZ   rp   r#   r"   r,   r*   <module>r      s"    
   0 0     e d  ) A ,
  $# $ %*! 
! }!  !  ~	! 
 !  T6\"! L &+## $ 
T6\" } 	
 ~  
F  $# $ %*UUU }U 	U
 ~U U T6\"U 
Ut &+## $ >>T6\"> }> 	>
 ~> > 
>F &+# $#
CI
T6\"
 
 ~	

 
 }
 

F  $# $%*!XCIX}X X ~	X
 X X T6\"X X 
Xx  .
4.)/r,   