U
    Zޫ^                     @   s   d dl Z d dlZd dlZd dlmZ d dlmZ d dlm	Z	m
Z
mZ d dlmZ d dlmZ d dlmZmZ erd dlmZmZmZmZ d d	lmZ d d
lmZ G dd deZdS )    N)parse)WHEEL_EXTENSION)redact_auth_from_urlsplit_auth_from_netlocsplitext)KeyBasedCompareMixin)MYPY_CHECK_RUNNING)path_to_urlurl_to_path)OptionalTextTupleUnion)HTMLPage)Hashesc                       s@  e Zd ZdZd7 fdd	Z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edd Zdd Zedd Zedd ZedZedd ZedZed d! Zed"Zed#d$ Zed%d& Zed'd( Zed)d* Zd+d, Zed-d. Zed/d0 Zed1d2 Zed3d4 Z d5d6 Z!  Z"S )8Linkz?Represents a parsed link from a Package Index's simple URL
    NTc                    s\   | drt|}t|| _|| _|| _|r2|nd| _|| _t	t
| j|t
d || _dS )a  
        :param url: url of the resource pointed to (href of the link)
        :param comes_from: instance of HTMLPage where the link was found,
            or string.
        :param requires_python: String containing the `Requires-Python`
            metadata field, specified in PEP 345. This may be specified by
            a data-requires-python attribute in the HTML link tag, as
            described in PEP 503.
        :param yanked_reason: the reason the file has been yanked, if the
            file has been yanked, or None if the file hasn't been yanked.
            This is the value of the "data-yanked" attribute, if present, in
            a simple repository HTML link. If the file has been yanked but
            no reason was provided, this should be the empty string. See
            PEP 592 for more information and the specification.
        :param cache_link_parsing: A flag that is used elsewhere to determine
                                   whether resources retrieved from this link
                                   should be cached. PyPI index urls should
                                   generally have this set to False, for
                                   example.
        z\\N)keydefining_class)
startswithr	   urllib_parseurlsplit_parsed_url_url
comes_fromrequires_pythonyanked_reasonsuperr   __init__cache_link_parsing)selfurlr   r   r   r   	__class__ =/tmp/pip-unpacked-wheel-huh_82lg/pip/_internal/models/link.pyr      s    
zLink.__init__c                 C   sF   | j rd| j }nd}| jr4dt| j| j|S tt| jS d S )Nz (requires-python:{}) z{} (from {}){})r   formatr   r   r   str)r   rpr#   r#   r$   __str__J   s      zLink.__str__c                 C   s
   d | S )Nz	<Link {}>)r&   r   r#   r#   r$   __repr__V   s    zLink.__repr__c                 C   s   | j S N)r   r*   r#   r#   r$   r    Z   s    zLink.urlc                 C   sP   | j d}t|}|s,t| j\}}|S t|}|sLtdj	f t
 |S )N/z&URL {self._url!r} produced no filename)pathrstrip	posixpathbasenamer   netlocr   unquoteAssertionErrorr&   locals)r   r.   namer2   	user_passr#   r#   r$   filename_   s    

zLink.filenamec                 C   s
   t | jS r,   )r
   r    r*   r#   r#   r$   	file_patho   s    zLink.file_pathc                 C   s   | j jS r,   )r   schemer*   r#   r#   r$   r:   t   s    zLink.schemec                 C   s   | j jS )z4
        This can contain auth information.
        )r   r2   r*   r#   r#   r$   r2   y   s    zLink.netlocc                 C   s   t | jjS r,   )r   r3   r   r.   r*   r#   r#   r$   r.      s    z	Link.pathc                 C   s   t t| jdS )Nr-   )r   r0   r1   r.   r/   r*   r#   r#   r$   r      s    zLink.splitextc                 C   s   |   d S N   )r   r*   r#   r#   r$   ext   s    zLink.extc                 C   s$   | j \}}}}}t||||d fS r,   )r   r   
urlunsplit)r   r:   r2   r.   queryfragmentr#   r#   r$   url_without_fragment   s    zLink.url_without_fragmentz[#&]egg=([^&]*)c                 C   s    | j | j}|sd S |dS r;   )_egg_fragment_researchr   groupr   matchr#   r#   r$   egg_fragment   s    zLink.egg_fragmentz[#&]subdirectory=([^&]*)c                 C   s    | j | j}|sd S |dS r;   )_subdirectory_fragment_rerC   r   rD   rE   r#   r#   r$   subdirectory_fragment   s    zLink.subdirectory_fragmentz2(sha1|sha224|sha384|sha256|sha512|md5)=([a-f0-9]+)c                 C   s    | j | j}|r|dS d S )N   _hash_rerC   r   rD   rE   r#   r#   r$   hash   s    
z	Link.hashc                 C   s    | j | j}|r|dS d S r;   rK   rE   r#   r#   r$   	hash_name   s    
zLink.hash_namec                 C   s$   t | jddd ddd S )N#r<   r   ?)r0   r1   r   splitr*   r#   r#   r$   show_url   s    zLink.show_urlc                 C   s
   | j dkS )Nfile)r:   r*   r#   r#   r$   is_file   s    zLink.is_filec                 C   s   | j otj| jS r,   )rT   osr.   isdirr9   r*   r#   r#   r$   is_existing_dir   s    zLink.is_existing_dirc                 C   s
   | j tkS r,   )r=   r   r*   r#   r#   r$   is_wheel   s    zLink.is_wheelc                 C   s   ddl m} | j|jkS )Nr   )vcs)pip._internal.vcsrY   r:   all_schemes)r   rY   r#   r#   r$   is_vcs   s    zLink.is_vcsc                 C   s
   | j d k	S r,   )r   r*   r#   r#   r$   	is_yanked   s    zLink.is_yankedc                 C   s
   | j d k	S r,   )rN   r*   r#   r#   r$   has_hash   s    zLink.has_hashc                 C   s@   |dks| j sdS | jdk	s t| jdk	s.t|j| j| jdS )zG
        Return True if the link has a hash and it is allowed.
        NF)
hex_digest)r^   rN   r4   rM   is_hash_allowed)r   hashesr#   r#   r$   r`      s
    zLink.is_hash_allowed)NNNT)#__name__
__module____qualname____doc__r   r)   r+   propertyr    r8   r9   r:   r2   r.   r   r=   rA   recompilerB   rG   rH   rI   rL   rM   rN   rR   rT   rW   rX   r\   r]   r^   r`   __classcell__r#   r#   r!   r$   r      sh       /



















r   )rU   r0   rg   pip._vendor.six.moves.urllibr   r   pip._internal.utils.filetypesr   pip._internal.utils.miscr   r   r   pip._internal.utils.modelsr   pip._internal.utils.typingr   pip._internal.utils.urlsr	   r
   typingr   r   r   r   pip._internal.index.collectorr   pip._internal.utils.hashesr   r   r#   r#   r#   r$   <module>   s   