
YЅ_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 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% d8 Z& e j'   Z( i d d 6d d 6Z) d   Z* d   Z+ d   Z, d   Z- d   Z. d   Z/ d   Z0 d   Z1 e2 d  Z3 d   Z4 d   Z5 d   Z6 d   Z7 d   Z8 d   Z9 d    Z: e; d! d"  Z< d#   Z= d$   Z> d%   Z? d&   Z@ d'   ZA d(   ZB d)   ZC d*   ZD d+ d,  ZE d-   ZF d.   ZG d/ jH d0  ZI eI d1 ZJ eI d2 ZK d3   ZL d4   ZM d5   ZN d0 d6  ZO d7   ZP d S(9   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   _netrciP   t   httpi  t   httpsc         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/site-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/site-packages/requests/utils.pyt	   super_len4   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/site-packages/requests/utils.pyt   get_netrc_authH   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=   (    (    s2   /usr/lib/python2.7/site-packages/requests/utils.pyt   guess_filenamev   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(   RA   R   R   t   boolt   intt
   ValueErrorR   (   t   value(    (    s2   /usr/lib/python2.7/site-packages/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(   RA   R   R   RE   RF   RG   t   collectionst   MappingR   t   list(   RH   (    (    s2   /usr/lib/python2.7/site-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(   RH   t   resultt   item(    (    s2   /usr/lib/python2.7/site-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   iRN   N(   RO   R(   R0   RP   (   RH   RR   RS   R=   (    (    s2   /usr/lib/python2.7/site-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    iRN   i   i   s   \\s   \s   \"(   t   replace(   RH   t   is_filename(    (    s2   /usr/lib/python2.7/site-packages/requests/utils.pyRP      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.
    (   RH   R=   (   t   cjt   cookie_dictt   cookie(    (    s2   /usr/lib/python2.7/site-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(   RY   RZ   t   cj2(    (    s2   /usr/lib/python2.7/site-packages/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(    (    s2   /usr/lib/python2.7/site-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/site-packages/requests/utils.pyt   get_encoding_from_headers6  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   errorsRW   t    t   final(   t   encodingR(   t   codecst   getincrementaldecodert   decodet   True(   t   iteratort   rRS   t   decodert   chunkt   rv(    (    s2   /usr/lib/python2.7/site-packages/requests/utils.pyt   stream_decode_response_unicodeJ  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/site-packages/requests/utils.pyt   iter_slices\  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.)Rw   RW   N(
   Ra   Rb   Rc   Rv   Rs   R   Rh   t   UnicodeErrorRQ   t	   TypeError(   R   t   tried_encodingsRz   (    (    s2   /usr/lib/python2.7/site-packages/requests/utils.pyt   get_unicode_from_responsed  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'Rx   (
   R0   t   rangeR   t   isalnumt   chrRF   RG   R   t   UNRESERVED_SETt   join(   t   urit   partst   it   ht   c(    (    s2   /usr/lib/python2.7/site-packages/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(    (    s2   /usr/lib/python2.7/site-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_atonR0   t   dotted_netmaskRF   (   t   ipt   nett   ipaddrt   netaddrt   bitst   netmaskt   network(    (    s2   /usr/lib/python2.7/site-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/site-packages/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(    (    s2   /usr/lib/python2.7/site-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   countRF   R0   RG   R   R   R   R   R~   (   t   string_networkR   (    (    s2   /usr/lib/python2.7/site-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   environRo   t   upper(   t   k(    (    s2   /usr/lib/python2.7/site-packages/requests/utils.pyt   <lambda>  s    t   no_proxyt    Rx   t   ,R'   i    (   R   R/   RW   R0   R   R   R   R~   t   endswithR   R   R   t   gaierrorR   (   R5   t	   get_proxyR   R/   R   t   proxy_ipR:   t   bypass(    (    s2   /usr/lib/python2.7/site-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   (   R5   (    (    s2   /usr/lib/python2.7/site-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.%sRy   Rx   t   Jythont
   IronPythont   UnknownR   s   %s/%s(   t   platformt   python_implementationt   python_versiont   syst   pypy_version_infot   majort   minort   microt   releaselevelR   t   systemt   releaseR2   R    (   R=   t   _implementationt   _implementation_versiont   p_systemt	   p_release(    (    s2   /usr/lib/python2.7/site-packages/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(   R   R   (   R   R   R   (    (    (    s2   /usr/lib/python2.7/site-packages/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   Rx   s   <> '"R5   RU   (   Rd   R0   RG   Rr   RQ   (	   RH   t   linkst   replace_charst   valR5   Ru   t   linkt   paramt   key(    (    s2   /usr/lib/python2.7/site-packages/requests/utils.pyt   parse_header_linksF  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(   R{   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/site-packages/requests/utils.pyt   guess_json_utfn  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   (   R5   t
   new_schemet   schemeR/   R*   Ru   t   queryt   fragment(    (    s2   /usr/lib/python2.7/site-packages/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.Rx   (   Rx   Rx   (   R   R   t   usernamet   passwordR4   R   (   R5   t   parsedt   auth(    (    s2   /usr/lib/python2.7/site-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   encodeR}   (   R   Rz   t   out(    (    s2   /usr/lib/python2.7/site-packages/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   iRx   (   R   t   rsplitR   (   R5   R   R/   R*   Ru   R   R   (    (    s2   /usr/lib/python2.7/site-packages/requests/utils.pyt   urldefragauth  s
    (   s   .netrcs   _netrc(Q   t   __doc__Rp   R{   RJ   R   R    R   Rd   R   R   R   Ra   Rx   R    R   t   compatR   RO   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_PATHt   DEFAULT_PORTSR   R$   R<   RD   RI   RM   RT   RV   R   RP   R\   R_   Rl   Rv   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  (    (    (    s2   /usr/lib/python2.7/site-packages/requests/utils.pyt   <module>
   sx   R					.					"							$
								-				#

			