3

V'ícþGã
@súdZddlZddlZddlZddlmZddlmZmZm	Z	m
Z
mZyddlZWne
k
rlddlZYnXGdd„deƒZGdd	„d	eƒZd
d„Zdd
„Zddd„ZGdd„deƒZGdd„dejeƒZdd„Zdd„Zdd„Zd dd„Zdd„ZdS)!z­
requests.cookies
~~~~~~~~~~~~~~~~

Compatibility code to be able to use `cookielib.CookieJar` with requests.

requests.utils imports from here, so be careful with imports.
éNé)Úto_native_string)Ú	cookielibÚurlparseÚ
urlunparseÚMorselÚMutableMappingc@sŽeZdZdZdd„Zdd„Zdd„Zdd	„Zd
d„Zdd
„Z	dd„Z
ddd„Zdd„Zdd„Z
dd„Zedd„ƒZedd„ƒZedd„ƒZdS) ÚMockRequestaò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.
    cCs ||_i|_t|jjƒj|_dS)N)Ú_rÚ_new_headersrÚurlÚschemeÚtype)ÚselfÚrequest©rú4/tmp/pip-build-gk9425m9/requests/requests/cookies.pyÚ__init__%szMockRequest.__init__cCs|jS)N)r)rrrrÚget_type*szMockRequest.get_typecCst|jjƒjS)N)rr
rÚnetloc)rrrrÚget_host-szMockRequest.get_hostcCs|jƒS)N)r)rrrrÚget_origin_req_host0szMockRequest.get_origin_req_hostcCsT|jjjdƒs|jjSt|jjddd}t|jjƒ}t|j||j|j	|j
|jgƒS)NÚHostzutf-8)Úencoding)r
ÚheadersÚgetrrrrr
ÚpathÚparamsÚqueryÚfragment)rÚhostÚparsedrrrÚget_full_url3szMockRequest.get_full_urlcCsdS)NTr)rrrrÚis_unverifiableAszMockRequest.is_unverifiablecCs||jjkp||jkS)N)r
rr)rÚnamerrrÚ
has_headerDszMockRequest.has_headerNcCs|jjj||jj||ƒƒS)N)r
rrr)rr$ÚdefaultrrrÚ
get_headerGszMockRequest.get_headercCstdƒ‚dS)zMcookielib has no legitimate use for this method; add it back if you find one.z=Cookie headers should be added with add_unredirected_header()N)ÚNotImplementedError)rÚkeyÚvalrrrÚ
add_headerJszMockRequest.add_headercCs||j|<dS)N)r)rr$ÚvaluerrrÚadd_unredirected_headerNsz#MockRequest.add_unredirected_headercCs|jS)N)r)rrrrÚget_new_headersQszMockRequest.get_new_headerscCs|jƒS)N)r#)rrrrÚunverifiableTszMockRequest.unverifiablecCs|jƒS)N)r)rrrrÚorigin_req_hostXszMockRequest.origin_req_hostcCs|jƒS)N)r)rrrrr \szMockRequest.host)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rrrrr"r#r%r'r+r-r.Úpropertyr/r0r rrrrr	s

r	c@s(eZdZdZdd„Zdd„Zdd„ZdS)	ÚMockResponsezÀ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.
    cCs
||_dS)z†Make a MockResponse for `cookielib` to read.

        :param headers: a httplib.HTTPMessage or analogous carrying the headers
        N)Ú_headers)rrrrrrhszMockResponse.__init__cCs|jS)N)r7)rrrrÚinfooszMockResponse.infocCs|jj|ƒdS)N)r7Ú
getheaders)rr$rrrr9rszMockResponse.getheadersN)r1r2r3r4rr8r9rrrrr6asr6cCs8t|dƒo|jsdSt|ƒ}t|jjƒ}|j||ƒdS)zì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
    Ú_original_responseN)Úhasattrr:r	r6ÚmsgÚextract_cookies)ÚjarrÚresponseÚreqÚresrrrÚextract_cookies_to_jarvs
