U
    +5i*                     @  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	m
Z
mZmZ e	rd dlZd dlmZ ejdkrxd dlmZ nd dlmZ dd	d
ddgZddddZedZG dd deZededZdddddZdddddd d!Zdddddd"d#Zdd$dd%d&d'd(Zed)Zdd*dd+d,d-Zdd*dd.d/d0ZG d1d deZ G d2d3 d3e Z!ej"d4d5d6G d7d dZ#ej"d4d5d6G d8d dZ$ej"d4d5d6G d9d	 d	Z%ej"d4d5d6G d:d
 d
Z&dS );    )annotationsN)Mapping)TYPE_CHECKINGAnyProtocolTypeVar)
Collection)      )SelfArchiveInfoDirInfo	DirectUrlDirectUrlValidationErrorVcsInfoz	list[str]returnc                   C  s   t S N)__all__ r   r   8/tmp/pip-unpacked-wheel-xj899fla/packaging/direct_url.py__dir__   s    r   _Tc                   @  s    e Zd ZedddddZdS )_FromMappingProtocolMapping[str, Any]r   dr   c                 C  s   d S r   r   clsr   r   r   r   
_from_dict#   s    z_FromMappingProtocol._from_dictN)__name__
__module____qualname__classmethodr   r   r   r   r   r   "   s   r   _FromMappingProtocolT)boundzlist[tuple[str, Any]]zdict[str, Any])datar   c                 C  s   dd | D S )Nc                 S  s   i | ]\}}|d k	r||qS r   r   ).0keyvaluer   r   r   
<dictcomp>+   s       z&_json_dict_factory.<locals>.<dictcomp>r   )r&   r   r   r   _json_dict_factory*   s    r+   r   ztype[_T]strz	_T | None)r   expected_typer(   r   c                 C  sF   |  | }dkrdS t||sBtdt|j d|j d|d|S )zBGet a value from the dictionary and verify it's the expected type.NzUnexpected type z (expected )context)get
isinstancer   typer    r   r-   r(   r)   r   r   r   _get.   s    
r5   c                 C  s    t | || }dkrt||S )zKGet a required value from the dictionary and verify it's the expected type.N)r5   _DirectUrlRequiredKeyErrorr4   r   r   r   _get_required;   s    r7   ztype[_FromMappingProtocolT]z_FromMappingProtocolT | None)r   target_typer(   r   c              
   C  sZ   t | t| }dkrdS z||W S  tk
rT } zt||d|W 5 d}~X Y nX dS )zIGet a dictionary value from the dictionary and convert it to a dataclass.Nr/   )r5   r   r   	Exceptionr   )r   r8   r(   r)   er   r   r   _get_objectB   s    r;   z.^\$\{[A-Za-z0-9-_]+\}(:\$\{[A-Za-z0-9-_]+\})?$Collection[str])netlocsafe_user_passwordsr   c                 C  s:   d| kr| S |  dd\}}||kr(| S t|r6| S |S )N@   )split _PEP610_USER_PASS_ENV_VARS_REGEXmatch)r=   r>   	user_passZnetloc_no_user_passr   r   r   _strip_auth_from_netlocS   s    
rE   )urlr>   r   c                 C  s6   t j| }t|j|}t j|j||j|j|j	fS )zurl with user:password part removed unless it is formed with
    environment variables as specified in PEP 610, or it is a safe user:password
    such as `git`.
    )
urllibparseurlsplitrE   r=   
urlunsplitschemepathqueryfragment)rF   r>   
parsed_urlr=   r   r   r   
_strip_url^   s    rP   c                   @  sL   e Zd ZU dZdZded< ded< ddddd	d
ddZddddZdS )r   z2Raised when when input data is not spec-compliant.N
str | Noner0   r,   messager/   zstr | ExceptionNone)causer0   r   c                C  sR   t |tr>|jr.|r$| d|j n|j| _n|| _|j| _n|| _t|| _d S )N.)r2   r   r0   rR   r,   )selfrT   r0   r   r   r   __init__v   s    

z!DirectUrlValidationError.__init__r   c                 C  s   | j r| j d| j S | jS )Nz in )r0   rR   rV   r   r   r   __str__   s    z DirectUrlValidationError.__str__)r    r!   r"   __doc__r0   __annotations__rW   rY   r   r   r   r   r   p   s   
c                      s$   e Zd Zddd fddZ  ZS )r6   r,   rS   )r(   r   c                   s   t  jd|d d S )NzMissing required valuer/   )superrW   )rV   r(   	__class__r   r   rW      s    z#_DirectUrlRequiredKeyError.__init__)r    r!   r"   rW   __classcell__r   r   r]   r   r6      s   r6   TF)frozeninitc                   @  sX   e Zd ZU ded< ded< dZded< ddddddd	d
dZedddddZdS )r   r,   vcs	commit_idNrQ   requested_revision)rd   rS   )rb   rc   rd   r   c                C  s.   t | d| t | d| t | d| d S )Nrb   rc   rd   object__setattr__)rV   rb   rc   rd   r   r   r   rW      s    zVcsInfo.__init__r   r   r   c                 C  s&   | t |tdt|tdt |tddS )Nrb   rd   rc   )rb   rd   rc   )r7   r,   r5   r   r   r   r   r      s
    


