|
| 1 | +from collections.abc import MutableMapping |
| 2 | + |
| 3 | + |
| 4 | +# straight from: https://github.com/kennethreitz/requests/blob/master/src/requests/structures.py |
| 5 | +class CaseInsensitiveDict(MutableMapping): |
| 6 | + """A case-insensitive ``dict``-like object. |
| 7 | +
|
| 8 | + Implements all methods and operations of |
| 9 | + ``MutableMapping`` as well as dict's ``copy``. Also |
| 10 | + provides ``lower_items``. |
| 11 | +
|
| 12 | + All keys are expected to be strings. The structure remembers the |
| 13 | + case of the last key to be set, and ``iter(instance)``, |
| 14 | + ``keys()``, ``items()``, ``iterkeys()``, and ``iteritems()`` |
| 15 | + will contain case-sensitive keys. However, querying and contains |
| 16 | + testing is case insensitive:: |
| 17 | +
|
| 18 | + cid = CaseInsensitiveDict() |
| 19 | + cid['Accept'] = 'application/json' |
| 20 | + cid['aCCEPT'] == 'application/json' # True |
| 21 | + list(cid) == ['Accept'] # True |
| 22 | +
|
| 23 | + For example, ``headers['content-encoding']`` will return the |
| 24 | + value of a ``'Content-Encoding'`` response header, regardless |
| 25 | + of how the header name was originally stored. |
| 26 | +
|
| 27 | + If the constructor, ``.update``, or equality comparison |
| 28 | + operations are given keys that have equal ``.lower()``s, the |
| 29 | + behavior is undefined. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__(self, data=None, **kwargs): |
| 33 | + self._store = {} |
| 34 | + if data is None: |
| 35 | + data = {} |
| 36 | + self.update(data, **kwargs) |
| 37 | + |
| 38 | + def __setitem__(self, key, value): |
| 39 | + # Use the lowercased key for lookups, but store the actual |
| 40 | + # key alongside the value. |
| 41 | + self._store[key.lower()] = (key, value) |
| 42 | + |
| 43 | + def __getitem__(self, key): |
| 44 | + return self._store[key.lower()][1] |
| 45 | + |
| 46 | + def __delitem__(self, key): |
| 47 | + del self._store[key.lower()] |
| 48 | + |
| 49 | + def __iter__(self): |
| 50 | + return (casedkey for casedkey, mappedvalue in self._store.values()) |
| 51 | + |
| 52 | + def __len__(self): |
| 53 | + return len(self._store) |
| 54 | + |
| 55 | + def lower_items(self): |
| 56 | + """Like iteritems(), but with all lowercase keys.""" |
| 57 | + return ((lowerkey, keyval[1]) for (lowerkey, keyval) in self._store.items()) |
| 58 | + |
| 59 | + def __eq__(self, other): |
| 60 | + from collections.abc import Mapping |
| 61 | + |
| 62 | + if isinstance(other, Mapping): |
| 63 | + other = CaseInsensitiveDict(other) |
| 64 | + else: |
| 65 | + return NotImplemented |
| 66 | + # Compare insensitively |
| 67 | + return dict(self.lower_items()) == dict(other.lower_items()) |
| 68 | + |
| 69 | + # Copy is required |
| 70 | + def copy(self): |
| 71 | + return CaseInsensitiveDict(self._store.values()) |
| 72 | + |
| 73 | + def __repr__(self): |
| 74 | + return str(dict(self.items())) |
| 75 | + |
| 76 | + def items(self): |
| 77 | + pass |
0 commit comments