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