Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/isort/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/isort/__pycache__/isort.cpython-37.pyc

B

�V��@s�dZddlmZmZmZmZddlZddlZddlZddl	Z	ddl
Z
ddlZddlm
Z
ddlmZddlmZddlmZddlmZdd	lmZdd
lmZddlmZdd
lmZddlTddddd�ZGdd�de�Zddd�ZdS)a�isort.py.

Exposes a simple library to sort through imports within Python code

usage:
    SortImports(file_name)
or:
    sorted = SortImports(file_contents=file_contents).output

Copyright (C) 2013  Timothy Edmund Crosley

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

�)�absolute_import�division�print_function�unicode_literalsN)�
namedtuple)�datetime)�unified_diff)�fnmatch)�glob)�path)�stdout�)�settings)�nsorted)�*ZSTANDARD_LIBRARYZFUTURE_LIBRARYZFIRST_PARTYZTHIRD_PARTY)�STDLIBZFUTUREZ
FIRSTPARTY�
THIRDPARTYc@seZdZdZdZd:dd�Zdd�Zedd��Zd	d
�Z	dd�Z
ed
d��Zdd�Zed;dd��Z
d<dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zd d!�Zd"d#�Zd$d%�Zd&d'�Zd(d)�Zd*d+�Zd,d-�Zed=d.d/��Zed0d1��Zed2d3��Zd4d5�Zd6d7�Zd8d9�Z dS)>�SortImportsFNcs�|s|rtj�tj�|��}|p&t��}t�|����_x�t	|�D]�\}	}
|	�
dd���}|dkr�t�j�
|��ttfkr�|	�d�r�tt�j|��|
���j|<q�tt�j|��|
���j|<qB|
�j|	<qBW�j�
dd�r��j�ddddd��t�jd	�}|���rd
t|�}n"|�d��d�}|��d
k�r>d}|�jd	<i�_i�_�fdd��j�
dg�D��_�fdd��j�
dg�D��_dd�t	�j�D��_d�_|}
|�p�d�_|�r>tj�|�}t� |�j��rd�_!�jd�r�t"d�#|��d}n:|�s>|�_t$|��_t%j&|�jd��}|�'�}WdQRX|dk�sRd|k�rVdS|�(d��_)t*�j)��_+�j+dk�s��j)dd�gdgfk�s��j�
dd��r�x�jD]}�j)�,|��q�Wt*�j)��_-g�_.iiiiid�d��_/i�_0i�_1�j�
d�}t2d|�dd�|D���_3x0t4�5�j3�jd �D]}t�id��j0|<�q2Wd!�_6d"�_7d"�_8d"�_9��:��j7d"k�r���;�t*�j.��j+�_<x.�j.�rˆj.d"��dk�rˆj.�=d"��q�W�j.�,d�d�>�j.��_?�j�
d#d��r�yt@��A�j.��jd$d!d�WnxtBk
�r�|�_?d�_Cy.t@��A�j)��jd$d!d�t"d%�#�j��Wn&tBk
�r~t"d&�#�j��YnXdSX|�r�j?�
dd��
d
d�|�
dd��
d
d�k�rֈjd�rt"d'�#�j��n:t"d(�#�j��d�_C|�s�j�
d)d�dk�r��D|�dS|�s.�j�
d)d�dk�r:��D|�n�|�rNtE�F�j?�n�|
�r�|�r��j?|k�rjdS��D|�d}xF|d*k�r�tGd+�#�j����}|d,k�r�dS|d-k�rztH�Id��qzWt%j&�j�jd.d/��}|�F�j?�WdQRXdS)0N�not_��sectionsZforce_alphabetical_sortFTr
)�'force_alphabetical_sort_within_sections�no_sections�lines_between_types�
from_first�indent� �'�"�tab�	csg|]}��|��qS�)�_format_simplified)�.0Zremoval)�selfr!�</opt/alt/python37/lib/python3.7/site-packages/isort/isort.py�
<listcomp>^sz(SortImports.__init__.<locals>.<listcomp>�remove_importscsg|]}��|��qSr!)�_format_natural)r#Zaddition)r$r!r%r&_s�add_importscSs&g|]\}}|�d�r|rd|�qS)Zimport_headingz# )�
startswith)r#�key�valuer!r!r%r&`szutf-8�verbosezbWARNING: {0} was skipped as it's listed in 'skip' setting or matches a glob in 'skip_glob' setting)�encodingzisort:skip_file�
Z
force_adds)�straight�from)r1r0�nested�aboveZSectionscSsg|]}|�qSr!r!)r#�namer!r!r%r&�s�forced_separater���Zatomic�execzSERROR: {0} isort would have introduced syntax errors, please report to the project!z'ERROR: {0} File contains syntax errors.z#SUCCESS: {0} Everything Looks Good!z*ERROR: {0} Imports are incorrectly sorted.�	show_diff)�yes�y�no�n�quit�qz)Apply suggested changes to '{0}' [y/n/q]?)r;r<)r=r>�w)r.�mode)J�osr�dirname�abspath�getcwdrZ	from_path�copy�configZ	itemsview�replace�lower�type�get�list�tupler*�set�
difference�union�update�str�isdigit�int�strip�
place_imports�import_placementsr'r)�_section_comments�
file_encoding�	file_pathZshould_skip�skipped�print�format�coding_check�io�open�read�split�in_lines�len�original_length�append�number_of_lines�	out_lines�comments�imports�as_maprr�	itertools�chain�index�import_index�_first_comment_index_start�_first_comment_index_end�_parse�_add_formatted_importsZ
length_change�pop�join�output�compile�_strip_top_comments�SyntaxError�incorrectly_sorted�
_show_diffr�write�input�sys�exit)r$rY�
file_contentsZwrite_to_stdout�checkr8Z
settings_pathZask_to_applyZsetting_overridesr+r,Z
access_keyr�	file_nameZfile_to_import_sortZ
add_importZ
section_names�section�answer�output_filer!)r$r%�__init__;s� 
  



