Clean up cookie docs and display them.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Cory Benfield · committed by Cory Benfield · parent b831317 · Browse files · 2 files·+54 added−40 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+54 added−40 removed
- other:+54 added−40 removed
Provenance
- Commit
f4ec3d2- Tree
1a0c1ba- Author
Cory Benfield <lukasaoz@gmail.com>- Committer
Cory Benfield <lukasaoz@gmail.com>- Parent
b831317
2 files changed+54 added−40 removed
modified:docs/api.rst+6 added
86 unmodified lines87 .. autofunction:: requests.utils.dict_from_cookiejar88 .. autofunction:: requests.utils.cookiejar_from_dict89 .. autofunction:: requests.utils.add_dict_to_cookiejar90 91+.. autoclass:: requests.cookies.RequestsCookieJar92+ :inherited-members:93 94+.. autoclass:: requests.cookies.CookieConflictError95+ :inherited-members:96+97+98 Encodings99 ~~~~~~~~~100 101 .. autofunction:: requests.utils.get_encodings_from_content164 unmodified linesmodified:requests/cookies.py+48 added-40 removed
155 unmodified lines156 Use .get and .set and include domain and path args in order to be more specific."""157 158 159 class RequestsCookieJar(cookielib.CookieJar, collections.MutableMapping):160- """Compatibility class; is a cookielib.CookieJar, but exposes a dict interface.160+ """Compatibility class; is a cookielib.CookieJar, but exposes a dict161+ interface.162 163 This is the CookieJar we create by default for requests and sessions that164 don't specify one, since some clients may expect response.cookies and165 session.cookies to support dict operations.166 166- Don't use the dict interface internally; it's just for compatibility with167- with external client code. All `requests` code should work out of the box168- with externally provided instances of CookieJar, e.g., LWPCookieJar and169- FileCookieJar.167+ Requests does not use the dict interface internally; it's just for168+ compatibility with external client code. All `requests` code should work169+ out of the box with externally provided instances of ``CookieJar``, e.g.170+ ``LWPCookieJar`` and ``FileCookieJar``.171 171- Caution: dictionary operations that are normally O(1) may be O(n).172-172 Unlike a regular CookieJar, this class is pickleable.174- """173 174+ .. warning:: dictionary operations that are normally O(1) may be O(n).175+ """176 def get(self, name, default=None, domain=None, path=None):177 """Dict-like get() that also supports optional domain and path args in178 order to resolve naming collisions from using one cookie jar over179- multiple domains. Caution: operation is O(n), not O(1)."""179+ multiple domains.180+181+ .. warning:: operation is O(n), not O(1)."""182 try:183 return self._find_no_duplicates(name, domain, path)184 except KeyError:185 return default14 unmodified lines200 self.set_cookie(c)201 return c202 203 def iterkeys(self):202- """Dict-like iterkeys() that returns an iterator of names of cookies from the jar.203- See itervalues() and iteritems()."""204+ """Dict-like iterkeys() that returns an iterator of names of cookies205+ from the jar. See itervalues() and iteritems()."""206 for cookie in iter(self):207 yield cookie.name208 209 def keys(self):208- """Dict-like keys() that returns a list of names of cookies from the jar.209- See values() and items()."""210+ """Dict-like keys() that returns a list of names of cookies from the211+ jar. See values() and items()."""212 return list(self.iterkeys())213 214 def itervalues(self):213- """Dict-like itervalues() that returns an iterator of values of cookies from the jar.214- See iterkeys() and iteritems()."""215+ """Dict-like itervalues() that returns an iterator of values of cookies216+ from the jar. See iterkeys() and iteritems()."""217 for cookie in iter(self):218 yield cookie.value219 220 def values(self):219- """Dict-like values() that returns a list of values of cookies from the jar.220- See keys() and items()."""221+ """Dict-like values() that returns a list of values of cookies from the222+ jar. See keys() and items()."""223 return list(self.itervalues())224 225 def iteritems(self):224- """Dict-like iteritems() that returns an iterator of name-value tuples from the jar.225- See iterkeys() and itervalues()."""226+ """Dict-like iteritems() that returns an iterator of name-value tuples227+ from the jar. See iterkeys() and itervalues()."""228 for cookie in iter(self):229 yield cookie.name, cookie.value230 231 def items(self):230- """Dict-like items() that returns a list of name-value tuples from the jar.231- See keys() and values(). Allows client-code to call "dict(RequestsCookieJar)232- and get a vanilla python dict of key value pairs."""232+ """Dict-like items() that returns a list of name-value tuples from the233+ jar. See keys() and values(). Allows client-code to call234+ ``dict(RequestsCookieJar)`` and get a vanilla python dict of key value235+ pairs."""236 return list(self.iteritems())237 238 def list_domains(self):239 """Utility method to list all the domains in the jar."""21 unmodified lines261 domains.append(cookie.domain)262 return False # there is only one domain in jar263 264 def get_dict(self, domain=None, path=None):262- """Takes as an argument an optional domain and path and returns a plain old263- Python dict of name-value pairs of cookies that meet the requirements."""265+ """Takes as an argument an optional domain and path and returns a plain266+ old Python dict of name-value pairs of cookies that meet the267+ requirements."""268 dictionary = {}269 for cookie in iter(self):270 if (domain is None or cookie.domain == domain) and (path is None271 or cookie.path == path):272 dictionary[cookie.name] = cookie.value273 return dictionary274 275 def __getitem__(self, name):272- """Dict-like __getitem__() for compatibility with client code. Throws exception273- if there are more than one cookie with name. In that case, use the more274- explicit get() method instead. Caution: operation is O(n), not O(1)."""276+ """Dict-like __getitem__() for compatibility with client code. Throws277+ exception if there are more than one cookie with name. In that case,278+ use the more explicit get() method instead. Caution: operation is O(n),279+ not O(1)."""280 281 return self._find_no_duplicates(name)282 283 def __setitem__(self, name, value):279- """Dict-like __setitem__ for compatibility with client code. Throws exception280- if there is already a cookie of that name in the jar. In that case, use the more281- explicit set() method instead."""284+ """Dict-like __setitem__ for compatibility with client code. Throws285+ exception if there is already a cookie of that name in the jar. In that286+ case, use the more explicit set() method instead."""287 288 self.set(name, value)289 290 def __delitem__(self, name):286- """Deletes a cookie given a name. Wraps cookielib.CookieJar's remove_cookie_by_name()."""291+ """Deletes a cookie given a name. Wraps ``cookielib.CookieJar``'s292+ ``remove_cookie_by_name()``."""293 remove_cookie_by_name(self, name)294 295 def set_cookie(self, cookie, *args, **kwargs):296 if hasattr(cookie.value, 'startswith') and cookie.value.startswith('"') and cookie.value.endswith('"'):8 unmodified lines305 else:306 super(RequestsCookieJar, self).update(other)307 308 def _find(self, name, domain=None, path=None):303- """Requests uses this method internally to get cookie values. Takes as args name304- and optional domain and path. Returns a cookie.value. If there are conflicting cookies,305- _find arbitrarily chooses one. See _find_no_duplicates if you want an exception thrown306- if there are conflicting cookies."""309+ """Requests uses this method internally to get cookie values. Takes as310+ args name and optional domain and path. Returns a cookie.value. If311+ there are conflicting cookies, _find arbitrarily chooses one. See312+ _find_no_duplicates if you want an exception thrown if there are313+ conflicting cookies."""314 for cookie in iter(self):315 if cookie.name == name:316 if domain is None or cookie.domain == domain:317 if path is None or cookie.path == path:1 unmodified line319 320 raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))321 322 def _find_no_duplicates(self, name, domain=None, path=None):316- """__get_item__ and get call _find_no_duplicates -- never used in Requests internally.317- Takes as args name and optional domain and path. Returns a cookie.value.318- Throws KeyError if cookie is not found and CookieConflictError if there are319- multiple cookies that match name and optionally domain and path."""323+ """__get_item__ and get call _find_no_duplicates -- never used in324+ Requests internally. Takes as args name and optional domain and path.325+ Returns a cookie.value. Throws KeyError if cookie is not found and326+ CookieConflictError if there are multiple cookies that match name and327+ optionally domain and path."""328 toReturn = None329 for cookie in iter(self):330 if cookie.name == name:331 if domain is None or cookie.domain == domain:115 unmodified lines447 :param cookies: Dictionary or CookieJar object to be added.448 """449 if not isinstance(cookiejar, cookielib.CookieJar):450 raise ValueError('You can only merge into CookieJar')443- 451+452 if isinstance(cookies, dict):453 cookiejar = cookiejar_from_dict(454 cookies, cookiejar=cookiejar, overwrite=False)455 elif isinstance(cookies, cookielib.CookieJar):7 unmodified lines