B
    `=                 @   s   d dl m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 eD Z	d dd	Z
defd
dZdd Zdd ZdefddZdd ZG dd dZe ZG dd dZG dd deZG dd deZeeG dd dZeeG dd dZdS )!    )SOURCE_SUFFIXESN)implementer)IAssetDescriptorc             C   s   g | ]}d | qS )z
__init__%s ).0xr   r   T/home/kop/projects/devel/pgwui/test_venv/lib/python3.7/site-packages/pyramid/path.py
<listcomp>	   s    r	      c             C   s2   t j| s.t|d }t|}t j|| } | S )N   )ospathisabscaller_modulepackage_pathjoin)r   levelmoduleprefixr   r   r   caller_path   s
    r   c             C   s(   | | j}|dpd}|j| }|S )N__name____main__)	_getframe	f_globalsgetmodules)r   sysmodule_globalsmodule_namer   r   r   r   r      s    
r   c             C   s`   | dks| j dkrdS | j }t| dd}|dkr4|S tj|}|d tkrP|S |ddd S )zIf this function is passed a module, return the dotted Python
    package name of the package in which the module lives.  If this
    function is passed a package, return the dotted Python package
    name of the package itself.Nr   __file__.r   r   )r   getattrr   r   split
init_namesrsplit)pkg_or_modulepkg_nameZpkg_filenameZsplittedr   r   r   package_name   s    r(   c             C   s   t | }t| tj| S )z= Return the package of a module or return the package itself )r(   
__import__r   r   )r&   r'   r   r   r   
package_of/   s    r*   c             C   sH   || d }t |dd}d|ks(d|kr,|S |jddd }tj| S )Nr   r    z__init__.pyz__init__$pyr!   r   )r"   r   r%   r   r   )r   r   r   fr(   r   r   r   caller_package6   s    r-   c             C   sF   t | dd }|d krBt| jd}y
|| _W n tk
r@   Y nX |S )N__abspath__r+   )r"   pkg_resourcesresource_filenamer   r.   	Exception)packager   r   r   r   r   B   s    
r   c               @   s   e Zd Zdd ZdS )_CALLER_PACKAGEc             C   s   dS )Nzpyramid.path.CALLER_PACKAGEr   )selfr   r   r   __repr__T   s    z_CALLER_PACKAGE.__repr__N)r   
__module____qualname__r5   r   r   r   r   r3   S   s   r3   c               @   s(   e Zd ZefddZdd Zdd ZdS )Resolverc             C   sf   |d t fkr|| _nNt|trXyt| W n" tk
rL   td|f Y nX tj| }t	|| _d S )Nz%The dotted name %r cannot be imported)
CALLER_PACKAGEr2   
isinstancestrr)   ImportError
ValueErrorr   r   r*   )r4   r2   r   r   r   __init__\   s    

zResolver.__init__c             C   s    | j tkrt j}n| j j}|S )N)r2   r9   r-   r   )r4   r(   r   r   r   get_package_namej   s    

zResolver.get_package_namec             C   s   | j tkrt }n| j }|S )N)r2   r9   r-   )r4   r2   r   r   r   get_packageq   s    
zResolver.get_packageN)r   r6   r7   r9   r>   r?   r@   r   r   r   r   r8   [   s   r8   c               @   s   e Zd ZdZdd ZdS )AssetResolvera  A class used to resolve an :term:`asset specification` to an
    :term:`asset descriptor`.

    .. versionadded:: 1.3

    The constructor accepts a single argument named ``package`` which may be
    any of:

    - A fully qualified (not relative) dotted name to a module or package

    - a Python module or package object

    - The value ``None``

    - The constant value :attr:`pyramid.path.CALLER_PACKAGE`.

    The default value is :attr:`pyramid.path.CALLER_PACKAGE`.

    The ``package`` is used when a relative asset specification is supplied
    to the :meth:`~pyramid.path.AssetResolver.resolve` method.  An asset
    specification without a colon in it is treated as relative.

    If ``package`` is ``None``, the resolver will
    only be able to resolve fully qualified (not relative) asset
    specifications.  Any attempt to resolve a relative asset specification
    will result in an :exc:`ValueError` exception.

    If ``package`` is :attr:`pyramid.path.CALLER_PACKAGE`,
    the resolver will treat relative asset specifications as
    relative to the caller of the :meth:`~pyramid.path.AssetResolver.resolve`
    method.

    If ``package`` is a *module* or *module name* (as opposed to a package or
    package name), its containing package is computed and this
    package is used to derive the package name (all names are resolved relative
    to packages, never to modules).  For example, if the ``package`` argument
    to this type was passed the string ``xml.dom.expatbuilder``, and
    ``template.pt`` is supplied to the
    :meth:`~pyramid.path.AssetResolver.resolve` method, the resulting absolute
    asset spec would be ``xml.minidom:template.pt``, because
    ``xml.dom.expatbuilder`` is a module object, not a package object.

    If ``package`` is a *package* or *package name* (as opposed to a module or
    module name), this package will be used to compute relative
    asset specifications.  For example, if the ``package`` argument to this
    type was passed the string ``xml.dom``, and ``template.pt`` is supplied
    to the :meth:`~pyramid.path.AssetResolver.resolve` method, the resulting
    absolute asset spec would be ``xml.minidom:template.pt``.
    c             C   st   t j|rt|S |}d|kr2|dd\}}n8| jtkrFt j}nt	| jdd}|dkrjt
