a
    aF                     @   s6  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m	Z	 zd dl
Z
W n eyf   dZY n0 dZe
jdd e d	Ze d
ZeedddZeedddZdddeeee ee	 eddddZeedddZd#eeedddZG dd dZG dd deZd$eee	 eeed d!d"ZdS )%    N)datetime)unified_diff)Path)OptionalTextIOTF)stripz\+[^+]z-[^-])import_linereturnc                 C   sF   |   } | dr,| dd} | dd} n| drB| dd} | S )Nfrom   import .import )r   
startswithreplace)r    r   +lib/python3.9/site-packages/isort/format.pyformat_simplified   s    

r   c                 C   s\   |   } | dsX| dsXd| vr.d|  S | d}|d}dd| d| S | S )Nr
   r   r   r   )r   r   splitpopjoin)r   partsendr   r   r   format_natural    s    


r   )outputcolor_output)
file_inputfile_output	file_pathr   r   r	   c           
   	   C   s   t ||}|du rdnt|}t|du r0t nt| j}t| jdd|jdd|d |d |tt d}|D ]}	|	|	 qzdS )a  Shows a unified_diff for the provided input and output against the provided file path.

    - **file_input**: A string that represents the contents of a file before changes.
    - **file_output**: A string that represents the contents of a file after changes.
    - **file_path**: A Path object that represents the file path of the file being changed.
    - **output**: A stream to output the diff to. If non is provided uses sys.stdout.
    - **color_output**: Use color in output if True.
    Nr   T)keependsz:beforez:after)ZfromfileZtofileZfromfiledateZ
tofiledate)
create_terminal_printerstrr   ZnowZfromtimestampstatst_mtimer   
splitlines	diff_line)
r   r   r   r   r   Zprinter	file_nameZ
file_mtimeZunified_diff_linesliner   r   r   show_unified_diff,   s    



r)   )r   r	   c                 C   sH   d }|dvrDt d|  d}| }|dv r0dS |dv rtd qdS )	N)ZyesynonquitqzApply suggested changes to 'z' [y/n/q]? )r+   r,   F)r-   r.      T)inputlowersysexit)r   Zanswerr   r   r   $ask_whether_to_apply_changes_to_fileM   s    r4   
)contentline_separatorr	   c                 C   s    |  |d dd dd} | S )Nr    )r   )r6   r7   r   r   r   remove_whitespaceY   s    r:   c                   @   s\   e Zd ZdZdZdeeee dddZedddd	Z	eddd
dZ
eddddZdS )BasicPrinterERRORSUCCESSNerrorsuccessr   c                 C   s   |pt j| _|| _|| _d S N)r2   stdoutr   success_messageerror_messageselfr?   r@   r   r   r   r   __init__b   s    zBasicPrinter.__init__)messager	   c                 C   s    t | jj| j|d| jd d S )N)r@   rH   file)printrC   formatr=   r   rF   rH   r   r   r   r@   g   s    zBasicPrinter.successc                 C   s    t | jj| j|dtjd d S )N)r?   rH   rI   )rK   rD   rL   r<   r2   stderrrM   r   r   r   r?   j   s    zBasicPrinter.errorr(   r	   c                 C   s   | j | d S rA   )r   write)rF   r(   r   r   r   r&   m   s    zBasicPrinter.diff_line)N)__name__
__module____qualname__r<   r=   r"   r   r   rG   r@   r?   r&   r   r   r   r   r;   ^   s   r;   c                       sV   e Zd Zeeee d fddZedeee edddZeddd	d
Z	  Z
S )ColoramaPrinterr>   c                    sN   t  j|||d | dtjj| _| dtjj| _tjj| _	tjj| _
d S )N)r   r<   r=   )superrG   
style_textcoloramaZForeZREDr<   ZGREENr=   
ADDED_LINEREMOVED_LINErE   	__class__r   r   rG   r   s
    
zColoramaPrinter.__init__N)textstyler	   c                 C   s    |d u r| S ||  t tjj S rA   )r"   rW   ZStyleZ	RESET_ALL)r\   r]   r   r   r   rV   |   s    zColoramaPrinter.style_textrO   c                 C   sB   d }t t|r| j}nt t|r*| j}| j| || d S rA   )	rematchADDED_LINE_PATTERNrX   REMOVED_LINE_PATTERNrY   r   rP   rV   )rF   r(   r]   r   r   r   r&      s    zColoramaPrinter.diff_line)N)rQ   rR   rS   r"   r   r   rG   staticmethodrV   r&   __classcell__r   r   rZ   r   rT   q   s   
rT   r   )colorr   r?   r@   r	   c                 C   s@   | r$t r$d}t|tjd td | r4t|||S t|||S )Nz
Sorry, but to use --color (color_output) the colorama python package is required.

Reference: https://pypi.org/project/colorama/

You can either install it separately on your system or as the colors extra for isort. Ex: 

$ pip install isort[colors]
rI   r/   )colorama_unavailablerK   r2   rN   r3   rT   r;   )rd   r   r?   r@   Zno_colorama_messager   r   r   r!      s    
r!   )r5   )Nr   r   )r^   r2   r   Zdifflibr   pathlibr   typingr   r   rW   ImportErrorre   initcompiler`   ra   r"   r   r   boolr)   r4   r:   r;   rT   r!   r   r   r   r   <module>   sD   


! 