2016-05-19 05:50:19 +00:00
|
|
|
from abc import ABCMeta, abstractmethod
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
from collections.abc import MutableMapping
|
|
|
|
except ImportError: # pragma: no cover
|
|
|
|
from collections import MutableMapping # Workaround for Python < 3.3
|
|
|
|
|
2016-05-31 23:12:10 +00:00
|
|
|
from netlib import basetypes
|
2016-05-19 05:50:19 +00:00
|
|
|
|
|
|
|
|
2016-10-17 02:15:22 +00:00
|
|
|
class _MultiDict(MutableMapping, basetypes.Serializable, metaclass=ABCMeta):
|
2016-05-19 05:50:19 +00:00
|
|
|
def __repr__(self):
|
2016-05-29 02:10:49 +00:00
|
|
|
fields = (
|
2016-05-19 05:50:19 +00:00
|
|
|
repr(field)
|
|
|
|
for field in self.fields
|
|
|
|
)
|
|
|
|
return "{cls}[{fields}]".format(
|
|
|
|
cls=type(self).__name__,
|
|
|
|
fields=", ".join(fields)
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
|
|
|
def _reduce_values(values):
|
2016-05-20 18:04:27 +00:00
|
|
|
"""
|
|
|
|
If a user accesses multidict["foo"], this method
|
|
|
|
reduces all values for "foo" to a single value that is returned.
|
|
|
|
For example, HTTP headers are folded, whereas we will just take
|
|
|
|
the first cookie we found with that name.
|
|
|
|
"""
|
2016-05-19 05:50:19 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@abstractmethod
|
2016-05-20 18:04:27 +00:00
|
|
|
def _kconv(key):
|
|
|
|
"""
|
|
|
|
This method converts a key to its canonical representation.
|
|
|
|
For example, HTTP headers are case-insensitive, so this method returns key.lower().
|
|
|
|
"""
|
2016-05-19 05:50:19 +00:00
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
values = self.get_all(key)
|
|
|
|
if not values:
|
|
|
|
raise KeyError(key)
|
|
|
|
return self._reduce_values(values)
|
|
|
|
|
|
|
|
def __setitem__(self, key, value):
|
|
|
|
self.set_all(key, [value])
|
|
|
|
|
|
|
|
def __delitem__(self, key):
|
|
|
|
if key not in self:
|
|
|
|
raise KeyError(key)
|
|
|
|
key = self._kconv(key)
|
|
|
|
self.fields = tuple(
|
|
|
|
field for field in self.fields
|
|
|
|
if key != self._kconv(field[0])
|
|
|
|
)
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
seen = set()
|
|
|
|
for key, _ in self.fields:
|
|
|
|
key_kconv = self._kconv(key)
|
|
|
|
if key_kconv not in seen:
|
|
|
|
seen.add(key_kconv)
|
|
|
|
yield key
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(set(self._kconv(key) for key, _ in self.fields))
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
if isinstance(other, MultiDict):
|
|
|
|
return self.fields == other.fields
|
|
|
|
return False
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return not self.__eq__(other)
|
|
|
|
|
|
|
|
def get_all(self, key):
|
|
|
|
"""
|
|
|
|
Return the list of all values for a given key.
|
|
|
|
If that key is not in the MultiDict, the return value will be an empty list.
|
|
|
|
"""
|
|
|
|
key = self._kconv(key)
|
|
|
|
return [
|
|
|
|
value
|
|
|
|
for k, value in self.fields
|
|
|
|
if self._kconv(k) == key
|
2016-05-20 23:37:36 +00:00
|
|
|
]
|
2016-05-19 05:50:19 +00:00
|
|
|
|
|
|
|
def set_all(self, key, values):
|
|
|
|
"""
|
|
|
|
Remove the old values for a key and add new ones.
|
|
|
|
"""
|
|
|
|
key_kconv = self._kconv(key)
|
|
|
|
|
|
|
|
new_fields = []
|
|
|
|
for field in self.fields:
|
|
|
|
if self._kconv(field[0]) == key_kconv:
|
|
|
|
if values:
|
|
|
|
new_fields.append(
|
2016-06-05 18:47:10 +00:00
|
|
|
(field[0], values.pop(0))
|
2016-05-19 05:50:19 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
new_fields.append(field)
|
|
|
|
while values:
|
|
|
|
new_fields.append(
|
|
|
|
(key, values.pop(0))
|
|
|
|
)
|
|
|
|
self.fields = tuple(new_fields)
|
|
|
|
|
|
|
|
def add(self, key, value):
|
|
|
|
"""
|
|
|
|
Add an additional value for the given key at the bottom.
|
|
|
|
"""
|
|
|
|
self.insert(len(self.fields), key, value)
|
|
|
|
|
|
|
|
def insert(self, index, key, value):
|
|
|
|
"""
|
|
|
|
Insert an additional value for the given key at the specified position.
|
|
|
|
"""
|
|
|
|
item = (key, value)
|
|
|
|
self.fields = self.fields[:index] + (item,) + self.fields[index:]
|
|
|
|
|
|
|
|
def keys(self, multi=False):
|
|
|
|
"""
|
|
|
|
Get all keys.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
multi(bool):
|
|
|
|
If True, one key per value will be returned.
|
|
|
|
If False, duplicate keys will only be returned once.
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
k
|
|
|
|
for k, _ in self.items(multi)
|
|
|
|
)
|
|
|
|
|
|
|
|
def values(self, multi=False):
|
|
|
|
"""
|
|
|
|
Get all values.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
multi(bool):
|
|
|
|
If True, all values will be returned.
|
|
|
|
If False, only the first value per key will be returned.
|
|
|
|
"""
|
|
|
|
return (
|
|
|
|
v
|
|
|
|
for _, v in self.items(multi)
|
|
|
|
)
|
|
|
|
|
|
|
|
def items(self, multi=False):
|
|
|
|
"""
|
|
|
|
Get all (key, value) tuples.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
multi(bool):
|
|
|
|
If True, all (key, value) pairs will be returned
|
|
|
|
If False, only the first (key, value) pair per unique key will be returned.
|
|
|
|
"""
|
|
|
|
if multi:
|
|
|
|
return self.fields
|
|
|
|
else:
|
2016-10-17 04:34:46 +00:00
|
|
|
return super().items()
|
2016-05-19 05:50:19 +00:00
|
|
|
|
2016-06-01 23:37:18 +00:00
|
|
|
def collect(self):
|
|
|
|
"""
|
|
|
|
Returns a list of (key, value) tuples, where values are either
|
2016-07-07 02:50:06 +00:00
|
|
|
singular if there is only one matching item for a key, or a list
|
2016-06-01 23:37:18 +00:00
|
|
|
if there are more than one. The order of the keys matches the order
|
|
|
|
in the underlying fields list.
|
|
|
|
"""
|
|
|
|
coll = []
|
|
|
|
for key in self:
|
|
|
|
values = self.get_all(key)
|
|
|
|
if len(values) == 1:
|
|
|
|
coll.append([key, values[0]])
|
|
|
|
else:
|
|
|
|
coll.append([key, values])
|
|
|
|
return coll
|
|
|
|
|
2016-05-19 05:50:19 +00:00
|
|
|
def to_dict(self):
|
|
|
|
"""
|
|
|
|
Get the MultiDict as a plain Python dict.
|
|
|
|
Keys with multiple values are returned as lists.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Simple dict with duplicate values.
|
2016-07-07 02:50:06 +00:00
|
|
|
>>> d = MultiDict([("name", "value"), ("a", False), ("a", 42)])
|
2016-05-19 05:50:19 +00:00
|
|
|
>>> d.to_dict()
|
|
|
|
{
|
|
|
|
"name": "value",
|
2016-07-07 02:50:06 +00:00
|
|
|
"a": [False, 42]
|
2016-05-19 05:50:19 +00:00
|
|
|
}
|
|
|
|
"""
|
2016-07-07 02:50:06 +00:00
|
|
|
return {
|
|
|
|
k: v for k, v in self.collect()
|
|
|
|
}
|
2016-05-19 05:50:19 +00:00
|
|
|
|
|
|
|
def get_state(self):
|
|
|
|
return self.fields
|
|
|
|
|
|
|
|
def set_state(self, state):
|
|
|
|
self.fields = tuple(tuple(x) for x in state)
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_state(cls, state):
|
2016-05-29 02:10:49 +00:00
|
|
|
return cls(state)
|
2016-05-19 05:50:19 +00:00
|
|
|
|
|
|
|
|
2016-05-20 23:37:36 +00:00
|
|
|
class MultiDict(_MultiDict):
|
2016-05-29 02:10:49 +00:00
|
|
|
def __init__(self, fields=()):
|
2016-10-17 04:34:46 +00:00
|
|
|
super().__init__()
|
2016-05-28 01:04:34 +00:00
|
|
|
self.fields = tuple(
|
2016-05-29 02:10:49 +00:00
|
|
|
tuple(i) for i in fields
|
2016-05-28 01:04:34 +00:00
|
|
|
)
|
2016-05-20 23:37:36 +00:00
|
|
|
|
2016-06-09 01:28:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def _reduce_values(values):
|
|
|
|
return values[0]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _kconv(key):
|
|
|
|
return key
|
|
|
|
|
2016-05-20 23:37:36 +00:00
|
|
|
|
2016-10-17 02:15:22 +00:00
|
|
|
class ImmutableMultiDict(MultiDict, metaclass=ABCMeta):
|
2016-05-19 05:50:19 +00:00
|
|
|
def _immutable(self, *_):
|
|
|
|
raise TypeError('{} objects are immutable'.format(self.__class__.__name__))
|
|
|
|
|
|
|
|
__delitem__ = set_all = insert = _immutable
|
|
|
|
|
2016-07-22 02:49:32 +00:00
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.fields)
|
|
|
|
|
2016-05-19 05:50:19 +00:00
|
|
|
def with_delitem(self, key):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
An updated ImmutableMultiDict. The original object will not be modified.
|
|
|
|
"""
|
|
|
|
ret = self.copy()
|
2016-10-17 04:34:46 +00:00
|
|
|
# FIXME: This is filthy...
|
2016-05-19 05:50:19 +00:00
|
|
|
super(ImmutableMultiDict, ret).__delitem__(key)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def with_set_all(self, key, values):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
An updated ImmutableMultiDict. The original object will not be modified.
|
|
|
|
"""
|
|
|
|
ret = self.copy()
|
2016-10-17 04:34:46 +00:00
|
|
|
# FIXME: This is filthy...
|
2016-05-19 05:50:19 +00:00
|
|
|
super(ImmutableMultiDict, ret).set_all(key, values)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
def with_insert(self, index, key, value):
|
|
|
|
"""
|
|
|
|
Returns:
|
|
|
|
An updated ImmutableMultiDict. The original object will not be modified.
|
|
|
|
"""
|
|
|
|
ret = self.copy()
|
2016-10-17 04:34:46 +00:00
|
|
|
# FIXME: This is filthy...
|
2016-05-19 05:50:19 +00:00
|
|
|
super(ImmutableMultiDict, ret).insert(index, key, value)
|
|
|
|
return ret
|
2016-05-20 23:37:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MultiDictView(_MultiDict):
|
|
|
|
"""
|
|
|
|
The MultiDictView provides the MultiDict interface over calculated data.
|
|
|
|
The view itself contains no state - data is retrieved from the parent on
|
|
|
|
request, and stored back to the parent on change.
|
|
|
|
"""
|
|
|
|
def __init__(self, getter, setter):
|
|
|
|
self._getter = getter
|
|
|
|
self._setter = setter
|
2016-10-17 04:34:46 +00:00
|
|
|
super().__init__()
|
2016-05-20 23:37:36 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _kconv(key):
|
|
|
|
# All request-attributes are case-sensitive.
|
|
|
|
return key
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _reduce_values(values):
|
|
|
|
# We just return the first element if
|
|
|
|
# multiple elements exist with the same key.
|
|
|
|
return values[0]
|
|
|
|
|
|
|
|
@property
|
|
|
|
def fields(self):
|
|
|
|
return self._getter()
|
|
|
|
|
|
|
|
@fields.setter
|
|
|
|
def fields(self, value):
|
2016-07-07 02:50:06 +00:00
|
|
|
self._setter(value)
|