U
    +5øiê'  ã                   @  sè   d dl mZ d dlZd dlmZmZ ddlmZ ddlm	Z	 ddd	d
ddgZ
ddœdd„ZG dd
„ d
eƒZG dd„ deƒZG dd„ deƒZG dd„ dƒZG dd	„ d	ƒZddddœdd„Ze d¡Zdddœdd„Zddd d!œd"d#„ZdS )$é    )ÚannotationsN)ÚMappingÚSequenceé   )Ú_ErrorCollector)ÚRequirementÚCyclicDependencyGroupÚDependencyGroupIncludeÚDependencyGroupResolverÚDuplicateGroupNamesÚInvalidDependencyGroupObjectÚresolve_dependency_groupsz	list[str]©Úreturnc                   C  s   t S ©N)Ú__all__© r   r   ú?/tmp/pip-unpacked-wheel-xj899fla/packaging/dependency_groups.pyÚ__dir__   s    r   c                   @  s   e Zd ZdZdS )r   z]
    The same dependency groups were defined twice, with different non-normalized names.
    N©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r      s   c                      s,   e Zd ZdZdddddœ‡ fdd„Z‡  ZS )r   z5
    The dependency group includes form a cycle.
    ÚstrÚNone)Úrequested_groupÚgroupÚinclude_groupr   c                   s\   || _ || _|| _||kr&|› d�}n|› d|› d|› d|› �}tƒ  d|› d|› �¡ d S )Nz includes itselfz -> ú, z0Cyclic dependency group include while resolving z: )r   r   r   ÚsuperÚ__init__)Úselfr   r   r   Úreason©Ú	__class__r   r   r!   '   s    ÿzCyclicDependencyGroup.__init__)r   r   r   r   r!   Ú__classcell__r   r   r$   r   r   "   s   c                   @  s   e Zd ZdZdS )r   ze
    A member of a dependency group was identified as a dict, but was not in a valid
    format.
    Nr   r   r   r   r   r   9   s   c                   @  s.   e Zd ZdZdddœdd„Zddœdd	„Zd
S )r	   ©r   r   r   )r   r   c                 C  s
   || _ dS )zˆ
        Initialize a DependencyGroupInclude.

        :param include_group: The name of the group referred to by this include.
        Nr'   )r"   r   r   r   r   r!   H   s    zDependencyGroupInclude.__init__r   c                 C  s   | j j› d| j›d�S )Nú(ú))r%   r   r   )r"   r   r   r   Ú__repr__P   s    zDependencyGroupInclude.__repr__N)r   r   r   Ú	__slots__r!   r*   r   r   r   r   r	   E   s   c                   @  sf   e Zd ZdZdddœdd„Zddd	œd
d„Zddd	œdd„Zdddddœdd„Zddddœdd„ZdS )r
   a\  
    A resolver for Dependency Group data.

    This class handles caching, name normalization, cycle detection, and other
    parsing requirements. There are only two public methods for exploring the data:
    ``lookup()`` and ``resolve()``.

    :param dependency_groups: A mapping, as provided via pyproject
        ``[dependency-groups]``.
    ú/Mapping[str, Sequence[str | Mapping[str, str]]]r   )Údependency_groupsr   c                 C  s2   t ƒ }t||ƒ| _i | _i | _i | _| d¡ d S )Nz$[dependency-groups] data was invalid)r   Ú_normalize_group_namesr-   Ú_parsed_groupsÚ_include_graph_ancestorsÚ_resolve_cacheÚfinalize)r"   r-   Úerrorsr   r   r   r!   `   s    þz DependencyGroupResolver.__init__r   z0tuple[Requirement | DependencyGroupInclude, ...])r   r   c              
   C  s@   t |ƒ}tƒ  d|›d�¡�}|  ||¡W  5 Q R £ S Q R X dS )zº
        Lookup a group name, returning the parsed dependency data for that group.
        This will not resolve includes.

        :param group: the name of the group to lookup
        ú[dependency-groups] data for ú was malformedN)Ú_normalize_namer   Úon_exitÚ_parse_group©r"   r   r3   r   r   r   Úlookups   s    
