Your IP : 216.73.216.213


Current Path : /opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/
Upload File :
Current File : //opt/alt/python37/lib/python3.7/site-packages/logilab/common/__pycache__/modutils.cpython-37.pyc

B

�d{T%\�@s.dZdZddlZddlZddlmZmZmZmZm	Z	m
Z
mZddlm
Z
mZmZmZmZddlmZmZmZddlmZddlmZyddlZWnek
r�dZYnXe�Zdd	lmZm Z ej!�"d
�r�dZ#dZ$nd
Z#dZ$yedd�Z%Wnek
�rdZ%YnXe�Z&e'e(ej)dge*ej)���Z+Gdd�de,�Z-Gdd�de�Z.d=dd�Z/d>dd�Z0d?dd�Z1dd�Z2d@dd�Z3dAd d!�Z4dBd"d#�Z5efd$d%�Z6efd&d'�Z7dCd)d*�Z8d+d,�Z9d-d.�Z:e%ffd/d0�Z;d1d2�Z<dDd3d4�Z=d5d6�Z>yddl?Z?Wnek
�rdZ?YnXdEd7d8�Z@d9d:�ZAd;d<�ZBdS)FaKPython modules manipulation utility functions.

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

:type STD_LIB_DIR: str
:var STD_LIB_DIR: directory where standard modules are located

:type BUILTIN_MODULES: dict
:var BUILTIN_MODULES: dictionary with builtin module names has key
zrestructuredtext en�N)�splitext�join�abspath�isdir�dirname�exists�basename)�find_module�load_module�	C_BUILTIN�PY_COMPILED�
PKG_DIRECTORY)�get_config_var�get_python_lib�get_python_version)�DistutilsPlatformError)�range)�
STD_BLACKLIST�_handle_blacklist�win)�pyZpyw)�dllZpyd)r)�so�)�standard_libz//c@seZdZdZdS)�NoSourceFilezaexception raised when we are not able to get a python
    source file for a precompiled file
    N)�__name__�
__module__�__qualname__�__doc__�r r �H/opt/alt/python37/lib/python3.7/site-packages/logilab/common/modutils.pyrMsrcs4eZdZdd�Zdd�Z�fdd�Zdd�Z�ZS)	�
LazyObjectcCs||_||_d|_dS)N)�module�obj�	_imported)�selfr#r$r r r!�__init__SszLazyObject.__init__cCs$|jdkrtt|j�|j�|_|jS)N)r%�getattr�load_module_from_namer#r$)r&r r r!�_getobjXs


zLazyObject._getobjc
sBytt|��|�Stk
r<}zt|��|�Sd}~XYnXdS)N)�superr"�__getattribute__�AttributeErrorr(r*)r&�attr�ex)�	__class__r r!r,^szLazyObject.__getattribute__cOs|��||�S)N)r*)r&�args�kwargsr r r!�__call__dszLazyObject.__call__)rrrr'r*r,r3�
__classcell__r r )r0r!r"Rsr"cCst|�d�||�S)a"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
    �.)�load_module_from_modpath�split)�dotted_name�path�use_sysr r r!r)hsr)cCs|r*ytjd�|�Stk
r(YnXg}d}x�|D]�}|�|�d�|�}d}t|�t|�krrtj�|�}n|r�tj�|�}|dkr�t||�\}}	}
t|||	|
�}|r�t	|||�t
|dd�}|s�t|�t|�kr�tdd�|t|�d����t|�g}|}q8W|S)aBLoad a python module from its splitted name.

    :type parts: list(str) or tuple(str)
    :param parts:
      python name of a module or package splitted 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
    r5N�__file__�zno module in %s)
�sys�modulesr�KeyError�append�len�getr	r
�setattrr(�ImportErrorr)�partsr9r:�modpathZ
prevmodule�partZcurnamer#Zmp_file�mp_filename�mp_desc�_filer r r!r6�s4



r6cCst||�}t|||�S)aLoad 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
    )�modpath_from_filer6)�filepathr9r:�	extrapathrFr r r!�load_module_from_file�s
