B
    (\-                 @   sr  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 d dlmZ ejdkrd dlZd dlmZ n d dlZd dlmZ d dlmZ yeZW n ek
r   eZY nX eZG dd deZG d	d
 d
e	ddZG dd dejZG dd dZ G dd dZ!dd Z"dd Z#dd Z$dd Z%dd Z&dd Z'd%dd Z(d!d" Z)d#d$ Z*dS )&    N)import_module)starmap)   )ConfigParser)imapc               @   s   e Zd ZdZdS )PackageNotFoundErrorzThe package was not found.N)__name__
__module____qualname____doc__ r   r   5lib/python3.7/site-packages/importlib_metadata/api.pyr       s   r   c               @   sN   e Zd ZdZedZdd Zedd Z	e
dd Ze
d	d
 Zdd ZdS )
EntryPointz:An entry point as defined by Python packaging conventions.zF(?P<module>[\w.]+)\s*(:\s*(?P<attr>[\w.]+))?\s*(?P<extras>\[.*\])?\s*$c             C   s@   | j | j}t|d}td|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.
        moduleNattr.)
patternmatchvaluer   groupfiltersplit	functoolsreducegetattr)selfr   r   Zattrsr   r   r   load<   s    zEntryPoint.loadc             C   s(   | j | j}ttd|dp"dS )Nz\w+extras )r   r   r   listrefinditerr   )r   r   r   r   r   r   F   s    zEntryPoint.extrasc                s    fdd  D S )Nc                s,   g | ]$} |D ]\}} |||qqS r   )items).0r   namer   )clsconfigr   r   
<listcomp>N   s   z+EntryPoint._from_config.<locals>.<listcomp>)sections)r%   r&   r   )r%   r&   r   _from_configK   s    zEntryPoint._from_configc             C   sD   t  }y|| W n$ tk
r8   |t| Y nX t|S )N)r   Zread_stringAttributeErrorZreadfpioStringIOr   r)   )r%   textr&   r   r   r   
_from_textS   s    zEntryPoint._from_textc             C   s   t | j| fS )zO
        Supply iter so one may construct dicts of EntryPoints easily.
        )iterr$   )r   r   r   r   __iter__]   s    zEntryPoint.__iter__N)r   r	   r
   r   r    compiler   r   propertyr   classmethodr)   r.   r0   r   r   r   r   r   $   s   

r   ZEntryPointBasezname value groupc               @   s*   e Zd ZdZd
ddZdd Zdd Zd	S )PackagePathz"A reference to a path in a packageutf-8c          	   C   s$   |   j|d
}| S Q R X d S )N)encoding)locateopenread)r   r6   streamr   r   r   	read_textg   s    zPackagePath.read_textc          	   C   s"   |   d
}| S Q R X d S )Nrb)r7   r8   r9   )r   r:   r   r   r   read_binaryk   s    zPackagePath.read_binaryc             C   s   | j | S )z'Return a path-like object for this path)distlocate_file)r   r   r   r   r7   o   s    zPackagePath.locateN)r5   )r   r	   r
   r   r;   r=   r7   r   r   r   r   r4   d   s   
r4   c               @   s   e Zd Zdd Zdd ZdS )FileHashc             C   s   | d\| _}| _d S )N=)	partitionmoder   )r   spec_r   r   r   __init__u   s    zFileHash.__init__c             C   s   d | j| jS )Nz<FileHash mode: {} value: {}>)formatrC   r   )r   r   r   r   __repr__x   s    zFileHash.__repr__N)r   r	   r
   rF   rH   r   r   r   r   r@   t   s   r@   c               @   s   e Zd ZdZejdd Zejdd Zedd Z	edd	 Z
ed
d Zedd Zedd Zedd Zedd Zedd Zdd Zdd Zedd Zdd Zdd Zed d! Zed"d# Zed$d% Zd&S )'DistributionzA Python distribution package.c             C   s   dS )zAttempt to load metadata file given by the name.

        :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_textc             C   s   dS )z[
        Given a path to a file in this distribution, return a path
        to it.
        Nr   )r   pathr   r   r   r?      s    zDistribution.locate_filec             C   s<   x6|   D ]"}||}t|d}|dk	r
