3
tûœ]%“  ã               @   sX  d Z ddlZddlZddlZddlZddlm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mZmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZmZ ddlmZ dd„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"dSdd„Z#dd„ Z$dd„ Z%dddd dTgifdUdVdWdXdYdZd[d\d]d^d_d`dadbdMœZ&f Z'dcZ(f Z)G dOdP„ dPeƒZ*dQdR„ Z+dS )dz imports checkers for Python codeé    N)Ú	sysconfig)Úmodutils)Úcached)ÚBaseChecker)Úcheck_messagesÚis_from_fallback_blockÚnode_ignores_exception)ÚEmptyReportError)Ú
DotBackendÚ
get_cycles)ÚIAstroidChecker)Ú	ParagraphÚVerbatimText)Úget_global_optionc                s$   | j dƒ‰ ‡ fdd„ttˆ ƒƒ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']
    Ú.c                s"   g | ]}d j ˆ d|d … ƒ‘qS )r   r   é   )Újoin)Ú.0Úi)Únames© ú9/tmp/pip-build-fn42xldr/pylint/pylint/checkers/imports.pyú
<listcomp>B   s    z$_qualified_names.<locals>.<listcomp>)ÚsplitÚrangeÚlen)Úmodnamer   )r   r   Ú_qualified_names9   s    
r   c             C   s:   t | tjƒr6| jr6| jƒ }t |tjƒr6|j|| jd�}|S )a   Get 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ÚModuleZrelative_to_absolute_name)Ú
importnoder   r"   r   r   r   Ú_get_import_nameE   s    r%   c       
         s  |rd||f n|‰ d}d}xÊ|j D ]À}|| kr2q$|jƒ | jƒ krP|j| jkrPq$t|tjƒr|t‡ fdd„|jD ƒƒräd}P q$t|tjƒr$||j	kr$xJ|jD ]@\}}	ˆ d|j
|f krºd}P |dkrš||krš|pÐ|	 ršd}P qšW |r$P q$W |rþtj|| ƒ rþ|S dS )zIreturn the node where [base.]<name> is imported or None if not found
    z%s.%sNFc             3   s   | ]}ˆ |d  kV  qdS )r   Nr   )r   Ziname)Úfullnamer   r   ú	<genexpr>d   s    z$_get_first_import.<locals>.<genexpr>TÚ*)ÚbodyÚscopeÚ
fromlinenor   r    ÚImportÚanyr   r!   r   r   Úare_exclusive)
ÚnodeÚcontextÚnameÚbaser   ÚaliasÚfirstÚfoundÚimported_nameZimported_aliasr   )r&   r   Ú_get_first_importW   s8    

r7   c             C   s(   xt |ƒD ]}||kr
dS q
W t| tƒS )NT)r   r   ÚImportError)r/   r   Zignored_modulesÚ	submoduler   r   r   Ú_ignore_import_failure{   s    r:   c             C   sZ   i }xP| D ]H\}}|f f}x&|j dƒD ]}|d j|i g gƒ}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
    r   r   r   )r   Ú
setdefault)Zmod_files_listZ	tree_defsÚmodÚfilesr/   Úprefixr   r   r   Ú_make_tree_defs†   s    r?   c       	      C   sÆ   g }| j ƒ }x®tt|dd„ d�ƒD ]–\}\}\}}|s<d}nddjt|ƒƒ }|dkrn|jd||f ƒ d	}n6|jd
|||f ƒ |t|ƒd krœd| }nd| }|r"|jt||ƒƒ q"W dj|ƒS )z2return a string which represents imports as a treec             S   s   | d S )Nr   r   )Úxr   r   r   Ú<lambda>—   s    z!_repr_tree_defs.<locals>.<lambda>)ÚkeyÚ z(%s)ú,Nz%s %sz  z	%s\-%s %sr   z%s  z%s| Ú
)ÚitemsÚ	enumerateÚsortedr   Úappendr   Ú_repr_tree_defs)	ÚdataZ
indent_strÚlinesZnodesr   r<   Úsubr=   Zsub_indent_strr   r   r   rJ   “   s     &
rJ   c             C   s¸   i }t | dd… dd�}|jdƒ xRt|jƒ ƒD ]B\}}d||< |j|ƒ x&|D ]}||krPd||< |j|ƒ qPW q0W x2t|jƒ ƒD ]"\}}x|D ]}|j||ƒ q’W q„W |j| ƒ dS )z0write dependencies as a dot (graphviz) file
    Né   ZLR)ZrankdirzURL="." node[shape="box"]r   éüÿÿÿ)r
   ÚemitrH   rF   Z	emit_nodeZ	emit_edgeÚgenerate)ÚfilenameÚdep_infoÚdoneÚprinterr   ÚdependenciesZ
