U
    |Ha                     @   s|  d Z ddlZddlZddlZddlZddlZzddlmZ W n  ek
r\   ddlmZ Y nX z.ddl	Z
ee
dszedddl	mZmZ W nZ ek
r   z ddlm
Z
 ddlmZmZ W n$ ek
r   ddl	mZmZ Y nX Y nX dgZdd	d
ZG dd deddZG dd dZdd ZG dd deZG dd deZdd Zee
d pfee
jeZerrene
jZdS )aG  
>>> hasattr(entry_points(), 'select')
True
>>> tuple(entry_points(group='console_scripts'))
(...)

Some usage is deprecated and may emit deprecation warnings
on later versions.

>>> import warnings
>>> warnings.filterwarnings('ignore', category=DeprecationWarning)

>>> entry_points()['console_scripts'][0]
EntryPoint...(...)
    N)filterfalse)ifilterfalseEntryPointszpackage without EntryPoints)distributions
EntryPointentry_pointsc                 c   sb   t  }|j}|dkr6t|j| D ]}|| |V  q n(| D ]"}||}||kr:|| |V  q:dS )zHList unique elements, preserving order. Remember all elements ever seen.N)setaddr   __contains__)iterablekeyseenZseen_addelementk r   E/tmp/pip-unpacked-wheel-b8lxdnbf/backports/entry_points_selectable.pyunique_everseen/   s    
r   c                   @   s   e Zd Zedd ZdS )Pairc                 C   s   | t tj|dd S )N=   )mapstrstripsplitclstextr   r   r   parseB   s    z
Pair.parseN)__name__
__module____qualname__classmethodr   r   r   r   r   r   A   s   r   z
name valuec                   @   sD   e Zd ZdZed Zedd Z	e
d
ddZe
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 k	r|jt|jdV  qd S )N)value)name_replacer   r   r#   ).0sectionr   r   r   	<genexpr>r   s   
z*Sectioned.section_pairs.<locals>.<genexpr>)filter_)readvalidr   r   r   r   section_pairsp   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[])filterr   r   r   
splitlines
startswithendswithr   )r   r)   linesr$   r#   Zsection_matchr   r   r   r*   x   s    
zSectioned.readc                 C   s   | o|  d S )N#)r1   )liner   r   r   r+      s    zSectioned.valid)N)r   r   r    __doc__textwrapdedentlstripZ_sampler!   r,   staticmethodr*   r+   r   r   r   r   r"   G   s   

r"   c                    sL   z j f |W S  tk
r"   Y nX  fdd|D }tttj| |S )Nc                 3   s   | ]}t  |V  qd S N)getattr)r&   paramepr   r   r(      s     z!compat_matches.<locals>.<genexpr>)matchesAttributeErrorallr   operatoreqvalues)r?   paramsattrsr   r>   r   compat_matches   s    rH   c                       sh   e Zd ZdZdZ fddZdd Zedd Zed	d
 Z	e
dd Ze
dd Zedd Z  ZS )r   zC
    An immutable collection of selectable EntryPoint objects.
    r   c                    sR   t |trtt| |S ztt| j|dW S  tk
rL   t	|Y nX dS )z;
        Get the EntryPoint in self matching name.
        r$   N)

isinstanceintsuperr   __getitem__nextiterselectStopIterationKeyError)selfr$   	__class__r   r   rM      s    
zEntryPoints.__getitem__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 |f r|V  qd S r;   )rH   r&   r?   rF   r   r   r(      s      z%EntryPoints.select.<locals>.<genexpr>r   rS   rF   r   rW   r   rP      s    zEntryPoints.selectc                 C   s   t dd | D S )zB
        Return the set of all names of all entry points.
        c                 s   s   | ]}|j V  qd S r;   rI   rV   r   r   r   r(      s     z$EntryPoints.names.<locals>.<genexpr>r   rS   r   r   r   names   s    zEntryPoints.namesc                 C   s   t dd | D S )z
        Return the set of all groups of all entry points.

        For coverage while SelectableGroups is present.
        >>> EntryPoints().groups
        set(...)
        c                 s   s   | ]}|j V  qd S r;   )grouprV   r   r   r   r(      s     z%EntryPoints.groups.<locals>.<genexpr>rZ   r[   r   r   r   groups   s    	zEntryPoints.groupsc                    s   |  fdd|  |D S )Nc                 3   s   | ]}|  V  qd S r;   )Z_forrV   distr   r   r(      s     z-EntryPoints._from_text_for.<locals>.<genexpr>)
