o
    g#                     @   s  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mZm	Z	m
Z
mZmZmZ ddlZddlmZ ddlmZmZ ddlmZmZ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! ddl"m#Z#m$Z$m%Z% ddl&m'Z' ddl(m)Z)m*Z* dd Z+dd Z,dd Z-dd Z.dLddZ/de0de	e0e
e0 f de0fddZ1de0de	e0e
e0 f de$de0fd d!Z2i d"d#d$d%d&d'gifd(d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9d:d;d<d=d>d?d@dAdBdCdDdEZ3dFZ4dGZ5dFZ6G dHdI dIeeZ7dJdK Z8dS )Mz imports checkers for Python code    N)	sysconfig)AnyDictListSetTupleUnion)nodes)BaseCheckerDeprecatedMixin)check_messagesget_import_nameis_from_fallback_blockis_node_in_guarded_import_blocknode_ignores_exception)EmptyReportError)
DotBackend
get_cycles)IAstroidChecker)PyLinter)	ParagraphSectionVerbatimText)CheckerStats)IsortDriverget_global_optionc                    s$   |  d  fddtt D S )zSplit 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   d|d  qS )r   r      )join).0inames U/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/pylint/checkers/imports.py
<listcomp>U   s   " z$_qualified_names.<locals>.<listcomp>)splitrangelen)modnamer#   r!   r$   _qualified_namesL   s   
r*   c           
         s  |r	| d| n| d}d}|j D ]b}|| u rq| |  u r(|j| jkr(qt|tjr?t fdd|jD r>d} n7qt|tjrt||j	krt|jD ]"\}}	 |j
 d| kr_d} n|dkro||kro|so|	sod} nqM|rt nq|rt|| s|S dS )zDreturn the node where [base.]<name> is imported or None if not foundr   NFc                 3   s    | ]	} |d  kV  qdS )r   Nr#   )r   inamefullnamer#   r$   	<genexpr>d       z$_get_first_import.<locals>.<genexpr>T*)bodyscope
fromlineno
isinstancer	   Importanyr"   
ImportFromlevelr)   astroidare_exclusive)
nodecontextnamebaser8   aliasfirstfoundimported_nameimported_aliasr#   r,   r$   _get_first_importX   sD   

rD   c                 C   s2   t |D ]	}||v r dS qt| rdS t| tS )NT)r*   r   r   ImportError)r;   r)   ignored_modules	submoduler#   r#   r$   _ignore_import_failure{   s   
rH   c                 C   sR   i }| D ]"\}}|df}| dD ]}|d |i g g}q|d  |7  < q|S )zget 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   )r&   
setdefault)mod_files_list	tree_defsmodfilesr;   prefixr#   r#   r$   _make_tree_defs   s   rO   c           	      C   s   g }|   }tt|dd dD ]R\}\}\}}|sdn
ddt| d}|du r8|| d	|  d
}n || d| d	|  |t|d krS| d
}n| d}|rb|t|| qd|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 (,)N z  z\-r   z| 
)items	enumeratesortedr   appendr(   _repr_tree_defs)	data
indent_strlinesnodes_itemsr    rL   subrM   sub_indent_strr#   r#   r$   r]      s   $

r]   filenamedep_inforeturnc                 C   s   i }t tjtj| d dd}|d t| D ]\}}d||< || |D ]}||vr;d||< || q,qt| D ]\}}|D ]}|	|| qIqC|
| S )+write dependencies as a dot (graphviz) filer   LR)rankdirzURL="." node[shape="box"]r   )r   ospathsplitextbasenameemitr[   rY   	emit_node	emit_edgegenerate)rd   re   doneprinterr)   dependencies
depmodnamer#   r#   r$   _dependencies_graph   s"    