depmodnamer   r   r   Ú_dependencies_graphª   s    



rW   c             C   s$   t | |ƒ |jtd|| f ƒƒ dS )z`generate a dependencies graph and add some information about it in the
    report's section
    z&%simports graph has been written to %sN)rW   rI   r   )rR   rS   ÚsectZgtyper   r   r   Ú_make_graph½   s    
rY   zUnable to import %szimport-errorz4Used when pylint has been unable to import a module.Z	old_namesÚF0401úold-import-errorú2Attempted relative import beyond top-level packageúrelative-beyond-top-levelúSUsed when a relative import tries to access too many levels in the current package.úCyclic import (%s)úcyclic-importúBUsed when a cyclic import between two or more modules is detected.úWildcard import %súwildcard-importú-Used when `from module import *` is detected.úUses of a deprecated module %rúdeprecated-moduleú/Used a module marked as deprecated is imported.úReimport %r (imported line %s)Ú
reimportedú0Used when a module is reimported multiple times.úModule import itselfúimport-selfú'Used when a module is importing itself.ú!Prefer importing %r instead of %rúpreferred-moduleú?Used when a module imported has a preferred replacement module.ú:__future__ import is not the first non docstring statementúmisplaced-futureúgPython 2.5 and greater require __future__ import to be the first non docstring statement in the module.ú!Multiple imports on one line (%s)úmultiple-importsúBUsed when import statement importing multiple modules is detected.ú%s should be placed before %súwrong-import-orderúuUsed when PEP8 import order is not respected (standard imports first, then third-party libraries, then local imports)ú'Imports from package %s are not groupedúungrouped-importsú-Used when imports are not grouped by packagesú5Import "%s" should be placed at the top of the moduleúwrong-import-positionú$Used when code and imports are mixedú-Import alias does not rename original packageúuseless-import-aliasúsUsed when an import alias is same as original package.e.g using import numpy as numpy instead of import numpy as npúImport outside toplevel (%s)úimport-outside-toplevelúwUsed when an import statement is used anywhere other than the module toplevel. Move this import to the top of the file.)ZE0401ZE0402ZR0401ZW0401ZW0402ZW0404ZW0406ZW0407ZW0410ZC0410ZC0411ZC0412ZC0413ZC0414ZC0415Úenchantc               @   s  e Zd ZdZeZdZeZdaZ	dbZ
de
ddd	d
œfdedddd
œfdddddd
œfdddddd
œfdddddd
œfdedddd
œfdedddd
œfdf dddd
œfdddd d!d
œfd"ddd d#d
œff
Zdcd%d&„Zed'd(„ ƒZd)d*„ Zd+d,„ Zd-d.„ ZeeŽ d/d0„ ƒZeeŽ d1d2„ ƒZeeŽ d3d4„ ƒZd5d6„ Ze Z Z Z Z Z Z Z Z!d7d8„ Z"e" Z# Z$Z%d9d:„ Z&d;d<„ Z'd=d>„ Z(d?d@„ Z)edAdB„ ƒZ*dCdD„ Z+dEdF„ Z,dGdH„ Z-dIdJ„ Z.dKdL„ Z/dMdN„ Z0dddOdP„Z1dQdR„ Z2dSdT„ Z3dUdV„ Z4e5dWdX„ ƒZ6e5dYdZ„ ƒZ7d[d\„ Z8d]d^„ Z9d_d`„ Z:d$S )eÚImportsCheckerz¾checks for
    * external modules dependencies
    * relative / wildcard imports
    * cyclic imports
    * uses of deprecated modules
    * uses of modules instead of preferred modules
    Úimportsé   Úoptparseútkinter.tixzdeprecated-modulesÚcsvz	<modules>zBDeprecated modules which should not be used, separated by a comma.)ÚdefaultÚtypeÚmetavarÚhelpzpreferred-modulesz<module:preferred-module>z?Couples of modules and preferred modules, separated by a comma.zimport-graphrC   Ústringz
