Merge pull request #2390 from Lukasa/cookiedocs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Clean up cookie docstrings, document cookie jar.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents b831317, 101425e · Browse files · 2 files·+55 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+55 added−40 removed
- other:+55 added−40 removed
Provenance
- Commit
2d1ffad- Tree
11c6f04- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent 1
b831317- Parent 2
101425e
2 files changed+55 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+49 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.279 280+ .. warning:: operation is O(n), not O(1)."""281+282 return self._find_no_duplicates(name)283 284 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."""285+ """Dict-like __setitem__ for compatibility with client code. Throws286+ exception if there is already a cookie of that name in the jar. In that287+ case, use the more explicit set() method instead."""288 289 self.set(name, value)290 291 def __delitem__(self, name):286- """Deletes a cookie given a name. Wraps cookielib.CookieJar's remove_cookie_by_name()."""292+ """Deletes a cookie given a name. Wraps ``cookielib.CookieJar``'s293+ ``remove_cookie_by_name()``."""294 remove_cookie_by_name(self, name)295 296 def set_cookie(self, cookie, *args, **kwargs):297 if hasattr(cookie.value, 'startswith') and cookie.value.startswith('"') and cookie.value.endswith('"'):8 unmodified lines306 else:307 super(RequestsCookieJar, self).update(other)308 309 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."""310+ """Requests uses this method internally to get cookie values. Takes as311+ args name and optional domain and path. Returns a cookie.value. If312+ there are conflicting cookies, _find arbitrarily chooses one. See313+ _find_no_duplicates if you want an exception thrown if there are314+ conflicting cookies."""315 for cookie in iter(self):316 if cookie.name == name:317 if domain is None or cookie.domain == domain:318 if path is None or cookie.path == path:1 unmodified line320 321 raise KeyError('name=%r, domain=%r, path=%r' % (name, domain, path))322 323 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."""324+ """Both ``__get_item__`` and ``get`` call this function: it's never325+ used elsewhere in Requests. Takes as args name and optional domain and326+ path. Returns a cookie.value. Throws KeyError if cookie is not found327+ and CookieConflictError if there are multiple cookies that match name328+ and optionally domain and path."""329 toReturn = None330 for cookie in iter(self):331 if cookie.name == name:332 if domain is None or cookie.domain == domain:115 unmodified lines448 :param cookies: Dictionary or CookieJar object to be added.449 """450 if not isinstance(cookiejar, cookielib.CookieJar):451 raise ValueError('You can only merge into CookieJar')443- 452+453 if isinstance(cookies, dict):454 cookiejar = cookiejar_from_dict(455 cookies, cookiejar=cookiejar, overwrite=False)456 elif isinstance(cookies, cookielib.CookieJar):7 unmodified lines