B
    0zf#                 @   s   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	Z	d dl
Z
dZG dd	 d	eZG d
d deZG dd deZee	dreneZdS )    )redirect_stdout)Differ)unified_diff)StringIO)PathNz4.2.0c               @   sT   e Zd ZdZeZdZdZdZdZ	dZ
dZdZd	Zd
d Zedd Zedd ZdS )Flake8IsortBaseZflake8_isortz0I001 isort found an import in the wrong positionz>I002 no configuration found (.isort.cfg or [isort] in configs)z4I003 isort expected 1 blank line in imports, found 0z4I004 isort found an unexpected blank line in importsz-I005 isort found an unexpected missing importFNTc             C   s   || _ || _d S )N)filenamelines)selftreer   r	    r   O/home/ankuromar296_gmail_com/.local/lib/python3.7/site-packages/flake8_isort.py__init__'   s    zFlake8IsortBase.__init__c             C   s   |j ddddd d S )Nz--isort-show-traceback
store_trueTz(Show full traceback with diff from isort)actionZparse_from_confighelp)
add_option)clsparserr   r   r   add_options+   s
    zFlake8IsortBase.add_optionsc             C   s   |j | _ |j| _d S )N)stdin_display_nameZisort_show_tracebackshow_traceback)r   optionsr   r   r   parse_options4   s    zFlake8IsortBase.parse_options)__name__
__module____qualname__name__version__versionisort_unsortedZno_config_msgisort_blank_reqisort_blank_unexpisort_add_unexpr   r   Zsearch_currentr   classmethodr   r   r   r   r   r   r      s   	r   c               @   s@   e Zd ZdZdd Zdd Zdd Zedd	 Zed
d Z	dS )Flake8Isort4zclass for isort <5c          	   c   s   | j | jk	r| j }nd }t }t|  tj|d| jddd}W d Q R X | |}x6| 	|D ](\}}| j
rx||7 }|d|t| fV  qbW d S )N T)	file_pathfile_contentscheckZ	show_diffr   )r   r   r   r   isortZSortImportsjoinr	   _format_isort_outputsortimports_linenum_msgr   type)r
   r'   buffersort_result	tracebackline_nummessager   r   r   run=   s    


zFlake8Isort4.runc             c   s   |j r
dS | | | | t }||j|j}d}dd |jD }x|D ]}|dddrj|d7 }qN|dddr|d7 }|	 d	kr|| j
fV  q|| jfV  qN|	 d
kr|d | jfV  qN|	 |krN|d | jfV  qNW dS )a  Parses isort.SortImports for line number changes and message

        Uses a diff.Differ comparison of SortImport `in_lines`:`out_lines` to
        yield the line numbers of import lines that have been moved or blank
        lines added.

        Args:
            sort_imports (isort.SortImports): The isorts results object.

        Yields:
            tuple: A tuple of the specific isort line number and message.
        Nr   c             S   s   h | ]}d  |qS )z+ {})format).0Z
add_importr   r   r   	<setcomp>h   s   z7Flake8Isort4.sortimports_linenum_msg.<locals>.<setcomp>z        z- -+)Zskipped_fixup_sortimports_wrapped_fixup_sortimports_eofr   comparein_lines	out_linesadd_imports
startswithstripr"   r    r!   r#   )r
   r0   Zdifferdiffr2   	additionsliner   r   r   r-   Q   s*    




z$Flake8Isort4.sortimports_linenum_msgc                sT   d dg}| fdd|   D 7 }t|dkr@|dd |d d|S )N)z+++z---z@@zERROR:r&   c                s(   g | ] }|  d dd  kr|qS ) r9   r   )rC   split)r6   rF   )filtering_outr   r   
<listcomp>   s   z5Flake8Isort4._format_isort_output.<locals>.<listcomp>r9   
)getvalue
splitlinesleninsertappendr+   )r
   Zisort_bufferZvalid_linesr   )rI   r   r,   z   s    

z!Flake8Isort4._format_isort_outputc             C   s\   dht | jB }xFt| jD ]8}| |krFt| jdkrT| j  q| jd P qW dS )ag  Ensure single end-of-file newline in `isort.SortImports.in_lines`

        isort fixes EOF blank lines but this change should be suppressed as
        Flake8 will also flag them.

        Args:
            sort_imports (isort.SortImports): The isorts results object.

        Returns:
            isort.SortImports: The modified isort results object.
        r&   r9   N)setrA   reversedr?   rC   rN   poprP   )sort_importsZ	to_removerF   r   r   r   r=      s    z#Flake8Isort4._fixup_sortimports_eofc             C   sX   xRt | jD ]D\}}d|krx2t | j| D ]\}}| j|| | q2W qW dS )a  Split-up wrapped imports newlines in `SortImports.out_lines`

        isort combines wrapped lines into a single list entry string in
        `out_lines` whereas `in_lines` are separate strings so for diff
        comparison these need to be comparable.

        Args:
            sort_imports (isort.SortImports): The isorts results object.

        Returns:
            isort.SortImports: The modified isort results object.
        rK   N)	enumerater@   rS   rM   rO   )rT   idxrF   Znew_idxnew_liner   r   r   r<      s
    z'Flake8Isort4._fixup_sortimports_wrappedN)
r   r   r   __doc__r4   r-   r,   staticmethodr=   r<   r   r   r   r   r%   :   s   )r%   c               @   s    e Zd ZdZdd Zdd ZdS )Flake8Isort5zclass for isort >=5c          
   c   sn  | j | jk	r(t| j }tjj|jd}nd }tjjt d}d| j	}d}d}t
|}t
 }t
 }y,t| tjj||||d}W d Q R X W nF tjjk
r   Y n0 tjjk
r }	 zt|	 W d d }	~	X Y nX |rj| }
dt|jdd|
jddd| j d| j d	}| d
 | }x:| |D ],\}}| jrR||7 }|d|t| fV  q:W d S )N)Zsettings_pathr&   F)input_streamoutput_streamconfigr'   T)keependsz	{}:beforez{}:after)fromfiletofilerK   r   )r   r   r   r*   settingsZConfigparentcwdr+   r	   r   r   apiZsort_stream
exceptionsZFileSkippedZ
ISortErrorwarningswarnrL   r   rM   r5   isort_linenum_msgr   r.   )r
   r'   Zisort_configZinput_stringr1   Zisort_changedr[   r\   Zisort_stdouteZoutlinesZ
diff_deltar2   r3   r   r   r   r4      sH    




zFlake8Isort5.runc             c   s6  d}g }g }x|  D ]}|dddrPt|dd dd dd }qn|sVq|dddrn|d7 }q|d	ddr| d	kr|| jfV  |d7 }q||dd  || jfV  |d7 }q|d
ddr| d
kr|| jfV  q|||f qW x0|D ](\}}|dd |kr|| j	fV  qW dS )zParse unified diff for changes and generate messages

        Args
        ----
        udiff : unified diff delta

        Yields
        ------
        tuple: A tuple of the specific isort line number and message.
        r   z@@r8      NrG   ,r9   r:   r;   )
rM   rB   intrH   rC   r"   rP   r    r!   r#   )r
   udiffr2   rE   movesrF   r   r   r   rh      s2    $


zFlake8Isort5.isort_linenum_msgN)r   r   r   rX   r4   rh   r   r   r   r   rZ      s   'rZ   rd   )
contextlibr   difflibr   r   ior   pathlibr   r*   rf   r   objectr   r%   rZ   hasattrZFlake8Isortr   r   r   r   <module>   s   *}U