o
    ƯhAg                     @   s  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 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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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%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'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*d+Z6d,e%defd-d.Z7ej8dee fd/d0Z9ej8d)ee0ef d,e%dee fd1d2Z:de-dd!ddddfd)ee0ef dee0 de.dee de1d3e1dee1ef d4e1d5ee dede1fd6d7Z;e-dddfde0de.dee d8ee1e/f d9e1dedeej< fd:d;Z=e-ddddfd"ede.dee d8ee1e/f d9e1d<eee0  dedeej< fd=d>Z>e-dddfd)ee0ef de.dee d8ee1e/f d9e1dedeej< fd?d@Z?e-dddfdAeee0ef  de.dee d8ee1e/f d9e1dedeej< fdBdCZ@de-fdDee de.dede.fdEdFZAdS )G)	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                   @   s    e Zd ZdZdZdZdZdZdS )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 r:   r:   F/var/www/Befach/backend/venv/lib/python3.10/site-packages/isort/api.pyr   +   s    r   Fcode	extensionconfig	file_pathdisregard_skip	show_diffconfig_kwargsreturnc           	   	   K   sJ   t | }t  }td||d|}t|||||||d |d | 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   Nr:   )r   _configr   seekread)	r<   r=   r>   r?   r@   rA   rB   input_streamoutput_streamr:   r:   r;   r   E   s   
	r   c                 K   s,   t d||d|}tt| |||||d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.
    rD   rA   r=   r>   r?   r@   Nr:   )rF   r   r   )r<   rA   r=   r>   r?   r@   rB   r:   r:   r;   r   i   s   r   TrI   rJ   raise_on_skipc              
   K   s  |p|r
|j dpd}|rFt }	t|  }
td|
|	|||||d|}|	d |
d t|
 |	 ||du r>|n||jd |S td||d|}t	|pTd}|sc|rc|
|rct||}|jrz|  }t||d	ddd
 W n ty   |tvrt||jrt| ddd Y nw t|} | st }ztj| ||||d}W n ty   t|w |jr|d zt| |d	ddd
 |d W n ty   |tvrt||jrt| ddd Y nw ||kr||  |S )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)rI   rJ   r=   r>   r?   r@   rL   r   T
file_inputfile_outputr?   outputcolor_outputrD   zPassed in contentexec)flagsdont_inheritz< Python AST errors found but ignored due to Cython extensionr/   
stacklevel)r=   r>   rL   Nr:   )suffixlstripr   rH   r   rG   r'   rS   rF   str
is_skippedr#   atomiccompileSyntaxErrorr,   r!   verboser   readabler   processr"   r$   write)rI   rJ   r=   r>   r?   r@   rA   rL   rB   _output_stream_input_streamchangedcontent_source_internal_outputfile_contentr:   r:   r;   r      s   




r   c                 K   s   t d||d|}|rt|  } t| t||||d}t|j|j|jd}|s:|j	r8|j
s8||p3d d dS ||p?d d |rut }	| d |  }
tt|
|	||||d |	d t|
|	 ||du rod	n||jd
 dS )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.
    rD   )rI   rJ   r=   r>   r?   r@   )colorerrorsuccess z Everything Looks Good!Tz1 Imports are incorrectly sorted and/or formatted.r   NrO   Fr:   )rF   r   rH   r   r(   r&   rS   format_errorformat_successr`   only_modifiedrl   rk   rG   r'   )rI   rA   r=   r>   r?   r@   rB   rf   printerrJ   file_contentsr:   r:   r;   r      sP   

r   filenamec              	   K   s   |}d|v r*| dd}|r*|| }	|jr!t|	d  d|   tdi |	d }tj| }
t|
j	f||||p<|
j
|d|W  d   S 1 sMw   Y  dS )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   rK   r:   )popsearchr`   printr.   r    r)   rH   r   streamrE   )rs   rA   r>   r?   r@   r=   rB   file_configrt   config_infosource_filer:   r:   r;   r   3  s*   
$r   r|   c                 C   s   | j | j jd S )Nz.isorted)rE   with_suffixrY   )r|   r:   r:   r;   	_tmp_file_  s   r~   c                   c   s    t d dV  d S )N)newliner   r:   r:   r:   r;    _in_memory_output_stream_contextc  s   r   c                 c   sT    t |}|jd|jdd}t| | |V  W d    d S 1 s#w   Y  d S )Nzw+rm   )encodingr   )r~   openr   shutilcopymode)rs   r|   tmp_filerJ   r:   r:   r;   _file_output_stream_contexth  s   "r   ask_to_applywrite_to_stdoutrR   c	              
   K   sp  |}