rv   sectgtypec                 C   s(   t | |}|t| d| f dS )z`generate a dependencies graph and add some information about it in the
    report's section
    z"imports graph has been written to N)rv   r\   r   )rd   re   rw   rx   
outputfiler#   r#   r$   _make_graph   s   
rz   E0401zUnable to import %simport-errorz4Used when pylint has been unable to import a module.	old_names)F0401zold-import-errorE0402)z2Attempted relative import beyond top-level packagerelative-beyond-top-levelzSUsed when a relative import tries to access too many levels in the current package.R0401)zCyclic import (%s)cyclic-importzBUsed when a cyclic import between two or more modules is detected.R0402)zUse 'from %s import %s' insteadconsider-using-from-importzEmitted when a submodule of a package is imported and aliased with the same name. E.g., instead of ``import concurrent.futures as futures`` use ``from concurrent import futures``W0401)zWildcard import %swildcard-importz-Used when `from module import *` is detected.W0402)zUses of a deprecated module %rzdeprecated-modulez/Used a module marked as deprecated is imported.W0404)zReimport %r (imported line %s)
reimportedz0Used when a module is reimported multiple times.W0406)zModule import itselfimport-selfz'Used when a module is importing itself.W0407)z!Prefer importing %r instead of %rpreferred-modulez?Used when a module imported has a preferred replacement module.W0410)z:__future__ import is not the first non docstring statementmisplaced-futurezgPython 2.5 and greater require __future__ import to be the first non docstring statement in the module.C0410)z!Multiple imports on one line (%s)multiple-importszBUsed when import statement importing multiple modules is detected.C0411)z%s should be placed before %swrong-import-orderzuUsed when PEP8 import order is not respected (standard imports first, then third-party libraries, then local imports)C0412)z'Imports from package %s are not groupedungrouped-importsz-Used when imports are not grouped by packagesC0413)z5Import "%s" should be placed at the top of the modulewrong-import-positionz$Used when code and imports are mixedC0414)z-Import alias does not rename original packageuseless-import-aliaszsUsed when an import alias is same as original package.e.g using import numpy as numpy instead of import numpy as npC0415)zImport outside toplevel (%s)import-outside-toplevelzwUsed when an import statement is used anywhere other than the module toplevel. Move this import to the top of the file.r#   )enchantc                   @   s  e Zd ZdZeZdZeZdZ	dZ
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ddddd	fddddd d	fd!dddd"d	ff
Z	#ddd$efd%d&Zed'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zee d1ejd2d#fd3d4Zee d1ejd2d#fd5d6Zee d1ejd2d#fd7d8Zd9d: Ze Z  Z! Z" Z# Z$ Z% Z&Z'd1ej(d2d#fd;d<Z)e) Z* Z+Z,d=d> Z-d?d@ Z.dAdB Z/dCdD Z0edEdF Z1dGdH Z2dIdJ Z3d1e4ejejf dKe5d2d#fdLdMZ6dNdO Z7d1e4ejejf d2d#fdPdQZ8dedRdSZ9dTdU Z:dVdW Z;dXdY Z<e=j>j?dZd[ Z@e=j>j?d\d] ZAd^d_ ZBd`da ZCdbdc ZDd#S )fImportsCheckerzchecks for
    * external modules dependencies
    * relative / wildcard imports
    * cyclic imports
    * uses of deprecated modules
    * uses of modules instead of preferred modules
    importsr#   z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-graphrS   stringz	<file.gv>zOutput a graph (.gv or any supported image format) of all (i.e. internal and external) dependencies to the given file (report RP0402 must not be disabled).zext-import-graphzOutput a graph (.gv or any supported image format) of external dependencies to the given file (report RP0402 must not be disabled).zint-import-graphzOutput a graph (.gv or any supported image format) of internal dependencies to 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-blocksFynz<y_or_n>zAnalyse 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__.Nlinterc                 C   s`   t | | i | _tt| _g | _d | _i | _	t | _