6,



zSortImports.__init__cCspxjt|�d�|j�d�|jd|jdt|jrBt�tj�	|j��nt�
��tt�
��d�D]}t�|�qZWdS)Nr
z:beforez:after)�fromfile�tofile�fromfiledate�
tofiledate)
r�
splitlinesrurYrQr�
fromtimestamprAr�getmtime�nowrr{)r$r�liner!r!r%rz�s
"zSortImports._show_diffcCs8t�|�}x"|r,|d�d�r,|dd�}qWd�|�S)z:Strips # comments that exist at the top of the given linesr�#r
Nr/)rEr*rt)�linesr!r!r%rw�s
zSortImports._strip_top_commentscs xD|jdD]6}|}|�d�s&d|}t||�s>t|d|�r|SqW|�d�rX|jjS|�d���fdd�tt��dd�D�}xP|D]H}xBt	|j�D]4}t
�||�}d	�|�
��}||j�|g�kr�|Sq�Wq�Wt�|j�d
�p�tj�d�}	d}
|	�r>��fd
d�td�|	��D�7��dd�td�|	��D�7�d�|	�}
xֈD]�}d�||�dd�f�}d�||�d�df�}
tj�|d��s�tj�|d��s�tj�|
��rDtj�|
��rDd|k�s�d|k�s�|	�r�|
|k�r�|jjSd|�
�k�s�d|�
�k�r|jjS|jdS�qDW|jdS)z�Tries to determine if a module is a python std import, third party import, or project code:

        if it can't determine - it assumes it is project code

        r5rz%s*�.csg|]}d��d|���qS)r�N)rt)r#Zfirst_k)�partsr!r%r&�sz,SortImports.place_module.<locals>.<listcomp>rr6z	known_{0}�virtual_envZVIRTUAL_ENVFcsg|]}|�kr|�qSr!r!)r#r)�pathsr!r%r&�sz{0}/lib/python*/site-packagescSsg|]}tj�|�r|�qSr!)rAr�isdir)r#rr!r!r%r&�sz	{0}/src/*z{0}/src/�/z.pyz.soz
site-packagesz
dist-packagesZpython2�python3�default_section)rF�endswithr	r*rZLOCALFOLDERra�rangerc�reversed�KNOWN_SECTION_MAPPINGrJr\rH�
PYTHONPATHrA�environr
rtrGr�existsr�rr)r$�module_namer5Z	path_globZmodule_names_to_checkZmodule_name_to_checkZ	placementZknown_placementZ
config_keyr�Zvirtual_env_src�prefix�module_pathZpackage_pathr!)r�r�r%�place_module�sF



 

