U
    P|jÿ   ã                   @   sd   d Z ddlmZ ddlmZmZmZ ddl	m
Z d dd„ edd	ƒD ƒ¡Zd
d„ ZG dd„ dƒZdS )z(
L{URLPath}, a representation of a URL.
é    )Úcast)ÚquoteÚunquoteÚ
urlunsplit)ÚURLó    c                 C   s   g | ]}t |ƒ d ¡‘qS )Úascii)ÚchrÚencode)Ú.0Úx© r   ú:/tmp/pip-unpacked-wheel-audpp6lv/twisted/python/urlpath.pyÚ
<listcomp>   s     r   é   é€   c                    s"   d|  ‰ t ‡ fdd„‡ fdd„ƒS )a  
    Attriute declaration to preserve mutability on L{URLPath}.

    @param name: a public attribute name
    @type name: native L{str}

    @return: a descriptor which retrieves the private version of the attribute
        on get and calls rerealize on set.
    Ú_c                    s
   t | ˆ ƒS ©N)Úgetattr©Úself©ZprivateNamer   r   Ú<lambda>   r   z"_rereconstituter.<locals>.<lambda>c                    s(   t | ˆ t|tƒr|n| d¡ƒp&|  ¡ S )NÚcharmap)ÚsetattrÚ
isinstanceÚbytesr
   Ú_reconstitute)r   Úvaluer   r   r   r      s    ý)Úproperty)Únamer   r   r   Ú_rereconstituter   s
    


þr!   c                   @   sØ   e Zd ZdZd+dd„Zdd„ ZedƒZed	ƒZed
ƒZ	edƒZ
edƒZedd„ ƒZd,dd„Zedd„ ƒZedd„ ƒZedd„ ƒZdd„ Zd-dd„Zd.dd„Zd/dd „Zd0d!d"„Zd#d$„ Zed%œd&d'„Zed%œd(d)„Zd*S )1ÚURLPathaØ  
    A representation of a URL.

    @ivar scheme: The scheme of the URL (e.g. 'http').
    @type scheme: L{bytes}

    @ivar netloc: The network location ("host").
    @type netloc: L{bytes}

    @ivar path: The path on the network location.
    @type path: L{bytes}

    @ivar query: The query argument (the portion after ?  in the URL).
    @type query: L{bytes}

    @ivar fragment: The page fragment (the portion after # in the URL).
    @type fragment: L{bytes}
    r   ó	   localhostc                 C   s2   |pd| _ || _|pd| _|| _|| _|  ¡  d S )Ns   httpó   /)Ú_schemeÚ_netlocÚ_pathÚ_queryÚ	_fragmentr   )r   ÚschemeÚnetlocÚpathÚqueryÚfragmentr   r   r   Ú__init__=   s    

zURLPath.__init__c                 C   s@   t t| j| j| j| j| jfƒtd�}t 	| 
d¡ d¡¡| _dS )zM
        Reconstitute this L{URLPath} from all its given attributes.
        ©Úsafer   N)Úurlquoter   r%   r&   r'   r(   r)   Ú	_allasciiÚ_URLÚfromTextr
   ÚdecodeÚ_url)r   Zurltextr   r   r   r   G   s    ÿüzURLPath._reconstituter*   r+   r,   r-   r.   c                 C   sœ   |   | ¡}|j|jpdgd�|_|jj d¡|_|j ¡  d¡|_t	|jjdd� 
¡  ¡  d¡|_t	|jjd� 
¡  ¡  d¡dd… |_|jj d¡|_|S )	a  
        Reconstruct all the public instance variables of this L{URLPath} from
        its underlying L{_URL}.

        @param urlInstance: the object to base this L{URLPath} on.
        @type urlInstance: L{_URL}

        @return: a new L{URLPath}
        Ú )r,   r   T)r,   Zrooted)r-   r   N)Ú__new__Úreplacer,   r7   r*   r
   r%   Ú	authorityr&   r4   ÚasURIÚasTextr'   r-   r(   r.   r)   )ÚclsZurlInstancer   r   r   r   Ú_fromURLY   s    
ÿ&zURLPath._fromURLFTc                    s8   | j j}dd„ ‰ |r ˆ fdd„‰ dg‡ fdd„|D ƒ S )a  
        Split this URL's path into its components.

        @param unquote: whether to remove %-encoding from the returned strings.

        @param copy: (ignored, do not use)

        @return: The components of C{self.path}
        @rtype: L{list} of L{bytes}
        c                 S   s
   |   d¡S )Nr   )r
   )r   r   r   r   r   {   r   z"URLPath.pathList.<locals>.<lambda>c                 S   s   |t | ƒƒS r   )Ú
urlunquote)r   Úmr   r   r   r   }   r   r   c                    s   g | ]}ˆ |ƒ‘qS r   r   )r   Úsegment©Zmapperr   r   r   ~   s     z$URLPath.pathList.<locals>.<listcomp>)r7   r,   )r   r   ÚcopyÚsegmentsr   rC   r   ÚpathListo   s
    zURLPath.pathListc                 C   s"   t |tƒstdƒ‚|  t |¡¡S )zý
        Make a L{URLPath} from a L{str} or L{unicode}.

        @param url: A L{str} representation of a URL.
        @type url: L{str} or L{unicode}.

        @return: a new L{URLPath} derived from the given string.
        @rtype: L{URLPath}
        z'url' must be a str)r   ÚstrÚ
