jpayne@7: """ jpayne@7: requests.structures jpayne@7: ~~~~~~~~~~~~~~~~~~~ jpayne@7: jpayne@7: Data structures that power Requests. jpayne@7: """ jpayne@7: jpayne@7: from collections import OrderedDict jpayne@7: jpayne@7: from .compat import Mapping, MutableMapping jpayne@7: jpayne@7: jpayne@7: class CaseInsensitiveDict(MutableMapping): jpayne@7: """A case-insensitive ``dict``-like object. jpayne@7: jpayne@7: Implements all methods and operations of jpayne@7: ``MutableMapping`` as well as dict's ``copy``. Also jpayne@7: provides ``lower_items``. jpayne@7: jpayne@7: All keys are expected to be strings. The structure remembers the jpayne@7: case of the last key to be set, and ``iter(instance)``, jpayne@7: ``keys()``, ``items()``, ``iterkeys()``, and ``iteritems()`` jpayne@7: will contain case-sensitive keys. However, querying and contains jpayne@7: testing is case insensitive:: jpayne@7: jpayne@7: cid = CaseInsensitiveDict() jpayne@7: cid['Accept'] = 'application/json' jpayne@7: cid['aCCEPT'] == 'application/json' # True jpayne@7: list(cid) == ['Accept'] # True jpayne@7: jpayne@7: For example, ``headers['content-encoding']`` will return the jpayne@7: value of a ``'Content-Encoding'`` response header, regardless jpayne@7: of how the header name was originally stored. jpayne@7: jpayne@7: If the constructor, ``.update``, or equality comparison jpayne@7: operations are given keys that have equal ``.lower()``s, the jpayne@7: behavior is undefined. jpayne@7: """ jpayne@7: jpayne@7: def __init__(self, data=None, **kwargs): jpayne@7: self._store = OrderedDict() jpayne@7: if data is None: jpayne@7: data = {} jpayne@7: self.update(data, **kwargs) jpayne@7: jpayne@7: def __setitem__(self, key, value): jpayne@7: # Use the lowercased key for lookups, but store the actual jpayne@7: # key alongside the value. jpayne@7: self._store[key.lower()] = (key, value) jpayne@7: jpayne@7: def __getitem__(self, key): jpayne@7: return self._store[key.lower()][1] jpayne@7: jpayne@7: def __delitem__(self, key): jpayne@7: del self._store[key.lower()] jpayne@7: jpayne@7: def __iter__(self): jpayne@7: return (casedkey for casedkey, mappedvalue in self._store.values()) jpayne@7: jpayne@7: def __len__(self): jpayne@7: return len(self._store) jpayne@7: jpayne@7: def lower_items(self): jpayne@7: """Like iteritems(), but with all lowercase keys.""" jpayne@7: return ((lowerkey, keyval[1]) for (lowerkey, keyval) in self._store.items()) jpayne@7: jpayne@7: def __eq__(self, other): jpayne@7: if isinstance(other, Mapping): jpayne@7: other = CaseInsensitiveDict(other) jpayne@7: else: jpayne@7: return NotImplemented jpayne@7: # Compare insensitively jpayne@7: return dict(self.lower_items()) == dict(other.lower_items()) jpayne@7: jpayne@7: # Copy is required jpayne@7: def copy(self): jpayne@7: return CaseInsensitiveDict(self._store.values()) jpayne@7: jpayne@7: def __repr__(self): jpayne@7: return str(dict(self.items())) jpayne@7: jpayne@7: jpayne@7: class LookupDict(dict): jpayne@7: """Dictionary lookup object.""" jpayne@7: jpayne@7: def __init__(self, name=None): jpayne@7: self.name = name jpayne@7: super().__init__() jpayne@7: jpayne@7: def __repr__(self): jpayne@7: return f"" jpayne@7: jpayne@7: def __getitem__(self, key): jpayne@7: # We allow fall-through here, so values default to None jpayne@7: jpayne@7: return self.__dict__.get(key, None) jpayne@7: jpayne@7: def get(self, key, default=None): jpayne@7: return self.__dict__.get(key, default)