Your IP : 216.73.216.196


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

B

A!p\���@sfdZddlZddlmZddlZddlZddlZddlZddlmZm	Z	ddl
mZmZddl
Z
ddlmZddlmZddlmZdd	lmZdd
lmZmZmZddlmZmZddlmZmZd
d�Z dd�Z!dd�Z"dd�Z#dd�Z$d:dd�Z%dd�Z&dd�Z'dddd d!gifd"d#d$d%d&d'd(d)d*ifd+d,d-d.d/d0d1d2d3�Z(d4Z)d5Z*Gd6d7�d7e�Z+d8d9�Z,dS);z imports checkers for Python code�N)�	sysconfig)�
are_exclusive�
decorators)�get_module_part�is_standard_module)�IAstroidChecker)�get_global_option)�EmptyReportError)�BaseChecker)�check_messages�node_ignores_exception�is_from_fallback_block)�
get_cycles�
DotBackend)�VerbatimText�	Paragraphcs$|�d���fdd�tt���D�S)z�Split the names of the given module into subparts

    For example,
        _qualified_names('pylint.checkers.ImportsChecker')
    returns
        ['pylint', 'pylint.checkers', 'pylint.checkers.ImportsChecker']
    �.cs"g|]}d��d|d���qS)rr�)�join)�.0�i)�names��H/opt/alt/python37/lib/python3.7/site-packages/pylint/checkers/imports.py�
<listcomp>Asz$_qualified_names.<locals>.<listcomp>)�split�range�len)�modnamer)rr�_qualified_names8s
rcCs:t|tj�r6|jr6|��}t|tj�r6|j||jd�}|S)aGet a prepared module name from the given import node

    In the case of relative imports, this will return the
    absolute qualified module name, which might be useful
    for debugging. Otherwise, the initial module name
    is returned unchanged.
    )�level)�
isinstance�astroid�
ImportFromr �root�ModuleZrelative_to_absolute_name)�
importnoderr$rrr�_get_import_nameDsr'c
s�|rd||fn|�d}d}x�|jD]�}||kr2q$|��|��krP|j|jkrPq$t|tj�r|t�fdd�|jD��r�d}Pq$t|tj�r$||j	kr$xH|jD]>\}}	�d|j
|fkr�d}P|dkr�||kr�|s�|	s�d}Pq�W|r$Pq$W|r�t||�s�|SdS)zIreturn the node where [base.]<name> is imported or None if not found
    z%s.%sNFc3s|]}�|dkVqdS)rNr)rZiname)�fullnamerr�	<genexpr>csz$_get_first_import.<locals>.<genexpr>T�*)�body�scope�
fromlinenor!r"�Import�anyrr#r rr)
�node�context�name�baser �alias�first�found�
imported_nameZimported_aliasr)r(r�_get_first_importVs8
r8cCs(xt|�D]}||kr
dSq
Wt|t�S)NT)rr�ImportError)r0rZignored_modulesZ	submodulerrr�_ignore_import_failurezsr:cCsZi}xP|D]H\}}|df}x&|�d�D]}|d�|igg�}q&W|d|7<q
W|S)z�get a list of 2-uple (module, list_of_files_which_import_this_module),
    it will return a dictionary to represent this as a tree
    rrrr)r�
setdefault)Zmod_files_listZ	tree_defs�mod�filesr0�prefixrrr�_make_tree_defs�sr?c	Cs�g}|��}x�tt|dd�d��D]�\}\}\}}|s<d}ndd�t|��}|dkrn|�d||f�d	}n6|�d
|||f�|t|�dkr�d|}nd
|}|r"|�t||��q"Wd�|�S)z2return a string which represents imports as a treecSs|dS)Nrr)�xrrr�<lambda>��z!_repr_tree_defs.<locals>.<lambda>)�key�z(%s)�,Nz%s %sz  z	%s\-%s %srz%s  z%s| �
)�items�	enumerate�sortedr�appendr�_repr_tree_defs)	�dataZ
indent_str�linesZnodesrr<�subr=Zsub_indent_strrrrrK�s &
rKcCs�i}t|dd�dd�}|�d�xRt|���D]B\}}d||<|�|�x&|D]}||krPd||<|�|�qPWq0Wx2t|���D]"\}}x|D]}|�||�q�Wq�W|�|�dS)z0write dependencies as a dot (graphviz) file
    N���ZLR)ZrankdirzURL="." node[shape="box"]r)r�emitrIrGZ	emit_nodeZ	emit_edgeZgenerate)�filename�dep_info�done�printerr�dependenciesZ
