3
tûœ]Ü.  ã               @   s®   d Z ddlZddlZddlZddlZddlmZmZmZmZm	Z	 ddl
mZ dd„ Zdd„ Zd	efd
d„ZG dd„ dƒZG dd„ deejƒZG dd„ dƒZeddfdd„ZdS )z{
Visitor doing some postprocessing on the astroid tree.
Try to resolve definitions (namespace) dictionary, relationship...
é    N)ÚbasesÚ
exceptionsÚmanagerÚmodutilsÚnode_classes)Úutilsc             C   s   dS )z@Handler used by interfaces to handle suspicious interface nodes.T© )Ú_r   r   ú</tmp/pip-build-fn42xldr/pylint/pylint/pyreverse/inspector.pyÚ_iface_hdlr   s    r   c             C   sn   t d| ƒ y| |ƒS  tjk
r@ } zt |ƒ W Y d d }~X n* tk
rh } ztjƒ  W Y d d }~X nX d S )Nzparsing %s...)Úprintr   ZAstroidBuildingExceptionÚ	ExceptionÚ	tracebackÚ	print_exc)ÚfuncÚmodnameÚexcr   r   r
   Ú_astroid_wrapper   s    r   Tc             c   s¤   yt j| ƒjdƒd }W n tjk
r.   dS X | rF|jƒ | k	rFdS tƒ }d}xBtj|ƒD ]4}|t	j
krpd}q\||kr\||ƒr\|j|ƒ |V  q\W |r tjƒ ‚dS )zEReturn an iterator on interfaces implemented by the given class node.Z__implements__r   NFT)r   ZInstanceÚgetattrr   ZNotFoundErrorÚframeÚsetr   Zunpack_inferÚastroidZUninferableÚaddÚInferenceError)ÚnodeZheritedZhandler_funcÚ
implementsÚfoundÚmissingZifacer   r   r
   Ú
interfaces'   s"    


r   c               @   s,   e Zd ZdZd
dd„Zddd„Zdd„ Zd	S )ÚIdGeneratorMixInz1Mixin adding the ability to generate integer uid.r   c             C   s
   || _ d S )N)Úid_count)ÚselfÚstart_valuer   r   r
   Ú__init__?   s    zIdGeneratorMixIn.__init__c             C   s
   || _ dS )zinit the id counter
        N)r    )r!   r"   r   r   r
   Úinit_counterB   s    zIdGeneratorMixIn.init_counterc             C   s   |  j d7  _ | j S )z"generate a new identifier
        é   )r    )r!   r   r   r
   Úgenerate_idG   s    zIdGeneratorMixIn.generate_idN)r   )r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r#   r$   r&   r   r   r   r
   r   <   s   

r   c               @   s†   e Zd ZdZddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	eZ
eZeZe	Zdd„ Zedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚLinkera  Walk on the project tree and resolve relationships.

    According to options the following attributes may be
    added to visited nodes:

    * uid,
      a unique identifier for the node (on astroid.Project, astroid.Module,
      astroid.Class and astroid.locals_type). Only if the linker
      has been instantiated with tag=True parameter (False by default).

    * Function
      a mapping from locals names to their bounded value, which may be a
      constant like a string or an integer, or an astroid node
      (on astroid.Module, astroid.Class and astroid.Function).

    * instance_attrs_type
      as locals_type but for klass member attributes (only on astroid.Class)

    * implements,
      list of implemented interface _objects_ (only on astroid.Class nodes)
    r   Fc             C   s,   t j| ƒ tjj| ƒ || _|| _|| _d S )N)r   r#   r   ÚLocalsVisitorÚinherited_interfacesÚtagÚproject)r!   r/   r-   r.   r   r   r
   r#   e   s
    
zLinker.__init__c             C   s.   | j r| jƒ |_x|jD ]}| j|ƒ qW dS )zbvisit a pyreverse.utils.Project node

         * optionally tag the node with a unique id
        N)r.   r&   ÚuidÚmodulesÚvisit)r!   r   Úmoduler   r   r
   Úvisit_projecto   s    