dd| jfdd| jff| _|  | _d S )NRP0401zExternal dependenciesRP0402zModules dependencies graph)r
   __init__statscollectionsdefaultdictsetimport_graph_imports_stack_first_non_import_node_module_pkg_allow_any_import_level_report_external_dependencies_report_dependencies_graphreports_compute_site_packages_site_packages)selfr   r#   r#   r$   r     s   

zImportsChecker.__init__c               	   C   s   dd } t  }ttdd }td |tjfD ]}tj|d}| |}|| qtj	
drKtd |tjfD ]}tj	|dddt  d	}|| q5|S )
Nc                 S   s   t jt j| S N)rj   rk   normcaseabspath)rk   r#   r#   r$   _normalized_path  s   z?ImportsChecker._compute_site_packages.<locals>._normalized_pathreal_prefix)rN   z/etc/debian_versionlocallibpythonzdist-packages)r   getattrsysfilterrN   r   get_python_libaddrj   rk   isfiler   get_python_version)r   pathsr   rN   rk   	libpythonr#   r#   r$   r     s$   
z%ImportsChecker._compute_site_packagesc                 C   s~   | j ji d | j jg d | j j| _tt| _i | _tt| _t	| dg d| _
tdd | jjD | _t| jj| _dS )3called before visiting project (i.e set of modules))rt   )cycleszignored-modules)r   c                 s   s"    | ]}d |v r| d V  qdS ):N)r&   )r   moduler#   r#   r$   r.     s    z&ImportsChecker.open.<locals>.<genexpr>N)r   	add_statsr   r   r   r   r   r   _excluded_edgesr   _ignored_modulesdictconfigpreferred_modulesallow_any_import_levelr   r   r#   r#   r$   open  s   

zImportsChecker.openc                 C   s.   t | j}|D ]}|| | j|  q|S r   )copydeepcopyr   difference_updater   )r   filtered_graphr;   r#   r#   r$   #_import_graph_without_ignored_edges  s   z2ImportsChecker._import_graph_without_ignored_edgesc                 C   sJ   | j dr!|  }t|}t||dD ]}| jdd|d qdS dS )r   r   )verticesz -> )argsN)r   is_message_enabledr   listr   add_messager   )r   graphr   cycler#   r#   r$   close  s   zImportsChecker.closec                 C   s   | j jS )z*Callback returning the deprecated modules.)r   deprecated_modulesr   r#   r#   r$   r     s   z!ImportsChecker.deprecated_modulesr;   rf   c                 C   s   |  | | | | | dd |jD }t|dkr(| jdd||d |D ]:}| || | || | 	||}t
|jtjrJ| | t
| tjrX| || |du r]q*| ||j q*dS )z*triggered when an import statement is seenc                 S      g | ]\}}|qS r#   r#   r   r=   _r#   r#   r$   r%          z/ImportsChecker.visit_import.<locals>.<listcomp>   r   , r   r;   N)_check_reimport_check_import_as_rename_check_toplevelr"   r(   r   r   check_deprecated_module_check_preferred_module_get_imported_moduler4   parentr	   Module_check_positionr2   _record_import_add_imported_moduler=   )r   r;   r"   r=   imported_moduler#   r#   r$   visit_import  s$   



zImportsChecker.visit_importc                 C   s   |j }| ||}| | | | | || | || | || | | | j|||j	d | 
| t|jtjrD| | t| tjrR| || |du rXdS |jD ]\}}|dkrp| ||j d|  q[| ||j q[dS )z'triggered when a from statement is seen)rm   r8   Nr0   r   )r)   r   r   _check_misplaced_futurer   r   _check_wildcard_imports_check_same_line_importsr   r8   r   r4   r   r	   r   r   r2   r   r"   r   r=   )r   r;   rm   r   r=   r   r#   r#   r$   visit_importfrom  s*   




zImportsChecker.visit_importfromc                 C   s   |  |\}}}t }t }d }|| | D ]>\}}	| jd|js#qt|tjr+|n|}
|	d\}}}|rM||krM||
v rMt	|du rM| j
d||d |}|
| qg | _d | _d S )Nr   r   Fr;   r   )_check_imports_orderr   r   r   r3   r4   r	   r7   	partitionr   r   r   r   r   )r   r;   std_importsext_importsloc_imports
met_importmet_fromcurrent_packageimport_nodeimport_namemetpackager   r#   r#   r$   leave_module/  s*   
zImportsChecker.leave_modulec                    s   | j d js
d S | jrd S t jtjsd S tjtj	g} fdd|D }|r6t
 tjtjfr6d S t tjrJdd  jD }t|rJd S  | _d S )Nr   c                    s   g | ]	}t  |r|qS r#   )r4   )r   allowedr;   r#   r$   r%   X  s
    