rBcCs t|ƒ}|j|ƒ|jƒjdƒS)zj
    Produce an appropriate Cookie header string to be sent with `request`, or None.

    :rtype: str
    ÚCookie)r	Úadd_cookie_headerr.r)r>rÚrrrrÚget_cookie_header‡s
rFcCs‚g}xV|D]N}|j|krq
|dk	r.||jkr.q
|dk	rB||jkrBq
|j|j|j|jfƒq
Wx |D]\}}}|j|||ƒqbWdS)zkUnsets a cookie by name, by default over all domains and paths.

    Wraps CookieJar.clear(), is O(n).
    N)r$ÚdomainrÚappendÚclear)Ú	cookiejarr$rGrÚ
clearablesÚcookierrrÚremove_cookie_by_name’s

rMc@seZdZdZdS)ÚCookieConflictErrorz£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.
    N)r1r2r3r4rrrrrN¥srNcsèeZdZdZd3dd„Zdd„Zdd„Zd	d
„Zdd„Zd
d„Z	dd„Z
dd„Zdd„Zdd„Z
dd„Zd4dd„Z‡fdd„Zdd„Zdd „Zd!d"„Z‡fd#d$„Z‡fd%d&„Zd5d'd(„Zd6d)d*„Zd+d,„Zd-d.„Zd/d0„Zd1d2„Z‡ZS)7ÚRequestsCookieJara±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.

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

    Unlike a regular CookieJar, this class is pickleable.

    .. warning:: dictionary operations that are normally O(1) may be O(n).
    NcCs(y|j|||ƒStk
r"|SXdS)zã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.

        .. warning:: operation is O(n), not O(1).
        N)Ú_find_no_duplicatesÚKeyError)rr$r&rGrrrrr½szRequestsCookieJar.getcKsX|dkr(t|||jdƒ|jdƒddSt|tƒr<t|ƒ}nt||f|Ž}|j|ƒ|S)z°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.
        NrGr)rGr)rMrÚ
isinstancerÚmorsel_to_cookieÚ
create_cookieÚ
set_cookie)rr$r,ÚkwargsÚcrrrÚsetÉs


zRequestsCookieJar.setccsxt|ƒD]}|jVq
WdS)z”Dict-like iterkeys() that returns an iterator of names of cookies
        from the jar.

        .. seealso:: itervalues() and iteritems().
        N)Úiterr$)rrLrrrÚiterkeysÚszRequestsCookieJar.iterkeyscCst|jƒƒS)zƒDict-like keys() that returns a list of names of cookies from the
        jar.

        .. seealso:: values() and items().
        )ÚlistrZ)rrrrÚkeysãszRequestsCookieJar.keysccsxt|ƒD]}|jVq
WdS)z•Dict-like itervalues() that returns an iterator of values of cookies
        from the jar.

        .. seealso:: iterkeys() and iteritems().
        N)rYr,)rrLrrrÚ
itervaluesëszRequestsCookieJar.itervaluescCst|jƒƒS)z„Dict-like values() that returns a list of values of cookies from the
        jar.

        .. seealso:: keys() and items().
        )r[r])rrrrÚvaluesôszRequestsCookieJar.valuesccs$xt|ƒD]}|j|jfVq
WdS)z•Dict-like iteritems() that returns an iterator of name-value tuples
        from the jar.

        .. seealso:: iterkeys() and itervalues().
        N)rYr$r,)rrLrrrÚ	iteritemsüszRequestsCookieJar.iteritemscCst|jƒƒS)zõDict-like items() that returns a list of name-value tuples from the
        jar. Allows client-code to call ``dict(RequestsCookieJar)`` and get a
        vanilla python dict of key value pairs.

        .. seealso:: keys() and values().
        )r[r_)rrrrÚitemsszRequestsCookieJar.itemscCs0g}x&t|ƒD]}|j|kr|j|jƒqW|S)z2Utility method to list all the domains in the jar.)rYrGrH)rÚdomainsrLrrrÚlist_domainss

