a
    0hƐ                     @  s  d Z ddlm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	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mZm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
dlmZ d
dlmZm Z  d
dl!m"Z"m#Z# d
dl$m%Z%m&Z& d
dl'm(Z(m)Z)m*Z* d
dlm+Z+m,Z, d
dl-m.Z. d
dl/m0Z0m1Z1 g dZ2G dd de3Z4G dd dZ5G dd dej6Z7G dd dZ8G dd de9Z:G dd de
j;Z<G d d! d!Z=G d"d# d#ej>d$Z?G d%d& d&eZ@G d'd( d(ZAG d)d* d*ZBG d+d, d,ZCe#G d-d. d.e"e@ZDG d/d0 d0e?ZEd1d#d2d3d4ZFd5d6d7d8ZGd1d9d2d:d;ZHd1d1d2d<d=ZIejJe*e0jKd>ZLdd6d?d@ZMd1dAd2dBdCZNd1dDd2dEdFZOdGd6dHdIZPdJdK ZQddLdMdNdOZRdd1dMdPdQZSdRdS ZTdS )Tz
APIs exposing metadata from third-party Python packages.

This codebase is shared between importlib.metadata in the stdlib
and importlib_metadata in PyPI. See
https://github.com/python/importlib_metadata/wiki/Development-Methodology
for more detail.
    )annotationsN)IterableMapping)suppress)import_module)MetaPathFinder)starmap)Any   )_meta)FreezableDefaultDictPair)
NullFinderinstall)method_cache	pass_none)always_iterablebucketunique_everseen)PackageMetadata
SimplePath)md_none)py39py311)DistributionDistributionFinderr   PackageNotFoundErrorr   distributiondistributionsentry_pointsfilesmetadatapackages_distributionsrequiresversionc                   @  s0   e Zd ZdZddddZeddddZdS )	r   zThe package was not found.strreturnc                 C  s   d| j  S )Nz"No package metadata was found for nameself r,   I/var/www/auris/lib/python3.9/site-packages/importlib_metadata/__init__.py__str__@   s    zPackageNotFoundError.__str__c                 C  s   | j \}|S N)argsr+   r)   r,   r,   r-   r)   C   s    zPackageNotFoundError.nameN)__name__
__module____qualname____doc__r.   propertyr)   r,   r,   r,   r-   r   =   s   r   c                   @  sJ   e Zd ZdZed Zedd Z	e
dddZe
dd	d
dZdS )	Sectioneda  
    A simple entry point config parser for performance

    >>> for item in Sectioned.read(Sectioned._sample):
    ...     print(item)
    Pair(name='sec1', value='# comments ignored')
    Pair(name='sec1', value='a = 1')
    Pair(name='sec1', value='b = 2')
    Pair(name='sec2', value='a = 2')

    >>> res = Sectioned.section_pairs(Sectioned._sample)
    >>> item = next(res)
    >>> item.name
    'sec1'
    >>> item.value
    Pair(name='a', value='1')
    >>> item = next(res)
    >>> item.value
    Pair(name='b', value='2')
    >>> item = next(res)
    >>> item.name
    'sec2'
    >>> item.value
    Pair(name='a', value='2')
    >>> list(res)
    []
    zm
        [sec1]
        # comments ignored
        a = 1
        b = 2

        [sec2]
        a = 2
        c                 C  s   dd | j || jdD S )Nc                 s  s,   | ]$}|j d ur|jt|jdV  qd S )N)value)r)   _replacer   parser8   ).0sectionr,   r,   r-   	<genexpr>t   s   
z*Sectioned.section_pairs.<locals>.<genexpr>)filter_)readvalid)clstextr,   r,   r-   section_pairsr   s    zSectioned.section_pairsNc                 c  sX   t |ttj|  }d }|D ]4}|do4|d}|rF|d}qt||V  qd S )N[]z[])filtermapr%   strip
splitlines
startswithendswithr   )rB   r>   linesr)   r8   Zsection_matchr,   r,   r-   r?   z   s    
zSectioned.readr%   linec                 C  s   | o|  d S )N#)rJ   rM   r,   r,   r-   r@      s    zSectioned.valid)N)r2   r3   r4   r5   textwrapdedentlstripZ_sampleclassmethodrC   staticmethodr?   r@   r,   r,   r,   r-   r7   I   s   