$zSortImports.place_modulecCs|j|j}|jd7_|S)zDReturns the current line from the file while incrementing the index.r
)rbrm)r$r�r!r!r%�	_get_line
szSortImports._get_linecCs,d|krdS|�d�rdS|�d�r(dSdS)zPIf the current line is an import line it will return its type (from or straight)z
isort:skipNzimport r0zfrom r1)r*)r�r!r!r%�_import_types

zSortImports._import_typecCs|j|jkS)z.returns True if we are at the end of the file.)rmrf)r$r!r!r%�_at_endszSortImports._at_endcCs�d}|rt|���}nt|�}|r^|dr^|��rDt|�dkrDd}n|dd���rZd}nd}|��}d�||d	krzdp|d||d
r�tt|��d|p�|�S)Nr�
order_by_typer
�Ar�B�Cz	{0}{1}{2}Zforce_to_topZlength_sort�:)rQrH�isupperrcr\)r�rFZsub_imports�ignore_caser�r!r!r%�_module_keyszSortImports._module_keyrcCs$|r d�|�|�dd�|��p"|S)z:
            Returns a string with comments added
        z
{0}  # {1}rz; )r\�_strip_commentsrt)r$rhZoriginal_stringr!r!r%�
_add_comments1szSortImports._add_commentscCsH|j�dd�}t|�|jdk�r|tjjk�rx�dD]�}dt�|�d}t�||�r6|�	��
|�s6t�||�}g}x@t|�d|jdp�|jdkr�|r�|�|�
��|�|�}qxW|s�|�
�}|�|jd|�|����}|jd	r�d
�|||�Sd�|||�Sq6Wn6t|�|jdk�rD|tjjk�rDd|k�rDd
�|�S|S)zZ
            Returns an import wrapped to the specified line-length, if possible.
        �multi_line_outputr�line_length)�importr�z\b��wrap_lengthrZuse_parenthesesz
{0}{1} (
{2})z{0}{1} \
{2}z# NOQAz{0}  # NOQA)rFrJrcr�	WrapModes�NOQA�re�escape�searchrTr*rarersrt�_wrap�lstripr\)r$r�Z	wrap_modeZsplitter�expZ
line_partsZ	next_lineZ	cont_liner!r!r%r�8s("
&
"

zSortImports._wrapcCs�x�|D]z}||jkrq||jkr4d�||j|�}n
d�|�}|jdd�|d�}|rb|�|�|�|�|jd�|�|��qWdS)Nzimport {0} as {1}z
import {0}r3r0)	r'rjr\rhrs�extendrer�rJ)r$�straight_modulesr��section_output�module�import_definitionZcomments_abover!r!r%�_add_straight_importsSs




z!SortImports._add_straight_importsc	s��x�|D�]����jkrqd���}t�j|d��}t|��fdd�d�}�jrj��fdd�|D�}x�t�|�D]�}�d|}�j�|d	�}	|	rvd
�||	�}
�jdr�d|kr��jd
s�|
||�	|�<qv��
||
�}�jd�|�}��||�}|�
|�|�|�qvW|r�jd��d�}d|k�rZ�jd
�rZ��
��|d�|���}�n�jd�r�g}
xn|D]f}��|||�}�jd��i��|d�}|�r�|d�|�r�d�p�d|�7}|
�
��
|��d}�qpWd�|
�}�n�d	}d|k�r |�
��|d�|���|�d�d}d}x�t�|�D]�}�jd��i��|d�}|�r,��|||�}|d�|�rrd�ptd|�7}�jdd��d�}|�r�|�|�|�
��
|��|�|�d}�q,W|�r�|d�|�}n��||d�|��}|�s�d}t|�dk�rRt|��jdk�s,�j�d��rRtjj�j�dd���}t�d |�j�}d!t|�d}�jd"}�jd#�p��jd}||t�|�||||�}�jd$�rp|�d�}t|�}t|�dk�r�td%d�|dd&�D��}nd}|}x~t|d&�|k�rNt|�|k�rN|d'k�rN|}|d8}||t�|�||||�}|�d�}�q�Wnt|��jdk�rp��
|�}|r�jdd��d�}|�r�|�|�|�
|�qWdS)(Nzfrom {0} import r1cs��|�jd��S)NT)r�rF)r+)r�r$r!r%�<lambda>i�z/SortImports._add_from_imports.<locals>.<lambda>)r+cs"g|]}d��|��jkr|�qS)z{0}.{1})r\r')r#r�)r�r$r!r%r&ksz1SortImports._add_from_imports.<locals>.<listcomp>r�Fz
{0} as {1}�combine_as_importsrZcombine_starr0r!z{0}*Zforce_single_liner2z{0} {1}�;z  #r/Tr3z, rr
r�Zforce_grid_wrapr�rZ_output_rrr�Zbalanced_wrappingcSsg|]}t|��qSr!)rc)r#r�r!r!r%r&�sr6�
)r'r\rKrirrErjrJrFrmr�rhr�re�removersrtr�rcrr��_fieldsrH�getattr�_output_gridra�min)r$�from_modulesr�r�r�Zimport_startZfrom_imports�from_import�	submoduleZ	import_asr�Zimport_statementrhZimport_statementsZsingle_import_line�commentZstar_importZabove_commentsZoutput_mode�	formatterZdynamic_indentrr�r�Z
line_countZminimum_lengthZnew_import_statementr!)r�r�r$r%�_add_from_importsbs�














