annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/site-packages/requests/structures.py @ 68:5028fdace37b

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