r7   c                   @  s&   e Zd ZU ded< ded< ded< dS )_EntryPointMatchr%   moduleattrextrasN)r2   r3   r4   __annotations__r,   r,   r,   r-   rU      s   
rU   c                   @  s   e Zd ZU dZedZded< ded< ded< dZded	< dddd
dddZ	ddddZ
eddddZeddddZeddddZejddddZdd Zdd Zed d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,dd-d.ZdS )/
EntryPointa  An entry point as defined by Python packaging conventions.

    See `the packaging docs on entry points
    <https://packaging.python.org/specifications/entry-points/>`_
    for more information.

    >>> ep = EntryPoint(
    ...     name=None, group=None, value='package.module:attr [extra1, extra2]')
    >>> ep.module
    'package.module'
    >>> ep.attr
    'attr'
    >>> ep.extras
    ['extra1', 'extra2']

    If the value package or module are not valid identifiers, a
    ValueError is raised on access.

    >>> EntryPoint(name=None, group=None, value='invalid-name').module
    Traceback (most recent call last):
    ...
    ValueError: ('Invalid object reference...invalid-name...
    >>> EntryPoint(name=None, group=None, value='invalid-name').attr
    Traceback (most recent call last):
    ...
    ValueError: ('Invalid object reference...invalid-name...
    >>> EntryPoint(name=None, group=None, value='invalid-name').extras
    Traceback (most recent call last):
    ...
    ValueError: ('Invalid object reference...invalid-name...

    The same thing happens on construction.

    >>> EntryPoint(name=None, group=None, value='invalid-name')
    Traceback (most recent call last):
    ...
    ValueError: ('Invalid object reference...invalid-name...

    zH(?P<module>[\w.]+)\s*(:\s*(?P<attr>[\w.]+)\s*)?((?P<extras>\[.*\])\s*)?$r%   r)   r8   groupNzDistribution | NonedistNone)r)   r8   r[   r'   c                 C  s   t | j|||d | j d S )Nr)   r8   r[   )varsupdaterV   )r+   r)   r8   r[   r,   r,   r-   __init__   s    zEntryPoint.__init__r	   r&   c                 C  s.   t | j}td| jpdd}tt||S )zLoad the entry point from its definition. If only a module
        is indicated by the value, return that module. Otherwise,
        return the named object.
        N .)r   rV   rF   rW   split	functoolsreducegetattr)r+   rV   attrsr,   r,   r-   load   s    
