annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/requests/structures.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 """
jpayne@69 2 requests.structures
jpayne@69 3 ~~~~~~~~~~~~~~~~~~~
jpayne@69 4
jpayne@69 5 Data structures that power Requests.
jpayne@69 6 """
jpayne@69 7
jpayne@69 8 from collections import OrderedDict
jpayne@69 9
jpayne@69 10 from .compat import Mapping, MutableMapping
jpayne@69 11
jpayne@69 12
jpayne@69 13 class CaseInsensitiveDict(MutableMapping):
jpayne@69 14 """A case-insensitive ``dict``-like object.
jpayne@69 15
jpayne@69 16 Implements all methods and operations of
jpayne@69 17 ``MutableMapping`` as well as dict's ``copy``. Also
jpayne@69 18 provides ``lower_items``.
jpayne@69 19
jpayne@69 20 All keys are expected to be strings. The structure remembers the
jpayne@69 21 case of the last key to be set, and ``iter(instance)``,
jpayne@69 22 ``keys()``, ``items()``, ``iterkeys()``, and ``iteritems()``
jpayne@69 23 will contain case-sensitive keys. However, querying and contains
jpayne@69 24 testing is case insensitive::
jpayne@69 25
jpayne@69 26 cid = CaseInsensitiveDict()
jpayne@69 27 cid['Accept'] = 'application/json'
jpayne@69 28 cid['aCCEPT'] == 'application/json' # True
jpayne@69 29 list(cid) == ['Accept'] # True
jpayne@69 30
jpayne@69 31 For example, ``headers['content-encoding']`` will return the
jpayne@69 32 value of a ``'Content-Encoding'`` response header, regardless
jpayne@69 33 of how the header name was originally stored.
jpayne@69 34
jpayne@69 35 If the constructor, ``.update``, or equality comparison
jpayne@69 36 operations are given keys that have equal ``.lower()``s, the
jpayne@69 37 behavior is undefined.
jpayne@69 38 """
jpayne@69 39
jpayne@69 40 def __init__(self, data=None, **kwargs):
jpayne@69 41 self._store = OrderedDict()
jpayne@69 42 if data is None:
jpayne@69 43 data = {}
jpayne@69 44 self.update(data, **kwargs)
jpayne@69 45
jpayne@69 46 def __setitem__(self, key, value):
jpayne@69 47 # Use the lowercased key for lookups, but store the actual
jpayne@69 48 # key alongside the value.
jpayne@69 49 self._store[key.lower()] = (key, value)
jpayne@69 50
jpayne@69 51 def __getitem__(self, key):
jpayne@69 52 return self._store[key.lower()][1]
jpayne@69 53
jpayne@69 54 def __delitem__(self, key):
jpayne@69 55 del self._store[key.lower()]
jpayne@69 56
jpayne@69 57 def __iter__(self):
jpayne@69 58 return (casedkey for casedkey, mappedvalue in self._store.values())
jpayne@69 59
jpayne@69 60 def __len__(self):
jpayne@69 61 return len(self._store)
jpayne@69 62
jpayne@69 63 def lower_items(self):
jpayne@69 64 """Like iteritems(), but with all lowercase keys."""
jpayne@69 65 return ((lowerkey, keyval[1]) for (lowerkey, keyval) in self._store.items())
jpayne@69 66
jpayne@69 67 def __eq__(self, other):
jpayne@69 68 if isinstance(other, Mapping):
jpayne@69 69 other = CaseInsensitiveDict(other)
jpayne@69 70 else:
jpayne@69 71 return NotImplemented
jpayne@69 72 # Compare insensitively
jpayne@69 73 return dict(self.lower_items()) == dict(other.lower_items())
jpayne@69 74
jpayne@69 75 # Copy is required
jpayne@69 76 def copy(self):
jpayne@69 77 return CaseInsensitiveDict(self._store.values())
jpayne@69 78
jpayne@69 79 def __repr__(self):
jpayne@69 80 return str(dict(self.items()))
jpayne@69 81
jpayne@69 82
jpayne@69 83 class LookupDict(dict):
jpayne@69 84 """Dictionary lookup object."""
jpayne@69 85
jpayne@69 86 def __init__(self, name=None):
jpayne@69 87 self.name = name
jpayne@69 88 super().__init__()
jpayne@69 89
jpayne@69 90 def __repr__(self):
jpayne@69 91 return f"<lookup '{self.name}'>"
jpayne@69 92
jpayne@69 93 def __getitem__(self, key):
jpayne@69 94 # We allow fall-through here, so values default to None
jpayne@69 95
jpayne@69 96 return self.__dict__.get(key, None)
jpayne@69 97
jpayne@69 98 def get(self, key, default=None):
jpayne@69 99 return self.__dict__.get(key, default)