<file.dot>zyCreate a graph of every (i.e. internal and external) dependencies in the given file (report RP0402 must not be disabled).zext-import-graphz_Create a graph of external dependencies in the given file (report RP0402 must not be disabled).zint-import-graphz_Create a graph of internal dependencies in the given file (report RP0402 must not be disabled).zknown-standard-libraryzYForce import order to recognize a module as part of the standard compatibility libraries.zknown-third-partyzJForce import order to recognize a module as part of a third party library.zallow-any-import-levelzNList of modules that can be imported at any level, not just the top level one.zanalyse-fallback-blocksFZynz<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.zallow-wildcard-with-allz8Allow wildcard imports from modules that define __all__.Nc             C   sZ   t j| |ƒ d | _d | _g | _d | _i | _tƒ | _dd| j	fdd| j
ff| _| jƒ | _d S )NZRP0401zExternal dependenciesZRP0402zModules dependencies graph)r   Ú__init__ÚstatsÚimport_graphÚ_imports_stackÚ_first_non_import_nodeÚ_module_pkgÚsetÚ_allow_any_import_levelÚ_report_external_dependenciesÚ_report_dependencies_graphZreportsÚ_compute_site_packagesZ_site_packages)ÚselfÚlinterr   r   r   r’   �  s    
zImportsChecker.__init__c              C   s¢   dd„ } t ƒ }ttdd ƒ}x6td |tjfƒD ]"}tj|d�}| |ƒ}|j|ƒ q,W tj	j
dƒržx>td |tjfƒD ]*}tj	j|dddtjƒ  d	ƒ}|j|ƒ qpW |S )
Nc             S   s   t jjt jj| ƒƒS )N)ÚosÚpathÚnormcaseÚabspath)r    r   r   r   Ú_normalized_path®  s    z?ImportsChecker._compute_site_packages.<locals>._normalized_pathÚreal_prefix)r>   z/etc/debian_versionÚlocalÚlibÚpythonzdist-packages)r˜   ÚgetattrÚsysÚfilterr>   r   Úget_python_libÚaddrŸ   r    Úisfiler   Úget_python_version)r£   Úpathsr¤   r>   r    Ú	libpythonr   r   r   rœ   ¬  s"    
z%ImportsChecker._compute_site_packagesc             C   s~   | j ji d� | j jg d� | j j| _tjtƒ| _i | _tjtƒ| _t	| dg d�| _
tdd„ | jjD ƒƒ| _t| jjƒ| _dS )z3called before visiting project (i.e set of modules))rV   )Zcycleszignored-modules)r�   c             s   s    | ]}d |kr|j d ƒV  qdS )ú:N)r   )r   Úmoduler   r   r   r'   Ð  s   z&ImportsChecker.open.<locals>.<genexpr>N)rž   Z	add_statsr“   ÚcollectionsÚdefaultdictr˜   r”   r—   Ú_excluded_edgesr   Ú_ignored_modulesÚdictÚconfigÚpreferred_modulesZallow_any_import_levelr™   )r�   r   r   r   ÚopenÅ  s    
zImportsChecker.openc             C   s2   t j| jƒ}x |D ]}|| j| j| ƒ qW |S )N)ÚcopyÚdeepcopyr”   Údifference_updaterµ   )r�   Zfiltered_graphr/   r   r   r   Ú#_import_graph_without_ignored_edgesÖ  s    
z2ImportsChecker._import_graph_without_ignored_edgesc             C   sJ   | j jdƒrF| jƒ }t|ƒ}x(t||d�D ]}| jddj|ƒd� q*W dS )z3called before visiting project (i.e set of modules)zcyclic-import)Úverticesz -> )ÚargsN)rž   Úis_message_enabledr¾   Úlistr   Úadd_messager   )r�   Úgraphr¿   Úcycler   r   r   ÚcloseÜ  s
    zImportsChecker.closec             C   sÒ   | j |ƒ | j|ƒ | j|ƒ dd„ |jD ƒ}t|ƒdkrP| jddj|ƒ|d� x||D ]t}| j||ƒ | j||ƒ | j	||ƒ}t