zRequestsCookieJar.list_domainscCs0g}x&t|ƒD]}|j|kr|j|jƒqW|S)z0Utility method to list all the paths in the jar.)rYrrH)rÚpathsrLrrrÚ
list_pathss

zRequestsCookieJar.list_pathscCs>g}x4t|ƒD](}|jdk	r*|j|kr*dS|j|jƒqWdS)zvReturns True if there are multiple domains in the jar.
        Returns False otherwise.

        :rtype: bool
        NTF)rYrGrH)rrarLrrrÚmultiple_domainssz"RequestsCookieJar.multiple_domainscCsJi}x@t|ƒD]4}|dks$|j|kr|dks6|j|kr|j||j<qW|S)z¾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.

        :rtype: dict
        N)rYrGrr,r$)rrGrÚ
dictionaryrLrrrÚget_dict+szRequestsCookieJar.get_dictcs*ytt|ƒj|ƒStk
r$dSXdS)NT)ÚsuperrOÚ__contains__rN)rr$)Ú	__class__rrri;szRequestsCookieJar.__contains__cCs
|j|ƒS)zÿ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.

        .. warning:: operation is O(n), not O(1).
        )rP)rr$rrrÚ__getitem__AszRequestsCookieJar.__getitem__cCs|j||ƒdS)zÓ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)rX)rr$r,rrrÚ__setitem__JszRequestsCookieJar.__setitem__cCst||ƒdS)zlDeletes a cookie given a name. Wraps ``cookielib.CookieJar``'s
        ``remove_cookie_by_name()``.
        N)rM)rr$rrrÚ__delitem__QszRequestsCookieJar.__delitem__csLt|jdƒr4|jjdƒr4|jjdƒr4|jjddƒ|_tt|ƒj|f|ž|ŽS)NÚ
startswithú"z\"Ú)r;r,rnÚendswithÚreplacerhrOrU)rrLÚargsrV)rjrrrUWs$zRequestsCookieJar.set_cookiecs@t|tjƒr,x.|D]}|jtj|ƒƒqWntt|ƒj|ƒdS)zAUpdates this jar with cookies from another CookieJar or dict-likeN)rRrÚ	CookieJarrUÚcopyrhrOÚupdate)rÚotherrL)rjrrrv\s
zRequestsCookieJar.updatecCs\xDt|ƒD]8}|j|kr
|dks*|j|kr
|dks<|j|kr
|jSq
Wtd|||fƒ‚dS)aÔRequests uses this method internally to get cookie values.

        If there are conflicting cookies, _find arbitrarily chooses one.
        See _find_no_duplicates if you want an exception thrown if there are
        conflicting cookies.

        :param name: a string containing name of cookie
        :param domain: (optional) string containing domain of cookie
        :param path: (optional) string containing path of cookie
        :return: cookie.value
        Nzname=%r, domain=%r, path=%r)rYr$rGrr,rQ)rr$rGrrLrrrÚ_findds

zRequestsCookieJar._findcCs|d}xXt|ƒD]L}|j|kr|dks.|j|kr|dks@|j|kr|dk	rTtd|ƒ‚|j}qW|rf|Std|||fƒ‚dS)aùBoth ``__get_item__`` and ``get`` call this function: it's never
        used elsewhere in Requests.

        :param name: a string containing name of cookie
        :param domain: (optional) string containing domain of cookie
        :param path: (optional) string containing path of cookie
        :raises KeyError: if cookie is not found
        :raises CookieConflictError: if there are multiple cookies
            that match name and optionally domain and path
        :return: cookie.value
        Nz(There are multiple cookies with name, %rzname=%r, domain=%r, path=%r)rYr$rGrrNr,rQ)rr$rGrÚtoReturnrLrrrrPxs

z%RequestsCookieJar._find_no_duplicatescCs|jjƒ}|jdƒ|S)z4Unlike a normal CookieJar, this class is pickleable.Ú
_cookies_lock)Ú__dict__ruÚpop)rÚstaterrrÚ__getstate__‘s