zVcsInfo._from_dict)r    r!   r"   r[   rd   rW   r#   r   r   r   r   r   r      s   
c                   @  sD   e Zd ZU dZded< dddddddZed	d
dddZdS )r   NzMapping[str, str] | Nonehashesrh   rS   )rh   r   c                C  s   t | d| d S )Nrh   re   )rV   rh   r   r   r   rW      s    zArchiveInfo.__init__r   r   r   c                 C  s   t |td}|d k	r6tdd | D s6tdddt |td}|d k	rd|kr^tddd|dd	\}}|d kr||i}n<||krtd
|ddd|| |krtd
|ddd| |dS )Nrh   c                 s  s   | ]}t |tV  qd S r   )r2   r,   )r'   hr   r   r   	<genexpr>   s     z)ArchiveInfo._from_dict.<locals>.<genexpr>zHash values must be stringsr/   hash=z3Invalid hash format (expected '<algorithm>=<hash>')r@   z
Algorithm z2 used in hash field is not present in hashes fieldz7 used in hash field has different value in hashes fieldri   )r5   r   allvaluesr   r,   rA   )r   r   rh   Zlegacy_hashhash_algorithm
hash_valuer   r   r   r      s6     


zArchiveInfo._from_dict)r    r!   r"   rh   r[   rW   r#   r   r   r   r   r   r      s
   
c                   @  sD   e Zd ZU dZded< dddddddZed	d
dddZdS )r   Nzbool | Noneeditablerr   rS   )rr   r   c                C  s   t | d| d S )Nrr   re   )rV   rr   r   r   r   rW      s    zDirInfo.__init__r   r   r   c                 C  s   | t |tddS )Nrr   rs   )r5   boolr   r   r   r   r      s    
zDirInfo._from_dict)r    r!   r"   rr   r[   rW   r#   r   r   r   r   r   r      s
   
c                   @  s   e Zd ZU dZded< dZded< dZded< dZd	ed
< dZded< ddddddddd	dddddZ	e
dddddZe
dddddZdddddddddd d!Zdd"d#d$ZdS )%r   z"A class representing a direct URL.r,   rF   NzArchiveInfo | Nonearchive_infozVcsInfo | Nonevcs_infozDirInfo | Nonedir_inforQ   subdirectory)ru   rv   rw   rx   rS   )rF   ru   rv   rw   rx   r   c                C  sJ   t | d| t | d| t | d| t | d| t | d| d S )NrF   ru   rv   rw   rx   re   )rV   rF   ru   rv   rw   rx   r   r   r   rW      s
    	zDirectUrl.__init__r   r   r   c              	   C  s   | t |tdt|tdt|tdt|tdt|tdd}t|jt|j	 t|j
 dkrdtd|j
d k	r|jd	std
dd|S )NrF   ru   rv   rw   rx   )rF   ru   rv   rw   rx   r@   z?Exactly one of vcs_info, archive_info, dir_info must be presentzfile://z3URL scheme must be file:// when dir_info is presentr/   )r7   r,   r;   r   r   r   r5   rt   rv   ru   rw   r   rF   
startswith)r   r   Z
direct_urlr   r   r   r     s.    




zDirectUrl._from_dictc                C  s
   |  |S )z@Create and validate a DirectUrl instance from a JSON dictionary.)r   r   r   r   r   	from_dict   s    zDirectUrl.from_dictFT)git)generate_legacy_hashstrip_user_passwordr>   rt   r<   )r|   r}   r>   r   c                C  sf   t j| td}|rN| jrN| jjrNtt| jj \}}| d| |d d< |rbt| j	||d< |S )a  Convert the DirectUrl instance to a JSON dictionary.

        :param generate_legacy_hash: If True, include a legacy `hash` field in
            `archive_info` for backward compatibility with tools that don't
            support the `hashes` field.
        :param strip_user_password: If True, strip user:password from the URL
            unless it is formed with environment variables as specified in PEP
            610, or it is a safe user:password such as `git`.
        :param safe_user_passwords: A collection of user:password strings that
            should not be stripped from the URL even if `strip_user_password` is
            True.
        )Zdict_factoryrm   ru   rl   rF   )
dataclassesZasdictr+   ru   rh   nextiteritemsrP   rF   )rV   r|   r}   r>   resrp   rq   r   r   r   to_dict%  s    zDirectUrl.to_dictr   c                 C  s   |  |   dS )zValidate the DirectUrl instance against the specification.

        Raises :class:`DirectUrlValidationError` if invalid.
        N)rz   r   rX   r   r   r   validate@  s    zDirectUrl.validate)r    r!   r"   rZ   r[   ru   rv   rw   rx   rW   r#   r   rz   r   r   r   r   r   r   r      s(   
)'
__future__r   r~   reurllib.parserG   collections.abcr   typingr   r   r   r   sysr   version_infor   Ztyping_extensionsr   r   r   r   r$   r+   r5   r7   r;   compilerB   rE   rP   r9   r   r6   Z	dataclassr   r   r   r   r   r   r   r   <module>   sP   
	-