3
%^6,                 @   s  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZyd dlZ	dZ
W n ek
rh   dZ
Y nX yd dlmZ W n  ek
r   d dlmZ Y nX ddlmZ ejddZejejejejejejejejejejejejiZeed	rejeej< eed
rejeej< dd ZejddZG dd deZ G dd dZ!G dd de!Z"G dd de"Z#G dd de!Z$G dd de!Z%e"e$fZ&e
re&e%f7 Z&e&e#f7 Z&dd Z'e dd Z(d'dd Z)d!d" Z*d#d$ Z+d(d%d&Z,dS ))    NTF)	lru_cache   )util
ModuleTypezyC_BUILTIN C_EXTENSION PKG_DIRECTORY PY_CODERESOURCE PY_COMPILED PY_FROZEN PY_RESOURCE PY_SOURCE PY_ZIPMODULE PY_NAMESPACEPY_RESOURCEPY_CODERESOURCEc             C   s   t |  S )N)	_ImpTypes)Zimp_type r	   C/tmp/pip-build-8app2_gc/astroid/astroid/interpreter/_import/spec.py_imp_type_to_module_type4   s    r   _ModuleSpecz4name type location origin submodule_search_locationsc               @   s   e Zd ZdZdddZdS )
ModuleSpeczDefines a class similar to PEP 420's ModuleSpec

    A module spec defines a name of a module, its type, location
    and where submodules can be found, if the module is a package.
    Nc             C   s   t j| |||||dS )N)nametypelocationoriginsubmodule_search_locations)r   __new__)clsr   module_typer   r   r   r	   r	   r
   r   D   s    zModuleSpec.__new__)NNN)__name__
__module____qualname____doc__r   r	   r	   r	   r
   r   =   s     r   c               @   s0   e Zd ZdZd	ddZejdd Zdd ZdS )
Finderz@A finder is a class which knows how to find a particular module.Nc             C   s   |pt j| _d S )N)syspath_path)selfr   r	   r	   r
   __init__Y   s    zFinder.__init__c             C   s   dS )a  Find the given module

        Each finder is responsible for each protocol of finding, as long as
        they all return a ModuleSpec.

        :param str modname: The module which needs to be searched.
        :param list module_parts: It should be a list of strings,
                                  where each part contributes to the module's
                                  namespace.
        :param list processed: What parts from the module parts were processed
                               so far.
        :param list submodule_path: A list of paths where the module
                                    can be looked into.
        :returns: A ModuleSpec, describing how and where the module was found,
                  None, otherwise.
        Nr	   )r   modnamemodule_parts	processedsubmodule_pathr	   r	   r
   find_module\   s    zFinder.find_modulec             C   s   dS )z7Get a list of extra paths where this finder can search.Nr	   )r   specr"   r	   r	   r
   contribute_to_patho   s    zFinder.contribute_to_path)N)	r   r   r   r   r   abcabstractmethodr$   r&   r	   r	   r	   r
   r   V   s   
r   c               @   s    e Zd ZdZdd Zdd ZdS )	ImpFinderz!A finder based on the imp module.c             C   s^   |d k	rt |}ytj||\}}}W n tk
r:   d S X |rH|j  t||t|d dS )N   )r   r   r   )listimpr$   ImportErrorcloser   r   )r   r    r!   r"   r#   streamZmp_filenameZmp_descr	   r	   r
   r$   v   s    zImpFinder.find_modulec                s\   |j d krd S t|j r. fddtjD }n*|jdkrP|j tjkrPttj}n|j g}|S )Nc                s8   g | ]0}t jjt jj|f  rt jj|f  qS r	   )osr   isdirjoin).0p)r"   r	   r
   
<listcomp>   s   z0ImpFinder.contribute_to_path.<locals>.<listcomp>	distutils)r   _is_setuptools_namespacer   r   r   r6   __path__r+   )r   r%   r"   r   r	   )r"   r
   r&      s    


zImpFinder.contribute_to_pathN)r   r   r   r   r$   r&   r	   r	   r	   r
   r)   s   s   r)   c               @   s    e Zd ZdZdd Zdd ZdS )ExplicitNamespacePackageFinderzNA finder for the explicit namespace packages, generated through pkg_resources.c             C   sL   |rdj ||g }tj|rH|tjkrHtj| j}t|ddtj|dS d S )N. 	namespace)r   r   r   r   r   )	r2   r   is_namespacer   modulesr8   r   r   PY_NAMESPACE)r   r    r!   r"   r#   r	   r	   r
   r$      s    z*ExplicitNamespacePackageFinder.find_modulec             C   s   |j S )N)r   )r   r%   r"   r	   r	   r
   r&      s    z1ExplicitNamespacePackageFinder.contribute_to_pathN)r   r   r   r   r$   r&   r	   r	   r	   r
   r9      s   r9   c                   s(   e Zd ZdZ fddZdd Z  ZS )	ZipFinderz8Finder that knows how to find a module inside zip files.c                s   t t| j| t|| _d S )N)superr@   r   _precache_zipimporters_zipimporters)r   r   )	__class__r	   r
   r      s    zZipFinder.__init__c             C   s>   yt || j\}}}W n tk