d|f t||S )a  
        Resolve the asset spec named as ``spec`` to an object that has the
        attributes and methods described in
        :class:`pyramid.interfaces.IAssetDescriptor`.

        If ``spec`` is an absolute filename
        (e.g. ``/path/to/myproject/templates/foo.pt``) or an absolute asset
        spec (e.g. ``myproject:templates.foo.pt``), an asset descriptor is
        returned without taking into account the ``package`` passed to this
        class' constructor.

        If ``spec`` is a *relative* asset specification (an asset
        specification without a ``:`` in it, e.g. ``templates/foo.pt``), the
        ``package`` argument of the constructor is used as the package
        portion of the asset spec.  For example:

        .. code-block:: python

           a = AssetResolver('myproject')
           resolver = a.resolve('templates/foo.pt')
           print(resolver.abspath())
           # -> /path/to/myproject/templates/foo.pt

        If the AssetResolver is constructed without a ``package`` argument of
        ``None``, and a relative asset specification is passed to
        ``resolve``, an :exc:`ValueError` exception is raised.
        :r   r   Nz.relative spec %r irresolveable without package)r   r   r   FSAssetDescriptorr#   r2   r9   r-   r   r"   r=   PkgResourcesAssetDescriptor)r4   specr   r(   r   r   r   resolve   s    

zAssetResolver.resolveN)r   r6   r7   __doc__rF   r   r   r   r   rA   y   s   1rA   c               @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )DottedNameResolveraa  A class used to resolve a :term:`dotted Python name` to a package or
    module object.

    .. versionadded:: 1.3

    The constructor accepts a single argument named ``package`` which may be
    any of:

    - A fully qualified (not relative) dotted name to a module or package

    - a Python module or package object

    - The value ``None``

    - The constant value :attr:`pyramid.path.CALLER_PACKAGE`.

    The default value is :attr:`pyramid.path.CALLER_PACKAGE`.

    The ``package`` is used when a relative dotted name is supplied to the
    :meth:`~pyramid.path.DottedNameResolver.resolve` method.  A dotted name
    which has a ``.`` (dot) or ``:`` (colon) as its first character is
    treated as relative.

    If ``package`` is ``None``, the resolver will only be able to resolve
    fully qualified (not relative) names.  Any attempt to resolve a
    relative name will result in an :exc:`ValueError` exception.

    If ``package`` is :attr:`pyramid.path.CALLER_PACKAGE`,
    the resolver will treat relative dotted names as relative to
    the caller of the :meth:`~pyramid.path.DottedNameResolver.resolve`
    method.

    If ``package`` is a *module* or *module name* (as opposed to a package or
    package name), its containing package is computed and this
    package used to derive the package name (all names are resolved relative
    to packages, never to modules).  For example, if the ``package`` argument
    to this type was passed the string ``xml.dom.expatbuilder``, and
    ``.mindom`` is supplied to the
    :meth:`~pyramid.path.DottedNameResolver.resolve` method, the resulting
    import would be for ``xml.minidom``, because ``xml.dom.expatbuilder`` is
    a module object, not a package object.

    If ``package`` is a *package* or *package name* (as opposed to a module or
    module name), this package will be used to relative compute
    dotted names.  For example, if the ``package`` argument to this type was
    passed the string ``xml.dom``, and ``.minidom`` is supplied to the
    :meth:`~pyramid.path.DottedNameResolver.resolve` method, the resulting
    import would be for ``xml.minidom``.
    c             C   s8   t |tstd|f | j}|tkr,t }| ||S )a  
        This method resolves a dotted name reference to a global Python
        object (an object which can be imported) to the object itself.

        Two dotted name styles are supported:

        - ``pkg_resources``-style dotted names where non-module attributes
          of a package are separated from the rest of the path using a ``:``
          e.g. ``package.module:attr``.

        - ``zope.dottedname``-style dotted names where non-module
          attributes of a package are separated from the rest of the path
          using a ``.`` e.g. ``package.module.attr``.

        These styles can be used interchangeably.  If the supplied name
        contains a ``:`` (colon), the ``pkg_resources`` resolution
        mechanism will be chosen, otherwise the ``zope.dottedname``
        resolution mechanism will be chosen.

        If the ``dotted`` argument passed to this method is not a string, a
        :exc:`ValueError` will be raised.

        When a dotted name cannot be resolved, a :exc:`ValueError` error is
        raised.

        Example:

        .. code-block:: python

           r = DottedNameResolver()
           v = r.resolve('xml') # v is the xml module

        z%r is not a string)r:   r;   r=   r2   r9   r-   _resolve)r4   dottedr2   r   r   r   rF     s    "
