3
tûœ]q1  ã               @   sb   d Z ddl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Z	d
d„ Z
G dd„ dƒZdS )zÄastroid manager: avoid multiple astroid build of a same module when
possible by providing a class responsible to get astroid representation
from various source and using a cache of built modules)
é    N)Ú
exceptions)Úspec)Úmodutils)Ú
transformsú.zipú.eggú.whlc             C   s"   yt | ƒS  tk
r   dS X d S )Nz???)ÚreprÚ	Exception)Úobj© r   ú2/tmp/pip-build-fn42xldr/astroid/astroid/manager.pyÚ	safe_repr    s    r   c               @   s®   e Zd ZdZdZi Zdd„ Zedd„ ƒZdd„ Z	d(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„Zd+dd„Zd,dd„Zd d!„ Zd"d#„ Zd$d%„ Zd&d'„ Zd	S )-ÚAstroidManagerzmthe astroid manager, responsible to build astroid from files
     or modules.

    Use the Borg pattern.
    zastroid loaderc             C   s\   t j| _| jsXi | _i | _g | _d| _d| _tƒ | _	t
jƒ | _| jj| _| jj| _d| _d S )NFéd   )r   ÚbrainÚ__dict__Úastroid_cacheÚ_mod_file_cacheÚ_failed_import_hooksÚalways_load_extensionsZoptimize_astÚsetÚextension_package_whitelistr   ZTransformVisitorÚ
_transformZregister_transformZunregister_transformZmax_inferable_values)Úselfr   r   r   Ú__init__1   s    


zAstroidManager.__init__c             C   s
   | j d S )NÚbuiltins)r   )r   r   r   r   Úbuiltins_moduleB   s    zAstroidManager.builtins_modulec             C   s   | j j|ƒS )z8Visit the transforms and apply them to the given *node*.)r   Úvisit)r   Únoder   r   r   Úvisit_transformsF   s    zAstroidManager.visit_transformsNTFc             C   sÌ   yt j|dd�}d}W n t jk
r,   Y nX |dkrdydjt j|ƒƒ}W n tk
rb   |}Y nX || jkrˆ| j| j|krˆ| j| S |r¨ddlm	} || ƒj
||ƒS |rº|rº| j|ƒS tjd|d�‚dS )	z.given a module name, return the astroid objectT)Zinclude_no_extNÚ.r   )ÚAstroidBuilderz"Unable to build an AST for {path}.)Úpath)r   Zget_source_fileZNoSourceFileÚjoinZmodpath_from_fileÚImportErrorr   ÚfileÚastroid.builderr"   Z
file_buildÚast_from_module_namer   ÚAstroidBuildingError)r   ÚfilepathÚmodnameÚfallbackÚsourcer"   r   r   r   Úast_from_fileJ   s(    



zAstroidManager.ast_from_filec             C   s   ddl m} || ƒjd|ƒS )Nr   )r"   Ú )r'   r"   Ústring_build)r   r+   r"   r   r   r   Ú_build_stub_modulee   s    z!AstroidManager._build_stub_modulec             C   s   ddl m} |||ƒS )Nr   )Úbuild_namespace_package_module)r'   r2   )r   r+   r#   r2   r   r   r   Ú_build_namespace_modulej   s    z&AstroidManager._build_namespace_modulec                sH   ˆj r
dS tj|ƒrdS |jdƒ‰ t‡ ‡fdd„tdtˆ ƒd ƒD ƒƒS )NTr!   c             3   s&   | ]}d j ˆ d|… ƒˆjkV  qdS )r!   N)r$   r   )Ú.0Úx)Úpartsr   r   r   ú	<genexpr>v   s   z5AstroidManager._can_load_extension.<locals>.<genexpr>é   )r   r   Zis_standard_moduleÚsplitÚanyÚrangeÚlen)r   r+   r   )r6   r   r   Ú_can_load_extensiono   s    

z"AstroidManager._can_load_extensionc       	      C   sð  || j kr| j | S |dkr&| j|ƒS tjƒ }|rDtjtjj|ƒƒ �z˜�y2| j||ƒ}|jt	j
jkr€| j|jƒ}|dk	r~|S nÒ|jt	j
jt	j
jfk�r|jt	j
jkr¼| j|ƒ r¼| j|ƒS ytj|ƒ}W n8 tk
�r } ztjd||jd�|‚W Y dd}~X nX | j||ƒS |jt	j
jk�r4tjd||jd�‚n|jt	j
jk�rR| j||jƒS |jdk�rltjd|d�‚| j|j|dd	�S  tjk
�rÚ } z>x2| jD ](}y||ƒS  tjk
�r¾   Y nX �qšW |‚W Y dd}~X nX W dtj|ƒ X dS )
z.given a module name, return the astroid objectÚ__main__Nz&Loading {modname} failed with:
{error})r+   r#   z)Unable to load compiled module {modname}.z'Can't find a file for module {modname}.)r+   F)r,   )r   r1   ÚosÚgetcwdÚchdirr#   ÚdirnameÚfile_from_module_nameÚtyper   Ú
ModuleTypeZPY_ZIPMODULEÚzip_import_dataÚlocationÚ	C_BUILTINÚC_EXTENSIONr=   r   Zload_module_from_namer
   r   ÚAstroidImportErrorÚast_from_moduleÚPY_COMPILEDZPY_NAMESPACEr3   Úsubmodule_search_locationsr.   r)   r   )	r   r+   Úcontext_fileZold_cwdZ