depmodnamerrr�_dependencies_graph�s



rVcCs$t||�|�td||f��dS)z`generate a dependencies graph and add some information about it in the
    report's section
    z&%simports graph has been written to %sN)rVrJr)rQrR�sectZgtyperrr�_make_graph�s
rXzUnable to import %szimport-errorz4Used when pylint has been unable to import a module.Z	old_names)ZF0401zimport-error)z2Attempted relative import beyond top-level packagezrelative-beyond-top-levelzSUsed when a relative import tries to access too many levels in the current package.)zCyclic import (%s)z
cyclic-importzBUsed when a cyclic import between two or more modules is detected.)zWildcard import %szwildcard-importz-Used when `from module import *` is detected.)zUses of a deprecated module %rzdeprecated-modulez/Used a module marked as deprecated is imported.z Relative import %r, should be %rzrelative-importzBUsed when an import relative to the package directory is detected.Z
maxversion)�r)zReimport %r (imported line %s)�
reimportedz0Used when a module is reimported multiple times.)zModule import itselfzimport-selfz'Used when a module is importing itself.)z:__future__ import is not the first non docstring statementzmisplaced-futurezgPython 2.5 and greater require __future__ import to be the first non docstring statement in the module.)z!Multiple imports on one line (%s)zmultiple-importszBUsed when import statement importing multiple modules is detected.)z%s should be placed before %szwrong-import-orderzuUsed when PEP8 import order is not respected (standard imports first, then third-party libraries, then local imports))z'Imports from package %s are not groupedzungrouped-importsz-Used when imports are not grouped by packages)z5Import "%s" should be placed at the top of the modulezwrong-import-positionz$Used when code and imports are mixed)z-Import alias does not rename original packagezuseless-import-aliaszsUsed when an import alias is same as original package.e.g using import numpy as numpy instead of import numpy as np)ZE0401ZE0402ZR0401ZW0401ZW0402ZW0403ZW0404ZW0406ZW0410ZC0410ZC0411ZC0412ZC0413ZC0414r)Zenchantc
@s�eZdZdZeZdZeZdZ	e
jdkr,dZndZdedd	d
d�fdd
dddd�fdd
dddd�fdd
dddd�fde
dd	dd�fdedd	dd�fdddddd�fdddddd�ffZd[d!d"�Zed#d$��Zd%d&�Zd'd(�Zd)d*�Zee�d+d,��Zee�d-d.��Zee�d/d0��Zd1d2�ZeZZZZZZ Z!Z"d3d4�Z#e#Z$Z%Z&d5d6�Z'd7d8�Z(d9d:�Z)d;d<�Z*ed=d>��Z+d?d@�Z,dAdB�Z-dCdD�Z.dEdF�Z/dGdH�Z0dIdJ�Z1d\dKdL�Z2dMdN�Z3dOdP�Z4dQdR�Z5e6j7dSdT��Z8e6j7dUdV��Z9dWdX�Z:dYdZ�Z;d S)]�ImportsCheckerz�checks for
    * external modules dependencies
    * relative / wildcard imports
    * cyclic imports
    * uses of deprecated modules
    �imports���)rY�)�optparse)r_ztkinter.tixzdeprecated-modulesZcsvz	<modules>zBDeprecated modules which should not be used, separated by a comma.)�default�type�metavar�helpzimport-graphrD�stringz
<file.dot>zyCreate a graph of every (i.e. internal and external) dependencies in the given file (report RP0402 must not be disabled).zext-import-graphz_Create a graph of external dependencies in the given file (report RP0402 must not be disabled).zint-import-graphz_Create a graph of internal dependencies in the given file (report RP0402 must not be disabled).zknown-standard-libraryzYForce import order to recognize a module as part of the standard compatibility libraries.zknown-third-partyzJForce import order to recognize a module as part of a third party library.zanalyse-fallback-blocksFZynz<y_or_n>z�Analyse import fallback blocks. This can be used to support both Python 2 and 3 compatible code, which means that the block might have code that exists only in one or another interpreter, leading to false positives when analysed.zallow-wildcard-with-allz8Allow wildcard imports from modules that define __all__.NcCsRt�||�d|_d|_g|_d|_i|_dd|jfdd|jff|_	|�
�|_dS)NZRP0401zExternal dependenciesZRP0402zModules dependencies graph)r
�__init__�stats�import_graph�_imports_stack�_first_non_import_node�_module_pkg�_report_external_dependencies�_report_dependencies_graphZreports�_compute_site_packagesZ_site_packages)�self�linterrrrre�s
zImportsChecker.__init__c	Cs�dd�}t�}ttdd�}x6td|tjf�D]"}tj|d�}||�}|�|�q,Wtj	�
d�r�x>td|tjf�D]*}tj	�|dddt��d	�}|�|�qpW|S)
NcSstj�tj�|��S)N)�os�path�normcase�abspath)rqrrr�_normalized_path�sz?ImportsChecker._compute_site_packages.<locals>._normalized_path�real_prefix)r>z/etc/debian_version�local�lib�pythonz
dist-packages)
�set�getattr�sys�filterr>r�get_python_lib�addrprq�isfiler�get_python_version)rt�pathsrur>rq�	libpythonrrrrm�s"
z%ImportsChecker._compute_site_packagescCsX|jjid�|jjgd�|jj|_t�t�|_i|_t�t�|_t	|dgd�|_
dS)z3called before visiting project (i.e set of modules))rU)Zcycleszignored-modules)r`N)roZ	add_statsrf�collections�defaultdictryrgrj�_excluded_edgesr�_ignored_modules)rnrrr�open�s
zImportsChecker.opencCs2t�|j�}x |D]}||�|j|�qW|S)N)�copy�deepcopyrg�difference_updater�)rnZfiltered_graphr0rrr�#_import_graph_without_ignored_edges�s
z2ImportsChecker._import_graph_without_ignored_edgescCsJ|j�d�rF|��}t|�}x(t||d�D]}|jdd�|�d�q*WdS)z3called before visiting project (i.e set of modules)z
cyclic-import)�verticesz -> )�argsN)ro�is_message_enabledr��listr�add_messager)rn�graphr��cyclerrr�close�s
zImportsChecker.closecCs�|�|�|�|�|��}dd�|jD�}t|�dkrN|jdd�|�|d�x�|D]x}|�||�|�||�}t	|j
tj�r�|�
|�t	|��tj�r�|�||�|dkr�qT|�||||�|�||j�qTWdS)z*triggered when an import statement is seencSsg|]\}}|�qSrr)rr2�_rrrr�sz/ImportsChecker.visit_import.<locals>.<listcomp>�zmultiple-importsz, )r�r0N)�_check_reimport�_check_import_as_renamer$rrr�r�_check_deprecated_module�_get_imported_moduler!�parentr"r%�_check_positionr,�_record_import�_check_relative_import�_add_imported_moduler2)rnr0�modnoderr2�imported_modulerrr�visit_import�s"