z@ImportsChecker.compute_first_non_import_node.<locals>.<listcomp>c                 S   s0   g | ]}t |tjo|jd o|jd qS )__)r4   r	   
AssignNamer=   
startswithendswith)r   targetr#   r#   r$   r%   b  s    

)r   r   r3   r   r4   r   r	   r   	TryExcept
TryFinallyr6   nodes_of_classr5   r7   Assigntargetsall)r   r;   nested_allowedis_nested_allowedvalid_targetsr#   r  r$   compute_first_non_import_nodeL  s*   

z,ImportsChecker.compute_first_non_import_nodec                 C   s   | j d|js
d S | jrd S t|j tjsd S |}t|jtjs-|j}t|jtjr#t|tj	tj
tjfrEt|tjtjfrEd S || _d S )Nr   )r   r   r3   r   r4   r   r2   r	   r   Ifr  r  r6   r  r5   r7   )r   r;   rootr#   r#   r$   visit_functiondefv  s   
z ImportsChecker.visit_functiondefc                 C   sF   |j }|dkr!| }|rt|tjr|j dks| jd|d d S d S )N
__future__r   r  )r)   previous_siblingr4   r	   r7   r   )r   r;   rm   prevr#   r#   r$   r     s   

z&ImportsChecker._check_misplaced_futurec                 C   sN   dd |j D }t|}| D ]\}}|dkr$| jd|||jfd qd S )Nc                 s   s    | ]\}}|V  qd S r   r#   r   r#   r#   r$   r.     s    z:ImportsChecker._check_same_line_imports.<locals>.<genexpr>r   r   r   )r"   r   CounterrY   r   r3   )r   r;   r"   counterr=   countr#   r#   r$   r     s   
z'ImportsChecker._check_same_line_importsc                 C   s"   | j r| jd|| d dS dS )zCheck `node` import or importfrom node position is correct

        Send a message  if `node` comes before another instruction
        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 dd }t |tjr0|jp)ddkr0d| }| j	||f dS )z&Record the package `node` imports fromNr   r   r   )
r4   r	   r7   r)   r=   r"   r&   r8   r   r\   )r   r;   importedmodnodeimportednamer#   r#   r$   r     s   zImportsChecker._record_importc                    s$   dd |D }t  fdd|D S )Nc                 S   r   r#   r#   )r   r  r   r#   r#   r$   r%     r   z6ImportsChecker._is_fallback_import.<locals>.<listcomp>c                 3   s    | ]	}t | V  qd S r   )r9   r:   )r   r  r  r#   r$   r.     r/   z5ImportsChecker._is_fallback_import.<locals>.<genexpr>)r6   )r;   r   r#   r  r$   _is_fallback_import  s   z"ImportsChecker._is_fallback_importc                 C   s$  g }g }g }g }g }g }g }g }	t | j}
| jD ]\}}|dr+d|dd  }n|dd }t|jtj }| j	
d|j }|
|}||f}|dv r|| |pZ|pZ|	}| ||rbq|r|s| jd|d|  dd|d d   dfd q|d	kr|| || |s|s|| |p|	}|r|s| jd|d
|  dd|d d   dfd q|dkr|| || |s|s|| |	}|r|s| jd|d|  dd|d d   dfd q|dkr|||f |s|s|	||f q|||fS )zChecks imports of module `node` are grouped by category

        Imports must follow this order: standard, 3rd party, local
        r   r   r   r   )FUTURESTDLIBzstandard import ""r   