zSortImports._add_from_importscs��j�dd�}t��j�jd�}�j�dd�r�gid��jd<xP|D]H}�jdd��j|�dg���jdd��j|�di��qFWd}g}�x�|D�]�}t�j|d�}t	|�fd	d
�d�}t
t�j|d����}t	|�fdd
�d�}g}�j�d
d��rb��||||��j�dd��rR|�rR|�rR|�dg�jd���
|||�nP��
|||��j�dd��r�|�r�|�r�|�dg�jd���||||��j�dd��rڇfdd�}t	||d�}|r�|}	|	�jk�r�|�j|	<q��j�dt|	���d�}
|
�rDd�|
�}|�jdd�k�rD|�d|�||dg�jd7}q�Wx,dd�|dd�D�dgk�r�|���qbWd}�j�jk�r��j}n�jdk�rƈjdk�rƈj}|�j|d�<|t|�}
x6dd��j|
|
d�D�dgk�r�j�|
��q�Wt�j�|
k�r�d}d�_xD�j|
d�D]2}��|��sB|���d��sB|���rB|}P�qBW�jddk�r�d d�t�jd�D��j|
d�<nH|�d!��s�|�d"��s�|�d#��r�ddg�j|
d�<ndg�j|
d�<�j�r~g}xvt�j�D]h\}}|� |�|�j!k�r|��j�j!|�t�j�|k�sh�j|d��dk�r|� d��qW|�_dS)$z�Adds the imports back to the file.

        (at the index of the first import) sorted alphabetically and split between groups

        rFr5r)r0r1r0r1)rcs��|�j�S)N)r�rF)r+)r$r!r%r��r�z4SortImports._add_formatted_imports.<locals>.<lambda>)r+cs��|�j�S)N)r�rF)r+)r$r!r%r��r�rrrrZforce_sort_within_sectionscs2t�dd|�}t�dd|�}�jds.|��}|S)Nz^from rz^import r�)r��subrFrH)r�)r$r!r%�	by_module�s

z5SortImports._add_formatted_imports.<locals>.by_moduleZimport_heading_z# {0}r
Zlines_between_sectionscSsg|]}|���qSr!)rT)r#�	characterr!r!r%r&sz6SortImports._add_formatted_imports.<locals>.<listcomp>r6Nr�cSsg|]}|���qSr!)rT)r#r�r!r!r%r&
sr�Zlines_after_importscSsg|]}d�qS)rr!)r#r�r!r!r%r&s�def�class�@)"rFrJrkrlrrir�rPrKr�sorted�keysr�r�rUrQrHr\rg�insertrsrnrdrprorc�	_in_quote�
_skip_linerTr*r��	enumeratererV)r$Zsort_ignore_caserr�rur�r�r�r��section_nameZ
section_titleZsection_commentZ	output_atZimports_tailZnext_constructr�Z
new_out_linesrmr!)r$r%rr�s�
"&

 &&$