zLinker.visit_projectc             C   s0   | j r| jƒ |_x|jƒ D ]}| j|ƒ qW dS )z[visit an astroid.Package node

         * optionally tag the node with a unique id
        N)r.   r&   r0   Úvaluesr2   )r!   r   Zsubelmtr   r   r
   Úvisit_packagey   s    
zLinker.visit_packagec             C   s4   t |dƒrdS tjtƒ|_g |_| jr0| jƒ |_dS )z¤visit an astroid.Module node

         * set the locals_type mapping
         * set the depends mapping
         * optionally tag the node with a unique id
        Úlocals_typeN)	ÚhasattrÚcollectionsÚdefaultdictÚlistr7   Údependsr.   r&   r0   )r!   r   r   r   r
   Úvisit_moduleƒ   s    
zLinker.visit_modulec             C   sÎ   t |dƒrdS tjtƒ|_| jr*| jƒ |_x0|jdd�D ] }t	|dg ƒ}|j
|ƒ ||_q8W tjtƒ|_x,|jjƒ D ]}x|D ]}| j||ƒ q~W qtW ytt|| jƒƒ|_W n tjk
rÈ   f |_Y nX dS )zÉvisit an astroid.Class node

         * set the locals_type and instance_attrs_type mappings
         * set the implements list and build it
         * optionally tag the node with a unique id
        r7   NF)ZrecursÚspecializations)r8   r9   r:   r;   r7   r.   r&   r0   Z	ancestorsr   Úappendr>   Úinstance_attrs_typeZinstance_attrsr5   Úhandle_assignattr_typer   r-   r   r   r   )r!   r   Zbaseobjr>   ZassignattrsZ
assignattrr   r   r
   Úvisit_classdef‘   s"    




zLinker.visit_classdefc             C   s.   t |dƒrdS tjtƒ|_| jr*| jƒ |_dS )zƒvisit an astroid.Function node

         * set the locals_type mapping
         * optionally tag the node with a unique id
        r7   N)r8   r9   r:   r;   r7   r.   r&   r0   )r!   r   r   r   r
   Úvisit_functiondef­   s
    
zLinker.visit_functiondefc             C   sÈ   t |dƒrdS d|_|j|jƒ kr,|jƒ }n|jƒ }yxt |dƒszt|tjƒrX| j|ƒ n"t|tj	ƒrp| j
|ƒ n
| j|ƒ |j|j }t|jƒ ƒ}tt|ƒ|B ƒ|j|j< W n tjk
rÂ   Y nX dS )zEvisit an astroid.AssignName node

        handle locals_type
        Ú_handledNTr7   )r8   rD   Únamer   ÚrootÚ
isinstancer   ÚClassDefrB   ÚFunctionDefrC   r=   r7   r   Úinferr;   r   )r!   r   r   Úcurrentr5   r   r   r
   Úvisit_assignname¾   s$    



zLinker.visit_assignnamec             C   sP   y4t | jƒ ƒ}t |j| j ƒ}t||B ƒ|j| j< W n tjk
rJ   Y nX dS )zNhandle an astroid.assignattr node

        handle instance_attrs_type
        N)r   rJ   r@   Úattrnamer;   r   r   )r   Úparentr5   rK   r   r   r
   rA   à   s    zLinker.handle_assignattr_typec             C   s@   |j ƒ j}x0|jD ]&}tj|d |ƒ}| j||d |ƒ qW dS )zJvisit an astroid.Import node

        resolve module dependencies
        r   N)rF   ÚfileÚnamesr   Úis_relativeÚ_imported_module)r!   r   Úcontext_filerE   Úrelativer   r   r
   Úvisit_importí   s    
