o
    2wi                     @   sl   d dl mZ d dlmZ dddZG dd dZdd	 Zd
d ZG dd de	Z
edZdd Zdd ZdS )    )Mapping)parseP   i  )httphttpsc                   @   s   e Zd ZdZdZd!ddZdd Zd	d
 Z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dd Zedd Zedd  ZdS )"URLa  Immutable URL class

    You build it from an url string.
    >>> url = URL('http://python.org/urls?param=asdfa')
    >>> url
    URL(http://python.org/urls?param=asdfa)

    You cast it to a tuple, it returns the same tuple as `urlparse.urlsplit`.
    >>> tuple(url)
    ('http', 'python.org', '/urls', 'param=asdfa', '')

    You can cast it as a string.
    >>> str(url)
    'http://python.org/urls?param=asdfa'
    _parsed Nc                 C   sh   t |trt|}n|}|\}}}}}}	|d ur't|}
|r%|d |
 n|
}t||||||	| _d S )N&)
isinstancestrurlparse_encode_paramsParseResultr	   )selfurlparamsparsedschemenetlocpathparsed_paramsqueryfragment
new_params r   Q/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/geventhttpclient/url.py__init__   s   
zURL.__init__c                 C   s
   | j  S N)r	   geturlr   r   r   r   __str__&   s   
zURL.__str__c                 C   s   d|  dS )NzURL()r   r!   r   r   r   __repr__)   s   zURL.__repr__c                 C   s   dd | j D S )Nc                 s   s     | ]}|d ur
|ndV  qd S Nr
   r   ).0valr   r   r   	<genexpr>-   s    zURL.__iter__.<locals>.<genexpr>r   r!   r   r   r   __iter__,   s   zURL.__iter__c                 C   s&   t |t| st| |}| j|jkS r   )r   typer	   )r   otherr   r   r   __eq__/   s   z
URL.__eq__c                 C   s   t | j|}|d ur|S dS r%   )getattrr	   )r   attrvaluer   r   r   __getattr__4   s   zURL.__getattr__c                 C      | j S r   )hostnamer!   r   r   r   host9      zURL.hostc                 C   r1   r   )usernamer!   r   r   r   user=   r4   zURL.userc                 C   sv   | j jd }|d ur2| r| rt|}ntd|d|  kr+dks0td td|S t| j j}|S )N   z+Port could not be cast to integer value as r   i  zPort out of range 0-65535)	r	   	_hostinfoisdigitisasciiint
ValueErrorDEFAULT_PORTSgetr   )r   portr   r   r   r?   A   s   
zURL.portc                 C   r1   r   )r   r!   r   r   r   query_stringO   r4   zURL.query_stringc                 C   s   | j s| jS | jd | j  S )N?)r   r   r!   r   r   r   request_uriS   s   zURL.request_uric           	      C   s   t |tr	t|}|jr|S |\}}}}}}| j}| j}|ds6|dr,| j| }n
| jdd | }t	
||||||}t| |S )z7Redirect to the other URL, relative to the current one./)r   r   r   r   r   
startswithendswithr   rstripr   r   r*   )	r   r+   r   r   r   r   r   r   r   r   r   r   redirectY   s   


zURL.redirectc                 C   s   t t| S r   )requote_urir   r!   r   r   r   quotedm   s   z
URL.quotedc                 C   s
   t | jS r   )rH   rB   r!   r   r   r   
quoted_uriq   s   
zURL.quoted_uri)r
   N)__name__
__module____qualname____doc__	__slots__r   r"   r$   r)   r,   r0   propertyr3   r6   r?   r@   rB   rG   rI   rJ   r   r   r   r   r      s0    






r   c                 C   s   t | ttfr	| S t| drSg }t| D ]7\}}t |ttfs$t|ds'|g}|D ]!}|durJ|t |tr;|dn|t |trF|dn|f q)qtj|ddS | S )zEncode parameters in a piece of data.
    Will successfully encode parameters when passed as a dict or a list of 2-tuples.
    r)   Nzutf-8T)doseq)	r   r   byteshasattrto_key_val_listappendencoder   	urlencode)dataresultkvsvr   r   r   r   v   s$   
r   c                 C   s@   | du rdS t | ttttfrtdt | tr|  } t| S )a  Take an object and test to see if it can be represented as a
    dictionary. If it can be, return a list of tuples, e.g.,
    ::
        >>> to_key_val_list([('key', 'val')])
        [('key', 'val')]
        >>> to_key_val_list({'key': 'val'})
        [('key', 'val')]
        >>> to_key_val_list('string')
        Traceback (most recent call last):
        ...
        ValueError: cannot encode objects that are not 2-tuples
    :rtype: list
    Nz+cannot encode objects that are not 2-tuples)	r   r   rR   boolr;   r<   r   itemslist)r/   r   r   r   rT      s   
rT   c                   @   s   e Zd ZdS )
InvalidURLN)rK   rL   rM   r   r   r   r   r`      s    r`   zBABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~c              	   C   s   |  d}tdt|D ]Q}|| dd }t|dkrT| rTz	tt|d}W n ty8   td| dw |tv rJ||| dd  ||< qd||  ||< qd||  ||< qd		|S )
zUn-escape any percent-escape sequences in a URI that are unreserved
    characters. This leaves all reserved, illegal and non-ASCII bytes encoded.

    :rtype: str
    %r7   r         z"Invalid percent-escape sequence: ''Nr
   )
splitrangelenisalnumchrr;   r<   r`   UNRESERVED_SETjoin)uripartsihcr   r   r   unquote_unreserved   s   

rq   c                 C   s>   d}d}z
t jt| |dW S  ty   t j| |d Y S w )zRe-quote the given URI.

    This function passes the given URI through an unquote/quote cycle to
    ensure that it is fully and consistently quoted.

    :rtype: str
    z!#$%&'()*+,/:;=?@[]~z!#$&'()*+,/:;=?@[]~)safe)r   quoterq   r`   )rl   safe_with_percentsafe_without_percentr   r   r   rH      s   rH   N)collections.abcr   urllibr   r   r=   r   r   rT   	Exceptionr`   	frozensetrj   rq   rH   r   r   r   r   <module>   s    
o