ValueErrorr?   r4   r5   )ÚklassÚurlr   r   r   Ú
fromString€   s    
zURLPath.fromStringc                 C   s(   t |tƒstdƒ‚t|td�}|  |¡S )zþ
        Make a L{URLPath} from a L{bytes}.

        @param url: A L{bytes} representation of a URL.
        @type url: L{bytes}

        @return: a new L{URLPath} derived from the given L{bytes}.
        @rtype: L{URLPath}

        @since: 15.4
        z'url' must be bytesr0   )r   r   rH   r2   r3   rK   )rI   rJ   Úquotedr   r   r   Ú	fromBytes�   s    
zURLPath.fromBytesc                 C   s   |   | ¡ ¡S )a  
        Make a L{URLPath} from a L{twisted.web.http.Request}.

        @param request: A L{twisted.web.http.Request} to make the L{URLPath}
            from.

        @return: a new L{URLPath} derived from the given request.
        @rtype: L{URLPath}
        )rM   Z
prePathURL)rI   Úrequestr   r   r   ÚfromRequest¡   s    zURLPath.fromRequestc                 C   s    |   |jd|r| jjndd�¡S )aÞ  
        Return a modified copy of C{self} using C{newURL}, keeping the query
        string if C{keepQuery} is C{True}.

        @param newURL: a L{URL} to derive a new L{URLPath} from
        @type newURL: L{URL}

        @param keepQuery: if C{True}, preserve the query parameters from
            C{self} on the new L{URLPath}; if C{False}, give the new L{URLPath}
            no query parameters.
        @type keepQuery: L{bool}

        @return: a new L{URLPath}
        r8   r   )r.   r-   )r?   r:   r7   r-   )r   ZnewURLÚ	keepQueryr   r   r   Ú_mod®   s    ÿzURLPath._modc                 C   s   |   | j | d¡¡|¡S )a…  
        Get the sibling of the current L{URLPath}.  A sibling is a file which
        is in the same directory as the current file.

        @param path: The path of the sibling.
        @type path: L{bytes}

        @param keepQuery: Whether to keep the query parameters on the returned
            L{URLPath}.
        @type keepQuery: L{bool}

        @return: a new L{URLPath}
        r   )rQ   r7   Úsiblingr6   ©r   r,   rP   r   r   r   rR   Á   s    zURLPath.siblingc                 C   s   |   | j | d¡¡|¡S )a)  
        Get the child of this L{URLPath}.

        @param path: The path of the child.
        @type path: L{bytes}

        @param keepQuery: Whether to keep the query parameters on the returned
            L{URLPath}.
        @type keepQuery: L{bool}

        @return: a new L{URLPath}
        r   )rQ   r7   Úchildr6   rS   r   r   r   rT   Ñ   s    zURLPath.childc                 C   s   |   | j d¡|¡S )zê
        Get the parent directory of this L{URLPath}.

        @param keepQuery: Whether to keep the query parameters on the returned
            L{URLPath}.
        @type keepQuery: L{bool}

        @return: a new L{URLPath}
        z..©rQ   r7   Úclick©r   rP   r   r   r   Úparentà   s    
zURLPath.parentc                 C   s   |   | j d¡|¡S )zë
        Get the current directory of this L{URLPath}.

        @param keepQuery: Whether to keep the query parameters on the returned
            L{URLPath}.
        @type keepQuery: L{bool}

        @return: a new L{URLPath}
        Ú.rU   rW   r   r   r   Úhereì   s    
zURLPath.herec                 C   s   |   | j | d¡¡¡S )a7  
        Return a path which is the URL where a browser would presumably take
        you if you clicked on a link with an HREF as given.

        @param st: A relative URL, to be interpreted relative to C{self} as the
            base URL.
        @type st: L{bytes}

        @return: a new L{URLPath}
        r   )r?   r7   rV   r6   )r   Ústr   r   r   rV   ø   s    zURLPath.click)Úreturnc                 C   s   t t| j ¡  ¡ ƒS )z=
        The L{str} of a L{URLPath} is its URL text.
        )r   rG   r7   r<   r=   r   r   r   r   Ú__str__  s    zURLPath.__str__c                 C   s   d  | j| j| j| j| j¡S )z{
        The L{repr} of a L{URLPath} is an eval-able expression which will
        construct a similar L{URLPath}.
        zGURLPath(scheme={!r}, netloc={!r}, path={!r}, query={!r}, fragment={!r}))Úformatr*   r+   r,   r-   r.   r   r   r   r   Ú__repr__  s    ûzURLPath.__repr__N)r   r#   r   r   r   )FT)F)F)F)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r/   r   r!   r*   r+   r,   r-   r.   Úclassmethodr?   rF   rK   rM   rO   rQ   rR   rT   rX   rZ   rV   rG   r]   r_   r   r   r   r   r"   )   s<            ÿ










r"   N)rc   Útypingr   Úurllib.parser   r2   r   r@   r   Z	hyperlinkr   r4   ÚjoinÚranger3   r!   r"   r   r   r   r   Ú<module>   s   