THIRDPARTYzthird party import "
FIRSTPARTYzfirst party import "LOCALFOLDER)r   r   r   r  r&   r4   r   r	   r   r   r   r3   place_moduler\   r)  r   r&  )r   _module_noder  third_party_importsfirst_party_importsexternal_importslocal_importsthird_party_not_ignoredfirst_party_not_ignoredlocal_not_ignoredisort_driverr;   r)   r
  nestedignore_for_import_orderimport_categorynode_and_package_importwrong_importr#   r#   r$   r     s   











z#ImportsChecker._check_imports_orderc              
   C   s  z| |W S  tjy"   t||| jrY d S | jd|d Y d S  tjyI } zd|dt|j}| jd|j	|d W Y d }~d S d }~w tj
y   | jdsYY d S t||| jrcY d S | jjsnt|rnY d S t||}| jdt||d Y d S w )	Nr   r  zCannot import z due to syntax error zsyntax-error)liner   r|   r   )do_import_moduler9   TooManyLevelsErrorrH   r   r   AstroidSyntaxErrorstrerrorlinenoAstroidBuildingExceptionr   r   r   analyse_fallback_blocksr   r   repr)r   
importnoder)   excmessagedotted_modnamer#   r#   r$   r     s2   

z#ImportsChecker._get_imported_moduleimportedmodnamec           	      C   s$  |  j}|  j}tjtj|d }z	tj	||}W n	 t
y(   Y nw t|jtjo4|j }||krB| jd|d d
S tj|s|dkr\|| jvr\|ddd | j|< | jd }||t }||vrq|| | j| | | jjd|jd	r|r| j| | d
S d
S d
S )z7notify an imported module, used to analyze dependenciesr   r   r  r   r   r   rt   r   )r?  N)r  filer=   rj   rk   rl   rm   r9   modutilsget_module_partrE   r4   r   r	   r  is_typing_guardr   is_standard_moduler   rsplitr   rI   r   r   r   r   r   rE  r   )	r   r;   rM  module_filecontext_namer>   in_type_checking_blockdependencies_statimportedmodnamesr#   r#   r$   r   0  s:   



z#ImportsChecker._add_imported_modulec                 C   s,   || j v r| jd|| j | |fd dS dS )z/check if the module has a preferred replacementr   r   N)r   r   )r   r;   mod_pathr#   r#   r$   r   [  s   

z&ImportsChecker._check_preferred_modulec                 C   s   |j }|D ]?}t|s d S |d jddd}|d }|d }||kr$qt|dkr2| jd|d qt|dkrD| jd	||d |fd
 qd S )Nr   r   r   )maxsplitr   r  r   r   r   )r"   r  rS  r(   r   )r   r;   r"   r=   splitted_packagesr  aliased_namer#   r#   r$   r   d  s&   
z&ImportsChecker._check_import_as_renamec              	   C   s   | j dsdS | }| }||fg}||ur ||df |D ]$\}}|jD ]\}	}
t|||	|||
}|durE| jd||	|jfd q)q"dS )z8check if the import is necessary (i.e. not already done)r   Nr   )	r   r   framer  r\   r"   rD   r   r3   )r   r;   rm   r8   r^  r  contextsknown_contextknown_levelr=   r?   r@   r#   r#   r$   r   {  s&   
zImportsChecker._check_reimportc                 C   s4   t |   }|st t|}|t| dS )z4return a verbatim layout for displaying dependenciesN)rO   _external_dependencies_inforY   r   r]   r\   r   )r   rw   r   _dummyre   tree_strr#   r#   r$   r     s
   z,ImportsChecker._report_external_dependenciesc                 C   s   | j d }|r| jjs| jjs| jjst | jj}|r#t|||d | jj}|r2t||  |d | jj}|rCt||  |d dS dS )rg   rt   rS   z	external z	internal N)	r   r   r   ext_import_graphint_import_graphr   rz   rb  _internal_dependencies_info)r   rw   r   rc  re   rd   r#   r#   r$   r     s&   