(z"SortImports._add_formatted_importsc	Cs�|d|�d�7}xp|r�|�d�}|�||d|�}t|�d�d�d|krt|�|d�|��d�||�}d}q|d|7}qW||jd	r�d
nddS)
N�(rz, r/r6r
z{0},z
{0}{1}�include_trailing_comma�,r�))rsr�rcrar\rF)	r$�	statementri�white_spacerr�rh�next_import�next_statementr!r!r%r�*s
zSortImports._output_gridcCsF|�||�d�d�d|}d�||d|�|�|jdr@dnd�S)Nrr�r/z{0}({1}{2}{3})z,
r�r)r�rsr\rtrF)r$r�rir�rr�rhZfirst_importr!r!r%�_output_vertical7szSortImports._output_verticalc	Csz||�d�7}xf|rt|�d�}|�||d|�}t|�d�d�d|krn|�|d�|��d�||�}d}|}qW|S)Nrz, r/r6�z{0}, \z
{0}{1})rsr�rcrar\)	r$r�rir�rr�rhr�r�r!r!r%�_output_hanging_indent@s
z"SortImports._output_hanging_indentcCs0d�||�|�|d|�|�|jdr*dnd�S)Nz{0}({1}
{2}{3}{4}
)z,
r�r�r)r\r�rtrF)r$r�rir�rr�rhr!r!r%�_output_vertical_hanging_indentLsz+SortImports._output_vertical_hanging_indentc	Cs�||�|d�d||�d�7}xJ|rl|�d�}d�||�}t|�d�d�d|krfd�|||�}|}q$W|jdr�|d	7}|S)
Nr�r/rz{0}, {1}r6r
z{0},
{1}{2}r�r�)r�rsr\rcrarF)	r$r�rir�rr�rhr�r�r!r!r%�_output_vertical_grid_commonUs"

z(SortImports._output_vertical_grid_commoncCs|�||||||�dS)Nr�)r�)r$r�rir�rr�rhr!r!r%�_output_vertical_gridasz!SortImports._output_vertical_gridcCs|�||||||�dS)Nz
))r�)r$r�rir�rr�rhr!r!r%�_output_vertical_grid_groupeddsz)SortImports._output_vertical_grid_groupedc	Cs�d�|d�|��}d�|�}|rFt|�dt|�|krVd�||�Snt|�|krV|S|r|d|krnd�||�Sd�||�Sn
d�|�SdS)	Nz{0}{1}z, r�z
{0}  # {1}r�z{0}  # NOQA {1}z{0}  # NOQA)r\rtrc)	r$r�rir�rr�rh�retvalZcomment_strr!r!r%�_output_noqags
zSortImports._output_noqacCsV|dkrg}d}|�d�}|dkrL|�||dd����d}|d|�}|||fS)z"Removes comments from import line.NFr�r6r
T)�findrerT)r�rh�new_comments�
comment_startr!r!r%r�xs
zSortImports._strip_commentscCsF|��}|�d�r,|�dd�}|�dd�}n|�d�rB|�dd�}|S)Nzfrom rz import r�zimport )rTr*rG)�import_liner!r!r%r"�s