zEntryPoint.loadc                 C  s   | j jS r/   )_matchrV   r*   r,   r,   r-   rV      s    zEntryPoint.modulec                 C  s   | j jS r/   )rj   rW   r*   r,   r,   r-   rW      s    zEntryPoint.attr	list[str]c                 C  s   t d| jjpdS )Nz\w+rb   )refindallrj   rX   r*   r,   r,   r-   rX      s    zEntryPoint.extrasrU   c                 C  s0   | j | j}|std| jtf i | S )NzlInvalid object reference. See https://packaging.python.org/en/latest/specifications/entry-points/#data-model)patternmatchr8   
ValueErrorrU   	groupdict)r+   ro   r,   r,   r-   rj      s    zEntryPoint._matchc                 C  s   t | j|d | S )Nr\   r_   r`   )r+   r\   r,   r,   r-   _for   s    zEntryPoint._forc                   s2     |  fdd|D }tttj| |S )a$  
        EntryPoint matches the given parameters.

        >>> ep = EntryPoint(group='foo', name='bar', value='bing:bong [extra1, extra2]')
        >>> ep.matches(group='foo')
        True
        >>> ep.matches(name='bar', value='bing:bong [extra1, extra2]')
        True
        >>> ep.matches(group='foo', name='other')
        False
        >>> ep.matches()
        True
        >>> ep.matches(extras=['extra1', 'extra2'])
        True
        >>> ep.matches(module='bing')
        True
        >>> ep.matches(attr='bong')
        True
        c                 3  s   | ]}t  |V  qd S r/   rg   )r;   paramr*   r,   r-   r=         z%EntryPoint.matches.<locals>.<genexpr>)_disallow_distallrG   operatoreqvalues)r+   paramsrh   r,   r*   r-   matches   s    
zEntryPoint.matchesc                 C  s   d| v rt ddS )a  
        Querying by dist is not allowed (dist objects are not comparable).
        >>> EntryPoint(name='fan', value='fav', group='fag').matches(dist='foo')
        Traceback (most recent call last):
        ...
        ValueError: "dist" is not suitable for matching...
        r\   zo"dist" is not suitable for matching. Instead, use Distribution.entry_points.select() on a located distribution.N)rp   r}   r,   r,   r-   rx     s    	zEntryPoint._disallow_distc                 C  s   | j | j| jfS r/   r^   r*   r,   r,   r-   _key%  s    zEntryPoint._keyc                 C  s   |   |  k S r/   r   r+   otherr,   r,   r-   __lt__(  s    zEntryPoint.__lt__c                 C  s   |   |  kS r/   r   r   r,   r,   r-   __eq__+  s    zEntryPoint.__eq__c                 C  s   t dd S )Nz!EntryPoint objects are immutable.)AttributeError)r+   r)   r8   r,   r,   r-   __setattr__.  s    zEntryPoint.__setattr__c                 C  s   d| j d| jd| jdS )NzEntryPoint(name=z, value=z, group=)r^   r*   r,   r,   r-   __repr__1  s    zEntryPoint.__repr__intc                 C  s   t |  S r/   )hashr   r*   r,   r,   r-   __hash__7  s    zEntryPoint.__hash__)r2   r3   r4   r5   rl   compilern   rY   r\   ra   ri   r6   rV   rW   rX   re   cached_propertyrj   rt   r~   rT   rx   r   r   r   r   r   r   r,   r,   r,   r-   rZ      s8   
(	
rZ   c                   @  sv   e Zd ZdZdZdddddZdd	 Zd d
ddZedd
ddZ	edd
ddZ
edd Zedd ZdS )EntryPointszC
    An immutable collection of selectable EntryPoint objects.
    r,   r%   rZ   r)   r'   c                 C  s6   zt t| j|dW S  ty0   t|Y n0 dS )z;
        Get the EntryPoint in self matching name.
        r(   N)nextiterselectStopIterationKeyErrorr1   r,   r,   r-   __getitem__B  s    zEntryPoints.__getitem__c                 C  s   d| j jt| f S )zz
        Repr with classname and tuple constructor to
        signal that we deviate from regular tuple behavior.
        z%s(%r))	__class__r2   tupler*   r,   r,   r-   r   K  s    zEntryPoints.__repr__r&   c                   s   t  fdd| D S )zv
        Select entry points from self that match the
        given parameters (typically group and/or name).
        c                 3  s$   | ]}t j|fi  r|V  qd S r/   )r   Z
ep_matchesr;   epr   r,   r-   r=   W  rw   z%EntryPoints.select.<locals>.<genexpr>)r   )r+   r}   r,   r   r-   r   R  s    zEntryPoints.selectzset[str]c                 C  s   dd | D S )zB
        Return the set of all names of all entry points.
        c                 S  s   h | ]
}|j qS r,   r(   r   r,   r,   r-   	<setcomp>^  rw   z$EntryPoints.names.<locals>.<setcomp>r,   r*   r,   r,   r-   namesY  s    zEntryPoints.namesc                 C  s   dd | D S )zC
        Return the set of all groups of all entry points.
        c                 S  s   h | ]
}|j qS r,   )r[   r   r,   r,   r-   r   e  rw   z%EntryPoints.groups.<locals>.<setcomp>r,   r*   r,   r,   r-   groups`  s    zEntryPoints.groupsc                   s   |  fdd|  |D S )Nc                 3  s   | ]}|  V  qd S r/   )rt   r   rr   r,   r-   r=   i  rw   z-EntryPoints._from_text_for.<locals>.<genexpr>)
_from_text)rA   rB   r\   r,   rr   r-   _from_text_forg  s    zEntryPoints._from_text_forc                 C  s   dd t | pdD S )Nc                 s  s&   | ]}t |jj|jj|jd V  qdS )r^   N)rZ   r8   r)   )r;   itemr,   r,   r-   r=   m  s   z)EntryPoints._from_text.<locals>.<genexpr>rb   )r7   rC   )rB   r,   r,   r-   r   k  s    zEntryPoints._from_textN)r2   r3   r4   r5   	__slots__r   r   r   r6   r   r   rS   r   rT   r   r,   r,   r,   r-   r   ;  s   	
r   c                   @  sX   e Zd ZU dZded< ded< ded< dd	d	d
ddZddddZddddZdS )PackagePathz"A reference to a path in a packagezFileHash | Noner   r   sizer   r\   utf-8r%   )encodingr'   c                 C  s   |   j|dS )Nr   )locate	read_text)r+   r   r,   r,   r-   r   z  s    zPackagePath.read_textbytesr&   c                 C  s   |    S r/   )r   
read_bytesr*   r,   r,   r-   read_binary}  s    zPackagePath.read_binaryr   c                 C  s   | j | S )z'Return a path-like object for this path)r\   locate_filer*   r,   r,   r-   r     s    zPackagePath.locateN)r   )r2   r3   r4   r5   rY   r   r   r   r,   r,   r,   r-   r   s  s   
r   c                   @  s*   e Zd ZdddddZddddZd	S )
FileHashr%   r]   )specr'   c                 C  s   | d\| _}| _d S )N=)	partitionmoder8   )r+   r   _r,   r,   r-   ra     s    zFileHash.__init__r&   c                 C  s   d| j  d| j dS )Nz<FileHash mode: z value: >)r   r8   r*   r,   r,   r-   r     s    zFileHash.__repr__N)r2   r3   r4   ra   r   r,   r,   r,   r-   r     s   r   c                   @  s  e Zd ZdZejddddZejdddd	d
Zedd dddZ	edddddddZ
edddddZedd dddZedd ZeddddZeedd d!d"d#Zeddd$d%Zed&d' Zeddd(d)Zed*dd+d,Zed-dd.d/Zd0d1 Zd2d3 Zd4d5 Zed6dd7d8Zd9d: Zd;d< Zed=d> Zed?d@ ZedAdB Z dCdD Z!dS )Er   aF  
    An abstract Python distribution package.

    Custom providers may derive from this class and define
    the abstract methods to provide a concrete implementation
    for their environment. Some providers may opt to override
    the default implementation of some properties to bypass
    the file-reading mechanism.
    
