
9Vc           @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z d d l m Z d d l m Z d d l m Z d d l m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z m Z d d l m Z m  Z  d d	 l! m" Z" d d
 l# m$ Z$ e f Z% d4 Z& e j'   Z( d   Z) d   Z* d   Z+ d   Z, d   Z- d   Z. d   Z/ d   Z0 e1 d  Z2 d   Z3 d   Z4 d   Z5 d   Z6 d   Z7 d   Z8 d   Z9 e: d d  Z; d   Z< d    Z= d!   Z> d"   Z? d#   Z@ d$   ZA d%   ZB d&   ZC d' d(  ZD d)   ZE d*   ZF d+ jG d,  ZH eH d- ZI eH d. ZJ d/   ZK d0   ZL d1   ZM d, d2  ZN d3   ZO d S(5   s   
requests.utils
~~~~~~~~~~~~~~

This module provides utility functions that are used within Requests
that are also useful for external consumption.

iNi   (   t   __version__(   t   certs(   t   parse_http_list(   t   quotet   urlparset   bytest   strt   OrderedDictt   unquotet   is_py2t   builtin_strt
   getproxiest   proxy_bypasst
   urlunparset
   basestring(   t   RequestsCookieJart   cookiejar_from_dict(   t   CaseInsensitiveDict(   t
   InvalidURLs   .netrct   _netrcc         C   s"   t  |  d  r |  j   }  n  |  S(   s/   Returns an internal sequence dictionary update.t   items(   t   hasattrR   (   t   d(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   dict_to_sequence)   s    c         C   s   t  |  d  r t |   St  |  d  r/ |  j St  |  d  rx y |  j   } Wn t j k
 rd qx Xt j |  j Sn  t  |  d  r t |  j    Sd  S(   Nt   __len__t   lent   filenot   getvalue(	   R   R   R   t   iot   UnsupportedOperationt   ost   fstatt   st_sizeR   (   t   oR   (    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt	   super_len2   s    
c   
      C   s2  yd d l  m  } m } d } x^ t D]V } y t j j d j |   } Wn t k
 r_ d SXt j j	 |  r& | } Pq& q& W| d k r d St
 |   } | j j d  d } yG | |  j |  } | r | d r d n d }	 | |	 | d f SWn | t f k
 rn XWn t t f k
 r-n Xd S(	   s;   Returns the Requests tuple auth for a given url from netrc.i(   t   netrct   NetrcParseErrors   ~/{0}Nt   :i    i   i   (   R#   R$   t   Nonet   NETRC_FILESR   t   patht
   expandusert   formatt   KeyErrort   existsR   t   netloct   splitt   authenticatorst   IOErrort   ImportErrort   AttributeError(
   t   urlR#   R$   t
   netrc_patht   ft   loct   rit   hostR   t   login_i(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   get_netrc_authF   s0    c         C   s[   t  |  d d  } | rW t | t  rW | d d k rW | d d k rW t j j |  Sd S(   s0   Tries to guess the filename of the given object.t   namei    t   <it   >N(   t   getattrR&   t
   isinstanceR   R   R(   t   basename(   t   objR;   (    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   guess_filenamet   s    %c         C   sD   |  d k r d St |  t t t t f  r: t d   n  t |   S(   s  Take an object and test to see if it can be represented as a
    dictionary. Unless it can not be represented as such, return an
    OrderedDict, e.g.,

    ::

        >>> from_key_val_list([('key', 'val')])
        OrderedDict([('key', 'val')])
        >>> from_key_val_list('string')
        ValueError: need more than 1 value to unpack
        >>> from_key_val_list({'key': 'val'})
        OrderedDict([('key', 'val')])
    s+   cannot encode objects that are not 2-tuplesN(   R&   R?   R   R   t   boolt   intt
   ValueErrorR   (   t   value(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   from_key_val_list|   s
    c         C   se   |  d k r d St |  t t t t f  r: t d   n  t |  t j  r[ |  j	   }  n  t
 |   S(   sz  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')
        ValueError: cannot encode objects that are not 2-tuples.
    s+   cannot encode objects that are not 2-tuplesN(   R&   R?   R   R   RC   RD   RE   t   collectionst   MappingR   t   list(   RF   (    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   to_key_val_list   s    c         C   sh   g  } x[ t  |   D]M } | d  | d k o8 d k n rS t | d d ! } n  | j |  q W| S(   s  Parse lists as described by RFC 2068 Section 2.

    In particular, parse comma-separated lists where the elements of
    the list may include quoted-strings.  A quoted-string could
    contain a comma.  A non-quoted string could have quotes in the
    middle.  Quotes are removed automatically after parsing.

    It basically works like :func:`parse_set_header` just that items
    may appear multiple times and case sensitivity is preserved.

    The return value is a standard :class:`list`:

    >>> parse_list_header('token, "quoted value"')
    ['token', 'quoted value']

    To create a header from the :class:`list` again, use the
    :func:`dump_header` function.

    :param value: a string with a list header.
    :return: :class:`list`
    i   it   "(   t   _parse_list_headert   unquote_header_valuet   append(   RF   t   resultt   item(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   parse_list_header   s    $c         C   s   i  } x t  |   D]~ } d | k r5 d | | <q n  | j d d  \ } }  |  d  |  d k ol d k n r t |  d d ! }  n  |  | | <q W| S(   sM  Parse lists of key, value pairs as described by RFC 2068 Section 2 and
    convert them into a python dict:

    >>> d = parse_dict_header('foo="is a fish", bar="as well"')
    >>> type(d) is dict
    True
    >>> sorted(d.items())
    [('bar', 'as well'), ('foo', 'is a fish')]

    If there is no value for a key it will be `None`:

    >>> parse_dict_header('key_without_value')
    {'key_without_value': None}

    To create a header from the :class:`dict` again, use the
    :func:`dump_header` function.

    :param value: a string with a dict header.
    :return: :class:`dict`
    t   =i   iRL   N(   RM   R&   R.   RN   (   RF   RP   RQ   R;   (    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   parse_dict_header   s    
$c         C   sq   |  rm |  d |  d k o% d k n rm |  d d !}  | sN |  d  d k rm |  j  d d  j  d d  Sn  |  S(	   s   Unquotes a header value.  (Reversal of :func:`quote_header_value`).
    This does not use the real unquoting but what browsers are actually
    using for quoting.

    :param value: the header value to unquote.
    i    iRL   i   i   s   \\s   \s   \"(   t   replace(   RF   t   is_filename(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyRN      s
    *c         C   s+   i  } x |  D] } | j  | | j <q W| S(   so   Returns a key/value dictionary from a CookieJar.

    :param cj: CookieJar object to extract cookies from.
    (   RF   R;   (   t   cjt   cookie_dictt   cookie(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   dict_from_cookiejar  s    c         C   s   t  |  } |  j |  |  S(   s   Returns a CookieJar from a key/value dictionary.

    :param cj: CookieJar to insert cookies into.
    :param cookie_dict: Dict of key/values to insert into CookieJar.
    (   R   t   update(   RW   RX   t   cj2(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   add_dict_to_cookiejar  s    c         C   sv   t  j d t  t j d d t j } t j d d t j } t j d  } | j |   | j |   | j |   S(   sl   Returns encodings from given content string.

    :param content: bytestring to extract encodings from.
    s   In requests 3.0, get_encodings_from_content will be removed. For more information, please see the discussion on issue #2266. (This warning should only appear once.)s!   <meta.*?charset=["\']*(.+?)["\'>]t   flagss+   <meta.*?content=["\']*;?charset=(.+?)["\'>]s$   ^<\?xml.*?encoding=["\']*(.+?)["\'>](   t   warningst   warnt   DeprecationWarningt   ret   compilet   It   findall(   t   contentt
   charset_ret	   pragma_ret   xml_re(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   get_encodings_from_content   s    c         C   s_   |  j  d  } | s d St j |  \ } } d | k rK | d j d  Sd | k r[ d Sd S(   sm   Returns encodings from given HTTP Header Dict.

    :param headers: dictionary to extract encoding from.
    s   content-typet   charsets   '"t   texts
   ISO-8859-1N(   t   getR&   t   cgit   parse_headert   strip(   t   headerst   content_typet   params(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   get_encoding_from_headers4  s    c         c   s   | j  d k r) x |  D] } | Vq Wd St j | j   d d  } x+ |  D]# } | j |  } | rK | VqK qK W| j d d t } | r | Vn  d S(   s   Stream decodes a iterator.Nt   errorsRU   t    t   final(   t   encodingR&   t   codecst   getincrementaldecodert   decodet   True(   t   iteratort   rRQ   t   decodert   chunkt   rv(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   stream_decode_response_unicodeH  s    	c         c   s=   d } x0 | t  |   k  r8 |  | | | !V| | 7} q	 Wd S(   s    Iterate over slices of a string.i    N(   R   (   t   stringt   slice_lengtht   pos(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   iter_slicesZ  s    c         C   s   t  j d t  g  } t |  j  } | rc y t |  j |  SWqc t k
 r_ | j |  qc Xn  y t |  j | d d SWn t	 k
 r |  j SXd S(   s   Returns the requested content back in unicode.

    :param r: Response object to get unicode content from.

    Tried:

    1. charset from content-type
    2. fall back and replace all unicode characters

    s   In requests 3.0, get_unicode_from_response will be removed. For more information, please see the discussion on issue #2266. (This warning should only appear once.)Ru   RU   N(
   R_   R`   Ra   Rt   Rq   R   Rf   t   UnicodeErrorRO   t	   TypeError(   R~   t   tried_encodingsRx   (    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   get_unicode_from_responseb  s    t4   ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzs   0123456789-._~c         C   s   |  j  d  } x t d t |   D] } | | d d !} t |  d k r | j   r y t t | d   } Wn! t k
 r t d |   n X| t k r | | | d | | <q d | | | | <q% d | | | | <q% Wd j	 |  S(   s   Un-escape any percent-escape sequences in a URI that are unreserved
    characters. This leaves all reserved, illegal and non-ASCII bytes encoded.
    t   %i   i    i   i   s%   Invalid percent-escape sequence: '%s'Rv   (
   R.   t   rangeR   t   isalnumt   chrRD   RE   R   t   UNRESERVED_SETt   join(   t   urit   partst   it   ht   c(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   unquote_unreserved  s    c         C   sK   d } d } y t  t |   d | SWn t k
 rF t  |  d | SXd S(   s   Re-quote the given URI.

    This function passes the given URI through an unquote/quote cycle to
    ensure that it is fully and consistently quoted.
    s   !#$%&'()*+,/:;=?@[]~s   !#$&'()*+,/:;=?@[]~t   safeN(   R   R   R   (   R   t   safe_with_percentt   safe_without_percent(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   requote_uri  s    c         C   s   t  j d t j |    d } | j d  \ } } t  j d t j t t |     d } t  j d t j |   d | @} | | @| | @k S(   s   
    This function allows you to check if on IP belongs to a network subnet
    Example: returns True if ip = 192.168.1.1 and net = 192.168.1.0/24
             returns False if ip = 192.168.1.1 and net = 192.168.100.0/24
    s   =Li    t   /(   t   structt   unpackt   sockett	   inet_atonR.   t   dotted_netmaskRD   (   t   ipt   nett   ipaddrt   netaddrt   bitst   netmaskt   network(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   address_in_network  s
    +#c         C   s/   d d d |  >d A} t  j t j d |   S(   st   
    Converts mask from /xx format to xxx.xxx.xxx.xxx
    Example: if mask is 24 function returns 255.255.255.0
    I    i   i    s   >I(   R   t	   inet_ntoaR   t   pack(   t   maskR   (    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyR     s    c         C   s-   y t  j |   Wn t  j k
 r( t SXt S(   N(   R   R   t   errort   FalseR|   (   t	   string_ip(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   is_ipv4_address  s
    c         C   s   |  j  d  d k r y t |  j d  d  } Wn t k
 rF t SX| d k  s_ | d k rc t Sy t j |  j d  d  Wq t j k
 r t SXn t St S(   s9   Very simple check of the cidr format in no_proxy variableR   i   i    i    (	   t   countRD   R.   RE   R   R   R   R   R|   (   t   string_networkR   (    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   is_valid_cidr  s    c         C   s  d   } | d  } t  |   j } | r | j d d  j d  } | j d  d } t |  r xs | D]( } t |  rk t | |  r t Sqk qk Wq x@ | D]5 } | j |  s | j d  d j |  r t Sq Wn  y t	 |  } Wn  t
 t j f k
 rt } n X| rt St S(   s:   
    Returns whether we should bypass proxies or not.
    c         S   s(   t  j j |   p' t  j j |  j    S(   N(   R   t   environRm   t   upper(   t   k(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   <lambda>  s    t   no_proxyt    Rv   t   ,R%   i    (   R   R-   RU   R.   R   R   R   R|   t   endswithR   R   R   t   gaierrorR   (   R3   t	   get_proxyR   R-   R   t   proxy_ipR8   t   bypass(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   should_bypass_proxies  s*    	+
c         C   s   t  |   r i  St   Sd S(   s%   Return a dict of environment proxies.N(   R   R   (   R3   (    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   get_environ_proxies  s    s   python-requestsc         C   s7  t  j   } | d k r' t  j   } n | d k r d t j j t j j t j j f } t j j d k r d j	 | t j j g  } q n< | d k r t  j   } n! | d k r t  j   } n d } y t  j
   } t  j   } Wn t k
 rd } d } n Xd	 j	 d
 |  t f d
 | | f d
 | | f g  S(   s4   Return a string representing the default user agent.t   CPythont   PyPys   %s.%s.%sRw   Rv   t   Jythont
   IronPythont   UnknownR   s   %s/%s(   t   platformt   python_implementationt   python_versiont   syst   pypy_version_infot   majort   minort   microt   releaselevelR   t   systemt   releaseR0   R    (   R;   t   _implementationt   _implementation_versiont   p_systemt	   p_release(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   default_user_agent  s.    	!
c           C   s2   t  i t   d 6d j d
  d 6d d 6d d	 6 S(   Ns
   User-Agents   , t   gzipt   deflates   Accept-Encodings   */*t   Accepts
   keep-alivet
   Connection(   s   gzips   deflate(   R   R   R   (    (    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   default_headers;  s
    
c   	      C   s   g  } d } x t  j d |   D] } y | j d d  \ } } Wn t k
 ra | d } } n Xi  } | j d  | d <xa | j d  D]P } y | j d  \ } }  Wn t k
 r Pn X|  j |  | | j |  <q W| j |  q W| S(	   s   Return a dict of parsed link headers proxies.

    i.e. Link: <http:/.../front.jpeg>; rel=front; type="image/jpeg",<http://.../back.jpeg>; rel=back;type="image/jpeg"

    s    '"s   , *<t   ;i   Rv   s   <> '"R3   RS   (   Rb   R.   RE   Rp   RO   (	   RF   t   linkst   replace_charst   valR3   Rs   t   linkt   paramt   key(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   parse_header_linksD  s"     t    t   asciii   i   c         C   s   |  d  } | t  j t  j f k r& d S| d  t  j k r= d S| d  t  j t  j f k r] d S| j t  } | d k r| d S| d k r | d  d  d  t k r d	 S| d
 d  d  t k r d Sn  | d k r | d  t	 k r d S| d
 t	 k r d Sn  d  S(   Ni   s   utf-32i   s	   utf-8-sigi   s   utf-16i    s   utf-8s	   utf-16-bei   s	   utf-16-les	   utf-32-bes	   utf-32-le(   Ry   t   BOM_UTF32_LEt   BOM32_BEt   BOM_UTF8t   BOM_UTF16_LEt   BOM_UTF16_BER   t   _nullt   _null2t   _null3R&   (   t   datat   samplet	   nullcount(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   guess_json_utfl  s*    
c         C   sS   t  |  |  \ } } } } } } | s7 | | } } n  t | | | | | | f  S(   s   Given a URL that may or may not have a scheme, prepend the given scheme.
    Does not replace a present scheme with the one provided as an argument.(   R   R   (   R3   t
   new_schemet   schemeR-   R(   Rs   t   queryt   fragment(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   prepend_scheme_if_needed  s    !c         C   sR   t  |   } y" t | j  t | j  f } Wn t t f k
 rM d } n X| S(   s_   Given a url with authentication components, extract them into a tuple of
    username,password.Rv   (   Rv   Rv   (   R   R   t   usernamet   passwordR2   R   (   R3   t   parsedt   auth(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   get_auth_from_url  s    "
c         C   sI   d } t |  t  r |  } n' t r6 |  j |  } n |  j |  } | S(   s   
    Given a string object, regardless of type, returns a representation of that
    string in the native string type, encoding and decoding where necessary.
    This assumes ASCII unless told otherwise.
    N(   R&   R?   R
   R	   t   encodeR{   (   R   Rx   t   out(    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   to_native_string  s    	c         C   sf   t  |   \ } } } } } } | s4 | | } } n  | j d d  d } t | | | | | d f  S(   sE   
    Given a url remove the fragment and the authentication part
    t   @i   iRv   (   R   t   rsplitR   (   R3   R   R-   R(   Rs   R   R   (    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   urldefragauth  s
    (   s   .netrcs   _netrc(P   t   __doc__Rn   Ry   RH   R   R   R   Rb   R   R   R   R_   Rv   R    R   t   compatR   RM   R   R   R   R   R   R   R	   R
   R   R   R   R   t   cookiesR   R   t
   structuresR   t
   exceptionsR   t   _hush_pyflakesR'   t   wheret   DEFAULT_CA_BUNDLE_PATHR   R"   R:   RB   RG   RK   RR   RT   R   RN   RZ   R]   Rj   Rt   R   R   R   t	   frozensetR   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R  (    (    (    s7   /tmp/pip-build-5Z5nTX/pip/pip/_vendor/requests/utils.pyt   <module>
   sv   R					.					"							$
								-				#

			