o
    ^K                  
   @   s*  d Z eZg dZddlZze W n ey   eZY nw i dddddd	d
ddddddddddddddddddddddd d!d"d#d$i d%d&d'd(d)d*d+d,d-d.d/d0d1d2d3d2d4d5d6d7d8d9d:d;d<d=d>d=d?d@dAd@dBdCd@ddDdEdDdDdFdDdGZdHZ	dIZ
dJZdKZdLe
eef ZdMZdNZdOZdPe ZdQZdReeeeef ZdSeeeeef ZdTZdUZdVe	eeef ZdWeeef ZeeejZeeejZdXdY ZdZd[ Zd\d] ZG d^d_ d_e Z!G d`da daZ"dbdcddi Z#ee#ejej$B Z%edeZ&dfdg Z'dS )hz/Functions for working with generic syntax URIs.)URIInvalidURIErrorfind_uris_in_textpossible_uri_remergeremove_dot_segments    Nacap674dav80dict2628dns53ftp21go1096gopher70h3231720httphttps443imap143ipp631z	iris.beep702ldap389mtqp1038mupdate3905nfs2049nntp119pop110rtsp554sip5060sips5061snmp161z	soap.beep605z
soap.beepstelnet23tftp69tip3372vemmi575zxmlrpc.beep602zxmlrpc.beepszz39.50r210zz39.50sprospero15252266673690)waisbzr+httpbzr+sshircsftpsshsvnsvn+sshz(?P<scheme>[a-z][-a-z0-9+.]*)z9(?P<userinfo>(?:[-a-z0-9._~!$&\'()*+,;=:]|%[0-9a-f]{2})*)zs(?P<host>[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|(?:[-a-z0-9._~!$&\'()*+,;=]|%[0-9a-f]{2})*|\[[0-9a-z:.]+\])z(?P<port>[0-9]*)z!(?P<authority>(?:%s@)?%s(?::%s)?)z2(?:/(?:[-a-z0-9._~!$&\'()*+,;=:@]|%[0-9a-f]{2})*)*z](?:[-a-z0-9._~!$&\'()*+,;=@]|%[0-9a-f]{2})+(?:/(?:[-a-z0-9._~!$&\'()*+,;=:@]|%[0-9a-f]{2})*)*z^(?:[-a-z0-9._~!$&\'()*+,;=:@]|%[0-9a-f]{2})+(?:/(?:[-a-z0-9._~!$&\'()*+,;=:@]|%[0-9a-f]{2})*)*z/(?:%s)? z(?P<hierpart>//%s%s|%s|%s|%s)z!(?P<relativepart>//%s%s|%s|%s|%s)z=(?P<query>(?:[-a-z0-9._~!$&\'()*+,;=:@/?\[\]]|%[0-9a-f]{2})*)z<(?P<fragment>(?:[-a-z0-9._~!$&\'()*+,;=:@/?]|%[0-9a-f]{2})*)z%s:%s(?:\?%s)?(?:#%s)?$z%s(?:\?%s)?(?:#%s)?$c                 C   s2   |r
| dkr
d| S |  d}| d|d  | S )a/  Merge two URI path components into a single path component.

    Follows rules specified in Section 5.2.3 of RFC 3986.

    The algorithm in the RFC treats the empty basepath edge case
    differently for URIs with and without an authority section, which
    is why the third argument is necessary.
    rL   /N   )rfind)basepathrelpathhas_authorityslash rT   //usr/lib/python3/dist-packages/lazr/uri/_uri.pyr      s   	
r   c                 C   s   g }| rz|  dr| dd } nh|  dr| dd } n\|  ds%| dkr.d| dd  } nJ|  d	s7| d
krId| dd  } t|dkrH|d= n/| dv rPd} n(|  dr\| dd}n| d}|dk rit| }|| d|  | |d } | sd|S )zqRemove '.' and '..' segments from a URI path.

    Follows the rules specified in Section 5.2.4 of RFC 3986.
    z../   Nz./   z/./z/.rM   z/../z/..   r   ).z..rL   rN   )
startswithlenfindappendjoin)pathoutputrS   rT   rT   rU   r      s0   




r   c              	   C   s   |  d}d}t|D ]=\}}|dkrqzt|dd d}W n	 ty(   Y qw t||v r<t||dd  ||< qd||dd f ||< qd|S )	zReturn a version of 's' where no unreserved characters are encoded.

    Unreserved characters are defined in Section 2.3 of RFC 3986.

    Percent encoded sequences are normalised to upper case.
    %zBABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~r   NrW      z%%%02X%srL   )split	enumerateint
ValueErrorchrr_   )stringresult
unreservedindexitemchrT   rT   rU   normalise_unreserved   s   

ro   c                   @   s   e Zd ZdZdS )r   zInvalid URIN)__name__
__module____qualname____doc__rT   rT   rT   rU   r      s    r   c                   @   s   e Zd ZdZ		d#ddZdd Zedd Zed	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" ZdS )$r   zA class that represents a URI.

    This class can represent arbitrary URIs that conform to the
    generic syntax described in RFC 3986.
    Nc	                 C   s  |dur |du r |du r |du r |du r |du r |du r |du s(|du s(J d|durt |trDz|d W n tyC   tdw t|}	|	du rStd| |	d| _|	d| _	|	d| _
|	d	| _|	d
}
|	d}|du r}|
| _n|
dt| d | _|	d| _|	d| _n5|du rtd|du r|dus|durtd|du rtd|| _|| _	|| _
|| _|| _|| _|| _|   | jdv r| j
std| j dS dS )zCreate a URI instance.

        Can be called with either a string URI or the component parts
        of the URI as keyword arguments.

        In either case, all arguments are expected to be appropriately
        URI encoded.
        NzeURI() must be called with a single string argument or with URI components given as keyword arguments.ASCIIz%URIs must consist of ASCII charactersz"%s" is not a valid URIschemeuserinfohostporthierpart	authorityrW   queryfragmentzURIs must have a schemez*host must be given if userinfo or port arezURIs must have a path)r   r   r   r   r5   r   mmsr,   rJ   rK   bzrrE   rF   z%s URIs must have a host name)
isinstanceunicodeencodeUnicodeEncodeErrorr   uri_patmatchgroupru   rv   rw   rx   r`   r\   r{   r|   
_normalise)selfuriru   rv   rw   rx   r`   r{   r|   r   ry   rz   rT   rT   rU   __init__   sz   





zURI.__init__c                 C   s   | j  | _ | jdurt| j| _| jdurt| j | _| jdkr'd| _n| jdur8| jt| j kr8d| _| jdurE| jdkrEd| _tt	| j| _| j
durXt| j
| _
| jduret| j| _dS dS )z(Perform normalisation of URI components.NrL   rM   )ru   lowerrv   ro   rw   rx   _default_portgetr`   r   r{   r|   r   rT   rT   rU   r     s$   





zURI._normalisec                 C   sH   | j du rdS | j }| jdurd| j|f }| jdur"d|| jf }|S )zThe authority part of the URINz%s@%s%s:%s)rw   rv   rx   r   rz   rT   rT   rU   rz   3  s   