r*   d S X t||d||dS )Negg)r   r   r   r   r   )_search_ziprC   r-   r   )r   r    r!   r"   r#   	file_typefilenamer   r	   r	   r
   r$      s    zZipFinder.find_module)r   r   r   r   r   r$   __classcell__r	   r	   )rD   r
   r@      s   r@   c               @   s    e Zd ZdZdd Zdd ZdS )PathSpecFinderz/Finder based on importlib.machinery.PathFinder.c       	      C   s`   t jjj||d}|r\|jdk}|s*|jnd }|r8tjnd }t|j||j|t	|j
pTg d}|S )N)r   r<   )r   r   r   r   r   )r<   N)	importlib	machinery
PathFinder	find_specr   r   r?   r   r   r+   r   )	r   r    r!   r"   r#   r%   Zis_namespace_pkgr   r   r	   r	   r
   r$      s    
zPathSpecFinder.find_modulec             C   s   |j tjkr|jS d S )N)r   r   r?   r   )r   r%   r"   r	   r	   r
   r&      s    z!PathSpecFinder.contribute_to_pathN)r   r   r   r   r$   r&   r	   r	   r	   r
   rJ      s   rJ   c             C   sp   y.t tjj| dd}|jd}W d Q R X W n tk
rB   Y n*X d|koRd|k}d|kobd|k}|pj|S d S )Nz__init__.pyrbi   s   pkgutils   extend_paths   pkg_resourcess   declare_namespace(__name__))openr0   r   r2   readIOError)r   r/   dataextend_pathdeclare_namespacer	   r	   r
   r7      s    r7   c             C   s   t | }|j| |S )N)setdifference_update)leftrightresultr	   r	   r
   _cached_set_diff   s    
r[   c             C   sd   t j}t| pt j}t|}t||}x8|D ]0}ytj|||< W q, tjk
rZ   w,Y q,X q,W |S )N)r   path_importer_cachetupler   r[   	zipimportzipimporterZipImportError)r   picZ	req_pathsZcached_pathsZ	new_pathsZ
entry_pathr	   r	   r
   rB     s    

rB   c             C   s   xt |j D ]\}}|d k	r|j| d }|r|jtjjj| sdtddj| dd  || f tj	tjj
|tjj tjjj|  |fS qW tddj|  d S )Nr   zNo module named %s in %s/%sr:   r   zNo module named %s)r+   itemsr$   r0   r   sepr2   r-   r   ZPY_ZIPMODULEabspath)modpathra   filepathimporterfoundr	   r	   r
   rF     s     
rF   c                sV    fddt D }x,|D ]$}|j||||}|d kr6q||fS W tddj| d S )Nc                s   g | ]}| qS r	   r	   )r3   finder)search_pathr	   r
   r5   (  s    z(_find_spec_with_path.<locals>.<listcomp>zNo module named %sr:   )_SPEC_FINDERSr$   r-   r2   )rj   r    r!   r"   r#   Zfindersri   r%   r	   )rj   r
   _find_spec_with_path'  s    

rl   c       	      C   s   |pt j}| dd } d}| dd }g }x\| r| jd}t|||||pJ|\}}|j| | rl|j||}|jtjkr,|j	|d}q,W |S )a  Find a spec for the given module.

    :type modpath: list or tuple
    :param modpath:
      split module's name (i.e name of a module or package split
      on '.'), with leading empty strings for explicit relative import

    :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)

    :rtype: ModuleSpec
    :return: A module spec, which describes how the module was
             found and where.
    Nr   )r   )
r   r   poprl   appendr&   r   r   PKG_DIRECTORY_replace)	re   r   r   r#   r!   r"   r    ri   r%   r	   r	   r
   rN   2  s    


rN   )N)N)-r'   collectionsr6   enumr,   r0   r   r^   importlib.machineryrK   Z_HAS_MACHINERYr-   	functoolsr   Zbackports.functools_lru_cacher;   r   Enumr   	C_BUILTINC_EXTENSIONro   PY_COMPILED	PY_FROZEN	PY_SOURCEr   hasattrr   r   r   
namedtupler   r   r   r)   r9   r@   rJ   rk   r7   r[   rB   rF   rl   rN   r	   r	   r	   r
   <module>	   sb   

-