_from_text)r   r   r`   r   r_   r   _from_text_for   s    zEntryPoints._from_text_forc                 C   s   t t| |pdS )N )	itertoolsstarmapr   _parse_groupsr   r   r   r   ra      s    zEntryPoints._from_textc                 C   s   dd t | D S )Nc                 s   s"   | ]}|j j|j j |jfV  qd S r;   )r#   r$   )r&   itemr   r   r   r(      s   z,EntryPoints._parse_groups.<locals>.<genexpr>)r"   r,   )r   r   r   r   rf      s    zEntryPoints._parse_groups)r   r   r    r6   	__slots__rM   rP   propertyr\   r^   r!   rb   ra   r:   rf   __classcell__r   r   rT   r   r      s   




c                   @   sH   e Zd ZdZedd Zedd Zedd Zedd	 Z	d
d Z
dS )SelectableGroupszs
    A backward- and forward-compatible result from
    entry_points that fully implements the dict interface.
    c                 C   s4   t d}t||d}t||}| dd |D S )Nr]   r   c                 s   s   | ]\}}|t |fV  qd S r;   rX   )r&   r]   epsr   r   r   r(      s     z(SelectableGroups.load.<locals>.<genexpr>)rC   
attrgettersortedrd   groupby)r   rm   Zby_groupZorderedZgroupedr   r   r   load   s    
zSelectableGroups.loadc                 C   s   t tj|  S )zH
        Reconstruct a list of all entrypoints from the groups.
        )r   rd   chainfrom_iterablerE   r[   r   r   r   _all   s    zSelectableGroups._allc                 C   s   | j jS r;   )rt   r^   r[   r   r   r   r^      s    zSelectableGroups.groupsc                 C   s   | j jS )zU
        for coverage:
        >>> SelectableGroups().names
        set(...)
        )rt   r\   r[   r   r   r   r\      s    zSelectableGroups.namesc                 K   s   |s| S | j jf |S r;   )rt   rP   rY   r   r   r   rP      s    zSelectableGroups.selectN)r   r   r    r6   r!   rq   ri   rt   r^   r\   rP   r   r   r   r   rk      s   



rk   c                  K   sD   dd }t jt|d}tjdd |t D }t|j	f | 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()).

    For compatibility, returns ``SelectableGroups`` object unless
    selection parameters are supplied. In the future, this function
    will return ``EntryPoints`` instead of ``SelectableGroups``
    even when no selection parameters are supplied.

    For maximum future compatibility, pass selection parameters
    or invoke ``.select`` with parameters on the result.

    :return: EntryPoints or SelectableGroups for all installed packages.
    c                 S   s
   | j d S )NName)metadatar_   r   r   r   	dist_name  s    z&entry_points_compat.<locals>.dist_namerl   c                 s   s   | ]}|j V  qd S r;   )r   )r&   r`   r   r   r   r(   	  s    z&entry_points_compat.<locals>.<genexpr>)
	functoolspartialr   rd   rr   rs   r   rk   rq   rP   )rF   rw   uniquerm   r   r   r   entry_points_compat   s    r{   )N)r6   collectionsr7   rd   rC   rx   r   ImportErrorr   Zimportlib_metadatarv   hasattrr   r   importlib.metadata__all__r   
namedtupler   r"   rH   listr   dictrk   r{   
issubclasstupleZneeds_backportr   r   r   r   r   <module>   sB   

A	;' 