str | Noner&   c                 C  s   dS )a  Attempt to load metadata file given by the name.

        Python distribution metadata is organized by blobs of text
        typically represented as "files" in the metadata directory
        (e.g. package-1.0.dist-info). These files include things
        like:

        - METADATA: The distribution metadata including fields
          like Name and Version and Description.
        - entry_points.txt: A series of entry points as defined in
          `the entry points spec <https://packaging.python.org/en/latest/specifications/entry-points/#file-format>`_.
        - RECORD: A record of files according to
          `this recording spec <https://packaging.python.org/en/latest/specifications/recording-installed-packages/#the-record-file>`_.

        A package may provide any set of files, including those
        not listed here or none at all.

        :param filename: The name of the file in the distribution info.
        :return: The text if found, otherwise None.
        Nr,   r+   filenamer,   r,   r-   r     s    zDistribution.read_textstr | os.PathLike[str]r   pathr'   c                 C  s   dS )a  
        Given a path to a file in this distribution, return a SimplePath
        to it.

        This method is used by callers of ``Distribution.files()`` to
        locate files within the distribution. If it's possible for a
        Distribution to represent files in the distribution as
        ``SimplePath`` objects, it should implement this method
        to resolve such objects.

        Some Distribution providers may elect not to resolve SimplePath
        objects within the distribution by raising a
        NotImplementedError, but consumers of such a Distribution would
        be unable to invoke ``Distribution.files()``.
        Nr,   r+   r   r,   r,   r-   r     s    zDistribution.locate_filer%   r   c                 C  sH   |st dztt| | j|dW S  tyB   t|Y n0 dS )a  Return the Distribution for the given package name.

        :param name: The name of the distribution package to search for.
        :return: The Distribution instance (or subclass thereof) for the named
            package, if found.
        :raises PackageNotFoundError: When the named package's distribution
            metadata cannot be found.
        :raises ValueError: When an invalid value is supplied for name.
        z A distribution name is required.r(   N)rp   r   r   _prefer_validdiscoverr   r   )rA   r)   r,   r,   r-   	from_name  s    zDistribution.from_nameNcontextz!DistributionFinder.Context | NoneIterable[Distribution])r   r'   c                  sB    r|rt d p"tjf i | tj fdd|  D S )a:  Return an iterable of Distribution objects for all packages.

        Pass a ``context`` or pass keyword arguments for constructing
        a context.

        :context: A ``DistributionFinder.Context`` object.
        :return: Iterable of Distribution objects for packages matching
          the context.
        z cannot accept context and kwargsc                 3  s   | ]}| V  qd S r/   r,   )r;   resolverr   r,   r-   r=     s   z(Distribution.discover.<locals>.<genexpr>)rp   r   Context	itertoolschainfrom_iterable_discover_resolvers)rA   r   kwargsr,   r   r-   r     s    zDistribution.discover)distsr'   c                 C  s"   t | dd }t|d |d S )z{
        Prefer (move to the front) distributions that have metadata.

        Ref python/importlib_resources#489.
        c                 S  s
   t | jS r/   )boolr!   rr   r,   r,   r-   <lambda>  rw   z,Distribution._prefer_valid.<locals>.<lambda>TF)r   r   r   )r   Zbucketsr,   r,   r-   r     s    zDistribution._prefer_validc                 C  s   t t| S )zReturn a Distribution for the indicated metadata path.

        :param path: a string or path-like object
        :return: a concrete Distribution instance for the path
        )PathDistributionpathlibPathr   r,   r,   r-   at  s    zDistribution.atc                  C  s   dd t jD } td| S )z9Search the meta_path for resolvers (MetadataPathFinders).c                 s  s   | ]}t |d dV  qdS )find_distributionsNru   )r;   finderr,   r,   r-   r=     s   z3Distribution._discover_resolvers.<locals>.<genexpr>N)sys	meta_pathrF   )Zdeclaredr,   r,   r-   r     s    z Distribution._discover_resolvers_meta.PackageMetadata | Nonec                 C  s(   |  dp|  dp|  d}| |S )av  Return the parsed metadata for this Distribution.

        The returned object will have keys that name the various bits of
        metadata per the
        `Core metadata specifications <https://packaging.python.org/en/latest/specifications/core-metadata/#core-metadata>`_.

        Custom providers may provide the METADATA file or override this
        property.
        METADATAzPKG-INFOrb   )r   _assemble_messager+   rB   r,   r,   r-   r!     s    
zDistribution.metadataz_meta.PackageMetadata)rB   r'   c                 C  s   ddl m} |t| S )Nr
   )	_adapters)rb   r   Messageemailmessage_from_string)rB   r   r,   r,   r-   r     s    zDistribution._assemble_messagec                 C  s   t | jd S )z8Return the 'Name' metadata for the distribution package.Namer   r!   r*   r,   r,   r-   r)      s    zDistribution.namec                 C  s   t | jS )z(Return a normalized version of the name.)Prepared	normalizer)   r*   r,   r,   r-   _normalized_name%  s    zDistribution._normalized_namec                 C  s   t | jd S )z;Return the 'Version' metadata for the distribution package.Versionr   r*   r,   r,   r-   r$   *  s    zDistribution.versionr   c                 C  s   t | d| S )z
        Return EntryPoints for this distribution.

        Custom providers may provide the ``entry_points.txt`` file
        or override this property.
        zentry_points.txt)r   r   r   r*   r,   r,   r-   r   /  s    zDistribution.entry_pointslist[PackagePath] | Nonec                   sJ   dfdd	 t  fdd}t dd }|| pD pD S )	a*  Files in this distribution.

        :return: List of PackagePath for this distribution or None

        Result is `None` if the metadata file that enumerates files
        (i.e. RECORD for dist-info, or installed-files.txt or
        SOURCES.txt for egg-info) is missing.
        Result may be empty if the metadata exists but is empty.

        Custom providers are recommended to provide a "RECORD" file (in
        ``read_text``) or override this property to allow for callers to be
        able to resolve filenames provided by the package.
        Nc                   s6   t | }|rt|nd |_|r&t|nd |_ |_|S r/   )r   r   r   r   r   r\   )r)   r   Zsize_strresultr*   r,   r-   	make_fileI  s
    z%Distribution.files.<locals>.make_filec                   s   dd l }t || S )Nr   )csvr   reader)rL   r   )r   r,   r-   