zImportsChecker.visit_importcCs�|j}|�||�}|�|�|�|�|�||�|�||�|�|�|j|||jd�t	|j
tj�rr|�
|�t	|��tj�r�|�||�|dkr�dS|��}|�||||�x@|jD]6\}}|dkr�|�|d|j|f�q�|�||j�q�WdS)z'triggered when a from statement is seen)�basenamer Nr*z%s.%s)rr�r��_check_misplaced_futurer��_check_wildcard_imports�_check_same_line_importsr�r r!r�r"r%r�r,r�r$r�rr�r2)rnr0r�r�r�r2r�rrr�visit_importfrom�s(



zImportsChecker.visit_importfromcCs�|�|�\}}}t�}d}xl|||D]\\}}|j�d|j�sBq(|�d�\}	}
}
|rv||	krv|	|krv|jd||	d�|	}|�|	�q(Wg|_d|_	dS)Nzungrouped-importsr)r0r�)
�_check_imports_orderryror�r-�	partitionr�r~rhri)rnr0�std_importsZext_importsZloc_importsZmetZcurrent_package�import_nodeZimport_name�packager�rrr�leave_module�s
zImportsChecker.leave_modulecs�|j�d�j�sdS|jrdSt�jtj�s0dStjtj	g}�fdd�|D�}|rlt
��tjtj
f��rldSt�tj�r�dd��jD�}t|�r�dS�|_dS)Nzwrong-import-positioncsg|]}t�|�r|�qSr)r!)rZallowed)r0rrrsz@ImportsChecker.compute_first_non_import_node.<locals>.<listcomp>cSs0g|](}t|tj�o*|j�d�o*|j�d��qS)�__)r!r"Z
AssignNamer2�
startswith�endswith)r�targetrrrr%s)ror�r-rir!r�r"r%�	TryExcept�
TryFinallyr/�nodes_of_classr.r#ZAssign�targets�all)rnr0Znested_allowedZis_nested_allowedZ
valid_targetsr)r0r�compute_first_non_import_nodes"
z,ImportsChecker.compute_first_non_import_nodecCs�|j�d|j�sdS|jrdSt|j��tj�s4dS|}xt|jtj�sP|j}q:Wt|tj	tj
tjf�r�t|�
tjtjf��r�dS||_dS)Nzwrong-import-position)ror�r-rir!r�r,r"r%ZIfr�r�r/r�r.r#)rnr0r$rrr�visit_functiondef8s
z ImportsChecker.visit_functiondefcCsF|j}|dkrB|��}|r>t|tj�r0|jdks>|jd|d�dSdS)N�
__future__zmisplaced-future)r0)rZprevious_siblingr!r"r#r�)rnr0r��prevrrrr�Ps
z&ImportsChecker._check_misplaced_futurecCsRdd�|jD�}t�|�}x2|��D]&\}}|dkr$|jd|||jfd�q$WdS)Ncss|]\}}|VqdS)Nr)rr2r�rrrr)`sz:ImportsChecker._check_same_line_imports.<locals>.<genexpr>rrZ)r0r�)rr��CounterrGr�r-)rnr0r�counterr2�countrrrr�^s

z'ImportsChecker._check_same_line_importscCs|jr|jd||��d�dS)z�Check `node` import or importfrom node position is correct

        Send a message  if `node` comes before another instruction
        zwrong-import-position)r0r�N)rir��	as_string)rnr0rrrr�fszImportsChecker._check_positioncCstt|tj�r|j}n|r|jnd}|s>|jdd�d�d}t|tj�r`|jpRddkr`d|}|j�	||f�dS)z&Record the package `node` imports fromNrrr)
r!r"r#rr2rrr rhrJ)rnr0�importedmodnodeZimportednamerrrr�pszImportsChecker._record_importcs$dd�|D�}t�fdd�|D��S)NcSsg|]\}}|�qSrr)rr�r�rrrr�sz6ImportsChecker._is_fallback_import.<locals>.<listcomp>c3s|]}t�|��VqdS)N)r"r)rr�)r0rrr)�sz5ImportsChecker._is_fallback_import.<locals>.<genexpr>)r/)r0r\r)r0r�_is_fallback_import�sz"ImportsChecker._is_fallback_importcCs8g}g}g}g}g}g}g}g}	tjd|jj|jjd�}
�x�|jD�]�\}}|�d�rjd|�d�d}
n|�d�d}
t|j	t
j�}|j�
d|j�}|
�|
�}||
f}|dk�r|�|�|p�|p�|	}|�||�r�qB|�r*|�s*|jd|d|��d	|dd��fd
�qB|dk�r�|�|�|�|�|�sH|�sH|�|�|�pP|	}|�r*|�s*|jd|d|��d	|dd��fd
�qB|d
k�r�|�|�|�|�|�s�|�s�|�|�|	}|�r*|�s*|jd|d|��d	|dd��fd
�qB|dkrB|�||
f�|sB|sB|	�||
f�qBW|||fS)z�Checks imports of module `node` are grouped by category

        Imports must follow this order: standard, 3rd party, local
        rD)�
file_contents�known_third_party�known_standard_libraryrrrzwrong-import-order)ZFUTUREZSTDLIBzstandard import "%s"z"%s")r0r�Z
THIRDPARTYzthird party import "%s"Z
FIRSTPARTYzfirst party import "%s"ZLOCALFOLDER)�isortZSortImports�configr�r�rhr�rr!r�r"r%ror�r-Zplace_modulerJr�r�r�)rnZ_module_noder�Zthird_party_importsZfirst_party_importsZexternal_importsZ
local_importsZthird_party_not_ignoredZfirst_party_not_ignoredZlocal_not_ignoredZ	isort_objr0rr��nestedZignore_for_import_orderZimport_categoryZnode_and_package_importZwrong_importrrrr��s~















z#ImportsChecker._check_imports_orderc
Cs�y
|�|�Stjk
r@t|||j�r.dS|jd|d�Yn�tjk
r�}z(d�|t|j	��}|jd|j
|d�Wdd}~XYnjtjk
r�|j�
d�s�dSt|||j�r�dS|jjs�t|�r�dSt||�}|jdt|�|d�YnXdS)Nzrelative-beyond-top-level)r0z+Cannot import {!r} due to syntax error {!r}zsyntax-error)�liner�zimport-error)r�r0)Zdo_import_moduler"ZTooManyLevelsErrorr:r�r�ZAstroidSyntaxError�format�str�error�linenoZAstroidBuildingExceptionror�r�Zanalyse_fallback_blocksr
r'�repr)rnr&r�exc�messageZdotted_modnamerrrr��s(
"

z#ImportsChecker._get_imported_modulecCsj|j�d�sdS|jdkrdS||kr*dS|��s>t|dd�rBdS|j|krf|jd||jf|d�dSdS)zpcheck relative import. node is either an Import or From node, modname
        the imported module name.
        zrelative-importNFr )r�r0)ror��fileZabsolute_import_activatedrzr2r�)rnr�r&r�Zimportedasnamerrrr��s

z%ImportsChecker._check_relative_importcCs�|��j}|��j}tj�tj�|��d}yt||�}Wntk
rNYnX||krh|j	d|d�n�t
|�s�|dkr�||jkr�|�dd�d|j|<|j
d�|t��}||kr�|�|�|j|�|�|jjd|jd	�s�|j|�|�d
S)z7notify an imported module, used to analyze dependenciesrzimport-self)r0rerrrUz
cyclic-import)r�N)r$r�r2rprq�splitextr�rr9r�rrj�rsplitrfr;ryr~rgror�r�r�)rnr0Zimportedmodname�module_fileZcontext_namer3Zimportedmodnamesrrrr�s&




z#ImportsChecker._add_imported_modulecCs<x6|jjD]*}||ks$|�|d�r
|jd||d�q
WdS)z!check if the module is deprecatedrzdeprecated-module)r0r�N)r��deprecated_modulesr�r�)rnr0Zmod_path�mod_namerrrr�2sz'ImportsChecker._check_deprecated_modulecCsh|j}x\|D]T}t|�sdS|d}|�d�}|d}|d}||krt|�dkr|jd|d�qWdS)Nrr���rzuseless-import-alias)r0)rr�r�rr�)rnr0rr2Z	real_nameZsplitted_packagesr7rrrr�8s

z&ImportsChecker._check_import_as_renamec	Cs�|j�d�sdS|��}|��}||fg}||k	r@|�|df�xT|D]L\}}xB|jD]8\}	}
t|||	|||
�}|dk	rV|jd||	|jfd�qVWqFWdS)z8check if the import is necessary (i.e. not already done)rZN)r0r�)	ror��framer$rJrr8r�r-)rnr0r�r r�r$ZcontextsZ
known_contextZknown_levelr2r4r5rrrr�Is
zImportsChecker._check_reimportcCs4t|�����}|st��t|�}|�t|��dS)z4return a verbatim layout for displaying dependenciesN)r?�_external_dependencies_inforGr	rKrJr)rnrWr��_dummyrRZtree_strrrrrk^s
z,ImportsChecker._report_external_dependenciescCs�|jd}|r&|jjs,|jjs,|jjs,t��|jj}|rFt|||d�|jj}|rdt||��|d�|jj}|r�t||��|d�dS)z+write dependencies as a dot (graphviz) filerUrDz	external z	internal N)	rfr�rgZext_import_graphZint_import_graphr	rXr��_internal_dependencies_info)rnrWr�r�rRrQrrrrlfs
z)ImportsChecker._report_dependencies_graphcCsnt�t�}x^|jd��D]L\}}xB|D]:}|j�||�}|�|�}|rL|sT|s(|s(||�|�q(WqW|S)z2build the internal or the external depedency graphrU)	r�r�ryrfrGrj�getr�r~)rn�internalr�Zimportee�	importers�importerr�Z	is_insiderrr�_filter_dependencies_graphys


z)ImportsChecker._filter_dependencies_graphcCs|jdd�S)zXreturn cached external dependencies information or build and
        cache them
        F)r�)r�)rnrrrr��sz*ImportsChecker._external_dependencies_infocCs|jdd�S)zXreturn cached internal dependencies information or build and
        cache them
        T)r�)r�)rnrrrr��sz*ImportsChecker._internal_dependencies_infocCsN|��jrdS|�|�}x0|jD]&\}}|dkr |s |jd|j|d�q WdS)Nr*zwildcard-import)r�r0)r$r��_wildcard_import_is_allowedrr�r)rnr0r�Zwildcard_import_is_allowedr2r�rrrr��s

z&ImportsChecker._check_wildcard_importscCs|jjo|dk	od|jkS)N�__all__)r�Zallow_wildcard_with_all�locals)rnr�rrrr��sz*ImportsChecker._wildcard_import_is_allowed)N)NN)<�__name__�
__module__�__qualname__�__doc__rZ__implements__r2�MSGSZmsgs�priorityr{�version_infor��DEFAULT_STANDARD_LIBRARY�DEFAULT_KNOWN_THIRD_PARTY�optionsre�staticmethodrmr�r�r�rr�r�r�r�Zvisit_tryfinallyZvisit_tryexceptZvisit_assignattrZvisit_assignZvisit_ifexpZvisit_comprehensionZ
visit_exprZvisit_ifr�Zvisit_classdefZ	visit_forZvisit_whiler�r�r�r�r�r�r�r�r�r�r�r�rkrlr�r�cachedr�r�r�r�rrrrr[s�


( 
T!

r[cCs|�t|��dS)z.required method to auto register this checker N)Zregister_checkerr[)rorrr�register�sr�)N)-r�r��	distutilsrrpr{r�r"rrZastroid.modutilsrrr�Zpylint.interfacesrZpylint.utilsrZpylint.exceptionsr	Zpylint.checkersr
Zpylint.checkers.utilsrrr
Zpylint.graphrrZpylint.reporters.ureports.nodesrrrr'r8r:r?rKrVrXr�r�r�r[r�rrrr�<module>sj$