
´Wc           @   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
 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" m# Z# m$ Z$ e f Z% d7 Z& e j'   Z( d   Z) d   Z* e+ d  Z, d   Z- d   Z. d   Z/ d   Z0 d   Z1 e+ 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'   ZD d( d)  ZE d*   ZF d+   ZG d, jH d-  ZI eI d. ZJ eI d/ ZK d0   ZL d1   ZM d2   ZN d- d3  ZO e jP d4  ZQ e jP d4  ZR d5   ZS d6   ZT d S(8   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
   InvalidURLt   InvalidHeadert   FileModeWarnings   .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)  d } d } t  |  d  r* t |   } n t  |  d  rE |  j } n t  |  d  ri t |  j    } nm t  |  d  r y |  j   } Wn t j k
 r q Xt j |  j } d |  j	 k r t
 j d t  q n  t  |  d  ry |  j   } Wqt t f k
 r| } qXn  t d | |  S(	   Ni    t   __len__t   lent   getvaluet   filenot   bs%  Requests has determined the content-length for this request using the binary size of the file: however, the file has been opened in text mode (i.e. without the 'b' flag in the mode). This may lead to an incorrect content-length. In Requests 3.0, support will be removed for files in text mode.t   tell(   R   R   R   R   t   iot   UnsupportedOperationt   ost   fstatt   st_sizet   modet   warningst   warnR   R   t   OSErrort   IOErrort   max(   t   ot   total_lengtht   current_positionR   (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt	   super_len/   s0    c         C   se  yG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
 |   } d } t |  t  r | j d  } n  | j j |  d }	 yG | |  j |	  }
 |
 r|
 d rd n d } |
 | |
 d	 f SWn# | t f k
 rE| rF  qF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   :t   asciii    i   i   (   R/   R0   t   Nonet   NETRC_FILESR"   t   patht
   expandusert   formatt   KeyErrort   existsR   t
   isinstanceR   t   decodet   netloct   splitt   authenticatorsR)   t   ImportErrort   AttributeError(   t   urlt   raise_errorsR/   R0   t
   netrc_patht   ft   loct   rit   splitstrt   hostR   t   login_i(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   get_netrc_auth_   s8    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   getattrR3   R:   R   R"   R5   t   basename(   t   objRK   (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   guess_filename   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')])

    :rtype: OrderedDict
    s+   cannot encode objects that are not 2-tuplesN(   R3   R:   R   R   t   boolt   intt
   ValueErrorR   (   t   value(    (    s2   /usr/lib/python2.7/dist-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(   s  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.

    :rtype: list
    s+   cannot encode objects that are not 2-tuplesN(   R3   R:   R   R   RR   RS   RT   t   collectionst   MappingR   t   list(   RU   (    (    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`
    :rtype: list
    i   it   "(   t   _parse_list_headert   unquote_header_valuet   append(   RU   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(   s^  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`
    :rtype: dict
    t   =i   iR[   N(   R\   R3   R=   R]   (   RU   R_   R`   RK   (    (    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.
    :rtype: str
    i    iR[   i   i   s   \\s   \s   \"(   t   replace(   RU   t   is_filename(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyR]     s
    *c         C   s+   i  } x |  D] } | j  | | j <q W| S(   s   Returns a key/value dictionary from a CookieJar.

    :param cj: CookieJar object to extract cookies from.
    :rtype: dict
    (   RU   RK   (   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.
    :rtype: CookieJar
    (   R   t   update(   Rf   Rg   t   cj2(    (    s2   /usr/lib/python2.7/dist-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=["\']*(.+?)["\'>](   R&   R'   t   DeprecationWarningt   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_contentG  s    c         C   s_   |  j  d  } | s d St j |  \ } } d | k rK | d j d  Sd | k r[ d Sd S(   s}   Returns encodings from given HTTP Header Dict.

    :param headers: dictionary to extract encoding from.
    :rtype: str
    s   content-typet   charsets   '"t   texts
   ISO-8859-1N(   t   getR3   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   errorsRd   t    t   final(   t   encodingR3   t   codecst   getincrementaldecoderR;   t   True(   t   iteratort   rR`   t   decodert   chunkt   rv(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   stream_decode_response_unicodep  s    	c         c   sd   d } | d k s | d k r- t |   } n  x0 | t |   k  r_ |  | | | !V| | 7} q0 Wd S(   s    Iterate over slices of a string.i    N(   R3   R   (   t   stringt   slice_lengtht   pos(    (    s2   /usr/lib/python2.7/dist-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

    :rtype: str
    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.)R   Rd   N(
   R&   R'   Rn   R   R~   R   Rs   t   UnicodeErrorR^   t	   TypeError(   R   t   tried_encodingsR   (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   get_unicode_from_response  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.

    :rtype: str
    t   %i   i    i   i   s%   Invalid percent-escape sequence: '%s'R   (
   R=   t   rangeR   t   isalnumt   chrRS   RT   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   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.

    :rtype: str
    s   !#$%&'()*+,/:;=?@[]~s   !#$&'()*+,/:;=?@[]~t   safeN(   R   R   R   (   R   t   safe_with_percentt   safe_without_percent(    (    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

    :rtype: bool
    s   =Li    t   /(   t   structt   unpackt   sockett	   inet_atonR=   t   dotted_netmaskRS   (   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(   s   Converts mask from /xx format to xxx.xxx.xxx.xxx

    Example: if mask is 24 function returns 255.255.255.0

    :rtype: str
    I    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(   s   
    :rtype: bool
    (   R   R   t   errort   FalseR   (   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(   sV   
    Very simple check of the cidr format in no_proxy variable.

    :rtype: bool
    R   i   i    i    (	   t   countRS   R=   RT   R   R   R   R   R   (   t   string_networkR   (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   is_valid_cidr	  s    c         C   s8  d   } | d  } t  |   j } | r d   | j d d  j d  D } | j d  d } t |  r x | D]8 } t |  r t | |  r t Squ | | k ru t Squ 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| r4t St S(	   sL   
    Returns whether we should bypass proxies or not.

    :rtype: bool
    c         S   s(   t  j j |   p' t  j j |  j    S(   N(   R"   t   environRz   t   upper(   t   k(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   <lambda>'  s    t   no_proxyc         s   s   |  ] } | r | Vq d  S(   N(    (   t   .0RH   (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pys	   <genexpr>2  s    t    R   t   ,R1   i    (   R   R<   Rd   R=   R   R   R   R   t   endswithR   R   R   t   gaierrorR   (   RA   t	   get_proxyR   R<   R   t   proxy_ipRH   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(   sA   
    Return a dict of environment proxies.

    :rtype: dict
    N(   R   R   (   RA   (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   get_environ_proxiesW  s    c         C   s   | p	 i  } t  |   } | j d k rC | j d | j | j   Sd | j d | j d | j | j g } d } x( | D]  } | | k rz | | } Pqz qz W| S(   s   Select a proxy for the url, if applicable.

    :param url: The url being for the request
    :param proxies: A dictionary of schemes or schemes and hosts to proxy URLs
    t   alls   all://s   ://N(   R   t   hostnameR3   Rz   t   scheme(   RA   t   proxiest   urlpartst
   proxy_keyst   proxyt	   proxy_key(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   select_proxyc  s    

s   python-requestsc         C   s   d |  t  f S(   sO   
    Return a string representing the default user agent.

    :rtype: str
    s   %s/%s(   R    (   RK   (    (    s2   /usr/lib/python2.7/dist-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(   s9   
    :rtype: requests.structures.CaseInsensitiveDict
    s
   User-Agents   , t   gzipt   deflates   Accept-Encodings   */*t   Accepts
   keep-alivet
   Connection(   R   R   (   R   R   R   (    (    (    s2   /usr/lib/python2.7/dist-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 6} 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"

    :rtype: list
    s    '"s   , *<t   ;i   R   s   <> '"RA   Rb   (   Ro   R=   RT   R}   R^   (	   RU   t   linkst   replace_charst   valRA   R   t   linkt   paramt   key(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   parse_header_links  s      t    R2   i   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(   s   
    :rtype: str
    i   s   utf-32i   s	   utf-8-sigi   s   utf-16i    s   utf-8Ns	   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   _null3R3   (   t   datat   samplet	   nullcount(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   guess_json_utf  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.

    :rtype: str
    (   R   R   (   RA   t
   new_schemeR   R<   R5   R   t   queryt   fragment(    (    s2   /usr/lib/python2.7/dist-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.

    :rtype: (str,str)
    R   (   R   R   (   R   R   t   usernamet   passwordR@   R   (   RA   t   parsedt   auth(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   get_auth_from_url  s    "
c         C   sC   t  |  t  r |  } n' t r0 |  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.
    (   R:   R
   R	   t   encodeR;   (   R   R   t   out(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   to_native_string  s    	s   ^\S[^\r\n]*$|^$c         C   s   |  \ } } t  | t  r$ t } n t } y& | j |  sO t d |   n  Wn- t k
 r t d | t |  f   n Xd S(   s   Verifies that header value is a string which doesn't contain
    leading whitespace or return characters. This prevents unintended
    header injection.

    :param header: tuple, in the format (name, value).
    s7   Invalid return character or leading space in header: %ss4   Header value %s must be of type str or bytes, not %sN(   R:   R   t   _CLEAN_HEADER_REGEX_BYTEt   _CLEAN_HEADER_REGEX_STRt   matchR   R   t   type(   t   headerRK   RU   t   pat(    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   check_header_validity  s    	c         C   sf   t  |   \ } } } } } } | s4 | | } } n  | j d d  d } t | | | | | d f  S(   sW   
    Given a url remove the fragment and the authentication part.

    :rtype: str
    t   @i   iR   (   R   t   rsplitR   (   RA   R   R<   R5   R   R   R   (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   urldefragauth#  s
    (   s   .netrcs   _netrc(U   t   __doc__R{   R   RW   R    R"   Ro   R   R   R&   R   R    R   t   compatR   R\   R   R   R   R   R   R   R	   R
   R   R   R   R   t   cookiesR   R   t
   structuresR   t
   exceptionsR   R   R   t   _hush_pyflakesR4   t   wheret   DEFAULT_CA_BUNDLE_PATHR   R.   R   RJ   RQ   RV   RZ   Ra   Rc   R]   Ri   Rl   Rw   R   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   Rp   R   R   R  R  (    (    (    s2   /usr/lib/python2.7/dist-packages/requests/utils.pyt   <module>	   sz   R				03				 	#						
	%
							6					"

	 			