zURI.authorityc                 C   s"   | j }|du r
| jS d|| jf S )z The hierarchical part of the URINz//%s%s)rz   r`   r   rT   rT   rU   	hier_part?  s   zURI.hier_partc                 C   sD   d| j | jf }| jd ur|d| j 7 }| jd ur |d| j 7 }|S )Nr   z?%sz#%s)ru   r   r{   r|   )r   r   rT   rT   rU   __str__H  s   

zURI.__str__c                 C   s   d| j jt| f S )Nz%s(%r))	__class__rp   strr   rT   rT   rU   __repr__P  s   zURI.__repr__c                 C   sL   t || jr$| j|jko#| j|jko#| j|jko#| j|jko#| j|jkS tS N)r   r   ru   rz   r`   r{   r|   NotImplemented)r   otherrT   rT   rU   __eq__S  s   



z
URI.__eq__c                 C   s   |  |}|tkrtS | S r   )r   r   )r   r   equalrT   rT   rU   __ne__]  s   
z
URI.__ne__c                 C   s   t | j| j| j| j| jfS r   )hashru   rz   r`   r{   r|   r   rT   rT   rU   __hash__d  s   zURI.__hash__c              	   K   sF   |s| S t | j| j| j| j| j| j| jd}|| | j	di |S )z;Replace one or more parts of the URI, returning the result.)ru   rv   rw   rx   r`   r{   r|   NrT   )
r   ru   rv   rw   rx   r`   r{   r|   updater   )r   parts	basepartsrT   rT   rU   replaceh  s   
zURI.replacec                 C   s~  z|  |W S  ty   Y nw t|}|du rtdt| jd}|d}|durZ|d|d< |d|d< |d|d< t|dd	t| d |d
< |d|d< nV|d}|d}|dkr|| j	|d
< |durv||d< n+| j
|d< n%|drt||d
< nt| j	|| jdud|d
< t|d
 |d
< ||d< | j|d< | j|d< | j|d< |d|d< | j di |S )zResolve the given URI reference relative to this URI.

        Uses the rules from Section 5.2 of RFC 3986 to resolve the new
        URI.
        NzInvalid relative reference)ru   rz   rv   rw   rx   relativepartrW   r`   r{   rL   rM   )rR   r|   rT   )r   r   relative_ref_patr   r   ru   r   r   r\   r`   r{   r[   r   rw   rv   rx   )r   	referencer   r   rz   r`   r{   rT   rT   rU   resolvew  sJ   










zURI.resolvec                 C   s   | drJ |  |S )zAppend the given path to this URI.

        The path must not start with a slash, but a slash is added to
        base URI (before appending the path), in case it doesn't end
        with a slash.
        rM   )r[   ensureSlashr   )r   r`   rT   rT   rU   r^     s   z
URI.appendc                 C   sf   | j |j ks| j|jkrdS | j|jkrdS | j}|ds"|d7 }|j}|ds.|d7 }||S )z9Returns True if the URI 'other' is contained by this one.FTrM   )ru   rz   r`   endswithr[   )r   r   rP   	otherpathrT   rT   rU   contains  s   