|jtjƒr–| j|ƒ t
|jƒ tjƒr²| j||ƒ |dkr¼qV| j||jƒ qVW dS )z*triggered when an import statement is seenc             S   s   g | ]\}}|‘qS r   r   )r   r1   Ú_r   r   r   r   ë  s    z/ImportsChecker.visit_import.<locals>.<listcomp>r‰   zmultiple-importsz, )rÀ   r/   N)Ú_check_reimportÚ_check_import_as_renameÚ_check_toplevelr   r   rÃ   r   Ú_check_deprecated_moduleÚ_check_preferred_moduleÚ_get_imported_moduler   Úparentr    r#   Ú_check_positionr*   Ú_record_importÚ_add_imported_moduler1   )r�   r/   r   r1   Úimported_moduler   r   r   Úvisit_importä  s"    




zImportsChecker.visit_importc             C   sì   |j }| j||ƒ}| j|ƒ | j|ƒ | j||ƒ | j||ƒ | j||ƒ | j|ƒ | j|||j	d� t
|jtjƒr~| j|ƒ t
|jƒ tjƒrš| j||ƒ |dkr¦dS x@|jD ]6\}}|dkrÖ| j|d|j|f ƒ q®| j||jƒ q®W dS )z'triggered when a from statement is seen)Úbasenamer   Nr(   z%s.%s)r   rÍ   rÉ   Ú_check_misplaced_futurerË   rÌ   Ú_check_wildcard_importsÚ_check_same_line_importsrÈ   r   r   rÎ   r    r#   rÏ   r*   rÐ   r   rÑ   r1   )r�   r/   rÔ   rÒ   r1   rÇ   r   r   r   Úvisit_importfromþ  s&    



zImportsChecker.visit_importfromc             C   s¶   | j |ƒ\}}}tƒ }tƒ }d }x„|| | D ]t\}}	| jjd|jƒsHq.t|tjjƒr\|}
n|}
|	j	dƒ\}}}|r”||kr”||
kr”| j
d||d� |}|
j|ƒ q.W g | _d | _d S )Nzungrouped-importsr   )r/   rÀ   )Ú_check_imports_orderr˜   rž   rÁ   r+   r   r    Znode_classesr!   Ú	partitionrÃ   r¬   r•   r–   )r�   r/   Ústd_importsZext_importsZloc_importsZ
met_importZmet_fromZcurrent_packageÚimport_nodeÚimport_nameZmetÚpackagerÇ   r   r   r   Úleave_module  s$    
zImportsChecker.leave_modulec                sž   | j jdˆ jƒsd S | jrd S tˆ jtjƒs0d S tjtj	g}‡ fdd„|D ƒ}|rlt
ˆ jtjtjfƒƒrld S tˆ tjƒr”dd„ ˆ jD ƒ}t|ƒr”d S ˆ | _d S )Nzwrong-import-positionc                s   g | ]}t ˆ |ƒr|‘qS r   )r   )r   Úallowed)r/   r   r   r   A  s    z@ImportsChecker.compute_first_non_import_node.<locals>.<listcomp>c             S   s0   g | ](}t |tjƒo*|jjd ƒo*|jjd ƒ‘qS )Ú__)r   r    Z
AssignNamer1   Ú
startswithÚendswith)r   Útargetr   r   r   r   K  s   )rž   rÁ   r+   r–   r   rÎ   r    r#   Ú	TryExceptÚ
TryFinallyr-   Únodes_of_classr,   r!   ÚAssignÚtargetsÚall)r�   r/   Znested_allowedZis_nested_allowedZvalid_targetsr   )r/   r   Úcompute_first_non_import_node4  s"    
z,ImportsChecker.compute_first_non_import_nodec             C   sŒ   | j jd|jƒsd S | jrd S t|jjƒ tjƒs4d S |}xt|jtjƒsP|j}q:W t|tj	tj
tjfƒr‚t|jtjtjfƒƒr‚d S || _d S )Nzwrong-import-position)rž   rÁ   r+   r–   r   rÎ   r*   r    r#   ÚIfræ   rå   r-   rç   r,   r!   )r�   r/   r"   r   r   r   Úvisit_functiondef^  s    
z ImportsChecker.visit_functiondefc             C   sF   |j }|dkrB|jƒ }|r>t|tjƒo.|j dks>| jd|d� d S d S )NÚ
__future__zmisplaced-future)r/   )r   Zprevious_siblingr   r    r!   rÃ   )r�   r/   rÔ   Úprevr   r   r   rÕ   v  s    
z&ImportsChecker._check_misplaced_futurec             C   sR   dd„ |j D ƒ}tj|ƒ}x2|jƒ D ]&\}}|dkr$| jd|||jfd� q$W d S )Nc             s   s   | ]\}}|V  qd S )Nr   )r   r1   rÇ   r   r   r   r'   †  s    z:ImportsChecker._check_same_line_imports.<locals>.<genexpr>r   ri   )r/   rÀ   )r   r³   ÚCounterrF   rÃ   r+   )r�   r/   r   Úcounterr1   Úcountr   r   r   r×   „  s
    