make_filesP  s    z&Distribution.files.<locals>.make_filesc                 S  s   t tdd | S )Nc                 S  s   |    S r/   )r   existsr   r,   r,   r-   r   Z  rw   z@Distribution.files.<locals>.skip_missing_files.<locals>.<lambda>)listrF   )Zpackage_pathsr,   r,   r-   skip_missing_filesX  s    z.Distribution.files.<locals>.skip_missing_files)NN)r   _read_files_distinfo_read_files_egginfo_installed_read_files_egginfo_sources)r+   r   r   r,   )r   r+   r-   r    9  s    
zDistribution.filesc                 C  s   |  d}|o| S )z+
        Read the lines of RECORD.
        RECORD)r   rI   r   r,   r,   r-   r   d  s    
z!Distribution._read_files_distinfoc                   sF     d}t dd|rs"dS  fdd| D }tdj|S )a  
        Read installed-files.txt and return lines in a similar
        CSV-parsable format as RECORD: each file must be placed
        relative to the site-packages directory and must also be
        quoted (since file names can contain literal commas).

        This file is written when the package is installed by pip,
        but it might not be written for other installation methods.
        Assume the file is accurate if it exists.
        zinstalled-files.txt_pathNc                 3  s8   | ]0}t |  j d  dd V  qdS )rb   T)Zwalk_upN)r   Zrelative_fixresolverelative_tor   as_posix)r;   r)   r+   subdirr,   r-   r=   ~  s   z=Distribution._read_files_egginfo_installed.<locals>.<genexpr>"{}")r   rg   rI   rG   format)r+   rB   pathsr,   r   r-   r   k  s    
z*Distribution._read_files_egginfo_installedc                 C  s   |  d}|otdj| S )a  
        Read SOURCES.txt and return lines in a similar CSV-parsable
        format as RECORD: each file name must be quoted (since it
        might contain literal commas).

        Note that SOURCES.txt is not a reliable source for what
        files are installed by a package. This file is generated
        for a source archive, and the files that are present
        there (e.g. setup.py) may not correctly reflect the files
        that are present after the package has been installed.
        zSOURCES.txtr   )r   rG   r   rI   r   r,   r,   r-   r     s    
z(Distribution._read_files_egginfo_sourceslist[str] | Nonec                 C  s   |   p|  }|ot|S )z6Generated requirements specified for this Distribution)_read_dist_info_reqs_read_egg_info_reqsr   )r+   reqsr,   r,   r-   r#     s    zDistribution.requiresc                 C  s   | j dS )NzRequires-Dist)r!   get_allr*   r,   r,   r-   r     s    z!Distribution._read_dist_info_reqsc                 C  s   |  d}t| j|S )Nzrequires.txt)r   r   _deps_from_requires_text)r+   sourcer,   r,   r-   r     s    
z Distribution._read_egg_info_reqsc                 C  s   |  t|S r/   )%_convert_egg_info_reqs_to_simple_reqsr7   r?   )rA   r   r,   r,   r-   r     s    z%Distribution._deps_from_requires_textc                 #  sJ   dd   fdd}dd }| D ]$}||j }|j | ||j V  q dS )a  
        Historically, setuptools would solicit and store 'extra'
        requirements, including those with environment markers,
        in separate sections. More modern tools expect each
        dependency to be defined separately, with any relevant
        extras and environment markers attached directly to that
        requirement. This method converts the former to the
        latter. See _test_deps_from_requires_text for an example.
        c                 S  s   | od|  dS )Nz
extra == ""r,   r(   r,   r,   r-   make_condition  s    zJDistribution._convert_egg_info_reqs_to_simple_reqs.<locals>.make_conditionc                   sX   | pd} |  d\}}}|r,|r,d| d}ttd | |g}|rTdd| S dS )Nrb   :(r   z; z and )r   r   rF   join)r<   extrasepmarkersZ
conditionsr  r,   r-   quoted_marker  s    zIDistribution._convert_egg_info_reqs_to_simple_reqs.<locals>.quoted_markerc                 S  s   dd| v  S )z
            PEP 508 requires a space between the url_spec and the quoted_marker.
            Ref python/importlib_metadata#357.
             @r,   )reqr,   r,   r-   url_req_space  s    zIDistribution._convert_egg_info_reqs_to_simple_reqs.<locals>.url_req_spaceN)r8   r)   )sectionsr	  r  r<   spacer,   r  r-   r     s    
z2Distribution._convert_egg_info_reqs_to_simple_reqsc                 C  s
   |  dS )Nzdirect_url.json)