|S q
W t|dS )af  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.
        N)_discover_resolversnextr   )r%   r$   resolverdistsr>   r   r   r   	from_name   s    

zDistribution.from_namec             C   s   t jdd |  D S )zReturn an iterable of Distribution objects for all packages.

        :return: Iterable of Distribution objects for all packages.
        c             s   s   | ]}| V  qd S )Nr   )r#   rN   r   r   r   	<genexpr>   s   z(Distribution.discover.<locals>.<genexpr>)	itertoolschainfrom_iterablerL   )r%   r   r   r   discover   s    zDistribution.discoverc              C   s   dd t jD } td| S )z#Search the meta_path for resolvers.c             s   s   | ]}t |d dV  qdS )Zfind_distributionsN)r   )r#   finderr   r   r   rQ      s   z3Distribution._discover_resolvers.<locals>.<genexpr>N)sys	meta_pathr   )Zdeclaredr   r   r   rL      s    
z Distribution._discover_resolversc             C   s$   t jdd |  D }|\}|S )Nc             s   s   | ]}|d gdV  qdS )r   )rK   Nr   )r#   rN   r   r   r   rQ      s   z*Distribution.find_local.<locals>.<genexpr>)rR   rS   rT   rL   )r%   rO   r>   r   r   r   
find_local   s
    zDistribution.find_localc             C   s   |  dp|  d}t|S )zReturn the parsed metadata for this Distribution.

        The returned object will have keys that name the various bits of
        metadata.  See PEP 566 for details.
        ZMETADATAzPKG-INFO)r;   _email_message_from_string)r   r-   r   r   r   metadata   s    zDistribution.metadatac             C   s
   | j d S )z;Return the 'Version' metadata for the distribution package.ZVersion)r[   )r   r   r   r   version   s    zDistribution.versionc             C   s   t | dS )Nzentry_points.txt)r   r.   r;   )r   r   r   r   entry_points   s    zDistribution.entry_pointsc                s2      p  }d fdd	}|o0t|t|S )Nc                s6   t | }|rt|nd |_|r&t|nd |_ |_|S )N)r4   r@   hashintsizer>   )r$   r^   Zsize_strresult)r   r   r   	make_file   s
    z%Distribution.files.<locals>.make_file)NN)_read_files_distinfo_read_files_egginfor   csvreader)r   Z
file_linesrb   r   )r   r   files   s    zDistribution.filesc             C   s   |  d}|o| S )z*
        Read the lines of RECORD
        ZRECORD)r;   
