o
    b                     @   sf  d Z ddlZddlmZ ddlZddlZddlZddlZddlmZm	Z	 ddl
mZm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 dd
lmZmZmZ ddlmZmZ ddlmZmZ dd Z dd Z!dd Z"dd Z#dd Z$d:ddZ%dd Z&dd Z'dddd d!gifd"d#d$d%d&d'd(d)d*ifd+d,d-d.d/d0d1d2d3Z(d4Z)d5Z*G d6d7 d7eZ+d8d9 Z,dS );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	Paragraphc                    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 V/var/www/secure340b-portal/env/lib/python3.10/site-packages/pylint/checkers/imports.py
<listcomp>A   s   " z$_qualified_names.<locals>.<listcomp>)splitrangelen)modnamer   r   r   _qualified_names8   s   
r    c                 C   s:   t | tjr| jr|  }t |tjr|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_nameD   s   r(   c           
         s   |rd||f n| d}d}|j D ]a}|| u rq| |  u r'|j| jkr'qt|tjr>t fdd|jD r=d} n6qt|tjrr||j	krr|jD ]!\}}	 d|j
|f kr]d} n|dkrm||krm|sm|	smd} nqL|rr nq|r|t|| s||S dS )zIreturn the node where [base.]<name> is imported or None if not found
    %s.%sNFc                 3   s    | ]	} |d  kV  qdS )r   Nr   )r   Zinamefullnamer   r   	<genexpr>c       z$_get_first_import.<locals>.<genexpr>T*)bodyscope
fromlinenor"   r#   Importanyr   r$   r!   r   r   )
nodecontextnamebaser!   aliasfirstfoundimported_nameZimported_aliasr   r*   r   _get_first_importV   sH   

r<   c                 C   s&   t |D ]	}||v r dS qt| tS )NT)r    r   ImportError)r4   r   Zignored_modules	submoduler   r   r   _ignore_import_failurez   s
   
r?   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)Zmod_files_listZ	tree_defsmodfilesr4   prefixr   r   r   _make_tree_defs   s   rD   c           	      C   s   g }|   }tt|dd dD ]K\}\}\}}|sd}n	ddt| }|du r6|d||f  d	}n|d
|||f  |t|d krMd| }nd| }|r[|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 z(%s),Nz%s %sz  z	%s\-%s %sr   z%s  z%s| 
)items	enumeratesortedr   appendr   _repr_tree_defs)	dataZ
indent_strlinesnodesr   rA   subrB   Zsub_indent_strr   r   r   rO      s"   $

rO   c                 C   s   i }t | dd dd}|d t| D ]\}}d||< || |D ]}||vr5d||< || q&qt| D ]\}}|D ]}||| qCq=||  dS )z0write dependencies as a dot (graphviz) file
    NZLR)ZrankdirzURL="." node[shape="box"]r   )r   emitrM   rK   Z	emit_nodeZ	emit_edgegenerate)filenamedep_infodoneprinterr   dependenciesZ
depmodnamer   r   r   _dependencies_graph   s"   