_load_jsonr*   r,   r,   r-   origin  s    zDistribution.originc                 C  s$   dd l }t|j| |dd dS )Nr   c                 S  s   t jf i | S r/   )typesSimpleNamespace)datar,   r,   r-   r     rw   z)Distribution._load_json.<locals>.<lambda>)object_hook)jsonr   loadsr   )r+   r   r  r,   r,   r-   r    s
    zDistribution._load_json)"r2   r3   r4   r5   abcabstractmethodr   r   rS   r   r   rT   r   r   r   r6   r!   r   r   r)   r   r$   r   r    r   r   r   r#   r   r   r   r   r  r  r,   r,   r,   r-   r     sZ   
	

	*

"
r   )	metaclassc                   @  s8   e Zd ZdZG dd dZeje fddddZdS )	r   z
    A MetaPathFinder capable of discovering installed distributions.

    Custom providers should implement this interface in order to
    supply metadata.
    c                   @  s.   e Zd ZdZdZdd ZeddddZdS )	zDistributionFinder.Contexta  
        Keyword arguments presented by the caller to
        ``distributions()`` or ``Distribution.discover()``
        to narrow the scope of a search for distributions
        in all DistributionFinders.

        Each DistributionFinder may expect any parameters
        and should attempt to honor the canonical
        parameters defined below when appropriate.

        This mechanism gives a custom provider a means to
        solicit additional details from the caller beyond
        "name" and "path" when searching distributions.
        For example, imagine a provider that exposes suites
        of packages in either a "public" or "private" ``realm``.
        A caller may wish to query only for distributions in
        a particular realm and could call
        ``distributions(realm="private")`` to signal to the
        custom provider to only include distributions from that
        realm.
        Nc                 K  s   t | | d S r/   rs   )r+   r   r,   r,   r-   ra     s    z#DistributionFinder.Context.__init__rk   r&   c                 C  s   t | dtjS )z
            The sequence of directory path that a distribution finder
            should search.

            Typically refers to Python installed package paths such as
            "site-packages" directories and defaults to ``sys.path``.
            r   )r_   getr   r   r*   r,   r,   r-   r     s    	zDistributionFinder.Context.path)r2   r3   r4   r5   r)   ra   r6   r   r,   r,   r,   r-   r     s
   r   r   r&   c                 C  s   dS )z
        Find distributions.

        Return an iterable of all Distribution instances capable of
        loading the metadata for packages matching the ``context``,
        a DistributionFinder.Context instance.
        Nr,   )r+   r   r,   r,   r-   r   
  s    z%DistributionFinder.find_distributionsN)r2   r3   r4   r5   r   r  r  r   r,   r,   r,   r-   r     s   +r   c                      sh   e Zd ZdZe  fddZdd Zdd Zdd	 Z	d
d Z
dd Zedd Zedd Z  ZS )FastPathaq  
    Micro-optimized class for searching a root for children.

    Root is a path on the file system that may contain metadata
    directories either as natural directories or within a zip file.

    >>> FastPath('').children()
    ['...']

    FastPath objects are cached and recycled for any given root.

    >>> FastPath('foobar') is FastPath('foobar')
    True
    c                   s   t  | S r/   )super__new__)rA   rootr   r,   r-   r  %  s    zFastPath.__new__c                 C  s
   || _ d S r/   )r  )r+   r  r,   r,   r-   ra   )  s    zFastPath.__init__c                 C  s   t | j|S r/   )r   r   r  )r+   childr,   r,   r-   joinpath,  s    zFastPath.joinpathc                 C  sl   t t  t| jpdW  d    S 1 s.0    Y  t t |  W  d    S 1 s^0    Y  g S Nrc   )r   	Exceptionoslistdirr  zip_childrenr*   r,   r,   r-   children/  s
    
.
&zFastPath.childrenc                 C  s>   ddl m} || j}|j }|j| _tdd |D S )Nr   )zipfilec                 s  s    | ]}| tjd d V  qdS )r
   r   N)rd   	posixpathr  )r;   r!  r,   r,   r-   r=   >  rw   z(FastPath.zip_children.<locals>.<genexpr>)Zzipp.compat.overlayr)  r   r  namelistr"  dictfromkeys)r+   r)  zip_pathr   r,   r,   r-   r'  6  s
    
zFastPath.zip_childrenc                 C  s   |  | j|S r/   )lookupmtimesearchr1   r,   r,   r-   r1  @  s    zFastPath.searchc                 C  sD   t t t| jjW  d    S 1 s,0    Y  | j  d S r/   )r   OSErrorr%  statr  st_mtimer/  cache_clearr*   r,   r,   r-   r0  C  s    
,zFastPath.mtimec                 C  s   t | S r/   )Lookup)r+   r0  r,   r,   r-   r/  I  s    zFastPath.lookup)r2   r3   r4   r5   re   	lru_cacher  ra   r"  r(  r'  r1  r6   r0  r   r/  __classcell__r,   r,   r   r-   r    s   

r  c                   @  s,   e Zd ZdZddddZdddd	Zd
S )r6  zK
    A micro-optimized class for searching a (fast) path for metadata.
    r  r   c           	      C  s   t j|j }|d}tt| _tt| _	|
 D ]}| }|dr|dd dd }t|}| j| || q8|r8|dkr8|dd dd }t|}| j	| || q8| j  | j	  dS )z
        Calculate all of the children representing metadata.

        From the children in the path, calculate early all of the
        children that appear to represent metadata (infos) or legacy
        metadata (eggs).
        z.eggz