zURI.containsc                 C   s<   t |dkrdS | jd}|d}|t | d |kS )z@Return True if the given domain name a parent of the URL's host.r   TrZ   N)r\   rw   rd   )r   domainour_segmentsdomain_segmentsrT   rT   rU   underDomain  s
   
zURI.underDomainc                 C   s"   | j dr| S | j| j d dS )z:Return a URI with the path normalised to end with a slash.rM   r`   )r`   r   r   r   rT   rT   rU   r     s   zURI.ensureSlashc                 C   s$   | j dr| j| j ddS | S )z>Return a URI with the path normalised to not end with a slash.rM   r   )r`   r   r   rstripr   rT   rT   rU   ensureNoSlash  s   zURI.ensureNoSlash)NNNNNNNN)rp   rq   rr   rs   r   r   propertyrz   r   r   r   r   r   r   r   r   r^   r   r   r   r   rT   rT   rT   rU   r      s,    
A


2
r   a  
\b
(?:about|gopher|http|https|sftp|news|ftp|mailto|file|irc|jabber|xmpp)
:
(?:
  (?:
    # "//" authority path-abempty
    //
    (?: # userinfo
      [%(unreserved)s:]*
      @
    )?
    (?: # host
      \d+\.\d+\.\d+\.\d+ |
      [%(unreserved)s]*
    )
    (?: # port
      : \d*
    )?
    (?: / [%(unreserved)s:@]* )*
  ) | (?:
    # path-absolute
    /
    (?: [%(unreserved)s:@]+
        (?: / [%(unreserved)s:@]* )* )?
  ) | (?:
    # path-rootless
    [%(unreserved)s@]
    [%(unreserved)s:@]*
    (?: / [%(unreserved)s:@]* )*
  )
)
(?: # query
  \?
  [%(unreserved)s:@/\?\[\]]*
)?
(?: # fragment
  \#
  [%(unreserved)s:@/\?]*
)?
rk   z-a-zA-Z0-9._~%!$&'()*+,;=z([,.?:);>]+)$c              	   c   sP    t | D ]}| }td|}zt|}W n	 ty!   Y qw |V  qdS )z8Scan a block of text for URIs, and yield the ones found.rL   N)possible_uri_patfinditerr   uri_trailers_patsubr   r   )textr   
uri_stringr   rT   rT   rU   r   A  s   r   )(rs   type__metaclass____all__rer   	NameErrorr   r   	scheme_reuserinfo_rehost_report_reauthority_repath_abempty_repath_noscheme_repath_rootless_repath_absolute_repath_empty_rehier_part_rerelative_part_requery_refragment_reuri_rerelative_ref_recompile
IGNORECASEr   r   r   r   ro   	Exceptionr   r   r   VERBOSEr   r   r   rT   rT   rT   rU   <module>   s  		
 !"%&4  A(*
