U
    
W[|#                     @   sr   d Z ddlmZmZ ddlmZmZ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eZdS )z(
L{URLPath}, a representation of a URL.
    )divisionabsolute_import)nativeStringunicodeurllib_parse
urlunquoteurlquote)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&   t 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   )r   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%d& Zd'd( Zd)S )0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   sB   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.
        Zsafer   N)r   urlparseZ
urlunsplitr(   r)   r*   r+   r,   	_allascii_URLfromTextr   decode_url)r   Zurltextr   r   r   r    E   s      zURLPath._reconstituter-   r.   r/   r0   r1   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)r0   r   N)__new__replacer/   r9   r-   r   r(   Z	authorityr)   r6   asURIasTextr*   r0   r+   r1   r,   )clsZurlInstancer   r   r   r   _fromURLW   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   )r   )r   mr   r   r   r   }   r
   r
   c                    s   g | ]} |qS r   r   )r   ZsegmentZmapperr   r   r   ~   s     z$URLPath.pathList.<locals>.<listcomp>)r9   r/   )r   ZunquotecopyZsegmentsr   rB   r   pathListo   s
    zURLPath.pathListc                 C   s:   t |ttfstdt |tr*| |S | 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 or unicode)	r   strr   
ValueErrorr   	fromBytesr@   r6   r7   )klassurlr   r   r   
fromString   s
    

zURLPath.fromStringc                 C   s<   t |tstdt|td}t |tr2|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 bytesr3   r   )r   r   rF   r   r5   r8   rJ   )rH   rI   Zquotedr   r   r   rG      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}
        )rG   Z
prePathURL)rH   Z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}
        r:   r   )r1   r0   )r@   r<   r9   r0   )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   )rM   r9   siblingr8   r   r/   rL   r   r   r   rN      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   )rM   r9   childr8   rO   r   r   r   rP      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..rM   r9   clickr   rL   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}
        .rQ   rS   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@   r9   rR   r8   )r   str   r   r   rR   
  s    zURLPath.clickc                 C   s   t | j  S )z=
        The L{str} of a L{URLPath} is its URL text.
        )r   r9   r=   r>   r   r   r   r   __str__  s    zURLPath.__str__c                 C   s   d| j | j| j| j| jf S )z{
        The L{repr} of a L{URLPath} is an eval-able expression which will
        construct a similar L{URLPath}.
        z=URLPath(scheme=%r, netloc=%r, path=%r, query=%r, fragment=%r))r-   r.   r/   r0   r1   r   r   r   r   __repr__  s
    zURLPath.__repr__N)r
   r&   r
   r
   r
   )FT)F)F)F)F)__name__
__module____qualname____doc__r2   r    r$   r-   r.   r/   r0   r1   classmethodr@   rD   rJ   rG   rK   rM   rN   rP   rT   rV   rR   rX   rY   r   r   r   r   r%   (   s8       










r%   N)r]   Z
__future__r   r   Ztwisted.python.compatr   r   r   r4   r   r   Z	hyperlinkr	   r6   joinranger5   r$   objectr%   r   r   r   r   <module>   s   