.dist-infoz	.egg-inforc   r   -zegg-infoN)r%  r   basenamer  lowerrK   r   r   infoseggsr(  
rpartitionr   r   r   appendr"  legacy_normalizefreeze)	r+   r   baseZbase_is_eggr!  lowr)   
normalizedlegacy_normalizedr,   r,   r-   ra   S  s     	






zLookup.__init__r   preparedc                 C  sP   |r| j |j ntj| j  }|r2| j|j ntj| j }t||S )zG
        Yield all infos and eggs matching the Prepared query.
        )r=  rE  r   r   r   r|   r>  rF  )r+   rH  r=  r>  r,   r,   r-   r1  p  s    zLookup.searchN)r2   r3   r4   r5   ra   r1  r,   r,   r,   r-   r6  N  s   r6  c                   @  sF   e Zd ZdZdZdZddddZedd Zed	d
 Z	dd Z
dS )r   a  
    A prepared search query for metadata on a possibly-named package.

    Pre-calculates the normalization to prevent repeated operations.

    >>> none = Prepared(None)
    >>> none.normalized
    >>> none.legacy_normalized
    >>> bool(none)
    False
    >>> sample = Prepared('Sample__Pkg-name.foo')
    >>> sample.normalized
    'sample_pkg_name_foo'
    >>> sample.legacy_normalized
    'sample__pkg_name.foo'
    >>> bool(sample)
    True
    Nr   r(   c                 C  s.   || _ |d u rd S | || _| || _d S r/   )r)   r   rE  rA  rF  r1   r,   r,   r-   ra     s
    zPrepared.__init__c                 C  s   t dd|  ddS )zC
        PEP 503 normalization plus dashes as underscores.
        z[-_.]+r:  r   )rl   subr<  replacer(   r,   r,   r-   r     s    zPrepared.normalizec                 C  s   |   ddS )z|
        Normalize the package name as found in the convention in
        older packaging tools versions and specs.
        r:  r   )r<  rJ  r(   r,   r,   r-   rA    s    zPrepared.legacy_normalizec                 C  s
   t | jS r/   )r   r)   r*   r,   r,   r-   __bool__  s    zPrepared.__bool__)r2   r3   r4   r5   rE  rF  ra   rT   r   rA  rK  r,   r,   r,   r-   r     s   

r   c                   @  sH   e Zd ZdZee fddddZedd Zeddd	d
Z	dS )MetadataPathFinderzA degenerate finder for distribution packages on the file system.

    This finder supplies only a find_distributions() method for versions
    of Python that do not have a PathFinder find_distributions().
    zIterable[PathDistribution]r&   c                 C  s   |  |j|j}tt|S )a   
        Find distributions.

        Return an iterable of all Distribution instances capable of
        loading the metadata for packages matching ``context.name``
        (or all names if ``None`` indicated) along the paths in the list
        of directories ``context.path``.
        )_search_pathsr)   r   rG   r   )rA   r   foundr,   r,   r-   r     s    z%MetadataPathFinder.find_distributionsc                   s(   t | tj fddtt|D S )z1Find metadata directories in paths heuristically.c                 3  s   | ]}|  V  qd S r/   )r1  )r;   r   rG  r,   r-   r=     s   z3MetadataPathFinder._search_paths.<locals>.<genexpr>)r   r   r   r   rG   r  )rA   r)   r   r,   rG  r-   rM    s    z MetadataPathFinder._search_pathsr]   c                 C  s   t j  d S r/   )r  r  r5  )rA   r,   r,   r-   invalidate_caches  s    z$MetadataPathFinder.invalidate_cachesN)
r2   r3   r4   r5   rS   r   r   r   rM  rO  r,   r,   r,   r-   rL    s   
rL  c                      sf   e Zd ZdddddZdddd	d
Zejje_dddddZe fddZ	e
dd Z  ZS )r   r   r]   r   c                 C  s
   || _ dS )zfConstruct a distribution.

        :param path: SimplePath indicating the metadata directory.
        N)r   r   r,   r,   r-   ra     s    zPathDistribution.__init__r   r   )r   r'   c                 C  sH   t ttttt$ | j|jddW  d    S 1 s:0    Y  d S )Nr   r   )	r   FileNotFoundErrorIsADirectoryErrorr   NotADirectoryErrorPermissionErrorr   r"  r   r   r,   r,   r-   r     s    2zPathDistribution.read_textc                 C  s   | j j| S r/   )r   parentr   r,   r,   r-   r     s    zPathDistribution.locate_filec                   s.   t jt| j}ttj| |p,t	 j
S )zz
        Performance optimization: where possible, resolve the
        normalized name from the file system path.
        )r%  r   r;  r%   r   r   r   r   _name_from_stemr  r   )r+   stemr   r,   r-   r     s    z!PathDistribution._normalized_namec                 C  s0   t j| \}}|dvrdS |d\}}}|S )a7  
        >>> PathDistribution._name_from_stem('foo-3.0.egg-info')
        'foo'
        >>> PathDistribution._name_from_stem('CherryPy-3.0.dist-info')
        'CherryPy'
        >>> PathDistribution._name_from_stem('face.egg-info')
        'face'
        >>> PathDistribution._name_from_stem('foo.bar')
        r9  Nr:  )r%  r   splitextr   )rV  r   extr)   r  restr,   r,   r-   rU    s
    z PathDistribution._name_from_stem)r2   r3   r4   ra   r   r   r5   r   r6   r   rT   rU  r8  r,   r,   r   r-   r     s   
