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