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