d|	v r*|	 dd}|r*|| }|jr!t|d  d|   tdi |d }
tj| |}|p6|j}t	d||
d|	}d}z:|rUt
|jtj||||d}n(|du rFz|jrbt }nt| |}|}t
|j|||||d}|d |r|s|r|jd t|j | ||d	u rdntt||jd
 |s|rtt|js	 W d   W |jst|}|jd	d W W d   dS W W d   dS |j  |jr|d |jd}t|| W d   n1 sw   Y  W d   n	1 sw   Y  |r&|jst|}||j |js&td|j  W |js5t|}|jd	d nG|jsEt|}|jd	d w w t
|j|||||d}|rx|rx|jd |d t|j | ||d	u rrdn||jd
 |j  W n% t y   t!| ddd Y n t"y   t!| ddd Y nw |W  d   S 1 sw   Y  dS )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.
    rt   Nr   ru   r   rD   F)rI   rJ   r>   r?   r@   r=   TrO   )
missing_okwzFixing z- unable to sort due to existing syntax errorsr/   rW   z5 unable to sort as isort introduces new syntax errorsr:   )#rv   rw   r`   rx   r.   r    r)   rH   rE   rF   r   ry   sysstdoutoverwrite_in_placer   r   rG   r'   r   r   rS   r%   r[   r~   unlinkcloser   r   copyfileobjreplacequietr!   r   r$   )rs   r=   r>   r?   r@   r   rA   r   rR   rB   rz   rt   r{   r|   actual_file_pathrf   output_stream_contextrJ   r   fsr:   r:   r;   r   s  s   



	
	)@
4
"


&r   uniquetop_onlyc                 k   s*    t dt| ||||d|E dH  dS )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.
    rI   r>   r?   r   r   Nr:   )r   r   )r<   r>   r?   r   r   rB   r:   r:   r;   r     s   r   _seenc                 k   s    t dd|i|}tj| |||d}|s|E dH  |du r!t n|}|D ]A}	|dtjfv r3|	 }
n%|tjkrB|	j d|	j	 }
n|tj
krK|	j}
n|tjkrX|	jdd }
|
rf|
|vrf||
 |	V  q%dS )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   NTrM   r   r:   )rF   r   importssetr   r9   	statementr8   r*   	attributer7   r6   splitadd)rI   r>   r?   r   r   r   rB   identified_importsseenidentified_importkeyr:   r:   r;   r     s,   





r   c              
   k   s    z/t j| }td|j||p|j||d|E dH  W d   W dS 1 s)w   Y  W dS  tyO } ztd|  d| dd W Y d}~dS d}~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/   rW   r:   )r    r)   rH   r   ry   rE   OSErrorr   )rs   r>   r?   r   r   rB   r|   rk   r:   r:   r;   r   B  s"   &	&r   pathsc                 +   sZ    t dd i| rt ndt fddttt|  g g D  E dH  dS )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   s"    | ]}t | d V  qdS ))r   r>   r   r   N)r   ).0	file_namer>   r   r   r   r:   r;   	<genexpr>w  s    

z(find_imports_in_paths.<locals>.<genexpr>r:   )rF   r   r   r   findmapr[   )r   r>   r?   r   r   rB   r:   r   r;   r   b  s   r   rE   c                 K   sJ   | 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.   )rE   r>   rB   r:   r:   r;   rF     s   rF   )B__all__
contextlibr   r   enumr   r    r   	itertoolsr   pathlibr   typingr   r   r   r   r   r   r   warningsr   isortr   rm   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   rF   r:   r:   r:   r;   <module>   s   $

&

#
	

i

E


,$

	

 




,


"