r   r%   )distribution_namer'   c                 C  s
   t | S )zGet the ``Distribution`` instance for the named package.

    :param distribution_name: The name of the distribution package as a string.
    :return: A ``Distribution`` instance (or subclass thereof).
    )r   r   rZ  r,   r,   r-   r     s    r   r   r&   c                  K  s   t jf i | S )z|Get all ``Distribution`` instances in the current environment.

    :return: An iterable of ``Distribution`` instances.
    )r   r   )r   r,   r,   r-   r     s    r   r   c                 C  s   t | jS )zGet the metadata for the named package.

    :param distribution_name: The name of the distribution package to query.
    :return: A PackageMetadata containing the parsed metadata.
    )r   r   r!   r[  r,   r,   r-   r!     s    r!   c                 C  s
   t | jS )zGet the version string for the named package.

    :param distribution_name: The name of the distribution package to query.
    :return: The version string for the package as defined in the package's
        "Version" metadata key.
    )r   r$   r[  r,   r,   r-   r$   '  s    r$   )keyc                  K  s0   t jdd tt D }t|jf i | S )a  Return EntryPoint objects for all installed packages.

    Pass selection parameters (group or name) to filter the
    result to entry points matching those properties (see
    EntryPoints.select()).

    :return: EntryPoints for all installed packages.
    c                 s  s   | ]}|j V  qd S r/   )r   )r;   r\   r,   r,   r-   r=   C  s   zentry_points.<locals>.<genexpr>)r   r   r   _uniquer   r   r   )r}   Zepsr,   r,   r-   r   :  s    	r   r   c                 C  s
   t | jS )zReturn a list of files for the named package.

    :param distribution_name: The name of the distribution package to query.
    :return: List of files composing the distribution.
    )r   r    r[  r,   r,   r-   r    I  s    r    r   c                 C  s
   t | jS )z
    Return a list of requirements for the named package.

    :return: An iterable of requirements, suitable for
        packaging.requirement.Requirement.
    )r   r#   r[  r,   r,   r-   r#   R  s    r#   zMapping[str, list[str]]c                  C  sL   t t} t D ]2}t|p"t|D ]}| | t|jd  q$qt	| S )z
    Return a mapping of top-level packages to their
    distributions.

    >>> import collections.abc
    >>> pkgs = packages_distributions()
    >>> all(isinstance(dist, collections.abc.Sequence) for dist in pkgs.values())
    True
    r   )
collectionsdefaultdictr   r   _top_level_declared_top_level_inferredr@  r   r!   r,  )Zpkg_to_distr\   pkgr,   r,   r-   r"   \  s
    


r"   c                 C  s   |  dpd S )Nztop_level.txtrb   )r   rd   rr   r,   r,   r-   r`  m  s    r`  r   r   c                 C  s   | j ^}}|r|S dS )zB
    Return the top-most parent as long as there is a parent.
    N)parts)r)   toprY  r,   r,   r-   _topmostq  s    
re  c                 C  s"   ddl }t| p || p t| S )a  
    Infer a possibly importable module name from a name presumed on
    sys.path.

    >>> _get_toplevel_name(PackagePath('foo.py'))
    'foo'
    >>> _get_toplevel_name(PackagePath('foo'))
    'foo'
    >>> _get_toplevel_name(PackagePath('foo.pyc'))
    'foo'
    >>> _get_toplevel_name(PackagePath('foo/__init__.py'))
    'foo'
    >>> _get_toplevel_name(PackagePath('foo.pth'))
    'foo.pth'
    >>> _get_toplevel_name(PackagePath('foo.dist-info'))
    'foo.dist-info'
    r   N)inspectre  getmodulenamer%   )r)   rf  r,   r,   r-   _get_toplevel_namey  s    rh  c                 C  s&   t ttt| j}dd }t||S )Nc                 S  s   d| vS r#  r,   r(   r,   r,   r-   importable_name  s    z,_top_level_inferred.<locals>.importable_name)setrG   rh  r   r    rF   )r\   	opt_namesri  r,   r,   r-   ra    s    ra  )Ur5   
__future__r   r  r^  r   re   r   rz   r%  r   r*  rl   r   rP   r  collections.abcr   r   
contextlibr   	importlibr   importlib.abcr   r   typingr	   rb   r   _collectionsr   r   _compatr   r   
_functoolsr   r   
_itertoolsr   r   r   r   r   _typingr   compatr   r   __all__ModuleNotFoundErrorr   r7   r  rU   rZ   r   r   PurePosixPathr   r   ABCMetar   r   r  r6  r   rL  r   r   r   r!   r$   partialZnormalized_namer]  r   r    r#   r"   r`  re  rh  ra  r,   r,   r,   r-   <module>   s~   	A ,8  L>931#7		
		