zLinker.visit_importc             C   s¨   |j }|jƒ j}|dk	r&tj||ƒ}nd}xx|jD ]n}|d dkrDq2d||d f }|jdƒdkrŠytj||ƒ}W n tk
rˆ   w2Y nX ||kr2| j	|||ƒ q2W dS )	zNvisit an astroid.ImportFrom node

        resolve module dependencies
        NFr   Ú*z%s.%sÚ.r%   éÿÿÿÿ)
r   rF   rO   r   rQ   rP   ÚfindZget_module_partÚImportErrorrR   )r!   r   ÚbasenamerS   rT   rE   Úfullnamer   r   r
   Úvisit_importfrom÷   s     
zLinker.visit_importfromc             C   s2   t jj| jjƒ}||krdS tj||fƒr.dS dS )z9return true if the module should be added to dependenciesr   r%   )ÚosÚpathÚdirnamer/   r   Zis_standard_module)r!   Úcontext_nameÚmod_pathÚpackage_dirr   r   r
   Úcompute_module  s    zLinker.compute_modulec             C   sj   |j ƒ }|j}|r2ddj|jdƒdd… ƒ|f }| j||ƒrft|dƒsNg |_|j}||krf|j|ƒ dS )z7Notify an imported module, used to analyze dependenciesz%s.%srW   Nr%   r<   rX   )rF   rE   ÚjoinÚsplitrd   r8   r<   r?   )r!   r   rb   rT   r3   ra   Z	mod_pathsr   r   r
   rR     s     
zLinker._imported_moduleN)r   F)r'   r(   r)   r*   r#   r4   r6   r=   rB   rC   Zlink_projectZlink_moduleZ
link_classZlink_functionrL   ÚstaticmethodrA   rU   r]   rd   rR   r   r   r   r
   r+   N   s"   



"
	r+   c               @   s:   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚProjectz,a project handle a set of modules / packagesÚ c             C   sN   || _ d | _g | _i | _| jj| _| jj| _| jj| _| jj| _| jj| _d S )N)	rE   r_   r1   ÚlocalsÚ__getitem__Ú__iter__r5   ÚkeysÚitems)r!   rE   r   r   r
   r#   *  s    



zProject.__init__c             C   s   || j |j< | jj|ƒ d S )N)rj   rE   r1   r?   )r!   r   r   r   r
   Ú
add_module5  s    zProject.add_modulec             C   s
   | j | S )N)rj   )r!   rE   r   r   r
   Ú
get_module9  s    zProject.get_modulec             C   s   | j S )N)r1   )r!   r   r   r
   Úget_children<  s    zProject.get_childrenc             C   s   d| j t| ƒt| jƒf S )Nz<Project %r at %s (%s modules)>)rE   ÚidÚlenr1   )r!   r   r   r
   Ú__repr__?  s    zProject.__repr__N)ri   )	r'   r(   r)   r*   r#   ro   rp   rq   rt   r   r   r   r
   rh   '  s   
rh   zno nameÚCVSc       
      C   sò   t jƒ }t|ƒ}xÜ| D ]Ô}tjj|ƒs8tj|jdƒƒ}n tjj	|ƒrTtjj
|dƒ}n|}||j|ƒ}|dkrnq|jpx|j|_|j|ƒ |j}	|jr|jdƒdkrxHtjtjj|jƒ|ƒD ].}||j|ƒ}|dks¸|j|	krÜq¸|j|ƒ q¸W qW |S )z0return a Project from a list of files or modulesrW   z__init__.pyNr#   r%   rX   )r   ZAstroidManagerrh   r^   r_   Úexistsr   Zfile_from_modpathrf   Úisdirre   Zast_from_filerO   ro   rE   ÚpackagerY   Zget_module_filesr`   )
ÚfilesÚfunc_wrapperÚproject_nameZ
black_listZastroid_managerr/   Z	somethingZfpathÚastÚ	base_namer   r   r
   Úproject_from_filesG  s,    

r~   )ru   )r*   r9   r^   r   r   r   r   r   r   r   Zpylint.pyreverser   r   r   r   r   r,   r+   rh   r~   r   r   r   r
   Ú<module>   s   
 Z!