ÿzDependencyGroupResolver.lookupztuple[Requirement, ...]c              
   C  sB   t |ƒ}tƒ  d|›d�¡�}|  |||¡W  5 Q R £ S Q R X dS )z
        Resolve a dependency group to a list of requirements.

        :param group: the name of the group to resolve
        r4   r5   N)r6   r   r7   Ú_resolver9   r   r   r   Úresolve�   s    
ÿzDependencyGroupResolver.resolver   )r   r   r3   r   c                 C  sà   || j kr| j | S |  ||¡}g }|D ]”}t|tƒrB| |¡ q(t|tƒr®t|jƒ}|| j 	|d¡kr~| 
t|||jƒ¡ q¼| j 	|d¡|f˜| j|< | |  |||¡¡ q(td|› �ƒ‚q(|jrÈdS t|ƒ| j |< | j | S )a”  
        This is a helper for cached resolution to strings. It preserves the name of the
        group which the user initially requested in order to present a clearer error in
        the event that a cycle is detected.

        :param group: The normalized name of the group to resolve.
        :param requested_group: The group which was used in the original, user-facing
            request.
        r   z+Invalid dependency group item after parse: )r1   r8   Ú
isinstancer   Úappendr	   r6   r   r0   ÚgetÚerrorr   Úextendr;   ÚNotImplementedErrorr3   Útuple)r"   r   r   r3   ÚparsedZresolved_groupÚitemr   r   r   r   r;   Ž   s<    




  ÿÿþÿÿz DependencyGroupResolver._resolve)r   r3   r   c                 C  s(  || j kr| j | S || jkr8| td|› d�ƒ¡ dS | j| }t|tƒrf| td|›d�ƒ¡ dS t|tƒsŠ| td|›d�ƒ¡ dS g }|D ]|}t|tƒr°| t	|ƒ¡ q’t|t
ƒrút| ¡ ƒdkrà| td|›�ƒ¡ n|d	 }| t|d
�¡ q’| td|›�ƒ¡ q’t|ƒ| j |< | j | S )NzDependency group 'z' not foundr   zDependency group z' contained a string rather than a list.z is not a sequence type.)úinclude-groupzInvalid dependency group item: rF   r'   )r/   r-   r@   ÚLookupErrorr=   r   Ú	TypeErrorr   r>   r   r   rC   Úkeysr   r	   )r"   r   r3   Z	raw_groupÚelementsrE   r   r   r   r   r8   È   sD    





ÿÿ
ÿ

ÿÿz$DependencyGroupResolver._parse_groupN)	r   r   r   r   r!   r:   r<   r;   r8   r   r   r   r   r
   T   s   :r,   r   ztuple[str, ...])r-   Úgroupsr   c                  s   t | ƒ‰ t‡ fdd„|D ƒƒS )zû
    Resolve a dependency group to a tuple of requirements, as strings.

    :param dependency_groups: the parsed contents of the ``[dependency-groups]`` table
        from ``pyproject.toml``
    :param groups: the name of the group(s) to resolve
    c                 3  s&   | ]}ˆ   |¡D ]}t|ƒV  qqd S r   )r<   r   )Ú.0r   Úr©Úresolverr   r   Ú	<genexpr>
  s       z,resolve_dependency_groups.<locals>.<genexpr>)r
   rC   )r-   rK   r   rN   r   r   ÿ   s    
z[-_.]+)Únamer   c                 C  s   t  d| ¡ ¡ S )Nú-)Ú_NORMALIZE_PATTERNÚsubÚlower)rQ   r   r   r   r6     s    r6   r   z,dict[str, Sequence[str | Mapping[str, str]]])r-   r3   r   c           	   
   C  s€   i }i }|   ¡ D ]*\}}t|ƒ}| |g ¡ |¡ |||< q|  ¡ D ]6\}}t|ƒdkrD| td|› dd |¡› d�ƒ¡ qD|S )Nr   z"Duplicate dependency group names: z (r   r)   )Úitemsr6   Ú
setdefaultr>   Úlenr@   r   Újoin)	r-   r3   Úoriginal_namesZnormalized_groupsZ
group_nameÚvalueZnormed_group_nameZnormed_nameÚnamesr   r   r   r.     s    
ÿÿr.   )Ú
__future__r   ÚreÚcollections.abcr   r   r3   r   Úrequirementsr   r   r   Ú
ValueErrorr   r   r   r	   r
   r   ÚcompilerS   r6   r.   r   r   r   r   Ú<module>   s,   ú
	 ,
