3
6ßL]›  ã               @   s®   d 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
 ddlmZmZ ddlmZmZ ddlmZmZ dd	lmZ G d
d„ deƒZG dd„ deƒZG dd„ dejƒZdS )z$ Classes used to represent modules. é    N)ÚFile)ÚAnyÚDictÚ	HasTraitsÚInstanceÚStré   )ÚAssignÚAssignFactory)ÚFunctionÚFunctionFactory)ÚKlassÚKlassFactory)Ú	Namespacec               @   sH   e Zd ZdZeeƒZeZeZ	e
ZeZeZeZdd„ Zdd„ Zdd„ ZdS )	ÚModulez A module. c             C   s
   d| j  S )z: Returns an informal string representation of the object. z	Module %s)Úfilename)Úself© r   úL/tmp/pip-install-5sebvq2j/envisage/envisage/developer/code_browser/module.pyÚ__str__4   s    zModule.__str__c             C   sN   i }| j |d< | j|d< | j|d< | j|d< | j|d< | j|d< | j|d< |S )z/ Returns the state of the object for pickling. Ú	namespacer   ÚnameÚdocÚ
attributesÚklassesÚ	functions)r   r   r   r   r   r   r   )r   Ústater   r   r   Ú__getstate__9   s    






zModule.__getstate__c             C   sJ   |d | _ |d | _|d | _|d | _|d | _|d | _|d | _dS )	z* Sets the state object duting unpickling. r   r   r   r   r   r   r   N)r   r   r   r   r   r   r   )r   r   r   r   r   Ú__setstate__G   s    






zModule.__setstate__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r   r   r   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S )ÚModuleFactoryz A factory for modules. Nc             C   sT   t |ƒ�}tj|jƒ ƒ}W dQ R X t||| j|ƒtj|dd�d�}t|ƒj|ƒ |S )z% Creates a module by parsing a file. NF)Úclean)r   r   r   r   )	ÚopenÚastÚparseÚreadr   Ú_get_module_nameÚget_docstringÚModuleVisitorÚvisit)r   r   r   ÚfÚnodeÚmoduler   r   r   Ú	from_file\   s    
zModuleFactory.from_filec             C   sX   t jjt jj|ƒƒ\}}|g}tt jj|ƒƒ}x|jrL|jd|jƒ |j	}q0W dj
|ƒS )zê Get the fully qualified module name for a filename.

        e.g. if the filename is:-

        '/enthought/envisage/core/core_plugin_definition.py'

        we would return:-

        'envisage.core.core_plugin_definition'

        r   Ú.)ÚosÚpathÚsplitextÚbasenamer   ÚdirnameÚ
is_packageÚinsertr   ÚparentÚjoin)r   r   r/   ÚextÚmodule_pathr9   r   r   r   r)   t   s    
zModuleFactory._get_module_name)N)r   r    r!   r"   r0   r)   r   r   r   r   r#   U   s   
r#   c               @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )r+   z' An AST visitor for top-level modules. c             C   s"   || _ tƒ | _tƒ | _tƒ | _dS )z Creates a new visitor. N)r/   r   Ú_klass_factoryr   Ú_function_factoryr
   Ú_assign_factory)r   r/   r   r   r   Ú__init__—   s
    zModuleVisitor.__init__c             C   s0   | j j| j|ƒ}|| jj|j< || jj|j< dS )z Visits a class node. N)r=   Úfrom_astr/   Úlocalsr   r   )r   r.   Úklassr   r   r   Úvisit_ClassDef¨   s    zModuleVisitor.visit_ClassDefc             C   s0   | j j| j|ƒ}|| jj|j< || jj|j< dS )z Visits a function node. N)r>   rA   r/   rB   r   r   )r   r.   Úfunctionr   r   r   Úvisit_FunctionDef±   s    zModuleVisitor.visit_FunctionDefc             C   sp   | j j| j|ƒ}t|jƒdkr0| jj|jƒ|_nd|_x4|jD ]*}|| jj|< || jj|< |j| jj	|< q>W dS )z Visits an assignment node. r   FN)
r?   rA   r/   ÚlenÚsourceZis_traitÚtargetsrB   r   Z	_is_trait)r   r.   ÚassignÚtargetr   r   r   Úvisit_Assign»   s    zModuleVisitor.visit_Assignc             C   s$   x|j D ]}|j| jj|j< qW dS )z Visits a from node. N)Únamesr/   Úimportsr   )r   r.   Úaliasr   r   r   Úvisit_ImportFromÏ   s    zModuleVisitor.visit_ImportFromc             C   s"   x|j D ]}d| jj|j< qW dS )z Visits an import node. Ú N)rM   r/   rN   r   )r   r.   rO   r   r   r   Úvisit_Import×   s    zModuleVisitor.visit_ImportN)
r   r    r!   r"   r@   rD   rF   rL   rP   rR   r   r   r   r   r+   �   s   	
r+   )r"   r&   r2   Zapptools.io.apir   Z
traits.apir   r   r   r   r   rJ   r	   r
   rE   r   r   rC   r   r   r   r   r   r#   ÚNodeVisitorr+   r   r   r   r   Ú<module>   s   C;