zRequestsCookieJar.__getstate__cCs$|jj|ƒd|jkr tjƒ|_dS)z4Unlike a normal CookieJar, this class is pickleable.rzN)r{rvÚ	threadingÚRLockrz)rr}rrrÚ__setstate__˜s
zRequestsCookieJar.__setstate__cCs"tƒ}|j|jƒƒ|j|ƒ|S)z(Return a copy of this RequestsCookieJar.)rOÚ
set_policyÚ
get_policyrv)rÚnew_cjrrrružs
zRequestsCookieJar.copycCs|jS)z&Return the CookiePolicy instance used.)Ú_policy)rrrrrƒ¥szRequestsCookieJar.get_policy)NNN)NN)NN)NN)r1r2r3r4rrXrZr\r]r^r_r`rbrdrergrirkrlrmrUrvrxrPr~rrurƒÚ
__classcell__rr)rjrrO«s2
				
	

rOcCsR|dkrdSt|dƒr|jƒStj|ƒ}|jƒx|D]}|jtj|ƒƒq6W|S)Nru)r;rurIrU)r>Únew_jarrLrrrÚ_copy_cookie_jarªs


rˆcKs¤d||ddddddddddiddœ
}t|ƒt|ƒ}|rLd	}t|t|ƒƒ‚|j|ƒt|d
ƒ|d<t|dƒ|d
<|djdƒ|d<t|dƒ|d<tjf|ŽS)zÊ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").
    rNrpú/FTÚHttpOnly)
Úversionr$r,ÚportrGrÚsecureÚexpiresÚdiscardÚcommentÚcomment_urlÚrestÚrfc2109z4create_cookie() got unexpected keyword arguments: %srŒÚport_specifiedrGÚdomain_specifiedÚ.Údomain_initial_dotrÚpath_specified)rXÚ	TypeErrorr[rvÚboolrnrrC)r$r,rVÚresultÚbadargsÚerrrrrrT¹s.
rTcCsÄd}|drPyttjƒt|dƒƒ}Wqrtk
rLtd|dƒ‚YqrXn"|drrd}tjtj|d|ƒƒ}t|dt|dƒd|d||j	|d	dd
|didt|dƒ|j
|d
p¾dd
S)zBConvert a Morsel object into a Cookie containing the one k/v pair.Nzmax-agezmax-age: %s must be integerrŽz%a, %d-%b-%Y %H:%M:%S GMTrFrGrrŠÚhttponlyrr‹r)
rr‘rrGrŽr$rrŒr’r“rr,r‹)ÚintÚtimeÚ
ValueErrorr™ÚcalendarÚtimegmÚstrptimerTršr)r,)ÚmorselrŽÚ
time_templaterrrrSÝs0


rSTcCsV|dkrtƒ}|dk	rRdd„|Dƒ}x,|D]$}|s:||kr*|jt|||ƒƒq*W|S)aCReturns 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.
    :rtype: CookieJar
    NcSsg|]
}|j‘qSr)r$)Ú.0rLrrrú
<listcomp>	sz'cookiejar_from_dict.<locals>.<listcomp>)rOrUrT)Úcookie_dictrJÚ	overwriteÚnames_from_jarr$rrrÚcookiejar_from_dictüs	
r¬cCszt|tjƒstdƒ‚t|tƒr.t||dd}nHt|tjƒrvy|j|ƒWn,tk
rtx|D]}|j|ƒq^WYnX|S)zÒ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.
    :rtype: CookieJar
    z!You can only merge into CookieJarF)rJrª)	rRrrtr¡Údictr¬rvÚAttributeErrorrU)rJÚcookiesÚ
cookie_in_jarrrrÚ
merge_cookiess

r±)NN)NT)r4rur r¢Ú_internal_utilsrÚcompatrrrrrrÚImportErrorÚdummy_threadingÚobjectr	r6rBrFrMÚRuntimeErrorrNrtrOrˆrTrSr¬r±rrrrÚ<module>
s.H
$