z'ImportsChecker._check_same_line_importsc             C   s   | j r| jd||jƒ d� dS )z‡Check `node` import or importfrom node position is correct

        Send a message  if `node` comes before another instruction
        zwrong-import-position)r/   rÀ   N)r–   rÃ   Ú	as_string)r�   r/   r   r   r   rÏ   Œ  s    zImportsChecker._check_positionc             C   st   t |tjƒr|j}n|r|jnd}|s>|jd d jdƒd }t |tjƒr`|jpRddkr`d| }| jj	||fƒ dS )z&Record the package `node` imports fromNr   r   r   )
r   r    r!   r   r1   r   r   r   r•   rI   )r�   r/   ZimportedmodnodeZimportednamer   r   r   rÐ   –  s    zImportsChecker._record_importc                s$   dd„ |D ƒ}t ‡ fdd„|D ƒƒS )Nc             S   s   g | ]\}}|‘qS r   r   )r   rÜ   rÇ   r   r   r   r   ­  s    z6ImportsChecker._is_fallback_import.<locals>.<listcomp>c             3   s   | ]}t j|ˆ ƒV  qd S )N)r    r.   )r   rÜ   )r/   r   r   r'   ®  s    z5ImportsChecker._is_fallback_import.<locals>.<genexpr>)r-   )r/   rˆ   r   )r/   r   Ú_is_fallback_import«  s    z"ImportsChecker._is_fallback_importc             C   sH  g }g }g }g }g }g }g }g }	t jd| jj| jjd�}
�x| jD �]ö\}}|jdƒrjd|jdƒd  }n|jdƒd }t|j	t
jƒ }| jjd|jƒ }|
j|ƒ}||f}|dk�r|j|ƒ |pÊ|pÊ|	}| j||ƒrÚqB|oâ| �r:| jd|d	|jƒ  d
|d d jƒ  fd� qB|dk�r’|j|ƒ |j|ƒ | �rL| �rL|j|ƒ |�pT|	}|�r:| �r:| jd|d|jƒ  d
|d d jƒ  fd� qB|dk�r
|j|ƒ |j|ƒ | �rÊ| �rÊ|j|ƒ |	}|�r:| �r:| jd|d|jƒ  d
|d d jƒ  fd� qB|dkrB|j||fƒ | rB| rB|	j||fƒ qBW |||fS )z„Checks imports of module `node` are grouped by category

        Imports must follow this order: standard, 3rd party, local
        rC   )Úfile_contentsÚknown_third_partyÚknown_standard_libraryr   r   r   zwrong-import-orderÚFUTUREÚSTDLIBzstandard import "%s"z"%s")r/   rÀ   Z
THIRDPARTYzthird party import "%s"Z
FIRSTPARTYzfirst party import "%s"ZLOCALFOLDER)rø   rù   )ÚisortZSortImportsr¸   rö   r÷   r•   râ   r   r   rÎ   r    r#   rž   rÁ   r+   Zplace_modulerI   rô   rÃ   ró   )r�   Z_module_noderÛ   Zthird_party_importsZfirst_party_importsZexternal_importsZlocal_importsZthird_party_not_ignoredZfirst_party_not_ignoredZlocal_not_ignoredZ	isort_objr/   r   rÞ   ÚnestedZignore_for_import_orderZimport_categoryZnode_and_package_importZwrong_importr   r   r   rÙ   °  s~    















