|
@@ -157,26 +157,28 @@ class CookieConflictError(RuntimeError):
|
|
|
|
|
|
|
|
|
|
|
|
|
class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
|
|
class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
|
|
|
- """Compatibility class; is a cookielib.CookieJar, but exposes a dict interface.
|
|
|
|
|
|
|
+ """Compatibility class; is a cookielib.CookieJar, but exposes a dict
|
|
|
|
|
+ interface.
|
|
|
|
|
|
|
|
This is the CookieJar we create by default for requests and sessions that
|
|
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
|
|
don't specify one, since some clients may expect response.cookies and
|
|
|
session.cookies to support dict operations.
|
|
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).
|
|
|
|
|
|
|
+ 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.
|
|
Unlike a regular CookieJar, this class is pickleable.
|
|
|
- """
|
|
|
|
|
|
|
|
|
|
|
|
+ .. warning:: dictionary operations that are normally O(1) may be O(n).
|
|
|
|
|
+ """
|
|
|
def get(self, name, default=None, domain=None, path=None):
|
|
def get(self, name, default=None, domain=None, path=None):
|
|
|
"""Dict-like get() that also supports optional domain and path args in
|
|
"""Dict-like get() that also supports optional domain and path args in
|
|
|
order to resolve naming collisions from using one cookie jar over
|
|
order to resolve naming collisions from using one cookie jar over
|
|
|
- multiple domains. Caution: operation is O(n), not O(1)."""
|
|
|
|
|
|
|
+ multiple domains.
|
|
|
|
|
+
|
|
|
|
|
+ .. warning:: operation is O(n), not O(1)."""
|
|
|
try:
|
|
try:
|
|
|
return self._find_no_duplicates(name, domain, path)
|
|
return self._find_no_duplicates(name, domain, path)
|
|
|
except KeyError:
|
|
except KeyError:
|
|
@@ -199,37 +201,38 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
|
|
|
return c
|
|
return c
|
|
|
|
|
|
|
|
def iterkeys(self):
|
|
def iterkeys(self):
|
|
|
- """Dict-like iterkeys() that returns an iterator of names of cookies from the jar.
|
|
|
|
|
- See itervalues() and iteritems()."""
|
|
|
|
|
|
|
+ """Dict-like iterkeys() that returns an iterator of names of cookies
|
|
|
|
|
+ from the jar. See itervalues() and iteritems()."""
|
|
|
for cookie in iter(self):
|
|
for cookie in iter(self):
|
|
|
yield cookie.name
|
|
yield cookie.name
|
|
|
|
|
|
|
|
def keys(self):
|
|
def keys(self):
|
|
|
- """Dict-like keys() that returns a list of names of cookies from the jar.
|
|
|
|
|
- See values() and items()."""
|
|
|
|
|
|
|
+ """Dict-like keys() that returns a list of names of cookies from the
|
|
|
|
|
+ jar. See values() and items()."""
|
|
|
return list(self.iterkeys())
|
|
return list(self.iterkeys())
|
|
|
|
|
|
|
|
def itervalues(self):
|
|
def itervalues(self):
|
|
|
- """Dict-like itervalues() that returns an iterator of values of cookies from the jar.
|
|
|
|
|
- See iterkeys() and iteritems()."""
|
|
|
|
|
|
|
+ """Dict-like itervalues() that returns an iterator of values of cookies
|
|
|
|
|
+ from the jar. See iterkeys() and iteritems()."""
|
|
|
for cookie in iter(self):
|
|
for cookie in iter(self):
|
|
|
yield cookie.value
|
|
yield cookie.value
|
|
|
|
|
|
|
|
def values(self):
|
|
def values(self):
|
|
|
- """Dict-like values() that returns a list of values of cookies from the jar.
|
|
|
|
|
- See keys() and items()."""
|
|
|
|
|
|
|
+ """Dict-like values() that returns a list of values of cookies from the
|
|
|
|
|
+ jar. See keys() and items()."""
|
|
|
return list(self.itervalues())
|
|
return list(self.itervalues())
|
|
|
|
|
|
|
|
def iteritems(self):
|
|
def iteritems(self):
|
|
|
- """Dict-like iteritems() that returns an iterator of name-value tuples from the jar.
|
|
|
|
|
- See iterkeys() and itervalues()."""
|
|
|
|
|
|
|
+ """Dict-like iteritems() that returns an iterator of name-value tuples
|
|
|
|
|
+ from the jar. See iterkeys() and itervalues()."""
|
|
|
for cookie in iter(self):
|
|
for cookie in iter(self):
|
|
|
yield cookie.name, cookie.value
|
|
yield cookie.name, cookie.value
|
|
|
|
|
|
|
|
def items(self):
|
|
def items(self):
|
|
|
- """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."""
|
|
|
|
|
|
|
+ """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."""
|
|
|
return list(self.iteritems())
|
|
return list(self.iteritems())
|
|
|
|
|
|
|
|
def list_domains(self):
|
|
def list_domains(self):
|
|
@@ -259,8 +262,9 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
|
|
|
return False # there is only one domain in jar
|
|
return False # there is only one domain in jar
|
|
|
|
|
|
|
|
def get_dict(self, domain=None, path=None):
|
|
def get_dict(self, domain=None, path=None):
|
|
|
- """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."""
|
|
|
|
|
|
|
+ """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."""
|
|
|
dictionary = {}
|
|
dictionary = {}
|
|
|
for cookie in iter(self):
|
|
for cookie in iter(self):
|
|
|
if (domain is None or cookie.domain == domain) and (path is None
|
|
if (domain is None or cookie.domain == domain) and (path is None
|
|
@@ -269,21 +273,24 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
|
|
|
return dictionary
|
|
return dictionary
|
|
|
|
|
|
|
|
def __getitem__(self, name):
|
|
def __getitem__(self, name):
|
|
|
- """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)."""
|
|
|
|
|
|
|
+ """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)."""
|
|
|
|
|
|
|
|
return self._find_no_duplicates(name)
|
|
return self._find_no_duplicates(name)
|
|
|
|
|
|
|
|
def __setitem__(self, name, value):
|
|
def __setitem__(self, name, value):
|
|
|
- """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."""
|
|
|
|
|
|
|
+ """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."""
|
|
|
|
|
|
|
|
self.set(name, value)
|
|
self.set(name, value)
|
|
|
|
|
|
|
|
def __delitem__(self, name):
|
|
def __delitem__(self, name):
|
|
|
- """Deletes a cookie given a name. Wraps cookielib.CookieJar's remove_cookie_by_name()."""
|
|
|
|
|
|
|
+ """Deletes a cookie given a name. Wraps ``cookielib.CookieJar``'s
|
|
|
|
|
+ ``remove_cookie_by_name()``."""
|
|
|
remove_cookie_by_name(self, name)
|
|
remove_cookie_by_name(self, name)
|
|
|
|
|
|
|
|
def set_cookie(self, cookie, *args, **kwargs):
|
|
def set_cookie(self, cookie, *args, **kwargs):
|
|
@@ -300,10 +307,11 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
|
|
|
super(RequestsCookieJar, self).update(other)
|
|
super(RequestsCookieJar, self).update(other)
|
|
|
|
|
|
|
|
def _find(self, name, domain=None, path=None):
|
|
def _find(self, name, domain=None, path=None):
|
|
|
- """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."""
|
|
|
|
|
|
|
+ """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."""
|
|
|
for cookie in iter(self):
|
|
for cookie in iter(self):
|
|
|
if cookie.name == name:
|
|
if cookie.name == name:
|
|
|
if domain is None or cookie.domain == domain:
|
|
if domain is None or cookie.domain == domain:
|
|
@@ -313,10 +321,11 @@ class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):
|
|
|
raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
|
|
raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))
|
|
|
|
|
|
|
|
def _find_no_duplicates(self, name, domain=None, path=None):
|
|
def _find_no_duplicates(self, name, domain=None, path=None):
|
|
|
- """__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."""
|
|
|
|
|
|
|
+ """Both ``__get_item__`` and ``get`` call this function: it's never
|
|
|
|
|
+ used elsewhere in Requests. 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."""
|
|
|
toReturn = None
|
|
toReturn = None
|
|
|
for cookie in iter(self):
|
|
for cookie in iter(self):
|
|
|
if cookie.name == name:
|
|
if cookie.name == name:
|
|
@@ -440,7 +449,7 @@ def merge_cookies(cookiejar, cookies):
|
|
|
"""
|
|
"""
|
|
|
if not isinstance(cookiejar, cookielib.CookieJar):
|
|
if not isinstance(cookiejar, cookielib.CookieJar):
|
|
|
raise ValueError('You can only merge into CookieJar')
|
|
raise ValueError('You can only merge into CookieJar')
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
if isinstance(cookies, dict):
|
|
if isinstance(cookies, dict):
|
|
|
cookiejar = cookiejar_from_dict(
|
|
cookiejar = cookiejar_from_dict(
|
|
|
cookies, cookiejar=cookiejar, overwrite=False)
|
|
cookies, cookiejar=cookiejar, overwrite=False)
|