ó
î%![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	 Z	 d d l
 m Z d d l
 m Z e j j d ƒ r£ d5 Z d6 Z n d7 Z d8 Z yF e e d e d e j ƒ e d e d e j ƒ e d e ƒ g ƒ Z Wn e k
 re ƒ  Z n Xe j d k r±e j e j j e j d ƒ ƒ y# e j e j j e j d ƒ ƒ Wq±e k
 r­y# e j e j j e j d ƒ ƒ Wq®e k
 r©q®Xq±Xn  e j ƒ  d k r%e j j e j d ƒ Z e j e ƒ y# e j e j j e j d ƒ ƒ Wn e k
 rn X[ n  e j d k r¢y e j Z Wn e k
 r]e j Z n Xd „  Z e j e d ƒ ƒ e j d9 k r¢e j e d ƒ ƒ q¢n  e ƒ  Z  e j ƒ  d k Z! e" j# e j$ e ƒ Z% d e& f d „  ƒ  YZ' d „  Z( d „  Z) e! d „ Z* d  „  Z+ i  Z, d! „  Z- d e d" „ Z/ d d d# „ Z0 d e d d$ „ Z1 d% „  Z2 d d d& „ Z3 d d' „ Z4 d d d( „ Z5 d d d) „ Z6 d d* „ Z7 e8 d+ „ Z9 e8 d, „ Z: d- „  Z; d d. „ Z< d/ „  Z= d d d0 „ Z> d1 „  Z? d2 „  Z@ d3 „  ZA d4 „  ZB d S(:   sV  Python modules manipulation utility functions.

:type PY_SOURCE_EXTS: tuple(str)
:var PY_SOURCE_EXTS: list of possible python source file extension

:type STD_LIB_DIRS: set of str
:var STD_LIB_DIRS: directories where standard modules are located

:type BUILTIN_MODULES: dict
:var BUILTIN_MODULES: dictionary with builtin module names has key
iÿÿÿÿN(   t   get_python_lib(   t   DistutilsPlatformErrori   (   t   spec(   t   utilt   wint   pyt   pywt   dllt   pydt   sot   standard_libt   prefixt   ntt   dllst   PyPyt   lib_pypyt   posixc         C   s'   d t  j d  } t j j t |  | ƒ S(   Ns   python%d.%di   (   t   syst   version_infot   ost   patht   joinR   (   R   t   base_python(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   _posix_path^   s    t   libi   i    t   lib64t   Jythont   NoSourceFilec           B   s   e  Z d  Z RS(   sa   exception raised when we are not able to get a python
    source file for a precompiled file
    (   t   __name__t
   __module__t   __doc__(    (    (    s/   lib/python2.7/site-packages/astroid/modutils.pyR   r   s   c         C   s   t  j j t  j j |  ƒ ƒ S(   N(   R   R   t   normcaset   abspath(   R   (    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   _normalize_pathw   s    c         C   s   t  j j t  j j |  ƒ ƒ S(   N(   R   R   t   realpatht
   expanduser(   R   (    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   _canonicalize_path{   s    c         C   sa   | s7 t  j d k r |  S|  j d ƒ r0 |  d  S|  Sn  |  j d ƒ \ } } } | r] | d S|  S(   Ni   i    s   .pyciÿÿÿÿs	   $py.classs   .py(   i   i    (   R   R   t   endswitht	   partition(   t   filenamet	   is_jythont   headt   has_pyclasst   _(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   _path_from_filename   s    c         C   sM   xF |  D]> } | | k r) | j  | ƒ q | | k r | j  | ƒ q q Wd S(   s`   remove files/directories in the black list

    dirnames/filenames are usually from os.walk
    N(   t   remove(   t	   blacklistt   dirnamest	   filenamest   norecurs(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   _handle_blacklist   s
    c         C   sI   y t  |  SWn6 t k
 rD |  s, t |  ƒ St |  ƒ } t  |  <| SXd S(   s   abspath with cachingN(   t   _NORM_PATH_CACHEt   KeyErrorR!   (   R   t   result(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   _cache_normalize_path›   s    
c         C   s   t  |  j d ƒ | | ƒ S(   s"  Load a Python module from its name.

    :type dotted_name: str
    :param dotted_name: python name of a module or package

    :type path: list or None
    :param path:
      optional list of path where the module or package should be
      searched (use sys.path if nothing or None is given)

    :type use_sys: bool
    :param use_sys:
      boolean indicating whether the sys.modules dictionary should be
      used or not


    :raise ImportError: if the module or package is not found

    :rtype: module
    :return: the loaded module
    t   .(   t   load_module_from_modpatht   split(   t   dotted_nameR   t   use_sys(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   load_module_from_name¨   s    c         C   s¼  | r5 y t  j d j |  ƒ SWq5 t k
 r1 q5 Xn  g  } d } xt|  D]l} | j | ƒ d j | ƒ } d } t | ƒ t |  ƒ k r t  j j | ƒ } n | r¸ t  j j | ƒ } n  | d k rt j	 | | ƒ \ } }	 }
 t j
 | | |	 |
 ƒ } | r| j ƒ  qn  | r&t | | | ƒ n  t | d d ƒ } | } | rZt j | ƒ rZqH n  | rŸt | ƒ t |  ƒ k rŸt d d j |  t | ƒ ƒ ƒ ‚ n  t j j | ƒ g } qH W| S(   s<  Load a python module from its split name.

    :type parts: list(str) or tuple(str)
    :param parts:
      python name of a module or package split on '.'

    :type path: list or None
    :param path:
      optional list of path where the module or package should be
      searched (use sys.path if nothing or None is given)

    :type use_sys: bool
    :param use_sys:
      boolean indicating whether the sys.modules dictionary should be used or not

    :raise ImportError: if the module or package is not found

    :rtype: module
    :return: the loaded module
    R7   t   __file__t    s   no module in %sN(   R   t   modulesR   R4   t   Nonet   appendt   lent   gett   impt   find_modulet   load_modulet   closet   setattrt   getattrR   t   is_namespacet   ImportErrorR   R   t   dirname(   t   partsR   R;   t   modpatht
   prevmodulet   partt   curnamet   modulet   mp_filet   mp_filenamet   mp_desct   _file(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyR8   Á   s<    &c         C   s   t  |  | ƒ } t | | | ƒ S(   s  Load a Python module from it's path.

    :type filepath: str
    :param filepath: path to the python module or package

    :type path: list or None
    :param path:
      optional list of path where the module or package should be
      searched (use sys.path if nothing or None is given)

    :type use_sys: bool
    :param use_sys:
      boolean indicating whether the sys.modules dictionary should be
      used or not


    :raise ImportError: if the module or package is not found

    :rtype: module
    :return: the loaded module
    (   t   modpath_from_fileR8   (   t   filepathR   R;   t	   extrapathRN   (    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   load_module_from_fileù   s    c         C   sn   g  } xa | D]Y } | j  | ƒ t j j |  | ƒ }  t |  ƒ s t j d j | ƒ ƒ } | sf t Sq q Wt S(   s2   check there are some __init__.py all along the wayR7   (	   RA   R   R   R   t	   _has_initR   RJ   t   Falset   True(   R   t   mod_pathRN   RP   t   old_namespace(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   check_modpath_has_init  s    c   	      C   sÚ  t  |  ƒ }  t j j t j j |  ƒ ƒ }  t j j |  ƒ d } | d  k	 rxÆ t j j	 t
 | ƒ D]¬ } t j j | ƒ } | rb t j j | t | ƒ  ƒ t j j | ƒ k rb g  | t | ƒ j t j ƒ D] } | rË | ^ qË } | | | d  ƒ r| | j d ƒ | Sqb qb Wn  xœ t j j	 t
 t j ƒ D]‚ } t | ƒ } | r.t j j | ƒ j | ƒ r.g  | t | ƒ j t j ƒ D] } | r~| ^ q~} | | | d  ƒ r°| Sq.q.Wt d |  d j t j ƒ f ƒ ‚ d  S(   Ni    iÿÿÿÿR7   s"   Unable to find module for %s in %ss   , 
(   R,   R   R   R"   R#   t   splitextR@   t   sixt   movest   mapR$   R    R   RB   R9   t   sepR   R6   t
   startswithRK   R   (	   R'   RY   t   is_package_cbt   baset   path_R   t   pkgt
   submodpathRN   (    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   modpath_from_file_with_callback   s&    4#!5c         C   s   t  |  | t ƒ S(   si  given a file path return the corresponding split module's name
    (i.e name of a module or package split on '.')

    :type filename: str
    :param filename: file's path for which we want the module's name

    :type extrapath: dict
    :param extrapath:
      optional extra search path, with path as key and package name for the path
      as value. This is usually useful to handle package split in multiple
      directories using __path__ trick.


    :raise ImportError:
      if the corresponding module's name has not been found

    :rtype: list(str)
    :return: the corresponding split module's name
    (   Rl   R`   (   R'   RY   (    (    s/   lib/python2.7/site-packages/astroid/modutils.pyRW   :  s    c         C   s   t  |  | | ƒ j S(   N(   t   file_info_from_modpatht   location(   RN   R   t   context_file(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   file_from_modpathQ  s    c         C   sÁ   | d k	 r! t j j | ƒ } n | } |  d d k rz y t d g |  d | | ƒ SWq± t k
 rv t |  | | ƒ SXn7 |  d d g k r± t j d d d	 t j j d
 t	 j
 ƒ St |  | | ƒ S(   sÿ  given a mod path (i.e. split module / package name), return the
    corresponding file, giving priority to source file over precompiled
    file if it exists

    :type modpath: list or tuple
    :param modpath:
      split module's name (i.e name of a module or package split
      on '.')
      (this means explicit relative imports that start with dots have
      empty strings in this list!)

    :type path: list or None
    :param path:
      optional list of path where the module or package should be
      searched (use sys.path if nothing or None is given)

    :type context_file: str or None
    :param context_file:
      context file to consider, necessary if the identifier has been
      introduced using a relative import unresolvable in the actual
      context (i.e. modutils)

    :raise ImportError: if there is no such module in the directory

    :rtype: (str or None, import type)
    :return:
      the path to the module's file or None if it's an integrated
      builtin module such as 'sys'
    i    t   xmlt   _xmlplusi   R   R   t   names   os.pathRn   t   module_typeN(   R@   R   R   RL   t   _spec_from_modpathRK   R   t
   ModuleSpecR=   RD   t	   PY_SOURCE(   RN   R   Ro   t   context(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyRm   T  s    %c         C   sp  |  j  d ƒ r d S|  j d ƒ } | d
 k	 rj | d t k rj t | ƒ d k r_ t |  ƒ ‚ n  | d Sn  d
 } d } | d d k r­ | d
 k	 sž t d ƒ ‚ g  } d } n  x0 | | d k rß | d 7} t j j	 | ƒ } q° Wx‰ t
 | t | ƒ ƒ D]r } y% t | | | d !d | d	 | ƒWqö t k
 rg| t d t | ƒ d ƒ k  rV‚  n  d j | |  ƒ SXqö W|  S(   s  given a dotted name return the module part of the name :

    >>> get_module_part('astroid.as_string.dump')
    'astroid.as_string'

    :type dotted_name: str
    :param dotted_name: full name of the identifier we are interested in

    :type context_file: str or None
    :param context_file:
      context file to consider, necessary if the identifier has been
      introduced using a relative import unresolvable in the actual
      context (i.e. modutils)


    :raise ImportError: if there is no such module in the directory

    :rtype: str or None
    :return:
      the module part of the name or None if we have not been able at
      all to import the given name

    XXX: deprecated, since it doesn't handle package precedence over module
    (see #10066)
    s   os.pathR7   i    i   R>   s.   explicit relative import, but no context_file?i   R   Ro   N(   Rf   R9   R@   t   BUILTIN_MODULESRB   RK   t   AssertionErrorR   R   RL   t   rangeRp   t   maxR   (   R:   Ro   RM   R   t   startit   i(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   get_module_part‚  s6    		
c   	      C   s±   g  } x¤ t  j |  ƒ D]“ \ } } } | | k r7 q n  t | | | ƒ | rg d | k rg d | (q n  x? | D]7 } t | ƒ rn t  j j | | ƒ } | j | ƒ qn qn Wq W| S(   sN  given a package directory return a list of all available python
    module's files in the package and its subpackages

    :type src_directory: str
    :param src_directory:
      path of the directory corresponding to the package

    :type blacklist: list or tuple
    :param blacklist: iterable
      list of files or directories to ignore.

    :type list_all: bool
    :param list_all:
        get files from all paths, including ones without __init__.py

    :rtype: list
    :return:
      the list of all available python module's files in the package and
      its subpackages
    s   __init__.py(    (   R   t   walkR2   t   _is_python_fileR   R   RA   (	   t   src_directoryR.   t   list_allt   filest	   directoryR/   R0   R'   t   src(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   get_module_files½  s    c         C   sš   t  j j t |  ƒ ƒ }  t  j j |  ƒ \ } } x4 t D], } d | | f } t  j j | ƒ r7 | Sq7 W| rŠ | rŠ t  j j | ƒ rŠ | St |  ƒ ‚ d S(   sœ  given a python module's file name return the matching source file
    name (the filename will be returned identically if it's a already an
    absolute path to a python source file...)

    :type filename: str
    :param filename: python module's file name


    :raise NoSourceFile: if no source file exists on the file system

    :rtype: str
    :return: the absolute path of the source file if it exists
    s   %s.%sN(   R   R   R    R,   Ra   t   PY_SOURCE_EXTSt   existsR   (   R'   t   include_no_extRh   t   orig_extt   extt   source_path(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   get_source_fileâ  s    c         C   s   t  j j |  ƒ d d t k S(   sN   
    rtype: bool
    return: True if the filename is a python source file
    i   (   R   R   Ra   Rˆ   (   R'   (    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   is_python_sourceû  s    c         C   s½   |  j  d ƒ d }  y t |  g ƒ } Wn t k
 r: t SX| d k rU t j |  ƒ St | ƒ } | j t	 t
 ƒ ƒ rz t S| d k r t } n  x' | D] } | j t	 | ƒ ƒ r– t Sq– Wt S(   sÐ  try to guess if a module is a standard python module (by default,
    see `std_path` parameter's description)

    :type modname: str
    :param modname: name of the module we are interested in

    :type std_path: list(str) or tuple(str)
    :param std_path: list of path considered has standard


    :rtype: bool
    :return:
      true if the module:
      - is located on the path listed in one of the directory in `std_path`
      - is a built-in module
    R7   i    N(   R9   Rp   RK   R\   R@   R   RJ   R!   Rf   R6   t   EXT_LIB_DIRt   STD_LIB_DIRSR]   (   t   modnamet   std_pathR'   R   (    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   is_standard_module  s     	c         C   s™   t  j j | ƒ s' t  j j | ƒ } n  | t j k r: t SyF t j |  j d ƒ d | g ƒ \ } } } | r{ | j	 ƒ  n  t
 SWn t k
 r” t SXd S(   sv  return true if the given module name is relative to the given
    file name

    :type modname: str
    :param modname: name of the module we are interested in

    :type from_file: str
    :param from_file:
      path of the module from which modname has been imported

    :rtype: bool
    :return:
      true if the module has been imported relatively to `from_file`
    R7   i    N(   R   R   t   isdirRL   R   R\   RD   RE   R9   RG   R]   RK   (   R’   t	   from_filet   streamR+   (    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   is_relative,  s    +c         C   sU  |  s t  ‚ d } | d k	 rr y" t j |  | g ƒ } | j } Wq„ t k
 rn t j |  | ƒ } | j } q„ Xn t j |  | ƒ } | j t j j k rì y/ t	 | j ƒ } | j
 d | d t j j ƒ SWqQt k
 rè | j
 d | ƒ SXne | j t j j k r| j
 d d ƒ S| j t j j k rQt | j ƒ } | j
 d | d t j j ƒ S| S(   sÃ   given a mod path (i.e. split module / package name), return the
    corresponding spec

    this function is used internally, see `file_from_modpath`'s
    documentation for more information
    Rn   t   typeN(   Rz   R@   R   t	   find_specRn   RK   R™   t
   ModuleTypet   PY_COMPILEDRŽ   t   _replaceRw   R   t	   C_BUILTINt   PKG_DIRECTORYR[   (   RN   R   Rx   Rn   t
   found_spec(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyRu   L  s,     c         C   s(   x! d D] } |  j  | ƒ r t Sq Wt S(   sk   return true if the given filename should be considered as a python file

    .pyc and .pyo are ignored
    s   .pys   .sos   .pyds   .pyw(   s   .pys   .sos   .pyds   .pyw(   R%   R]   R\   (   R'   RŒ   (    (    s/   lib/python2.7/site-packages/astroid/modutils.pyR   m  s    c         C   sT   t  j j |  d ƒ } x8 t d D], } t  j j | d | ƒ r  | d | Sq  Wd S(   s\   if the given directory has a valid __init__ file, return its path,
    else return None
    t   __init__t   pyct   pyoR7   (   R¢   R£   N(   R   R   R   Rˆ   R‰   R@   (   R…   t   mod_or_packRŒ   (    (    s/   lib/python2.7/site-packages/astroid/modutils.pyR[   x  s
    c         C   s   |  j  t j j k S(   N(   R™   R   R›   t   PY_NAMESPACE(   t   specobj(    (    s/   lib/python2.7/site-packages/astroid/modutils.pyRJ   ƒ  s    c         C   s   |  j  t j j k S(   N(   R™   R   R›   RŸ   (   R¦   (    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   is_directory†  s    (   R   R   (   R   R   (   R   (   R	   I       (C   R   RD   R   t   platformR   t   distutils.sysconfigR    t   distutils.errorsR   Rb   t   interpreter._importR   R   Rf   Rˆ   t   PY_COMPILED_EXTSt   setR]   R   t   exec_prefixR‘   Rs   t   addR   R   t   real_prefixt   AttributeErrort   base_exec_prefixt   python_implementationt   _rootR   t   maxsizeR   t	   IS_JYTHONt   dictt   fromkeyst   builtin_module_namesRy   t	   ExceptionR   R!   R$   R,   R2   R3   R6   R@   R<   R8   RZ   R`   Rl   RW   Rp   Rm   R   R\   R‡   RŽ   R   R”   R˜   Ru   R   R[   RJ   R§   (    (    (    s/   lib/python2.7/site-packages/astroid/modutils.pyt   <module>   s˜   	###						8	.;%	)	 !			