z#ImportsChecker._check_imports_orderc             C   sö   y
|j |ƒS  tjk
r@   t||| jƒr.d S | jd|d� Y n² tjk
r† } z*dj|t|j	ƒƒ}| jd|j
|d� W Y d d }~X nl tjk
rð   | jjdƒs¦d S t||| jƒr¸d S | jj rÎt|ƒrÎd S t||ƒ}| jdt|ƒ|d� Y nX d S )Nzrelative-beyond-top-level)r/   z+Cannot import {!r} due to syntax error {!r}zsyntax-error)ÚlinerÀ   zimport-error)rÀ   r/   )Zdo_import_moduler    ZTooManyLevelsErrorr:   r¶   rÃ   ZAstroidSyntaxErrorÚformatÚstrÚerrorÚlinenoZAstroidBuildingExceptionrž   rÁ   r¸   Zanalyse_fallback_blocksr   r%   Úrepr)r�   r$   r   ÚexcÚmessageZdotted_modnamer   r   r   rÍ     s(    
"
z#ImportsChecker._get_imported_modulec             C   sø   |j ƒ j}|j ƒ j}tjjtjj|ƒƒd }ytj||ƒ}W n t	k
rP   Y nX ||krj| j
d|d� nŠtj|ƒsô|dkrœ|| jkrœ|jddƒd | j|< | jd j|tƒ ƒ}||krÂ|j|ƒ | j| j|ƒ | jjd|jd	�sô| j| j|ƒ d
S )z7notify an imported module, used to analyze dependenciesr   zimport-self)r/   r’   r   r   rV   zcyclic-import)rü   N)r"   Úfiler1   rŸ   r    ÚsplitextrÔ   r   Zget_module_partr8   rÃ   Zis_standard_moduler—   Úrsplitr“   r;   r˜   r¬   r”   rž   rÁ   r   rµ   )r�   r/   ZimportedmodnameZmodule_fileZcontext_namer2   Zimportedmodnamesr   r   r   rÑ     s&    





z#ImportsChecker._add_imported_modulec             C   s<   x6| j jD ]*}||ks$|j|d ƒr
| jd||d� q
W dS )z!check if the module is deprecatedr   zdeprecated-module)r/   rÀ   N)r¸   Údeprecated_modulesrâ   rÃ   )r�   r/   Úmod_pathÚmod_namer   r   r   rË   @  s    z'ImportsChecker._check_deprecated_modulec             C   s(   || j kr$| jd|| j | |fd� dS )z/check if the module has a preferred replacementzpreferred-module)r/   rÀ   N)r¹   rÃ   )r�   r/   r  r   r   r   rÌ   F  s
    
z&ImportsChecker._check_preferred_modulec             C   sh   |j }x\|D ]T}t|ƒsd S |d }|jdƒ}|d }|d }||krt|ƒdkr| jd|d� qW d S )Nr   r   r   zuseless-import-alias)r/   éÿÿÿÿ)r   rê   r  r   rÃ   )r�   r/   r   r1   Z	real_nameZsplitted_packagesr6   r   r   r   rÉ   O  s    

