o
    b                      @   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   8/usr/lib/python3/dist-packages/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privateNamer   r   <lambda>      
 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d-d!d"Zd#d$ Zd%efd&d'Zd%efd(d)Zd*S ).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   urltextr   r   r   r   G   s   zURLPath._reconstituter,   r-   r.   r/   r0   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.   rooted)r/   r   N)__new__replacer.   r9   r,   r
   r'   	authorityr(   r6   asURIasTextr)   r/   r*   r0   r+   )cls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   }   s    r   c                    s   g | ]} |qS r   r   )r   segmentmapperr   r   r   ~   s    z$URLPath.pathList.<locals>.<listcomp>)r9   r.   )r   r   copysegmentsr   rH   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
ValueErrorrD   r6   r7   )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 bytesr2   )r   r   rN   r4   r5   rQ   )rO   rP   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}
        )rS   
prePathURL)rO   requestr   r   r   fromRequest   s   zURLPath.fromRequestc                 C   s&   |  |jd|r| jjdS 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}
        r;   r   )r0   r/   )rD   r>   r9   r/   )r   newURL	keepQueryr   r   r   _mod   s
   zURLPath._modc                 C      |  | 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   )rY   r9   siblingr8   r   r.   rX   r   r   r   r[      s   zURLPath.siblingc                 C   rZ   )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   )rY   r9   childr8   r\   r   r   r   r]      s   zURLPath.childc                 C      |  | 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..rY   r9   clickr   rX   r   r   r   parent      
zURLPath.parentc                 C   r^   )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}
        .r_   ra   r   r   r   here   rc   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   )rD   r9   r`   r8   )r   str   r   r   r`      s   zURLPath.clickreturnc                 C   s   t t| j  S )z=
        The L{str} of a L{URLPath} is its URL text.
        )r   rM   r9   r@   rA   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/   r0   r   r   r   r   __repr__  s   zURLPath.__repr__N)r   r%   r   r   r   )FT)F)__name__
__module____qualname____doc__r1   r   r#   r,   r-   r.   r/   r0   classmethodrD   rL   rQ   rS   rV   rY   r[   r]   rb   re   r`   rM   rh   rj   r   r   r   r   r$   )   s6    










r$   N)rn   typingr   urllib.parser   r4   r   rE   r   	hyperlinkr   r6   joinranger5   r#   r$   r   r   r   r   <module>   s   