r\   c                 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&%simports graph has been written to %sN)r\   rN   r   )rW   rX   sectgtyper   r   r   _make_graph   s   
r_   zUnable to import %simport-errorz4Used when pylint has been unable to import a module.Z	old_names)ZF0401r`   )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.)zCyclic import (%s)cyclic-importzBUsed when a cyclic import between two or more modules is detected.)zWildcard import %swildcard-importz-Used when `from module import *` is detected.)zUses of a deprecated module %rdeprecated-modulez/Used a module marked as deprecated is imported.z Relative import %r, should be %rrelative-importzBUsed when an import relative to the package directory is detected.Z
maxversion)   r   )zReimport %r (imported line %s)
reimportedz0Used when a module is reimported multiple times.)zModule import itselfimport-selfz'Used when a module is importing itself.)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.)z!Multiple imports on one line (%s)multiple-importszBUsed when import statement importing multiple modules is detected.)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))z'Imports from package %s are not groupedungrouped-importsz-Used when imports are not grouped by packages)z5Import "%s" should be placed at the top of the modulewrong-import-positionz$Used when code and imports are mixed)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)ZE0401ZE0402ZR0401ZW0401ZW0402ZW0403ZW0404ZW0406ZW0410ZC0410ZC0411ZC0412ZC0413ZC0414r   )Zenchantc                   @   s  e Zd ZdZeZdZeZdZ	e
jdk rdZndZ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fZd[d!d"Zed#d$ Zd%d& Zd'd( Zd)d* Zee d+d, Zee d-d. Zee d/d0 Zd1d2 Ze Z Z Z Z Z Z  Z!Z"d3d4 Z#e# Z$ Z%Z&d5d6 Z'd7d8 Z(d9d: Z)d;d< Z*ed=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 )]ImportsCheckerzchecks for
    * external modules dependencies
    * relative / wildcard imports
    * cyclic imports
    * uses of deprecated modules
    imports)rf      )optparse)rs   ztkinter.tixzdeprecated-modulescsvz	<modules>zBDeprecated modules which should not be used, separated by a comma.)defaulttypemetavarhelpzimport-graphrH   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analyse-fallback-blocksFZ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__.Nc                 C   sR   t | | d | _d | _g | _d | _i | _dd| jfdd| jff| _	| 
 | _d S )NZRP0401zExternal dependenciesZRP0402zModules dependencies graph)r
   __init__statsimport_graph_imports_stack_first_non_import_node_module_pkg_report_external_dependencies_report_dependencies_graphZreports_compute_site_packagesZ_site_packages)selflinterr   r   r   rz     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)ospathnormcaseabspath)r   r   r   r   _normalized_path  s   z?ImportsChecker._compute_site_packages.<locals>._normalized_pathreal_prefix)rC   z/etc/debian_versionlocallibpythonzdist-packages)setgetattrsysfilterrC   r   get_python_libaddr   r   isfiler   get_python_version)r   pathsr   rC   r   	libpythonr   r   r   r     s$   
z%ImportsChecker._compute_site_packagesc                 C   sX   | j ji d | j jg d | j j| _tt| _i | _tt| _t	| dg d| _
dS )3called before visiting project (i.e set of modules))r[   )Zcycleszignored-modules)ru   N)r   Z	add_statsr{   collectionsdefaultdictr   r|   r   _excluded_edgesr   _ignored_modulesr   r   r   r   open  s   
zImportsChecker.openc                 C   s.   t | j}|D ]}|| | j|  q|S r   )copydeepcopyr|   difference_updater   )r   Zfiltered_graphr4   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   rb   )verticesz -> )argsN)r   is_message_enabledr   listr   add_messager   )r   graphr   cycler   r   r   close  s   zImportsChecker.closec                 C   s   |  | | | | }dd |jD }t|dkr'| jdd||d |D ]<}| || | ||}t	|j
tjrC| | t	| tjrQ| || |du rVq)| |||| | ||j q)dS )z*triggered when an import statement is seenc                 S      g | ]\}}|qS r   r   r   r6   _r   r   r   r         z/ImportsChecker.visit_import.<locals>.<listcomp>   rj   z, r   r4   N)_check_reimport_check_import_as_renamer%   r   r   r   r   _check_deprecated_module_get_imported_moduler"   parentr#   r&   _check_positionr0   _record_import_check_relative_import_add_imported_moduler6   )r   r4   modnoder   r6   imported_moduler   r   r   visit_import  s$   


zImportsChecker.visit_importc                 C   s   |j }| ||}| | | | | || | || | | | j|||jd t	|j
tjr9| | t	| tjrG| || |du rMdS | }| |||| |jD ]\}}|dkrp| |d|j|f  q\| ||j q\dS )z'triggered when a from statement is seen)basenamer!   Nr.   r)   )r   r   r   _check_misplaced_futurer   _check_wildcard_imports_check_same_line_importsr   r!   r"   r   r#   r&   r   r0   r   r%   r   r   r   r6   )r   r4   r   r   r   r6   r   r   r   r   visit_importfrom  s*   



zImportsChecker.visit_importfromc                 C   s   |  |\}}}t }d }|| | D ].\}}| jd|js q|d\}	}
}
|r:||	kr:|	|v r:| jd||	d |	}||	 qg | _d | _	d S )Nrl   r   r4   r   )
_check_imports_orderr   r   r   r1   	partitionr   r   r}   r~   )r   r4   std_importsZext_importsZloc_importsZmetZcurrent_packageimport_nodeimport_name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 )Nrm   c                    s   g | ]	}t  |r|qS r   )r"   )r   allowedr4   r   r   r     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 )__)r"   r#   Z
AssignNamer6   
startswithendswith)r   targetr   r   r   r   $  s    

)r   r   r1   r~   r"   r   r#   r&   	TryExcept
TryFinallyr3   nodes_of_classr2   r$   Assigntargetsall)r   r4   Znested_allowedZis_nested_allowedZvalid_targetsr   r   r   compute_first_non_import_node  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 )Nrm   )r   r   r1   r~   r"   r   r0   r#   r&   Ifr   r   r3   r   r2   r$   )r   r4   r%   r   r   r   visit_functiondef8  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__ri   r   )r   Zprevious_siblingr"   r#   r$   r   )r   r4   r   prevr   r   r   r   P  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   rg   r   )r   r   CounterrK   r   r1   )r   r4   r   counterr6   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
        rm   r   N)r~   r   	as_string)r   r4   r   r   r   r   f  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   )
r"   r#   r$   r   r6   r   r   r!   r}   rN   )r   r4   importedmodnodeZimportednamer   r   r   r   p  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   )r#   r   )r   r   r   r   r   r,     r-   z5ImportsChecker._is_fallback_import.<locals>.<genexpr>)r3   )r4   rp   r   r   r   _is_fallback_import  s   z"ImportsChecker._is_fallback_importc                 C   s  g }g }g }g }g }g }g }g }	t jd| jj| jjd}
| jD ]\}}|dr2d|dd  }n|dd }t|j	t
j }| jd|j }|
|}||f}|dv r|| |pa|pa|	}| ||riq|r|s| jd|d|  d	|d d   fd
 q|dkr|| || |s|s|| |p|	}|r|s| jd|d|  d	|d d   fd
 q|dkr|| || |s|s|| |	}|r|s| j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
        rH   )file_contentsknown_third_partyknown_standard_libraryr   r   r   rk   )ZFUTUREZSTDLIBzstandard import "%s"z"%s"r   Z
THIRDPARTYzthird party import "%s"Z
FIRSTPARTYzfirst party import "%s"ZLOCALFOLDER)isortZSortImportsconfigr   r   r}   r   r   r"   r   r#   r&   r   r   r1   Zplace_modulerN   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_objr4   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   z| |W S  tjy"   t||| jrY d S | jd|d Y d S  tjyG } zd|t|j	}| jd|j
|d W Y d }~d S d }~w tjy~   | jdsWY d S t||| jraY d S | jjslt|rlY d S t||}| jdt||d Y d S w )Nra   r   z+Cannot import {!r} due to syntax error {!r}zsyntax-error)liner   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     s0   
 

z#ImportsChecker._get_imported_modulec                 C   sj   | j dsdS |jdu rdS ||u rdS | st|ddr!dS |j|kr3| jd||jf|d dS dS )zpcheck relative import. node is either an Import or From node, modname
        the imported module name.
        re   NFr!   r   )r   r   fileZabsolute_import_activatedr   r6   r   )r   r   r'   r   Zimportedasnamer   r   r   r     s    

z%ImportsChecker._check_relative_importc                 C   s   |  j}|  j}tjtj|d }zt||}W n	 ty&   Y nw ||kr4| j	d|d d
S t
|sz|dkrL|| jvrL|ddd | j|< | jd |t }||vr_|| | j| | | jjd|jd	s|| j| | d
S d
S d
S )z7notify an imported module, used to analyze dependenciesr   rh   r   rz   r   r   r[   rb   )r   N)r%   r   r6   r   r   splitextr   r   r=   r   r   r   rsplitr{   r@   r   r   r|   r   r   r   r   )r   r4   ZimportedmodnameZmodule_fileZcontext_namer7   Zimportedmodnamesr   r   r   r     s.   



z#ImportsChecker._add_imported_modulec                 C   s8   | j jD ]}||ks||d r| jd||d qdS )z!check if the module is deprecatedr   rd   r   N)r   deprecated_modulesr   r   )r   r4   Zmod_pathmod_namer   r   r   r   2  s
   z'ImportsChecker._check_deprecated_modulec                 C   sf   |j }|D ]+}t|s d S |d }|d}|d }|d }||kr0t|dkr0| jd|d qd S )Nr   r   r   rn   r   )r   r   r   r   r   )r   r4   r   r6   Z	real_nameZsplitted_packagesr;   r   r   r   r   8  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)rg   Nr   )	r   r   framer%   rN   r   r<   r   r1   )r   r4   r   r!   r   r%   ZcontextsZknown_contextZknown_levelr6   r8   r9   r   r   r   r   I  s&   
zImportsChecker._check_reimportc                 C   s4   t |   }|st t|}|t| dS )z4return a verbatim layout for displaying dependenciesN)rD   _external_dependencies_inforK   r	   rO   rN   r   )r   r]   r   _dummyrX   Z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 )z+write dependencies as a dot (graphviz) filer[   rH   z	external z	internal N)	r{   r   r|   Zext_import_graphZint_import_graphr	   r_   r   _internal_dependencies_info)r   r]   r   r   rX   rW   r   r   r   r   f  s&   
z)ImportsChecker._report_dependencies_graphc                 C   sf   t t}| jd  D ]$\}}|D ]}| j||}||}|r$|s(|s/|s/|| | qq|S )z2build the internal or the external depedency graphr[   )	r   r   r   r{   rK   r   getr   r   )r   internalr   Zimportee	importersimporterr   Z	is_insider   r   r   _filter_dependencies_graphy  s   

z)ImportsChecker._filter_dependencies_graphc                 C      | j ddS )zXreturn cached external dependencies information or build and
        cache them
        Fr   r   r   r   r   r   r        z*ImportsChecker._external_dependencies_infoc                 C   r   )zXreturn cached internal dependencies information or build and
        cache them
        Tr   r  r   r   r   r   r     r  z*ImportsChecker._internal_dependencies_infoc                 C   sJ   |  jrd S | |}|jD ]\}}|dkr"|s"| jd|j|d qd S )Nr.   rc   r   )r%   r   _wildcard_import_is_allowedr   r   r   )r   r4   r   Zwildcard_import_is_allowedr6   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   Zallow_wildcard_with_alllocals)r   r   r   r   r   r    s
   z*ImportsChecker._wildcard_import_is_allowedr   )NN)<__name__
__module____qualname____doc__r   Z__implements__r6   MSGSZmsgspriorityr   version_infor   DEFAULT_STANDARD_LIBRARYDEFAULT_KNOWN_THIRD_PARTYoptionsrz   staticmethodr   r   r   r   r   r   r   r   r   Zvisit_tryfinallyZvisit_tryexceptZvisit_assignattrZvisit_assignZvisit_ifexp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   cachedr   r   r   r  r   r   r   r   ro     s    

V




(

T!



ro   c                 C   s   |  t|  dS )z.required method to auto register this checker N)Zregister_checkerro   )r   r   r   r   register  s   r  r   )-r	  r   	distutilsr   r   r   r   r#   r   r   Zastroid.modutilsr   r   r   Zpylint.interfacesr   Zpylint.utilsr   Zpylint.exceptionsr	   Zpylint.checkersr
   Zpylint.checkers.utilsr   r   r   Zpylint.graphr   r   Zpylint.reporters.ureports.nodesr   r   r    r(   r<   r?   rD   rO   r\   r_   r
  r  r  ro   r  r   r   r   r   <module>   sr   $
P     