zDottedNameResolver.resolvec             C   s.   t |tr*| j}|tkrt }| ||S |S )a  
        This method behaves just like
        :meth:`~pyramid.path.DottedNameResolver.resolve`, except if the
        ``dotted`` value passed is not a string, it is simply returned.  For
        example:

        .. code-block:: python

           import xml
           r = DottedNameResolver()
           v = r.maybe_resolve(xml)
           # v is the xml module; no exception raised
        )r:   r;   r2   r9   r-   rI   )r4   rJ   r2   r   r   r   maybe_resolve5  s    
z DottedNameResolver.maybe_resolvec             C   s$   d|kr|  ||S | ||S d S )NrB   )_pkg_resources_style_zope_dottedname_style)r4   rJ   r2   r   r   r   rI   J  s    zDottedNameResolver._resolvec             C   sf   | dr6|std|f |dkr,|j}n
|j| }tjd| }t|drX| S |dS dS )z package.module:attr style )r!   rB   z.relative name %r irresolveable without packagezx=%srF   FN)	
startswithr=   r   r/   
EntryPointparsehasattrrF   load)r4   valuer2   epr   r   r   rL   P  s    


z'DottedNameResolver._pkg_resources_stylec          	   C   s  t |dd}|sd}|dkr>|dkr2td|f |d}nd|d}|d s|dkrftd|f |d}|d x|d s|  |d q|W || }|d}t|}xN|D ]F}|d| 7 }yt ||}W q tk
r   t| t ||}Y qX qW |S )z package.module.attr style r   Nr!   z.relative name %r irresolveable without packager   )r"   r=   r#   popr)   AttributeError)r4   rS   r2   r   nameusedfoundnr   r   r   rM   d  s<    






z)DottedNameResolver._zope_dottedname_styleN)	r   r6   r7   rG   rF   rK   rI   rL   rM   r   r   r   r   rH      s   1)rH   c               @   sH   e Zd ZeZdd Zdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dS )rD   c             C   s   || _ || _d S )N)r'   r   )r4   r'   r   r   r   r   r>     s    z$PkgResourcesAssetDescriptor.__init__c             C   s   d| j | jf S )Nz%s:%s)r'   r   )r4   r   r   r   absspec  s    z#PkgResourcesAssetDescriptor.absspecc             C   s   t j| j| j| jS )N)r   r   abspathr/   r0   r'   )r4   r   r   r   r\     s    z#PkgResourcesAssetDescriptor.abspathc             C   s   | j | j| jS )N)r/   resource_streamr'   r   )r4   r   r   r   stream  s    z"PkgResourcesAssetDescriptor.streamc             C   s   | j | j| jS )N)r/   resource_isdirr'   r   )r4   r   r   r   isdir  s    z!PkgResourcesAssetDescriptor.isdirc             C   s   | j | j| jS )N)r/   resource_listdirr'   r   )r4   r   r   r   listdir  s    z#PkgResourcesAssetDescriptor.listdirc             C   s   | j | j| jS )N)r/   resource_existsr'   r   )r4   r   r   r   exists  s    z"PkgResourcesAssetDescriptor.existsN)r   r6   r7   r/   r>   r[   r\   r^   r`   rb   rd   r   r   r   r   rD     s   rD   c               @   sD   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dS )rC   c             C   s   t j|| _d S )N)r   r   r\   )r4   r   r   r   r   r>     s    zFSAssetDescriptor.__init__c             C   s   t d S )N)NotImplementedError)r4   r   r   r   r[     s    zFSAssetDescriptor.absspecc             C   s   | j S )N)r   )r4   r   r   r   r\     s    zFSAssetDescriptor.abspathc             C   s   t | jdS )Nrb)openr   )r4   r   r   r   r^     s    zFSAssetDescriptor.streamc             C   s   t j| jS )N)r   r   r`   )r4   r   r   r   r`     s    zFSAssetDescriptor.isdirc             C   s   t | jS )N)r   rb   r   )r4   r   r   r   rb     s    zFSAssetDescriptor.listdirc             C   s   t j| jS )N)r   r   rd   )r4   r   r   r   rd     s    zFSAssetDescriptor.existsN)
r   r6   r7   r>   r[   r\   r^   r`   rb   rd   r   r   r   r   rC     s   rC   )r
   )importlib.machineryr   r   r/   r   Zzope.interfacer   Zpyramid.interfacesr   r$   r   r   r(   r*   r-   r   r3   r9   r8   rA   rH   rD   rC   r   r   r   r   <module>   s(   
` 3