splitlines)r   r-   r   r   r   rc      s    
z!Distribution._read_files_distinfoc             C   s   |  d}|otdj| S )z`
        SOURCES.txt might contain literal commas, so wrap each line
        in quotes.
        zSOURCES.txtz"{}")r;   maprG   rh   )r   r-   r   r   r   rd      s    
z Distribution._read_files_egginfoc             C   s   |   p|  S )N)_read_dist_info_reqs_read_egg_info_reqs)r   r   r   r   requires   s    zDistribution.requiresc             C   s   | j d }|otd | S )NzRequires-Dist)r[   r   rh   )r   rD   r   r   r   rj      s    
z!Distribution._read_dist_info_reqsc             C   s   |  d}| |S )Nzrequires.txt)r;   _deps_from_requires_text)r   sourcer   r   r   rk      s    
z Distribution._read_egg_info_reqsc             C   s4   |  | }dd t|tdD }| |S )Nc             S   s&   i | ]\}}t ttd ||qS )line)r   ri   operator
itemgetter)r#   sectionZresultsr   r   r   
<dictcomp>   s   z9Distribution._deps_from_requires_text.<locals>.<dictcomp>rr   )_read_sectionsrh   rR   groupbyrp   rq   %_convert_egg_info_reqs_to_simple_reqs)r%   rn   Zsection_pairsr(   r   r   r   rm      s    z%Distribution._deps_from_requires_textc             c   s@   d }x6t d | D ](}td|}|r0|d}qt V  qW d S )Nz	\[(.*)\]$   )r   r    r   r   locals)linesrr   ro   Zsection_matchr   r   r   rt     s    
zDistribution._read_sectionsc             #   sJ   dd   fdd}x0|   D ]$\}}x|D ]}||| V  q,W qW 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j | dS )Nzextra == "{name}")r$   )rG   )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j|d}ttd | |g}|rTdd| S dS )Nr   :z({markers}))markersz; z and )rB   rG   r   r   join)rr   Zextrasepr|   Z
conditions)rz   r   r   parse_condition  s    zKDistribution._convert_egg_info_reqs_to_simple_reqs.<locals>.parse_conditionN)r"   )r(   r   rr   ZdepsZdepr   )rz   r   rv     s
    
z2Distribution._convert_egg_info_reqs_to_simple_reqsN)r   r	   r
   r   abcabstractmethodr;   r?   r3   rP   rU   staticmethodrL   rY   r2   r[   r\   r]   rg   rc   rd   rl   rj   rk   rm   rt   rv   r   r   r   r   rI   |   s&   		


rI   c             C   s(   t jdk rt| }t|S t| S )N)r   )rW   version_infor+   r,   emailZmessage_from_fileZmessage_from_string)r-   Z	io_bufferr   r   r   rZ   '  s    


rZ   c             C   s
   t | S )zGet the ``Distribution`` instance for the given package.

    :param package: The name of the package as a string.
    :return: A ``Distribution`` instance (or subclass thereof).
    )rI   rP   )packager   r   r   distribution0  s    r   c               C   s   t  S )z|Get all ``Distribution`` instances in the current environment.

    :return: An iterable of ``Distribution`` instances.
    )rI   rU   r   r   r   r   distributions9  s    r   c               C   s   t  S )zGet the ``Distribution`` instance for the package in CWD.

    :return: A ``Distribution`` instance (or subclass thereof).
    )rI   rY   r   r   r   r   local_distributionA  s    r   c             C   s   t | jS )zGet the metadata for the package.

    :param package: The name of the distribution package to query.
    :return: An email.Message containing the parsed metadata.
    )rI   rP   r[   )r   r   r   r   r[   I  s    r[   c             C   s
   t | jS )zGet the version string for the named package.

    :param package: 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\   R  s    r\   c             C   sH   t jdd t D }td}t||d}t ||}dd |D S )zwReturn EntryPoint objects for all installed packages.

    :return: EntryPoint objects for all installed packages.
    c             s   s   | ]}|j V  qd S )N)r]   )r#   r>   r   r   r   rQ   b  s    zentry_points.<locals>.<genexpr>r   )keyc             S   s   i | ]\}}t ||qS r   )tuple)r#   r   epsr   r   r   rs   f  s   z entry_points.<locals>.<dictcomp>)rR   rS   rT   r   rp   
attrgettersortedru   )r$   r   Zby_groupZorderedZgroupedr   r   r   r]   \  s    
r]   c             C   s
   t | jS )N)r   rg   )r   r   r   r   rg   l  s    rg   c             C   s
   t | jS )z
    Return a list of requirements for the indicated distribution.

    :return: An iterator of requirements, suitable for
    packaging.requirement.Requirement.
    )r   rl   )r   r   r   r   rl   p  s    rl   )N)+r+   r    r   re   rW   r   rp   r   rR   collections	importlibr   r   r   ZpathlibZconfigparserr   Zpathlib2Zbackports.configparserr   ri   ModuleNotFoundErrorZ	BaseClass	NameErrorImportErrortypeZ__metaclass__r   
namedtupler   Z	PosixPathr4   r@   rI   rZ   r   r   r   r[   r\   r]   rg   rl   r   r   r   r   <module>   sJ   

@ ,			

