ó
^\kdc           @   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# d2 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& d' „ ZA d( „  ZB d) „  ZC d* jD d+ ƒ ZE eE d, ZF eE d- ZG d. „  ZH d/ „  ZI d0 „  ZJ d+ d1 „ ZK d S(3   s•   
requests.utils
~~~~~~~~~~~~~~

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

iÿÿÿÿNi   (   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(    (    sA   /var/www/testScraper/venv/build/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   (    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/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(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/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   <iÿÿÿÿt   >N(   t   getattrR%   R   R'   t   basename(   t   objR:   (    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/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(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/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   (    (    sA   /var/www/testScraper/venv/build/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   iÿÿÿÿt   "(   t   _parse_list_headert   unquote_header_valuet   append(   RE   t   resultt   item(    (    sA   /var/www/testScraper/venv/build/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   iÿÿÿÿRK   N(   RL   R%   R-   RM   (   RE   RO   RP   R:   (    (    sA   /var/www/testScraper/venv/build/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    iÿÿÿÿRK   i   i   s   \\s   \s   \"(   t   replace(   RE   t   is_filename(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/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(    (    sA   /var/www/testScraper/venv/build/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(   RV   RW   t   cj2(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/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(    (    sA   /var/www/testScraper/venv/build/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(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/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(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/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(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/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   (    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/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(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/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Ž   (    (    sA   /var/www/testScraper/venv/build/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_netmaskRC   (   t   ipt   nett   ipaddrt   netaddrt   bitst   netmaskt   network(    (    sA   /var/www/testScraper/venv/build/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    (    (    sA   /var/www/testScraper/venv/build/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   FalseRx   (   t	   string_ip(    (    sA   /var/www/testScraper/venv/build/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   countRC   R-   RD   R¨   R™   Rš   R§   Rx   (   t   string_networkR¦   (    (    sA   /var/www/testScraper/venv/build/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“ i  Sqk qk WqÝ x@ | D]5 } | j | ƒ sÒ | j d ƒ d j | ƒ r¡ i  Sq¡ Wn  y t | ƒ } Wn  t	 t
 j f k
 rt } n X| ri  St ƒ  S(   s%   Return a dict of environment proxies.c         S   s(   t  j j |  ƒ p' t  j j |  j ƒ  ƒ S(   N(   R   t   environRi   t   upper(   t   k(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/utils.pyt   <lambda>Ø  s    t   no_proxyt    Rr   t   ,R$   i    (   R   R,   RT   R-   Rª   R­   R£   t   endswithR   R„   R™   t   gaierrorR¨   R   (   R2   t	   get_proxyR²   R,   Rœ   t   proxy_ipR7   t   bypass(    (    sA   /var/www/testScraper/venv/build/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.%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(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/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(   s   gzips   deflates   compress(   R   RÏ   R�   (    (    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/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(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/utils.pyt   parse_header_links,  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(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/utils.pyt   guess_json_utfT  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(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/utils.pyt   except_on_missing_schemeq  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(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/utils.pyt   get_auth_from_urlz  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(    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/utils.pyt   to_native_string‡  s    	(   s   .netrcs   _netrc(L   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ö   (    (    (    sA   /var/www/testScraper/venv/build/pip/pip/_vendor/requests/utils.pyt   <module>
   sp   F					.					"							"
								0		#

				