zSortImports._format_simplifiedcCsX|��}|�d�sT|�d�sTd|kr.d�|�S|�d�}|�d�}d�d�|�|�S|S)Nzfrom zimport r�z
import {0}r6zfrom {0} import {1})rTr*r\rarsrt)r�r��endr!r!r%r(�s


zSortImports._format_naturalcCsV|j}|jdkr$|�d�r$d|_dS|jrB|�d�sBd|_|j|_d|ksTd|k�rBd}|jdkr~|�d�sv|�d�r~|j|_x�|t|�k�r@||d	kr�|d7}n�|jr�|||t|j��|jkr�d|_|j|jkr�|j|_nT||d
k�r&|||d�}|dk�r||_|d
7}n
|||_n||dk�r6P|d7}q�W|�pT|j�pT|jS)Nr
r�TFrrrr6�\)rrr�)z"""z'''r�)r�rmr*�_in_top_commentrprorc)r$r��	skip_linermZ
long_quoter!r!r%r��s<




zSortImports._skip_linecCsx|�dd�}xdD]}|�|d�}qW|��}xdD]}||kr4|�|�q4Wd�|�}|�dd�}|�dd��dd	�S)
NZ_importz[[i]])r�r�r�r�r)r1r�z{ z{|z }z|})rGrar�rt)r$�
import_stringZ
remove_syntaxZimport_listr+r!r!r%�
_strip_syntax�s


zSortImports._strip_syntaxc	Cs�d|_d|_�x�|���s�|��}|j}|�|�}||jkrX|sX|jdkr|jd|_qd|kr�|�d�r�|�	d�d�	�d�
�}g|j|<||j|<d|kr�x8dd	�|�	d�D�D] }|r�|�d
�s�|�d�s�d}q�W|�
|�}|r�|r�|j�|�q�x�d
d	�|�	d�D�D�]t}|�
|�}|�s8|j�|��q|�dd�}|jdk�r\|jd|_i}|�|�\}}	}
dd�|�|����	d�D�}|dk�r�t|�dk�r�|ddk�r�|
�r�|	d||d<d|k�r^|���s^x||���d��sZ|���sZ|�|��|	�\}}	}
|�|���}|dk�rJ|�rJd|k�rJ|
�rJ|	d||<|d|7}�q�Wn�x�|���d��r|�|��|	�\}}	}
|�|���}|dk�r�|�r�d|k�r�|
�r�|	d||<|���d��s�|���d��r�|d|7}n|���d�|��}�q`W|dk�rj|�dd�}|�	d�}|d�	d�}
d�|
ddd�|
dd��g|dd��}dd�|�|��	�D�}d |k�r@|�d �dt|�k�r@x�d |k�r>|�d �}|dk�r�|dd!||d}||d|j|<n||d}||d|j|<|jd"�s,|	|jd#|<g}	|||d�=�q�W|dk�r|�d�}|�|�}|dk�rxtd$�||��|j ||}xD|D]<}|�!|�}|�r�||jd%�"|i�|<|	�|	�|���q�W|	�r�|jd�"|g��#|	�t|j�t$|j|j%d�dk�r�|j�r"|jd���p$d}x�|�d��r�|�d&��s�|�d'��s�d|k�r�|jd(d�"|g��&d|j�d��t|j�t$|jd|j%d�dk�r�|jd��}nd}�q(W|d|jk�r�|jt|jd(d�!|g��8_|�!|d��r||�'|�nt(|�||<�q�xf|D�]\}|	�rB|	|jd#|<d}	t|j�t$|j|j%d�dk�rH|j�rz|jd���p|d}x�|�d��r|�d&��s|�d'��sd|k�r|jd(d#�"|g��&d|j�d��t|j�t$|jd|j%d�dk�r|jd��}nd}�q�W|jd|jk�rH|jt|jd(d#�!|g��8_|�|�}|dk�rltd$�||��|j ||�)|��q$W�qWqWdS))z9Parses a python file taking out and categorizing imports.Fr6r
zisort:imports-r�rr�css|]}|��VqdS)N)rT)r#�partr!r!r%�	<genexpr>�sz%SortImports._parse.<locals>.<genexpr>zfrom zimport Tcss|]}|��VqdS)N)rT)r#r�r!r!r%r��sr rcSsg|]}|r|�qSr!r!)r#r�r!r!r%r&�sz&SortImports._parse.<locals>.<listcomp>r1r�rr�r�r/r�z importzimport(zimport (z import rNcSs g|]}|�dd��dd��qS)z{|z{ z|}z })rG)r#�itemr!r!r%r&s�asr�r�r0z[WARNING: could not place module {0} of line {1} -- Do you need to define a default section?r2z"""z'''r3)*r�r�r�r�rmr�rWrnr*ra�upperrUrVr�rgrerGr�r�rTrcr��rstripr�rtrjrFrhrsr�r[r\rirJ�
setdefaultr��maxrpr�rPrM�add)r$r�Zstatement_indexr�r�r�Zimport_typeZnested_commentsr�rhr�Z
stripped_liner�r�rirmr�Zimport_fromZ
placed_module�rootZimport_nameZassociated_comment�lastr!r!r%rq�s�





,   

2"








 &
&$
$ &

$


zSortImports._parse)NNFFFNF)FF)r)N)!�__name__�
__module__�__qualname__ryrZr�rz�staticmethodrwr�r�r�r�r�r�r�r�r�rrr�r�r�r�r�r�r�r�r�r"r(r�r�rqr!r!r!r%r7sB
4

ga
		#r�utf-8c	Cslt�d�}|}t�|d��H}x@t|d�D]2\}}t�||�}|rP|d�d�}P|dkr(Pq(WWdQRX|S)Nscoding[:=]\s*([-\w.]+)�rbr
r�asciir�)r�rvr^r_r��findall�decode)�fname�default�patternZcoding�fZline_numberr��groupsr!r!r%r]_s
r])r)�__doc__�
__future__rrrrrEr^rkrAr�r}�collectionsrr�difflibrr	r
rr�rrrZnaturalrZ	pie_slicer��objectrr]r!r!r!r%�<module>s:.