o
    e                     @   s   d dl Z e jr$d dlmZ d dlmZ d dlmZ d dlm	Z	 e
efZnd dlZd dlmZmZ d dlm	Z	 ddd	Zd
d ZG dd deZdS )    N)parse)	urlencode)
quote_plus)Mapping)r   r   P   i  )httphttpsc                 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)	
isinstancestrbytesboolint
ValueErrorr   itemslist)value r   DD:\Projects\ConvertPro\env\Lib\site-packages\geventhttpclient/url.pyto_key_val_list   s   
r   c                   @   s   e Zd ZdZdZdZd"ddZed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dd Zdd Zd d! ZdS )$URLa=   A mutable URL class

    You build it from a url string.
    >>> url = URL('http://getgauss.com/urls?param=asdfa')
    >>> url
    URL(http://getgauss.com/urls?param=asdfa)

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

    You can cast it as a string.
    >>> str(url)
    'http://getgauss.com/urls?param=asdfa'

    You can change attributes.
    >>> url.host = 'infrae.com'
    >>> url
    URL(http://infrae.com/urls?auth_token=asdfaisdfuasdf&param=asdfa)
    )	schemehostportpathqueryfragmentuserpasswordparams Nc                 C   s&  |d urt |\}}}}}nd\}}}}}|| _|| _d\}}	}
}|rnd|v r?|dd\}}d|v r=|dd\}}	n|}|dr[|dd\}
}|
d}
|rZt|d}nd|v rl|dd\}
}t|}n|}
|svt	| j}|
| _
|| _|| _|	| _|pd	| _|d
d| _|| _d S )N)r   r   /r   r   )NNr   N@   :[]z[]r    z%20)urlparseurlsplitr   r   rsplit
startswithstripr   DEFAULT_PORTSgetr   r   r   r   r   replacer   r   )selfurlr   r   netlocr   r   r   r   r   r   r   Zuser_pwZport_ptr   r   r   __init__F   s>   




zURL.__init__c                 C   s   | j ddS )NF)auth)full_netlocr/   r   r   r   r1   q   s   z
URL.netlocTc                 C   s   d}| j r|r|| j 7 }| jr|d| j 7 }|d7 }d| jv r)|d| j d 7 }n|| j7 }| jd u r5|S t| j| jkr@|S |dt| j 7 }|S )Nr   r#   r!   r$   r%   )r   Zpasswortr   r   r,   r-   r   r
   )r/   r3   bufr   r   r   r4   u   s   




zURL.full_netlocc                 C   sB   t |  }| jD ]}t| |}t|tr| }t||| q|S Ntype	__slots__getattrr	   dictcopysetattrr/   clonekeyvalr   r   r   __copy__   s   



zURL.__copy__c                 C   s   dt |  S )NzURL(%s)r
   r5   r   r   r   __repr__   s   zURL.__repr__c                 C   s   t | j|  | j| j| jfS r7   )iterr   r4   r   query_stringr   r5   r   r   r   __iter__   s   zURL.__iter__c                 C   s   t t| S r7   )r'   
urlunsplittupler5   r   r   r   __str__   s   zURL.__str__c                 C   s   t | t |kS r7   rD   r/   otherr   r   r   __eq__   s   z
URL.__eq__c                 C   s   t | ttfr	| S t| dr| S t| drWg }t| D ]5\}}t |ts)t|ds,|g}|D ]!}|durO|t |tr@|dn|t |trK|dn|f q.qt|ddS | S )zEncode parameters in a piece of data.
        Will successfully encode parameters when passed as a dict or a list of
        2-tuples.
        readrH   Nzutf-8T)doseq)	r	   r
   r   hasattrr   
basestringappendencoder   )dataresultkvsvr   r   r   _encode_params   s(   

zURL._encode_paramsc                 C   s8   g }| j r|| j  | jr|| | j d|S )N&)r   rS   r   rZ   joinr/   r   r   r   r   rG      s   
zURL.query_stringc                 C   s   | j }|s| jS | jd | S )N?)rG   r   r]   r   r   r   request_uri   s   zURL.request_uric                 C   sl   | dr| jdr|  j|dd  7  _| jS n| jds,|  jd| 7  _| jS |  j|7  _| jS )Nr    r"   )r*   r   endswith)r/   r   r   r   r   append_to_path   s   
zURL.append_to_pathc                 C   s   t |t| st| |}|js| j|_| j|_| j|_|jds@| jdr1| j|j |_|S | jddd d |j |_|S )z8 Redirect to the other URL, relative to the current one r    r"   r   )	r	   r9   r   r   r   r   r*   r`   r)   rL   r   r   r   redirect   s   zURL.redirectc                 C   sJ   t |  }| jdd D ]}t| |}t|tr| }t||| q|S )z7 Remove fragment and authentication for proxy handling N   r8   r?   r   r   r   stripped_auth   s   


zURL.stripped_auth)NN)T)__name__
__module____qualname____doc__r:   Zquoting_safer2   propertyr1   r4   rC   rE   rH   rK   rN   staticmethodrZ   rG   r_   ra   rb   rd   r   r   r   r   r   -   s,    
+

	


r   )sixPY3urllibr   r'   urllib.parser   r   collections.abcr   r
   r   rR   collectionsr,   r   objectr   r   r   r   r   <module>   s    
