ó
Be¤Vc        
   @   s  d  Z  d d l m Z m Z d d l Z d d l Z yD d d l m Z d d l m Z d d l m	 Z	 d d l m
 Z
 WnQ e k
 rË d d l m Z d d l m Z d d l m	 Z	 d d l m
 Z
 n Xe j d k  rä d Z n8 y d d	 l m Z Wn! e k
 rd d	 l m Z n Xe j d
 k  r4d Z n8 y d d l m Z Wn! e k
 rkd d l m Z n Xe j d k Z e r d d l Z d d l m Z n d d l Z d d l m Z y
 e Z Wn e k
 råe e  f Z n Xd e f d „  ƒ  YZ! d Z" d S(   sþ   Private module full of compatibility hacks.

Primarily this is for downstream redistributions of requests that unvendor
urllib3 without providing a shim.

.. warning::

    This module is private. If you use it, and something breaks, you were
    warned
iÿÿÿÿ(   t   Mappingt   MutableMappingN(   t
   connection(   t   fields(   t   filepost(   t   poolmanageri  (   t   timeouti  (   t	   appenginei   i    (   t	   urlencodet   HTTPHeaderDictc           B   s  e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 e si e j Z e j Z n  e ƒ  Z d „  Z d	 „  Z e d
 „ Z d „  Z d „  Z d „  Z d „  Z e Z e Z e Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  e! d „  ƒ Z" RS(   sp  
    :param headers:
        An iterable of field-value pairs. Must not contain multiple field names
        when compared case-insensitively.

    :param kwargs:
        Additional field-value pairs to pass in to ``dict.update``.

    A ``dict`` like container for storing HTTP Headers.

    Field names are stored and compared case-insensitively in compliance with
    RFC 7230. Iteration provides the first case-sensitive key seen for each
    case-insensitive pair.

    Using ``__setitem__`` syntax overwrites fields that compare equal
    case-insensitively in order to maintain ``dict``'s api. For fields that
    compare equal, instead create a new ``HTTPHeaderDict`` and use ``.add``
    in a loop.

    If multiple fields that are equal case-insensitively are passed to the
    constructor or ``.update``, the behavior is undefined and some will be
    lost.

    >>> headers = HTTPHeaderDict()
    >>> headers.add('Set-Cookie', 'foo=bar')
    >>> headers.add('set-cookie', 'baz=quxx')
    >>> headers['content-length'] = '7'
    >>> headers['SET-cookie']
    'foo=bar, baz=quxx'
    >>> headers['Content-Length']
    '7'
    c         K   sq   t  t |  ƒ j ƒ  i  |  _ | d  k	 rW t | t ƒ rG |  j | ƒ qW |  j | ƒ n  | rm |  j | ƒ n  d  S(   N(   t   superR	   t   __init__t
   _containert   Nonet
   isinstancet
   _copy_fromt   extend(   t   selft   headerst   kwargs(    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyR   \   s    	c         C   s*   | | f |  j  | j ƒ  <|  j  | j ƒ  S(   N(   R   t   lower(   R   t   keyt   val(    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   __setitem__g   s    c         C   s$   |  j  | j ƒ  } d j | d ƒ S(   Ns   , i   (   R   R   t   join(   R   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   __getitem__k   s    c         C   s   |  j  | j ƒ  =d  S(   N(   R   R   (   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   __delitem__o   s    c         C   s   | j  ƒ  |  j k S(   N(   R   R   (   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   __contains__r   s    c         C   s„   t  | t ƒ r$ t | d ƒ r$ t St  | t |  ƒ ƒ sN t |  ƒ | ƒ } n  t d „  |  j ƒ  Dƒ ƒ t d „  | j ƒ  Dƒ ƒ k S(   Nt   keysc         s   s'   |  ] \ } } | j  ƒ  | f Vq d  S(   N(   R   (   t   .0t   kt   v(    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pys	   <genexpr>z   s    c         s   s'   |  ] \ } } | j  ƒ  | f Vq d  S(   N(   R   (   R   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pys	   <genexpr>{   s    (   R   R    t   hasattrt   Falset   typet   dictt
   itermerged(   R   t   other(    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   __eq__u   s     c         C   s   |  j  | ƒ S(   N(   R&   (   R   R%   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   __ne__}   s    c         C   s   t  |  j ƒ S(   N(   t   lenR   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   __len__†   s    c         c   s'   x  |  j  j ƒ  D] } | d Vq Wd  S(   Ni    (   R   t   values(   R   t   vals(    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   __iter__‰   s    c         C   sG   y |  | } Wn' t  k
 r7 | |  j k r3 ‚  n  | SX|  | =| Sd S(   s¢   D.pop(k[,d]) -> v, remove specified key and return its value.

        If key is not found, d is returned if given, otherwise KeyError is
        raised.
        N(   t   KeyErrort   _HTTPHeaderDict__marker(   R   R   t   defaultt   value(    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   popŽ   s    
c         C   s#   y |  | =Wn t  k
 r n Xd  S(   N(   R-   (   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   discard¢   s    c         C   s}   | j  ƒ  } | | f } |  j j | | ƒ } | | k	 ry t | t ƒ rX | j | ƒ qy | d | d | g |  j | <n  d S(   sâ   Adds a (name, value) pair, doesn't overwrite the value if it already
        exists.

        >>> headers = HTTPHeaderDict(foo='bar')
        >>> headers.add('Foo', 'baz')
        >>> headers['foo']
        'bar, baz'
        i    i   N(   R   R   t
   setdefaultR   t   listt   append(   R   R   R   t	   key_lowert   new_valsR+   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   add¨   s    	c         O   s]  t  | ƒ d k r0 t d j t  | ƒ ƒ ƒ ‚ n  t  | ƒ d k rL | d n d } t | t ƒ r‘ xÈ | j ƒ  D] \ } } |  j | | ƒ qn Wn› t | t ƒ rÈ x‰ | D] } |  j | | | ƒ q§ Wnd t | d ƒ rxR | j	 ƒ  D] } |  j | | | ƒ qä Wn' x$ | D] \ } } |  j | | ƒ qWx* | j
 ƒ  D] \ } } |  j | | ƒ q9Wd S(   sÀ   Generic import function for any type of header-like object.
        Adapted version of MutableMapping.update in order to insert items
        with self.add instead of self.__setitem__
        i   s8   extend() takes at most 1 positional arguments ({} given)i    R   N(    (   R(   t	   TypeErrort   formatR   R	   t	   iteritemsR8   R    R    R   t   items(   R   t   argsR   R%   R   R   R0   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyR   ¿   s"    	"c         C   sR   y |  j  | j ƒ  } Wn t k
 r+ g  SXt | t ƒ rF | d g S| d Sd S(   sm   Returns a list of all the values for the named field. Returns an
        empty list if the key doesn't exist.i   N(   R   R   R-   R   t   tuple(   R   R   R+   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   getlistÙ   s    c         C   s#   d t  |  ƒ j t |  j ƒ  ƒ f S(   Ns   %s(%s)(   R"   t   __name__R#   R$   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   __repr__ë   s    c         C   s\   xU | D]M } | j  | ƒ } t | t ƒ r: t | ƒ } n  | g | |  j | j ƒ  <q Wd  S(   N(   R?   R   R4   R   R   (   R   R%   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyR   î   s
    c         C   s    t  |  ƒ ƒ  } | j |  ƒ | S(   N(   R"   R   (   R   t   clone(    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   copyö   s    c         c   sL   xE |  D]= } |  j  | j ƒ  } x! | d D] } | d | f Vq+ Wq Wd S(   s8   Iterate over all header lines, including duplicate ones.i   i    N(   R   R   (   R   R   R+   R   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyR;   û   s    c         c   sD   x= |  D]5 } |  j  | j ƒ  } | d d j | d ƒ f Vq Wd S(   s:   Iterate over all headers, merging duplicate ones together.i    s   , i   N(   R   R   R   (   R   R   R   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyR$     s    c         C   s   t  |  j ƒ  ƒ S(   N(   R4   R;   (   R   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyR<     s    c         C   s˜   g  } x… | j  D]z } | j d ƒ rY | d \ } } | | d | j ƒ  f | d <q n  | j d d ƒ \ } } | j | | j ƒ  f ƒ q W|  | ƒ S(   s4   Read headers from a Python 2 httplib message object.t    s   	iÿÿÿÿs   
t   :i   (   RD   s   	(   R   t
   startswitht   rstript   splitR5   t   strip(   t   clst   messageR   t   lineR   R0   (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   from_httplib  s    N(#   R@   t
   __module__t   __doc__R   R   R   R   R   R   R&   R'   t   PY3R   t   iterkeyst
   itervaluest   objectR.   R)   R,   R1   R2   R8   R   R?   t
   getheaderst   getallmatchingheaderst   igetRA   R   RC   R;   R$   R<   t   classmethodRM   (    (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyR	   :   s:    																				t
   basestringR   R   R   R   R   t   queueR   t
   gaecontrib(   i   i    (
   s
   basestrings
   connections   fieldss   fileposts   poolmanagers   timeoutR	   s   queues	   urlencodes
   gaecontrib(#   RO   t   collectionsR    R   t   syst   requestst   requests.packages.urllib3R   R   R   R   t   ImportErrort   urllib3t	   __build__R   R   t   requests.packages.urllib3.utilt   urllib3.utilRZ   t!   requests.packages.urllib3.contribR   t   urllib3.contribt   version_infoRP   RY   t   urllib.parseR   t   Queuet   urllibRX   t	   NameErrort   strt   bytesR	   t   __all__(    (    (    s=   /usr/lib/python2.7/dist-packages/requests_toolbelt/_compat.pyt   <module>
   s\   		
æ         