z&ImportsChecker._check_import_as_renamec          	   C   sš   | j jdƒsdS |jƒ }|jƒ }||fg}||k	r@|j|dfƒ xT|D ]L\}}xB|jD ]8\}	}
t|||	|||
ƒ}|dk	rV| jd||	|jfd� qVW qFW dS )z8check if the import is necessary (i.e. not already done)ri   N)r/   rÀ   )	rž   rÁ   Úframer"   rI   r   r7   rÃ   r+   )r�   r/   rÔ   r   r  r"   ZcontextsZknown_contextZknown_levelr1   r3   r4   r   r   r   rÈ   `  s    
zImportsChecker._check_reimportc             C   s4   t | jƒ jƒ ƒ}|stƒ ‚t|ƒ}|jt|ƒƒ dS )z4return a verbatim layout for displaying dependenciesN)r?   Ú_external_dependencies_inforF   r	   rJ   rI   r   )r�   rX   rÇ   Ú_dummyrS   Ztree_strr   r   r   rš   u  s
    z,ImportsChecker._report_external_dependenciesc             C   sŠ   | j d }| s*| jjp&| jjp&| jj r0tƒ ‚| jj}|rJt|||dƒ | jj}|rht|| jƒ |dƒ | jj}|r†t|| jƒ |dƒ dS )z+write dependencies as a dot (graphviz) filerV   rC   z	external z	internal N)	r“   r¸   r”   Zext_import_graphZint_import_graphr	   rY   r  Ú_internal_dependencies_info)r�   rX   rÇ   r  rS   rR   r   r   r   r›   }  s    

z)ImportsChecker._report_dependencies_graphc             C   sr   t jtƒ}xb| jd jƒ D ]P\}}xF|D ]>}| jj||ƒ}|j|ƒ}|rL|sX| r(| r(|| j|ƒ q(W qW |S )z3build the internal or the external dependency graphrV   )	r³   r´   r˜   r“   rF   r—   Úgetrâ   r¬   )r�   ÚinternalrÄ   ZimporteeÚ	importersÚimporterrÞ   Z	is_insider   r   r   Ú_filter_dependencies_graph�  s    


z)ImportsChecker._filter_dependencies_graphc             C   s   | j dd�S )zXreturn cached external dependencies information or build and
        cache them
        F)r  )r  )r�   r   r   r   r  ›  s    z*ImportsChecker._external_dependencies_infoc             C   s   | j dd�S )zXreturn cached internal dependencies information or build and
        cache them
        T)r  )r  )r�   r   r   r   r  ¢  s    z*ImportsChecker._internal_dependencies_infoc             C   sP   |j ƒ jrd S | j|ƒ}x2|jD ](\}}|dkr | r | jd|j|d� q W d S )Nr(   zwildcard-import)rÀ   r/   )r"   rÞ   Ú_wildcard_import_is_allowedr   rÃ   r   )r�   r/   rÒ   Zwildcard_import_is_allowedr1   rÇ   r   r   r   rÖ   ©  s    

z&ImportsChecker._check_wildcard_importsc             C   s   | j jo|d k	od|jkS )NÚ__all__)r¸   Zallow_wildcard_with_allÚlocals)r�   rÒ   r   r   r   r  ³  s    z*ImportsChecker._wildcard_import_is_allowedc                sj   t |jƒ tjƒrdS t |tjƒr*|jg}ndd„ |jD ƒ}‡ fdd„|D ƒ}|rfˆ jddj|ƒ|d� dS )zFCheck whether the import is made outside the module toplevel.
        Nc             S   s   g | ]}|d  ‘qS )r   r   )r   r1   r   r   r   r   Å  s    z2ImportsChecker._check_toplevel.<locals>.<listcomp>c                s   g | ]}|ˆ j kr|‘qS r   )r™   )r   r1   )r�   r   r   r   É  s    zimport-outside-toplevelz, )rÀ   r/   )	r   r*   r    r#   r!   r   r   rÃ   r   )r�   r/   Zmodule_namesZscoped_importsr   )r�   r   rÊ   º  s    
zImportsChecker._check_topleveléþÿÿÿ)rŠ   r‹   )N)NN);Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Z__implements__r1   ÚMSGSZmsgsÚpriorityr  ÚDEFAULT_PREFERRED_MODULESÚDEFAULT_STANDARD_LIBRARYÚDEFAULT_KNOWN_THIRD_PARTYÚoptionsr’   Ústaticmethodrœ   rº   r¾   rÆ   r   rÓ   rØ   rß   rë   Zvisit_tryfinallyZvisit_tryexceptZvisit_assignattrZvisit_assignZvisit_ifexpZvisit_comprehensionZ
visit_exprZvisit_ifrí   Zvisit_classdefZ	visit_forZvisit_whilerÕ   r×   rÏ   rÐ   rô   rÙ   rÍ   rÑ   rË   rÌ   rÉ   rÈ   rš   r›   r  r   r  r  rÖ   r  rÊ   r   r   r   r   r‡   !  s°   
( 
T!	

r‡   c             C   s   | j t| ƒƒ dS )z.required method to auto register this checker N)Zregister_checkerr‡   )rž   r   r   r   ÚregisterÒ  s    r#  )N)rZ   r[   )r\   r]   r^   )r_   r`   ra   )rb   rc   rd   )re   rf   rg   )rh   ri   rj   )rk   rl   rm   )rn   ro   rp   )rq   rr   rs   )rt   ru   rv   )rw   rx   ry   )rz   r{   r|   )r}   r~   r   )r€   r�   r‚   )rƒ   r„   r…   )r†   ),r  r³   r»   rŸ   r©   Ú	distutilsr   r    rú   r   Zastroid.decoratorsr   Zpylint.checkersr   Zpylint.checkers.utilsr   r   r   Zpylint.exceptionsr	   Zpylint.graphr
   r   Zpylint.interfacesr   Zpylint.reporters.ureports.nodesr   r   Zpylint.utilsr   r   r%   r7   r:   r?   rJ   rW   rY   r  r  r   r  r‡   r#  r   r   r   r   Ú<module>   s    $

                                 6