rNcCs(x"|D]}t||�}t|�sdSqWdS)z2check there are some __init__.py all along the wayFT)r�	_has_init)r9Zmod_pathrGr r r!�_check_init�s


rPcCstt|��d}|dk	r�xr|D]j}t|�}|r|dt|��|krdd�|t|�d��tj�D�}t||dd��r||�d�|SqWx\tjD]R}t|�}|r�|�	|�r�dd�|t|�d��tj�D�}t||dd��r�|Sq�Wt
d|d	�tj�f��dS)
augiven a file path return the corresponding splitted module's name
    (i.e name of a module or package splitted 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 splitted in multiple
      directories using __path__ trick.


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

    :rtype: list(str)
    :return: the corresponding splitted module's name
    rNcSsg|]}|r|�qSr r )�.0�pkgr r r!�
<listcomp>�sz%modpath_from_file.<locals>.<listcomp>���r5cSsg|]}|r|�qSr r )rQrRr r r!rS�sz"Unable to find module for %s in %sz, 
)rrrAr7�os�seprPr=r9�
startswithrDr)�filenamerM�baseZpath_r9Z
submodpathrFr r r!rK�s 
""rKcCs||dk	rt|�}n|}|ddkr\ytdg|dd�||�Stk
rXt|||�SXn|ddgkrptjjSt|||�S)a�given a mod path (i.e. splitted 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:
      splitted module's name (i.e name of a module or package splitted
      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
    :return:
      the path to the module's file or None if it's an integrated
      builtin module such as 'sys'
    Nr�xmlZ_xmlplusrrUr9)r�_file_from_modpathrDrUr9r;)rFr9�context_file�contextr r r!�file_from_modpath�s
r^c	Cs|�d�rdS|�d�}|dk	rH|dtkrHt|�dkr@t|��|dSd}d}|ddkrt|dk	sltd��g}d}x ||dkr�|d7}t|�}qvWxrt|t|��D]`}yt|||d�||d	�Wq�tk
�r|t	dt|�d�ks�d�
|d|��SXq�W|S)
agiven a dotted name return the module part of the name :

    >>> get_module_part('logilab.common.modutils.get_module_part')
    'logilab.common.modutils'

    :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)
    zos.pathr5Nr�r<z.explicit relative import, but no context_file?r)r9r\)rWr7�BUILTIN_MODULESrArD�AssertionErrorrrr^�maxr)r8r\rEr9Zstarti�ir r r!�get_module_part.s6


rdcCs�g}x�t�|�D]�\}}}t|||�d|kr<d|dd�<q||krl|t|�d��tjd�}|�||�xN|D]F}t|�rr|dkrrt||�}	||	t|�d�}
|�|
�tjd��qrWqW|S)aSgiven a package directory return a list of all available python
    modules in the package and its subpackages

    :type package: str
    :param package: the python name for the package

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

    :type blacklist: list or tuple
    :param blacklist:
      optional list of files or directory to ignore, default to
      the value of `logilab.common.STD_BLACKLIST`

    :rtype: list
    :return:
      the list of all available python modules in the package and its
      subpackages
    z__init__.pyr Nr5���)	rU�walkrrA�replacerVr@�_is_python_filer)�package�
src_directory�	blacklistr>�	directory�dirnames�	filenamesZdir_packagerX�srcr#r r r!�get_modulesis

rpcCsng}xdt�|�D]V\}}}t|||�d|kr<d|dd�<qx(|D] }t|�rBt||�}|�|�qBWqW|S)agiven 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:
      optional list of files or directory to ignore, default to the value of
      `logilab.common.STD_BLACKLIST`

    :rtype: list
    :return:
      the list of all available python module's files in the package and
      its subpackages
    z__init__.pyr N)rUrfrrhrr@)rjrk�filesrlrmrnrXror r r!�get_module_files�s

rrFcCsVtt|��\}}x$tD]}d||f}t|�r|SqW|rJ|sJt|�rJ|St|��dS)a�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
    z%s.%sN)rr�PY_SOURCE_EXTSrr)rXZinclude_no_extrYZorig_ext�ext�source_pathr r r!�get_source_file�s
rvcCs`g}xVttj���D]D\}}t|dd�}|rx*|D]"}|�|�r2|�|�tj|=Pq2WqW|S)z5remove submodules of `directories` from `sys.modules`r;N)�listr=r>�itemsr(rWr@)�directories�cleaned�modnamer#Zmodfilerlr r r!�cleanup_sys_modules�s



r|cCst|�ddd�tkS)zN
    rtype: bool
    return: True if the filename is a python source file
    rN)rrs)rXr r r!�is_python_source�sr}c
