comparison lib/python3.8/site-packages/pip/_vendor/requests/structures.py @ 0:9e54283cc701 draft

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