ó
î%![c           @   s©   d  Z  d d l Z d d l 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 d l m Z d „  Z d	 e f d
 „  ƒ  YZ d S(   sÄ   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)
iÿÿÿÿN(   t
   exceptions(   t   spec(   t   modutils(   t
   transforms(   t   utilc         C   s'   y t  |  ƒ SWn t k
 r" d SXd  S(   Ns   ???(   t   reprt	   Exception(   t   obj(    (    s.   lib/python2.7/site-packages/astroid/manager.pyt	   safe_repr   s    t   AstroidManagerc           B   s¹   e  Z d  Z d Z i  Z d „  Z d „  Z d e e	 d „ Z
 d „  Z d „  Z d „  Z d d „ Z d	 „  Z d
 „  Z d d „ Z d d „ Z d d „ Z d „  Z d „  Z d d „ Z RS(   sm   the astroid manager, responsible to build astroid from files
     or modules.

    Use the Borg pattern.
    s   astroid loaderc         C   s‚   t  j |  _ |  j s~ i  |  _ i  |  _ g  |  _ t |  _ t |  _ t	 ƒ  |  _
 t j ƒ  |  _ |  j j |  _ |  j j |  _ n  d  S(   N(   R	   t   braint   __dict__t   astroid_cachet   _mod_file_cachet   _failed_import_hookst   Falset   always_load_extensionst   optimize_astt   sett   extension_package_whitelistR   t   TransformVisitort
   _transformt   register_transformt   unregister_transform(   t   self(    (    s.   lib/python2.7/site-packages/astroid/manager.pyt   __init__,   s    						c         C   s   |  j  j | ƒ S(   s8   Visit the transforms and apply them to the given *node*.(   R   t   visit(   R   t   node(    (    s.   lib/python2.7/site-packages/astroid/manager.pyt   visit_transforms<   s    c         C   s	  y t  j | d t ƒ} t } Wn t  j k
 r5 n X| d k r{ y d j t  j | ƒ ƒ } Wq{ t k
 rw | } q{ Xn  | |  j k r« |  j | j	 | k r« |  j | S| r× d d l
 m } | |  ƒ j | | ƒ S| rð | rð |  j | ƒ St j d d | ƒ‚ d S(   s.   given a module name, return the astroid objectt   include_no_extt   .iÿÿÿÿ(   t   AstroidBuilders"   Unable to build an AST for {path}.t   pathN(   R   t   get_source_filet   Truet   NoSourceFilet   Nonet   joint   modpath_from_filet   ImportErrorR   t   filet   astroid.builderR   t
   file_buildt   ast_from_module_nameR    t   AstroidBuildingError(   R   t   filepatht   modnamet   fallbackt   sourceR   (    (    s.   lib/python2.7/site-packages/astroid/manager.pyt   ast_from_file@   s&    
%c         C   s&   d d l  m } | |  ƒ j d | ƒ S(   Niÿÿÿÿ(   R   t    (   R)   R   t   string_build(   R   R.   R   (    (    s.   lib/python2.7/site-packages/astroid/manager.pyt   _build_stub_moduleV   s    c         C   s   d d l  m } | | | ƒ S(   Niÿÿÿÿ(   t   build_namespace_package_module(   R)   R5   (   R   R.   R    R5   (    (    s.   lib/python2.7/site-packages/astroid/manager.pyt   _build_namespace_moduleZ   s    c            s_   ˆ j  r t St j | ƒ r  t S| j d ƒ ‰  t ‡  ‡ f d †  t d t ˆ  ƒ d ƒ Dƒ ƒ S(   NR   c         3   s+   |  ]! } d  j  ˆ  |  ƒ ˆ j k Vq d S(   R   N(   R%   R   (   t   .0t   x(   t   partsR   (    s.   lib/python2.7/site-packages/astroid/manager.pys	   <genexpr>e   s   i   (   R   R"   R   t   is_standard_modulet   splitt   anyt   ranget   len(   R   R.   (    (   R9   R   s.   lib/python2.7/site-packages/astroid/manager.pyt   _can_load_extension^   s    	c   	   	   C   sw  | |  j  k r |  j  | S| d k r3 |  j | ƒ St j ƒ  } | ra t j t j j | ƒ ƒ n  zy¥|  j | | ƒ } | j t	 j
 j k r³ |  j | j ƒ } | d	 k	 rÈ| Sn| j t	 j
 j t	 j
 j f k rj| j t	 j
 j k r|  j | ƒ r|  j | ƒ Sy t j | ƒ } Wn> t k
 rY} t j t j d d | d | j d | ƒƒ n X|  j | | ƒ S| j t	 j
 j k r t j d d | d | j ƒ‚ n( | j t	 j
 j k rÈ|  j | | j ƒ S| j d	 k rït j d d | ƒ‚ n  |  j | j | d t ƒSWnU t j  k
 r`} x6 |  j! D]+ } y | | ƒ SWq(t j  k
 rRq(Xq(W| ‚ n XWd	 t j | ƒ Xd	 S(
   s.   given a module name, return the astroid objectt   __main__s&   Loading {modname} failed with:
{error}R.   R    t   errors)   Unable to load compiled module {modname}.s'   Can't find a file for module {modname}.R/   N("   R   R4   t   ost   getcwdt   chdirR    t   dirnamet   file_from_module_namet   typeR   t
   ModuleTypet   PY_ZIPMODULEt   zip_import_datat   locationR$   t	   C_BUILTINt   C_EXTENSIONR?   R   t   load_module_from_nameR   R   t   reraiseR    t   AstroidImportErrort   ast_from_modulet   PY_COMPILEDt   PY_NAMESPACER6   t   submodule_search_locationsR1   R   R,   R   (	   R   R.   t   context_filet   old_cwdt
   found_spect   modulet   ext   et   hook(    (    s.   lib/python2.7/site-packages/astroid/manager.pyR+   h   s\     	
c   
      C   s  t  d  k r d  Sd d l m } | |  ƒ } xÏ d D]Ç } y& | j | t j j d ƒ \ } } Wn t k
 ru q3 n Xym t  j	 | | ƒ } | j
 t j j d ƒ } | j | ƒ rÀ | d } n  | j | j | ƒ | | ƒ }	 |	 SWq3 t k
 rù q3 q3 Xq3 Wd  S(	   Niÿÿÿÿ(   R   s   .zips   .eggi   R   s	   .__init__(   s   .zips   .egg(   t	   zipimportR$   R)   R   t   rsplitRB   R    t   sept
   ValueErrort   zipimportert   replacet
   is_packageR3   t
   get_sourceR   (
   R   R-   R   t   buildert   extt   eggpatht   resourcet   importert   zmodnameRX   (    (    s.   lib/python2.7/site-packages/astroid/manager.pyRJ   ¥   s(    &c         C   sð   y' |  j  | | f } t j ƒ  d } Wn— t k
 rÀ y2 t j | j d ƒ d | ƒ} t j ƒ  d } Wn> t k
 r© } t j	 d d | d | ƒ} t j ƒ  d } n X| |  j  | | f <n Xt
 | t j ƒ rì t j t j | | ƒ n  | S(   Ni   R   RU   s6   Failed to import module {modname} with error:
{error}.R.   RA   (   R   t   syst   exc_infot   KeyErrorR   t   file_info_from_modpathR;   R'   R    RP   t
   isinstanceR,   t   sixRO   (   R   R.   t   contextfilet   valuet	   tracebackRY   (    (    s.   lib/python2.7/site-packages/astroid/manager.pyRF   »   s$    c         C   s   | p | j  } | |  j k r) |  j | Sy, | j } t j | ƒ rT |  j | | ƒ SWn t k
 rh n Xd d l m } | |  ƒ j	 | | ƒ S(   s3   given an imported module, return the astroid objectiÿÿÿÿ(   R   (
   t   __name__R   t   __file__R   t   is_python_sourceR1   t   AttributeErrorR)   R   t   module_build(   R   RX   R.   R-   R   (    (    s.   lib/python2.7/site-packages/astroid/manager.pyRQ   Ï   s    	c      	   C   s   | d k r^ y | j } Wq^ t k
 rZ t j t j d d | d t | ƒ d | ƒƒ q^ Xn  |  j | ƒ } | j	 | j
 ƒ d S(   s   get astroid for the given classs,   Unable to get module for class {class_name}.t   clst
   class_reprR.   i    N(   R$   t
   __module__Rv   R   RO   R    R,   R   R+   t   getattrRs   (   R   t   klassR.   t
   modastroid(    (    s.   lib/python2.7/site-packages/astroid/manager.pyt   ast_from_classÞ   s    #c   	   	   c   s¯  t  | d ƒ r+ t | t ƒ r+ | j } n | } y | j } Wny t k
 ry t j t j	 d d | d t
 | ƒ ƒƒ nA t k
 r¹ } t j t j d d | d t
 | ƒ d | ƒƒ n Xy | j } Wny t k
 rt j t j	 d d | d t
 | ƒ ƒƒ nA t k
 rB} t j t j d d | d t
 | ƒ d | ƒƒ n X|  j | ƒ } | | k rƒxJ | j | | ƒ D] } | VqqWn( x% | j | | ƒ D] } | j ƒ  Vq–Wd	 S(
   s!   infer astroid for the given classt	   __class__s&   Unable to get module for {class_repr}.Rx   Ry   sB   Unexpected error while retrieving module for {class_repr}:
{error}RA   s%   Unable to get name for {class_repr}:
s@   Unexpected error while retrieving name for {class_repr}:
{error}N(   t   hasattrRn   RG   R   Rz   Rv   R   RO   R    R,   R   R   RP   Rs   R+   t   igetattrt   instantiate_class(	   R   R   t   contextR|   R.   RY   t   nameR}   t   inferred(    (    s.   lib/python2.7/site-packages/astroid/manager.pyt   infer_ast_from_somethingê   s:      c         C   s   |  j  j | ƒ d S(   s}  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   t   append(   R   R[   (    (    s.   lib/python2.7/site-packages/astroid/manager.pyt   register_failed_import_hook  s    c         C   s   |  j  j | j | ƒ d S(   s<   Cache a module if no module with the same name is known yet.N(   R   t
   setdefaultR„   (   R   RX   (    (    s.   lib/python2.7/site-packages/astroid/manager.pyt   cache_module  s    c         C   s0   |  j  j ƒ  d d  l } | j j d | ƒ d  S(   Niÿÿÿÿt   astroid_builtin(   R   t   cleart   astroid.raw_buildingt   raw_buildingt   _astroid_bootstrapping(   R   R‹   t   astroid(    (    s.   lib/python2.7/site-packages/astroid/manager.pyt   clear_cache  s    N(   Rs   Rz   t   __doc__R„   R
   R   R   R$   R"   R   R1   R4   R6   R?   R+   RJ   RF   RQ   R~   R†   Rˆ   RŠ   R‘   (    (    (    s.   lib/python2.7/site-packages/astroid/manager.pyR	   "   s$   					
=		#	
	(   R’   RB   Rj   R\   Ro   R   R    t   astroid.interpreter._importR   R   R   R   R   t   objectR	   (    (    (    s.   lib/python2.7/site-packages/astroid/manager.pyt   <module>   s   	