3
6ßL]  ã               @   s8   d Z ddlZddlZddlmZmZ G dd„ deƒZdS )z A Python namespace. é    N)ÚDictÚ	HasTraitsc               @   s<   e Zd ZdZeZeZeZdd„ Zdd„ Z	dd„ Z
dd	„ Zd
S )Ú	Namespacez A Python namespace. c             C   sª   dS | jj|dƒsd}q¦| j | }t|jƒdkr>| j|jƒ}q¦d}nb| j|ƒrŠ| j|ƒ}| j|ƒ}|dk	r„|j	dƒ}|j|d
 ƒ}q¦d}n| j
dk	r¢| j
j|ƒ}nd}|S )z4 Attempt to resolve a name to see if it is a trait. TZTraitFactoryZTraitZInstanceFr   NÚ.é   éÿÿÿÿ)ÚlocalsÚ	_is_traitÚgetÚlenÚsourceÚis_traitÚis_importedÚget_next_module_nameÚget_next_moduleÚsplitÚ	namespace)ÚselfÚnamer   ÚnextÚmodule_nameÚmoduleÚ
components© r   úO/tmp/pip-install-5sebvq2j/envisage/envisage/developer/code_browser/namespace.pyr      s*       





zNamespace.is_traitc             C   s   |j dƒ}|d | jkS )z% Returns TRUE if a name is imported. r   r   )r   Úimports)r   r   r   r   r   r   r   M   s    
zNamespace.is_importedc             C   s”   |j dƒ}t|ƒdkr"| j| }nng }| j|d  }t|ƒdkrX|j|ƒ dj|ƒ}|S |j|d ƒ x|dd… D ]}|j|ƒ qtW dj|ƒ}|S )zA Returns the name of the module that a symbol was imported from. r   r   r   r   )r   r   r   ÚappendÚjoin)r   Úsymbolr   r   ÚpathÚvalueÚ	componentr   r   r   r   T   s    



zNamespace.get_next_module_namec             C   s�   ddl m}m} tjj| jƒ}|||gtj ƒ}|dk	rˆtjj|ƒrRtjj	|dƒ}|j
dƒsf|j
dƒrld}qŒ|jtjƒr~d}qŒ||ƒ}nd}|S )zÊ Returns a parsed module with the specified name.

        Returns None if the module cannot be found or is 'ignored'.  We ignore
        all modules in the Python core, extension modules etc.

        r   )Úfind_moduleÚ	read_fileNz__init__.pyz.pydz.so)Zenclbrr"   r#   Úosr   ÚdirnameÚfilenameÚsysÚisdirr   ÚendswithÚ
startswithÚprefix)r   r   r"   r#   r%   r&   r   r   r   r   r   r   s    	
zNamespace.get_next_moduleN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r	   r   r   r   r   r   r   r   r   r      s   /r   )r/   r$   r'   Z
traits.apir   r   r   r   r   r   r   Ú<module>   s   