ó
\žÍRc           @   s$  d  Z  d d l Z d d l Z d d l m Z m Z m Z m Z y d d l Z e Wn e	 k
 rs d d l
 Z n Xd e f d „  ƒ  YZ d e f d „  ƒ  YZ d	 „  Z d
 „  Z d d d „ Z d e f d „  ƒ  YZ d e j e j f d „  ƒ  YZ d „  Z d „  Z d e d „ Z d „  Z d S(   sŠ   
Compatibility code to be able to use `cookielib.CookieJar` with requests.

requests.utils imports from here, so be careful with imports.
iÿÿÿÿNi   (   t	   cookielibt   urlparset
   urlunparset   Morselt   MockRequestc           B   s¡   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d d „ Z d	 „  Z d
 „  Z d „  Z e d „  ƒ Z e d „  ƒ Z e d „  ƒ Z RS(   sò  Wraps a `requests.Request` to mimic a `urllib2.Request`.

    The code in `cookielib.CookieJar` expects this interface in order to correctly
    manage cookie policies, i.e., determine whether a cookie can be set, given the
    domains of the request and the cookie.

    The original request object is read-only. The client is responsible for collecting
    the new headers via `get_new_headers()` and interpreting them appropriately. You
    probably want `get_cookie_header`, defined below.
    c         C   s.   | |  _  i  |  _ t |  j  j ƒ j |  _ d  S(   N(   t   _rt   _new_headersR   t   urlt   schemet   type(   t   selft   request(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   __init__!   s    		c         C   s   |  j  S(   N(   R	   (   R
   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   get_type&   s    c         C   s   t  |  j j ƒ j S(   N(   R   R   R   t   netloc(   R
   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   get_host)   s    c         C   s
   |  j  ƒ  S(   N(   R   (   R
   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   get_origin_req_host,   s    c         C   sl   |  j  j j d ƒ s |  j  j S|  j  j d } t |  j  j ƒ } t | j | | j | j | j	 | j
 g ƒ S(   Nt   Host(   R   t   headerst   getR   R   R   R   t   patht   paramst   queryt   fragment(   R
   t   hostt   parsed(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   get_full_url/   s    
c         C   s   t  S(   N(   t   True(   R
   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   is_unverifiable=   s    c         C   s   | |  j  j k p | |  j k S(   N(   R   R   R   (   R
   t   name(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt
   has_header@   s    c         C   s%   |  j  j j | |  j j | | ƒ ƒ S(   N(   R   R   R   R   (   R
   R   t   default(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt
   get_headerC   s    c         C   s   t  d ƒ ‚ d S(   sM   cookielib has no legitimate use for this method; add it back if you find one.s=   Cookie headers should be added with add_unredirected_header()N(   t   NotImplementedError(   R
   t   keyt   val(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt
   add_headerF   s    c         C   s   | |  j  | <d  S(   N(   R   (   R
   R   t   value(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   add_unredirected_headerJ   s    c         C   s   |  j  S(   N(   R   (   R
   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   get_new_headersM   s    c         C   s
   |  j  ƒ  S(   N(   R   (   R
   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   unverifiableP   s    c         C   s
   |  j  ƒ  S(   N(   R   (   R
   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   origin_req_hostT   s    c         C   s
   |  j  ƒ  S(   N(   R   (   R
   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyR   X   s    N(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R   t   NoneR    R$   R&   R'   t   propertyR(   R)   R   (    (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyR      s   
										t   MockResponsec           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   sÀ   Wraps a `httplib.HTTPMessage` to mimic a `urllib.addinfourl`.

    ...what? Basically, expose the parsed HTTP headers from the server response
    the way `cookielib` expects to see them.
    c         C   s   | |  _  d S(   s†   Make a MockResponse for `cookielib` to read.

        :param headers: a httplib.HTTPMessage or analogous carrying the headers
        N(   t   _headers(   R
   R   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyR   d   s    c         C   s   |  j  S(   N(   R0   (   R
   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   infok   s    c         C   s   |  j  j | ƒ d  S(   N(   R0   t
   getheaders(   R
   R   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyR2   n   s    (   R*   R+   R,   R   R1   R2   (    (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyR/   ]   s   		c         C   sN   t  | d ƒ o | j s d St | ƒ } t | j j ƒ } |  j | | ƒ d S(   sì   Extract the cookies from the response into a CookieJar.

    :param jar: cookielib.CookieJar (not necessarily a RequestsCookieJar)
    :param request: our own requests.Request object
    :param response: urllib3.HTTPResponse object
    t   _original_responseN(   t   hasattrR3   R   R/   t   msgt   extract_cookies(   t   jarR   t   responset   reqt   res(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   extract_cookies_to_jarr   s    	c         C   s,   t  | ƒ } |  j | ƒ | j ƒ  j d ƒ S(   sO   Produce an appropriate Cookie header string to be sent with `request`, or None.t   Cookie(   R   t   add_cookie_headerR'   R   (   R7   R   t   r(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   get_cookie_headerƒ   s    c         C   sµ   g  } x{ |  D]s } | j  | k r | d k s= | | j k r€ | d k sX | | j k r} | j | j | j | j  f ƒ q} q€ q q Wx* | D]" \ } } } |  j | | | ƒ q‹ Wd S(   sk   Unsets a cookie by name, by default over all domains and paths.

    Wraps CookieJar.clear(), is O(n).
    N(   R   R-   t   domainR   t   appendt   clear(   t	   cookiejarR   R@   R   t
   clearablest   cookie(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   remove_cookie_by_nameŠ   s    ,t   CookieConflictErrorc           B   s   e  Z d  Z RS(   sž   There are two cookies that meet the criteria specified in the cookie jar.
    Use .get and .set and include domain and path args in order to be more specific.(   R*   R+   R,   (    (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyRG   š   s   t   RequestsCookieJarc           B   sï   e  Z d  Z d d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d d d „ Z d d d „ Z d „  Z d „  Z d „  Z RS(   s™  Compatibility class; is a cookielib.CookieJar, but exposes a dict interface.

    This is the CookieJar we create by default for requests and sessions that
    don't specify one, since some clients may expect response.cookies and
    session.cookies to support dict operations.

    Don't use the dict interface internally; it's just for compatibility with
    with external client code. All `requests` code should work out of the box
    with externally provided instances of CookieJar, e.g., LWPCookieJar and
    FileCookieJar.

    Caution: dictionary operations that are normally O(1) may be O(n).

    Unlike a regular CookieJar, this class is pickleable.
    c         C   s0   y |  j  | | | ƒ SWn t k
 r+ | SXd S(   sÍ   Dict-like get() that also supports optional domain and path args in
        order to resolve naming collisions from using one cookie jar over
        multiple domains. Caution: operation is O(n), not O(1).N(   t   _find_no_duplicatest   KeyError(   R
   R   R   R@   R   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyR   °   s    c         K   s|   | d k r; t |  | d | j d ƒ d | j d ƒ ƒd St | t ƒ rY t | ƒ } n t | | |  } |  j | ƒ | S(   s§   Dict-like set() that also supports optional domain and path args in
        order to resolve naming collisions from using one cookie jar over
        multiple domains.R@   R   N(   R-   RF   R   t
   isinstanceR   t   morsel_to_cookiet   create_cookiet
   set_cookie(   R
   R   R%   t   kwargst   c(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   set¹   s    +c         c   s#   x t  |  ƒ D] } | j Vq Wd S(   sy   Dict-like iterkeys() that returns an iterator of names of cookies from the jar.
        See itervalues() and iteritems().N(   t   iterR   (   R
   RE   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   iterkeysÉ   s    c         C   s   t  |  j ƒ  ƒ S(   sh   Dict-like keys() that returns a list of names of cookies from the jar.
        See values() and items().(   t   listRS   (   R
   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   keysÏ   s    c         c   s#   x t  |  ƒ D] } | j Vq Wd S(   sz   Dict-like itervalues() that returns an iterator of values of cookies from the jar.
        See iterkeys() and iteritems().N(   RR   R%   (   R
   RE   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt
   itervaluesÔ   s    c         C   s   t  |  j ƒ  ƒ S(   si   Dict-like values() that returns a list of values of cookies from the jar.
        See keys() and items().(   RT   RV   (   R
   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   valuesÚ   s    c         c   s,   x% t  |  ƒ D] } | j | j f Vq Wd S(   sz   Dict-like iteritems() that returns an iterator of name-value tuples from the jar.
        See iterkeys() and itervalues().N(   RR   R   R%   (   R
   RE   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt	   iteritemsß   s    c         C   s   t  |  j ƒ  ƒ S(   s×   Dict-like items() that returns a list of name-value tuples from the jar.
        See keys() and values(). Allows client-code to call "dict(RequestsCookieJar)
        and get a vanilla python dict of key value pairs.(   RT   RX   (   R
   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   itemså   s    c         C   sC   g  } x6 t  |  ƒ D]( } | j | k r | j | j ƒ q q W| S(   s2   Utility method to list all the domains in the jar.(   RR   R@   RA   (   R
   t   domainsRE   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   list_domainsë   s
    c         C   sC   g  } x6 t  |  ƒ D]( } | j | k r | j | j ƒ q q W| S(   s0   Utility method to list all the paths in the jar.(   RR   R   RA   (   R
   t   pathsRE   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt
   list_pathsó   s
    c         C   sS   g  } xF t  |  ƒ D]8 } | j d k	 r; | j | k r; t S| j | j ƒ q Wt S(   sW   Returns True if there are multiple domains in the jar.
        Returns False otherwise.N(   RR   R@   R-   R   RA   t   False(   R
   RZ   RE   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   multiple_domainsû   s    c         C   sj   i  } x] t  |  ƒ D]O } | d k s4 | j | k r | d k sO | j | k r | j | | j <q q W| S(   s—   Takes as an argument an optional domain and path and returns a plain old
        Python dict of name-value pairs of cookies that meet the requirements.N(   RR   R-   R@   R   R%   R   (   R
   R@   R   t
   dictionaryRE   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   get_dict  s    'c         C   s   |  j  | ƒ S(   sé   Dict-like __getitem__() for compatibility with client code. Throws exception
        if there are more than one cookie with name. In that case, use the more
        explicit get() method instead. Caution: operation is O(n), not O(1).(   RI   (   R
   R   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   __getitem__  s    c         C   s   |  j  | | ƒ d S(   sÊ   Dict-like __setitem__ for compatibility with client code. Throws exception
        if there is already a cookie of that name in the jar. In that case, use the more
        explicit set() method instead.N(   RQ   (   R
   R   R%   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   __setitem__  s    c         C   s   t  |  | ƒ d S(   sS   Deletes a cookie given a name. Wraps cookielib.CookieJar's remove_cookie_by_name().N(   RF   (   R
   R   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   __delitem__  s    c         O   sm   t  | j d ƒ rQ | j j d ƒ rQ | j j d ƒ rQ | j j d d ƒ | _ n  t t |  ƒ j | | | Ž S(   Nt
   startswitht   "s   \"t    (   R4   R%   Re   t   endswitht   replacet   superRH   RN   (   R
   RE   t   argsRO   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyRN   !  s    6c         C   sM   t  | t j ƒ r3 x4 | D] } |  j | ƒ q Wn t t |  ƒ j | ƒ d S(   sA   Updates this jar with cookies from another CookieJar or dict-likeN(   RK   R    t	   CookieJarRN   Rj   RH   t   update(   R
   t   otherRE   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyRm   &  s    c         C   s†   xf t  |  ƒ D]X } | j | k r | d k s= | j | k re | d k sX | j | k rb | j Sqe q q Wt d | | | f ƒ ‚ d S(   s6  Requests uses this method internally to get cookie values. Takes as args name
        and optional domain and path. Returns a cookie.value. If there are conflicting cookies,
        _find arbitrarily chooses one. See _find_no_duplicates if you want an exception thrown
        if there are conflicting cookies.s   name=%r, domain=%r, path=%rN(   RR   R   R-   R@   R   R%   RJ   (   R
   R   R@   R   RE   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   _find.  s    c         C   sº   d } xŠ t |  ƒ D]| } | j | k r | d k sC | j | k r | d k s^ | j | k rŒ | d k	 r} t d | ƒ ‚ n  | j } qŒ q q q W| r | St d | | | f ƒ ‚ d S(   sA  __get_item__ and get call _find_no_duplicates -- never used in Requests internally.
        Takes as args name and optional domain and path. Returns a cookie.value.
        Throws KeyError if cookie is not found and CookieConflictError if there are
        multiple cookies that match name and optionally domain and path.s(   There are multiple cookies with name, %rs   name=%r, domain=%r, path=%rN(   R-   RR   R   R@   R   RG   R%   RJ   (   R
   R   R@   R   t   toReturnRE   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyRI   ;  s    c         C   s    |  j  j ƒ  } | j d ƒ | S(   s4   Unlike a normal CookieJar, this class is pickleable.t   _cookies_lock(   t   __dict__t   copyt   pop(   R
   t   state(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   __getstate__M  s    c         C   s5   |  j  j | ƒ d |  j  k r1 t j ƒ  |  _ n  d S(   s4   Unlike a normal CookieJar, this class is pickleable.Rq   N(   Rr   Rm   t	   threadingt   RLockRq   (   R
   Ru   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   __setstate__T  s    c         C   s   t  ƒ  } | j |  ƒ | S(   s(   Return a copy of this RequestsCookieJar.(   RH   Rm   (   R
   t   new_cj(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyRs   Z  s    	N(   R*   R+   R,   R-   R   RQ   RS   RU   RV   RW   RX   RY   R[   R]   R_   Ra   Rb   Rc   Rd   RN   Rm   Ro   RI   Rv   Ry   Rs   (    (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyRH   Ÿ   s.   											

							c         K   s  t  d d d |  d | d d d d d d	 d
 t d d d t d d d d d i d d 6d t ƒ } t | ƒ t | ƒ } | r™ d } t | t | ƒ ƒ ‚ n  | j | ƒ t | d ƒ | d <t | d ƒ | d <| d j	 d ƒ | d <t | d ƒ | d <t
 j |   S(   sÊ   Make a cookie from underspecified parameters.

    By default, the pair of `name` and `value` will be set for the domain ''
    and sent on every request (this is sometimes called a "supercookie").
    t   versioni    R   R%   t   portR@   Rg   R   t   /t   securet   expirest   discardt   commentt   comment_urlt   restt   HttpOnlyt   rfc2109s4   create_cookie() got unexpected keyword arguments: %st   port_specifiedt   domain_specifiedt   .t   domain_initial_dott   path_specifiedN(   t   dictR-   R^   R   RQ   t	   TypeErrorRT   Rm   t   boolRe   R    R<   (   R   R%   RO   t   resultt   badargst   err(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyRM   a  s0    	c         C   sð   d } |  d r' t j ƒ  |  d } n9 |  d r` d } t j t j |  d | ƒ ƒ t j } n  t d |  d d t |  d ƒ d t d |  d d | d |  j d	 |  d	 d
 d d i |  d d 6d t d t |  d ƒ d |  j	 d |  d pì d ƒ S(   sB   Convert a Morsel object into a Cookie containing the one k/v pair.s   max-ageR   s   %a, %d-%b-%Y %H:%M:%S GMTR   R‚   R€   R@   R   R   R|   Rƒ   t   httponlyR„   R…   R~   R%   R{   i    N(
   R-   t   timet   mktimet   strptimet   timezoneRM   R   R^   R"   R%   (   t   morselR   t   time_template(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyRL   „  s*    

#

	
	c         C   s‡   | d k r t ƒ  } n  |  d k	 rƒ g  | D] } | j ^ q+ } x@ |  D]5 } | s_ | | k rG | j t | |  | ƒ ƒ qG qG Wn  | S(   s-  Returns a CookieJar from a key/value dictionary.

    :param cookie_dict: Dict of key/values to insert into CookieJar.
    :param cookiejar: (optional) A cookiejar to add the cookies to.
    :param overwrite: (optional) If False, will not replace cookies
        already in the jar with new ones.
    N(   R-   RH   R   RN   RM   (   t   cookie_dictRC   t	   overwriteRE   t   names_from_jarR   (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   cookiejar_from_dictŸ  s    $c         C   s§   t  |  t j ƒ s! t d ƒ ‚ n  t  | t ƒ rK t | d |  d t ƒ}  nX t  | t j ƒ r£ y |  j | ƒ Wq£ t k
 rŸ x | D] } |  j	 | ƒ q… Wq£ Xn  |  S(   s¼   Add cookies to cookiejar and returns a merged CookieJar.

    :param cookiejar: CookieJar object to add the cookies to.
    :param cookies: Dictionary or CookieJar object to be added.
    s!   You can only merge into CookieJarRC   R™   (
   RK   R    Rl   t
   ValueErrorR‹   R›   R^   Rm   t   AttributeErrorRN   (   RC   t   cookiest   cookie_in_jar(    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   merge_cookies³  s    (   R,   R’   t   collectionst   compatR    R   R   R   Rw   t   ImportErrort   dummy_threadingt   objectR   R/   R;   R?   R-   RF   t   RuntimeErrorRG   Rl   t   MutableMappingRH   RM   RL   R   R›   R    (    (    (    s4   /usr/lib/python2.7/dist-packages/requests/cookies.pyt   <module>   s&   "H		Â	#	