Cs�|�d�d}yt|g�}Wn"tk
r>}zdSd}~XYnX|dkrLdSt|�}|�t�rbdSx|D]}|�t|��rhdSqhWdS)a�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
    r5rNrF)r7r^rDrrW�EXT_LIB_DIR)r{Zstd_pathrXr/r9r r r!�is_standard_module�s

rcCsRt|�st|�}|tjkrdSyt|�d�d|g�dStk
rLdSXdS)avreturn 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`
    Fr5rTN)rrr=r9r	r7rD)r{�	from_filer r r!�is_relatives
r�cCs�t|�dkst�|dk	rRyt||g�\}}Wq`tk
rNt||�\}}Yq`Xnt||�\}}|tkr�yt|�Stk
r�|SXn|tkr�dS|tkr�t	|�}|S)z�given a mod path (i.e. splitted module / package name), return the
    corresponding file

    this function is used internally, see `file_from_modpath`'s
    documentation for more information
    rN)
rAra�_module_filerDrrvrrr
rO)rFr9r]�mtyperHr r r!r["s"r[cCs�xv|��D]j\}}|dk	r
|�|d�r
|�d�|��sXtdd�|dd��||f��tt|�dd�|�|fSq
Wtdd�|���dS)Nr�/zNo module named %s in %s/%sr5rzNo module named %s)rxr	rrD�ZIPFILEr)rF�picrL�importerr r r!�_search_zip=s r�c	spyhtj}|dkrtjp|}xF|D]>}||kr yt�|�||<Wq tjk
r\d||<Yq Xq Wd}Wntk
r�d}YnXtdk	r�|dtjkr�|dtj	kr�t
|�dkr�tj	|�d�}|j}g��x�|�rf|d}yt
||�\}}	}
Wn0tk
�r"|�rt||�dd�S�Yn�X|�r�|	�r�dd�|D�}y>|�tt|	���}t||�\}
}}||�|�k�rz|
|fSWn*tk
�r�Yntk
�r�YnXd}��|�d��|
d}|r�|tk�r�td	d
�|�d
���f��y(tt|	d���}|�d�}WdQRXWntk
�r2|	g}Yq�Xd
|k�r^d|k�r^�fdd�tjD�}q�|	g}q�W||	fS)a�get a module type / file path

    :type modpath: list or tuple
    :param modpath:
      splitted module's name (i.e name of a module or package splitted
      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: tuple(int, str)
    :return: the module type flag and the file path for a module
    NTFrrr_cSsg|]}t|��qSr )r)rQ�xr r r!rS�sz _module_file.<locals>.<listcomp>zNo module %s in %sr5z__init__.pyi�pkgutil�extend_pathcs,g|]$}tt|f����rt|f����qSr )rr)rQ�p)�importedr r!rS�s)r=�path_importer_cacher9�	zipimport�zipimporter�ZipImportErrorr-�
pkg_resources�_namespace_packagesr>rA�pop�__path__r	rDr��indexrr�
ValueErrorr@r
r�open�read�IOError)rFr9r��_pathZ__pathZ	checkeggsr#r{�_rHrIZfullabspathZ	pathindexZemtypeZemp_filename�zippathr��stream�datar )r�r!r�Lsl





r�cCs xdD]}|�|�rdSqWdS)zkreturn true if the given filename should be considered as a python file

    .pyc and .pyo are ignored
    )z.pyz.soz.pydz.pywTF)�endswith)rXrtr r r!rh�s

rhcCs<t|d�}x,tdD] }t|d|�r|d|SqWdS)z\if the given directory has a valid __init__ file, return its path,
    else return None
    r')ZpycZpyor5N)rrsr)rlZmod_or_packrtr r r!rO�s

rO)Nr)Nr)NrN)N)NN)N)F)NN)N)Cr�
__docformat__r=rU�os.pathrrrrrrr�impr	r
rrr
�distutils.sysconfigrrr�distutils.errorsrZ	six.movesrr�rD�objectr�Zlogilab.commonrr�platformrWrsZPY_COMPILED_EXTSZSTD_LIB_DIRr~�dict�zip�builtin_module_namesrAr`�	Exceptionrr"r)r6rNrPrKr^rdrprrrvr|r}rr�r[r�r�r�rhrOr r r r!�<module>sf$



3
	
(
/
;( 
	&



_