
qJ[c           @   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 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 d d l m Z m Z d d	 l m Z d d
 l  m! Z! m" Z" e f Z# d3 Z$ e j%   Z& d   Z' d   Z( d   Z) d   Z* d   Z+ d   Z, d   Z- d   Z. e/ d  Z0 d   Z1 d   Z2 d   Z3 d   Z4 d   Z5 d   Z6 d   Z7 e8 d d  Z9 d   Z: d    Z; d!   Z< d"   Z= d#   Z> d$   Z? d%   Z@ d&   ZA d' d(  ZB d)   ZC d*   ZD d+ jE d,  ZF eF d- ZG eF d. ZH d/   ZI d0   ZJ d1   ZK d, d2  ZL d S(4   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_bypass(   t   RequestsCookieJart   cookiejar_from_dict(   t   CaseInsensitiveDict(   t   MissingSchemat
   InvalidURLs   .netrct   _netrcc         C   s"   t  |  d  r |  j   }  n  |  S(   s/   Returns an internal sequence dictionary update.t   items(   t   hasattrR   (   t   d(    (    s2   /usr/lib/python2.7/dist-packages/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   (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt	   super_len0   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(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   get_netrc_authD   s0    c         C   sL   t  |  d d  } | rH | d d k rH | d d k rH t j j |  Sd S(   s0   Tries to guess the filename of the given object.t   namei    t   <it   >N(   t   getattrR%   R   R'   t   basename(   t   objR:   (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   guess_filenamer   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%   t
   isinstanceR   R   t   boolt   intt
   ValueErrorR   (   t   value(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   from_key_val_listy   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%   RA   R   R   RB   RC   RD   t   collectionst   MappingR   t   list(   RE   (    (    s2   /usr/lib/python2.7/dist-packages/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(   RE   t   resultt   item(    (    s2   /usr/lib/python2.7/dist-packages/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   iRK   N(   RL   R%   R-   RM   (   RE   RO   RP   R:   (    (    s2   /usr/lib/python2.7/dist-packages/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    iRK   i   i   s   \\s   \s   \"(   t   replace(   RE   t   is_filename(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyRM      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.
    (   RE   R:   (   t   cjt   cookie_dictt   cookie(    (    s2   /usr/lib/python2.7/dist-packages/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(   RV   RW   t   cj2(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   add_dict_to_cookiejar  s    c         C   sf   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!   <meta.*?charset=["\']*(.+?)["\'>]t   flagss+   <meta.*?content=["\']*;?charset=(.+?)["\'>]s$   ^<\?xml.*?encoding=["\']*(.+?)["\'>](   t   ret   compilet   It   findall(   t   contentt
   charset_ret	   pragma_ret   xml_re(    (    s2   /usr/lib/python2.7/dist-packages/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(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   get_encoding_from_headers,  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   errorsRT   t    t   final(   t   encodingR%   t   codecst   getincrementaldecodert   decodet   True(   t   iteratort   rRP   t   decodert   chunkt   rv(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   stream_decode_response_unicode@  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(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   iter_slicesR  s    c         C   s   g  } t  |  j  } | rS y t |  j |  SWqS t k
 rO | j |  qS 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. every encodings from ``<meta ... charset=XXX>``

    3. fall back and replace all unicode characters

    Rq   RT   N(   Rp   Rm   R   Rb   t   UnicodeErrorRN   t	   TypeError(   Rz   t   tried_encodingsRt   (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   get_unicode_from_responseZ  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'Rr   (
   R-   t   rangeR   t   isalnumt   chrRC   RD   R   t   UNRESERVED_SETt   join(   t   urit   partst   it   ht   c(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   unquote_unreserved  s    c         C   s   t  t |   d d 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.
    t   safes   !#$%&'()*+,/:;=?@[]~(   R   R   (   R   (    (    s2   /usr/lib/python2.7/dist-packages/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_netmaskRC   (   t   ipt   nett   ipaddrt   netaddrt   bitst   netmaskt   network(    (    s2   /usr/lib/python2.7/dist-packages/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
    l    i   i    s   >I(   R   t	   inet_ntoaR   t   pack(   t   maskR   (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyR     s    c         C   s-   y t  j |   Wn t  j k
 r( t SXt S(   N(   R   R   t   errort   FalseRx   (   t	   string_ip(    (    s2   /usr/lib/python2.7/dist-packages/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   countRC   R-   RD   R   R   R   R   Rx   (   t   string_networkR   (    (    s2   /usr/lib/python2.7/dist-packages/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   environRi   t   upper(   t   k(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   <lambda>  s    t   no_proxyt    Rr   t   ,R$   i    (   R   R,   RT   R-   R   R   R   Rx   t   endswithR   R   R   t   gaierrorR   (   R2   t	   get_proxyR   R,   R   t   proxy_ipR7   t   bypass(    (    s2   /usr/lib/python2.7/dist-packages/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   (   R2   (    (    s2   /usr/lib/python2.7/dist-packages/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.%sRs   Rr   t   Jythont
   IronPythont   UnknownR   s   %s/%s(   t   platformt   python_implementationt   python_versiont   syst   pypy_version_infot   majort   minort   microt   releaselevelR   t   systemt   releaseR/   R    (   R:   t   _implementationt   _implementation_versiont   p_systemt	   p_release(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   default_user_agent
  s.    	!
c           C   s+   t  i t   d 6d j d	  d 6d d 6 S(
   Ns
   User-Agents   , t   gzipt   deflatet   compresss   Accept-Encodings   */*t   Accept(   R   R   R   (   R   R   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   default_headers)  s    
c   	      C   s   g  } d } x |  j  d  D] } y | j  d d  \ } } Wn t k
 r^ | 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    '"R   t   ;i   Rr   s   <> '"R2   RR   (   R-   RD   Rl   RN   (	   RE   t   linkst   replace_charst   valR2   Ro   t   linkt   paramt   key(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   parse_header_links1  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(   Ru   t   BOM_UTF32_LEt   BOM32_BEt   BOM_UTF8t   BOM_UTF16_LEt   BOM_UTF16_BER   t   _nullt   _null2t   _null3R%   (   t   datat   samplet	   nullcount(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   guess_json_utfY  s*    
c         C   s7   t  |   \ } } } } } } | s3 t d   n  d S(   sK   Given a URL, raise a MissingSchema exception if the scheme is missing.
    s&   Proxy URLs must have explicit schemes.N(   R   R   (   R2   t   schemeR,   R'   Ro   t   queryt   fragment(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   except_on_missing_schemev  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.Rr   (   Rr   Rr   (   R   R   t   usernamet   passwordR1   R   (   R2   t   parsedt   auth(    (    s2   /usr/lib/python2.7/dist-packages/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%   RA   R
   R	   t   encodeRw   (   R   Rt   t   out(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   to_native_string  s    	(   s   .netrcs   _netrc(M   t   __doc__Rj   Ru   RG   R   R   R   R^   R   R   R   Rr   R    R   t   compatR   RL   R   R   R   R   R   R   R	   R
   R   R   t   cookiesR   R   t
   structuresR   t
   exceptionsR   R   t   _hush_pyflakesR&   t   wheret   DEFAULT_CA_BUNDLE_PATHR   R!   R9   R@   RF   RJ   RQ   RS   R   RM   RY   R\   Rf   Rp   R~   R   R   t	   frozensetR   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   R   (    (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   <module>
   sr   F					.					"							"
								-			#

				