z)ImportsChecker._report_dependencies_graphc                 C   sf   t t}| jd  D ]$\}}|D ]}| j||}||}|r$|s(|s/|s/|| | qq|S )z3build the internal or the external dependency graphrt   )	r   r   r   r   rY   r   getr  r   )r   internalr   importee	importersimporterr
  	is_insider#   r#   r$   _filter_dependencies_graph  s   

z)ImportsChecker._filter_dependencies_graphc                 C      | j ddS )zXreturn cached external dependencies information or build and
        cache them
        Fri  rn  r   r#   r#   r$   rb       z*ImportsChecker._external_dependencies_infoc                 C   ro  )zXreturn cached internal dependencies information or build and
        cache them
        Trp  rq  r   r#   r#   r$   rg    rr  z*ImportsChecker._internal_dependencies_infoc                 C   sJ   |  jrd S | |}|jD ]\}}|dkr"|s"| jd|j|d qd S )Nr0   r   r   )r  r
  _wildcard_import_is_allowedr"   r   r)   )r   r;   r   wildcard_import_is_allowedr=   r   r#   r#   r$   r     s   

z&ImportsChecker._check_wildcard_importsc                 C   s   | j jo|d uod|jv S )N__all__)r   allow_wildcard_with_alllocals)r   r   r#   r#   r$   rs    s
   z*ImportsChecker._wildcard_import_is_allowedc                    s\   t   tjr
dS  fdd jD }fdd|D }|r,jdd| d dS dS )z=Check whether the import is made outside the module toplevel.Nc                    s4   g | ]}t  tjr j d |d  n|d qS )r   r   )r4   r	   r7   r)   r   r=   r  r#   r$   r%     s    
z2ImportsChecker._check_toplevel.<locals>.<listcomp>c                    s   g | ]	}| j vr|qS r#   )r   rx  r   r#   r$   r%     s    r   r   r   )r4   r2   r	   r   r"   r   r   )r   r;   module_namesscoped_importsr#   )r;   r   r$   r     s   


zImportsChecker._check_toplevelr   )NN)E__name__
__module____qualname____doc__r   __implements__r=   MSGSmsgsprioritydefault_deprecated_modulesDEFAULT_PREFERRED_MODULESDEFAULT_STANDARD_LIBRARYDEFAULT_KNOWN_THIRD_PARTYoptionsr   r   staticmethodr   r   r   r   r   r   r	   r5   r   r7   r   r   r  r  visit_tryfinallyvisit_tryexceptvisit_assignattrvisit_assignvisit_ifexpvisit_comprehension
visit_exprvisit_ifFunctionDefr  visit_classdef	visit_forvisit_whiler   r   r   r   r)  r   r   r   rC  r   r   r   r   r   r   rn  r9   
decoratorscachedrb  rg  r   rs  r   r#   r#   r#   r$   r   ,  s   o

(

P
+	




r   c                 C   s   |  t|  dS )z-required method to auto register this checkerN)register_checkerr   )r   r#   r#   r$   register  s   r  r   )9r~  r   r   rj   r   	distutilsr   typingr   r   r   r   r   r   r9   r	   pylint.checkersr
   r   pylint.checkers.utilsr   r   r   r   r   pylint.exceptionsr   pylint.graphr   r   pylint.interfacesr   pylint.lintr   pylint.reporters.ureports.nodesr   r   r   pylint.typingr   pylint.utilsr   r   r*   rD   rH   rO   r]   rC  rv   rz   r  r  r  r  r   r  r#   r#   r#   r$   <module>   s   . #
"
$).39>DINT]     G