python (3.11.7)
       1  """
       2  requests.structures
       3  ~~~~~~~~~~~~~~~~~~~
       4  
       5  Data structures that power Requests.
       6  """
       7  
       8  from collections import OrderedDict
       9  
      10  from .compat import Mapping, MutableMapping
      11  
      12  
      13  class ESC[4;38;5;81mCaseInsensitiveDict(ESC[4;38;5;149mMutableMapping):
      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 ((lowerkey, keyval[1]) for (lowerkey, keyval) in self._store.items())
      66  
      67      def __eq__(self, other):
      68          if isinstance(other, Mapping):
      69              other = CaseInsensitiveDict(other)
      70          else:
      71              return NotImplemented
      72          # Compare insensitively
      73          return dict(self.lower_items()) == dict(other.lower_items())
      74  
      75      # Copy is required
      76      def copy(self):
      77          return CaseInsensitiveDict(self._store.values())
      78  
      79      def __repr__(self):
      80          return str(dict(self.items()))
      81  
      82  
      83  class ESC[4;38;5;81mLookupDict(ESC[4;38;5;149mdict):
      84      """Dictionary lookup object."""
      85  
      86      def __init__(self, name=None):
      87          self.name = name
      88          super().__init__()
      89  
      90      def __repr__(self):
      91          return f"<lookup '{self.name}'>"
      92  
      93      def __getitem__(self, key):
      94          # We allow fall-through here, so values default to None
      95  
      96          return self.__dict__.get(key, None)
      97  
      98      def get(self, key, default=None):
      99          return self.__dict__.get(key, default)