found_specÚmoduleÚexÚeÚhookr   r   r   r(   z   s`    





z#AstroidManager.ast_from_module_namec       
      C   sÆ   t d krd S ddlm} || ƒ}x tD ]˜}y|j|tjj dƒ\}}W n tk
r\   w&Y nX yHt j	|| ƒ}|j
tjjdƒ}|j|ƒr�|d }|j|j|ƒ||ƒ}	|	S  tk
r¼   w&Y q&X q&W d S )Nr   )r"   r8   r!   z	.__init__)Ú	zipimportr'   r"   ÚZIP_IMPORT_EXTSÚrsplitr?   r#   ÚsepÚ
ValueErrorÚzipimporterÚreplaceÚ
is_packager0   Ú
get_sourcer
   )
r   r*   r"   ZbuilderÚextZeggpathÚresourceÚimporterZzmodnamerO   r   r   r   rF   ¹   s(    

zAstroidManager.zip_import_datac             C   s–   y| j ||f }W nn tk
r€   ytj|jdƒ|d�}W n2 tk
rl } ztjd||d�}W Y d d }~X nX || j ||f< Y nX t|tj	ƒr’|‚|S )Nr!   )rN   z6Failed to import module {modname} with error:
{error}.)r+   Úerror)
r   ÚKeyErrorr   Zfile_info_from_modpathr9   r%   r   rJ   Ú
isinstancer)   )r   r+   ZcontextfileÚvaluerP   r   r   r   rC   Ñ   s    z$AstroidManager.file_from_module_namec             C   sp   |p|j }|| jkr| j| S y |j}tj|ƒr<| j||ƒS W n tk
rR   Y nX ddlm} || ƒj	||ƒS )z3given an imported module, return the astroid objectr   )r"   )
Ú__name__r   Ú__file__r   Zis_python_sourcer.   ÚAttributeErrorr'   r"   Zmodule_build)r   rO   r+   r*   r"   r   r   r   rK   ä   s    



zAstroidManager.ast_from_modulec             C   sh   |dkrNy
|j }W n: tk
rL } ztjd|t|ƒ|d�|‚W Y dd}~X nX | j|ƒ}|j|jƒd S )zget astroid for the given classNz,Unable to get module for class {class_name}.)ÚclsÚ
class_reprr+   r   )Ú
__module__re   r   r)   r   r(   Úgetattrrc   )r   Úklassr+   ÚexcÚ
modastroidr   r   r   Úast_from_classô   s    

zAstroidManager.ast_from_classc       	      c   sr  t |dƒrt|tƒ r|j}n|}y
|j}W nn tk
rd } ztjd|t|ƒd�|‚W Y dd}~X n8 t	k
rš } ztj
d|t|ƒd�|‚W Y dd}~X nX y
|j}W np tk
rÞ } ztjd|t|ƒd�|‚W Y dd}~X n: t	k
�r } ztj
d|t|ƒd�|‚W Y dd}~X nX | j|ƒ}||k�rLx@|j||ƒD ]}|V  �q:W n"x |j||ƒD ]}|jƒ V  �qZW dS )z!infer astroid for the given classÚ	__class__z&Unable to get module for {class_repr}.)rf   rg   NzBUnexpected error while retrieving module for {class_repr}:
{error}z%Unable to get name for {class_repr}:
z@Unexpected error while retrieving name for {class_repr}:
{error})Úhasattrra   rD   rn   rh   re   r   r)   r   r
   rJ   rc   r(   ZigetattrZinstantiate_class)	r   r   Úcontextrj   r+   rk   Únamerl   Úinferredr   r   r   Úinfer_ast_from_something  sJ    







z'AstroidManager.infer_ast_from_somethingc             C   s   | j j|ƒ dS )a}  Registers a hook to resolve imports that cannot be found otherwise.

        `hook` must be a function that accepts a single argument `modname` which
        contains the name of the module or package that could not be imported.
        If `hook` can resolve the import, must return a node of type `astroid.Module`,
        otherwise, it must raise `AstroidBuildingError`.
        N)r   Úappend)r   rR   r   r   r   Úregister_failed_import_hook/  s    z*AstroidManager.register_failed_import_hookc             C   s   | j j|j|ƒ dS )z<Cache a module if no module with the same name is known yet.N)r   Ú
setdefaultrq   )r   rO   r   r   r   Úcache_module9  s    zAstroidManager.cache_modulec             C   s   ddl m} |jƒ  dS )zàBootstrap the required AST modules needed for the manager to work

        The bootstrap usually involves building the AST for the builtins
        module, which is required by the rest of astroid to work correctly.
        r   )Úraw_buildingN)Úastroidrx   Z_astroid_bootstrapping)r   rx   r   r   r   Ú	bootstrap=  s    zAstroidManager.bootstrapc             C   s   | j jƒ  | jƒ  dS )z@Clear the underlying cache. Also bootstraps the builtins module.N)r   Úclearrz   )r   r   r   r   Úclear_cacheG  s    
zAstroidManager.clear_cache)NTF)N)N)N)N)rc   rh   Ú__qualname__Ú__doc__rq   r   r   Úpropertyr   r    r.   r1   r3   r=   r(   rF   rC   rK   rm   rs   ru   rw   rz   r|   r   r   r   r   r   '   s(   

?


,

r   )r   r   r   )r~   r?   rS   ry   r   Zastroid.interpreter._importr   r   r   rT   r   r   r   r   r   r   Ú<module>   s   