2011-03-28 10:11:19 +08:00
|
|
|
import copy
|
2013-08-03 13:41:15 +08:00
|
|
|
import warnings
|
2013-08-11 02:48:46 +08:00
|
|
|
from collections import OrderedDict
|
2014-02-27 05:48:20 +08:00
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
from django.utils import six
|
2014-02-27 05:48:20 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango19Warning
|
2010-01-03 14:54:56 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2006-06-08 13:00:13 +08:00
|
|
|
class MergeDict(object):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-12-02 03:22:05 +08:00
|
|
|
A simple class for creating new "virtual" dictionaries that actually look
|
2005-07-13 09:25:57 +08:00
|
|
|
up values in more than one dictionary, passed in the constructor.
|
2008-02-03 10:02:41 +08:00
|
|
|
|
2008-02-16 13:15:09 +08:00
|
|
|
If a key appears in more than one of the given dictionaries, only the
|
2008-02-03 10:02:41 +08:00
|
|
|
first occurrence will be used.
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
|
|
|
def __init__(self, *dicts):
|
2013-10-16 04:36:49 +08:00
|
|
|
warnings.warn('`MergeDict` is deprecated, use `dict.update()` '
|
2014-02-27 05:48:20 +08:00
|
|
|
'instead.', RemovedInDjango19Warning, 2)
|
2005-07-13 09:25:57 +08:00
|
|
|
self.dicts = dicts
|
|
|
|
|
2013-04-17 23:20:31 +08:00
|
|
|
def __bool__(self):
|
|
|
|
return any(self.dicts)
|
|
|
|
|
|
|
|
def __nonzero__(self):
|
|
|
|
return type(self).__bool__(self)
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def __getitem__(self, key):
|
2007-11-26 02:07:57 +08:00
|
|
|
for dict_ in self.dicts:
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
2007-11-26 02:07:57 +08:00
|
|
|
return dict_[key]
|
2005-07-13 09:25:57 +08:00
|
|
|
except KeyError:
|
|
|
|
pass
|
2013-04-26 14:57:39 +08:00
|
|
|
raise KeyError(key)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2007-04-25 15:25:22 +08:00
|
|
|
def __copy__(self):
|
|
|
|
return self.__class__(*self.dicts)
|
2006-09-23 21:55:04 +08:00
|
|
|
|
2007-02-26 00:37:31 +08:00
|
|
|
def get(self, key, default=None):
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
|
|
|
return self[key]
|
|
|
|
except KeyError:
|
|
|
|
return default
|
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
# This is used by MergeDicts of MultiValueDicts.
|
2005-07-13 09:25:57 +08:00
|
|
|
def getlist(self, key):
|
2007-11-26 02:07:57 +08:00
|
|
|
for dict_ in self.dicts:
|
2012-07-25 15:12:59 +08:00
|
|
|
if key in dict_:
|
2007-11-26 02:07:57 +08:00
|
|
|
return dict_.getlist(key)
|
2008-02-03 10:02:41 +08:00
|
|
|
return []
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
def _iteritems(self):
|
2010-02-23 07:38:18 +08:00
|
|
|
seen = set()
|
2007-11-26 02:07:57 +08:00
|
|
|
for dict_ in self.dicts:
|
2012-07-25 15:12:59 +08:00
|
|
|
for item in six.iteritems(dict_):
|
|
|
|
k = item[0]
|
2010-02-23 07:38:18 +08:00
|
|
|
if k in seen:
|
|
|
|
continue
|
|
|
|
seen.add(k)
|
|
|
|
yield item
|
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
def _iterkeys(self):
|
|
|
|
for k, v in self._iteritems():
|
2010-02-23 07:38:18 +08:00
|
|
|
yield k
|
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
def _itervalues(self):
|
|
|
|
for k, v in self._iteritems():
|
2010-02-23 07:38:18 +08:00
|
|
|
yield v
|
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
if six.PY3:
|
|
|
|
items = _iteritems
|
|
|
|
keys = _iterkeys
|
|
|
|
values = _itervalues
|
|
|
|
else:
|
|
|
|
iteritems = _iteritems
|
|
|
|
iterkeys = _iterkeys
|
|
|
|
itervalues = _itervalues
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return list(self.iteritems())
|
2010-02-23 07:38:18 +08:00
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
def keys(self):
|
|
|
|
return list(self.iterkeys())
|
2010-02-23 07:38:18 +08:00
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
def values(self):
|
|
|
|
return list(self.itervalues())
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def has_key(self, key):
|
2007-11-26 02:07:57 +08:00
|
|
|
for dict_ in self.dicts:
|
|
|
|
if key in dict_:
|
2005-07-13 09:25:57 +08:00
|
|
|
return True
|
|
|
|
return False
|
2007-10-19 09:26:09 +08:00
|
|
|
|
2007-09-11 22:04:40 +08:00
|
|
|
__contains__ = has_key
|
2012-07-25 15:12:59 +08:00
|
|
|
|
|
|
|
__iter__ = _iterkeys
|
2007-04-25 15:25:22 +08:00
|
|
|
|
|
|
|
def copy(self):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""Returns a copy of this object."""
|
2007-02-28 08:35:50 +08:00
|
|
|
return self.__copy__()
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2010-09-11 03:24:24 +08:00
|
|
|
def __str__(self):
|
|
|
|
'''
|
|
|
|
Returns something like
|
|
|
|
|
|
|
|
"{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}"
|
|
|
|
|
|
|
|
instead of the generic "<object meta-data>" inherited from object.
|
|
|
|
'''
|
|
|
|
return str(dict(self.items()))
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
'''
|
|
|
|
Returns something like
|
|
|
|
|
|
|
|
MergeDict({'key1': 'val1', 'key2': 'val2'}, {'key3': 'val3'})
|
|
|
|
|
|
|
|
instead of generic "<object meta-data>" inherited from object.
|
|
|
|
'''
|
|
|
|
dictreprs = ', '.join(repr(d) for d in self.dicts)
|
|
|
|
return '%s(%s)' % (self.__class__.__name__, dictreprs)
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
class SortedDict(dict):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""
|
|
|
|
A dictionary that keeps its keys in the order in which they're inserted.
|
|
|
|
"""
|
2008-08-25 12:00:15 +08:00
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
instance = super(SortedDict, cls).__new__(cls, *args, **kwargs)
|
|
|
|
instance.keyOrder = []
|
|
|
|
return instance
|
|
|
|
|
2006-06-03 21:37:34 +08:00
|
|
|
def __init__(self, data=None):
|
2013-08-03 13:41:15 +08:00
|
|
|
warnings.warn(
|
|
|
|
"SortedDict is deprecated and will be removed in Django 1.9.",
|
2014-02-27 05:48:20 +08:00
|
|
|
RemovedInDjango19Warning, stacklevel=2
|
2013-08-03 13:41:15 +08:00
|
|
|
)
|
2012-11-11 06:35:46 +08:00
|
|
|
if data is None or isinstance(data, dict):
|
|
|
|
data = data or []
|
|
|
|
super(SortedDict, self).__init__(data)
|
|
|
|
self.keyOrder = list(data) if data else []
|
2007-10-14 12:17:02 +08:00
|
|
|
else:
|
2012-11-11 06:35:46 +08:00
|
|
|
super(SortedDict, self).__init__()
|
|
|
|
super_set = super(SortedDict, self).__setitem__
|
2007-11-30 04:09:54 +08:00
|
|
|
for key, value in data:
|
2012-11-11 06:35:46 +08:00
|
|
|
# Take the ordering from first key
|
|
|
|
if key not in self:
|
2007-11-30 04:09:54 +08:00
|
|
|
self.keyOrder.append(key)
|
2012-11-11 06:35:46 +08:00
|
|
|
# But override with last value in data (dict() does this)
|
|
|
|
super_set(key, value)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-11-12 11:12:47 +08:00
|
|
|
def __deepcopy__(self, memo):
|
2011-03-28 10:11:19 +08:00
|
|
|
return self.__class__([(key, copy.deepcopy(value, memo))
|
2012-11-11 06:35:46 +08:00
|
|
|
for key, value in self.items()])
|
2007-10-22 08:52:54 +08:00
|
|
|
|
2012-04-30 22:12:38 +08:00
|
|
|
def __copy__(self):
|
|
|
|
# The Python's default copy implementation will alter the state
|
|
|
|
# of self. The reason for this seems complex but is likely related to
|
|
|
|
# subclassing dict.
|
|
|
|
return self.copy()
|
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def __setitem__(self, key, value):
|
2009-12-12 14:00:59 +08:00
|
|
|
if key not in self:
|
2006-05-02 09:31:56 +08:00
|
|
|
self.keyOrder.append(key)
|
2009-12-12 14:00:59 +08:00
|
|
|
super(SortedDict, self).__setitem__(key, value)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def __delitem__(self, key):
|
2007-11-26 02:10:45 +08:00
|
|
|
super(SortedDict, self).__delitem__(key)
|
2006-05-02 09:31:56 +08:00
|
|
|
self.keyOrder.remove(key)
|
|
|
|
|
|
|
|
def __iter__(self):
|
2009-12-12 14:00:59 +08:00
|
|
|
return iter(self.keyOrder)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-10-15 19:16:46 +08:00
|
|
|
def __reversed__(self):
|
|
|
|
return reversed(self.keyOrder)
|
|
|
|
|
2007-10-22 08:52:54 +08:00
|
|
|
def pop(self, k, *args):
|
2007-11-26 02:10:45 +08:00
|
|
|
result = super(SortedDict, self).pop(k, *args)
|
2007-10-22 08:52:54 +08:00
|
|
|
try:
|
|
|
|
self.keyOrder.remove(k)
|
|
|
|
except ValueError:
|
|
|
|
# Key wasn't in the dictionary in the first place. No problem.
|
|
|
|
pass
|
|
|
|
return result
|
|
|
|
|
|
|
|
def popitem(self):
|
2007-11-26 02:10:45 +08:00
|
|
|
result = super(SortedDict, self).popitem()
|
2007-10-22 08:52:54 +08:00
|
|
|
self.keyOrder.remove(result[0])
|
|
|
|
return result
|
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
def _iteritems(self):
|
2007-09-16 11:27:38 +08:00
|
|
|
for key in self.keyOrder:
|
2009-12-12 14:00:59 +08:00
|
|
|
yield key, self[key]
|
2007-09-16 11:27:38 +08:00
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
def _iterkeys(self):
|
|
|
|
for key in self.keyOrder:
|
|
|
|
yield key
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
def _itervalues(self):
|
2007-09-16 11:27:38 +08:00
|
|
|
for key in self.keyOrder:
|
2009-12-12 14:00:59 +08:00
|
|
|
yield self[key]
|
2007-09-16 11:27:38 +08:00
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
if six.PY3:
|
|
|
|
items = _iteritems
|
|
|
|
keys = _iterkeys
|
|
|
|
values = _itervalues
|
|
|
|
else:
|
|
|
|
iteritems = _iteritems
|
|
|
|
iterkeys = _iterkeys
|
|
|
|
itervalues = _itervalues
|
|
|
|
|
|
|
|
def items(self):
|
2012-11-11 06:35:46 +08:00
|
|
|
return [(k, self[k]) for k in self.keyOrder]
|
2012-07-25 15:12:59 +08:00
|
|
|
|
|
|
|
def keys(self):
|
2012-11-11 06:35:46 +08:00
|
|
|
return self.keyOrder[:]
|
2012-07-25 15:12:59 +08:00
|
|
|
|
|
|
|
def values(self):
|
2012-11-11 06:35:46 +08:00
|
|
|
return [self[k] for k in self.keyOrder]
|
2012-07-25 15:12:59 +08:00
|
|
|
|
2007-11-26 02:07:57 +08:00
|
|
|
def update(self, dict_):
|
2012-07-25 15:12:59 +08:00
|
|
|
for k, v in six.iteritems(dict_):
|
2009-12-12 14:00:59 +08:00
|
|
|
self[k] = v
|
2006-05-02 09:31:56 +08:00
|
|
|
|
|
|
|
def setdefault(self, key, default):
|
2009-12-12 14:00:59 +08:00
|
|
|
if key not in self:
|
2006-05-02 09:31:56 +08:00
|
|
|
self.keyOrder.append(key)
|
2007-11-26 02:10:45 +08:00
|
|
|
return super(SortedDict, self).setdefault(key, default)
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2007-03-09 13:34:42 +08:00
|
|
|
def copy(self):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""Returns a copy of this object."""
|
2007-03-16 23:10:48 +08:00
|
|
|
# This way of initializing the copy means it works for subclasses, too.
|
2012-04-30 22:12:38 +08:00
|
|
|
return self.__class__(self)
|
2007-03-09 13:34:42 +08:00
|
|
|
|
2007-04-25 15:30:54 +08:00
|
|
|
def __repr__(self):
|
|
|
|
"""
|
|
|
|
Replaces the normal dict.__repr__ with a version that returns the keys
|
|
|
|
in their sorted order.
|
|
|
|
"""
|
2013-08-30 07:20:00 +08:00
|
|
|
return '{%s}' % ', '.join('%r: %r' % (k, v) for k, v in six.iteritems(self))
|
2007-04-25 15:30:54 +08:00
|
|
|
|
2008-02-22 05:27:44 +08:00
|
|
|
def clear(self):
|
|
|
|
super(SortedDict, self).clear()
|
|
|
|
self.keyOrder = []
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2013-08-11 03:00:12 +08:00
|
|
|
class OrderedSet(object):
|
2013-05-10 19:52:04 +08:00
|
|
|
"""
|
|
|
|
A set which keeps the ordering of the inserted items.
|
2013-08-11 02:48:46 +08:00
|
|
|
Currently backs onto OrderedDict.
|
2013-05-10 19:52:04 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, iterable=None):
|
2013-08-11 02:48:46 +08:00
|
|
|
self.dict = OrderedDict(((x, None) for x in iterable) if iterable else [])
|
2013-05-10 19:52:04 +08:00
|
|
|
|
|
|
|
def add(self, item):
|
|
|
|
self.dict[item] = None
|
|
|
|
|
|
|
|
def remove(self, item):
|
|
|
|
del self.dict[item]
|
|
|
|
|
|
|
|
def discard(self, item):
|
|
|
|
try:
|
|
|
|
self.remove(item)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.dict.keys())
|
|
|
|
|
|
|
|
def __contains__(self, item):
|
|
|
|
return item in self.dict
|
|
|
|
|
2014-10-16 18:43:46 +08:00
|
|
|
def __bool__(self):
|
2013-05-10 19:52:04 +08:00
|
|
|
return bool(self.dict)
|
|
|
|
|
2014-10-16 18:43:46 +08:00
|
|
|
def __nonzero__(self): # Python 2 compatibility
|
|
|
|
return type(self).__bool__(self)
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
class MultiValueDictKeyError(KeyError):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
class MultiValueDict(dict):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2007-10-19 09:26:09 +08:00
|
|
|
A subclass of dictionary customized to handle multiple values for the
|
|
|
|
same key.
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
>>> d = MultiValueDict({'name': ['Adrian', 'Simon'], 'position': ['Developer']})
|
|
|
|
>>> d['name']
|
|
|
|
'Simon'
|
|
|
|
>>> d.getlist('name')
|
|
|
|
['Adrian', 'Simon']
|
2011-05-22 23:05:29 +08:00
|
|
|
>>> d.getlist('doesnotexist')
|
|
|
|
[]
|
|
|
|
>>> d.getlist('doesnotexist', ['Adrian', 'Simon'])
|
|
|
|
['Adrian', 'Simon']
|
2005-07-13 09:25:57 +08:00
|
|
|
>>> d.get('lastname', 'nonexistent')
|
|
|
|
'nonexistent'
|
|
|
|
>>> d.setlist('lastname', ['Holovaty', 'Willison'])
|
|
|
|
|
|
|
|
This class exists to solve the irritating problem raised by cgi.parse_qs,
|
|
|
|
which returns a list for every key, even though most Web forms submit
|
|
|
|
single name-value pairs.
|
|
|
|
"""
|
2005-11-30 12:08:46 +08:00
|
|
|
def __init__(self, key_to_list_mapping=()):
|
2007-11-26 02:10:45 +08:00
|
|
|
super(MultiValueDict, self).__init__(key_to_list_mapping)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-12-29 07:11:07 +08:00
|
|
|
def __repr__(self):
|
2007-11-26 02:10:45 +08:00
|
|
|
return "<%s: %s>" % (self.__class__.__name__,
|
|
|
|
super(MultiValueDict, self).__repr__())
|
2005-12-29 07:11:07 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def __getitem__(self, key):
|
2005-11-30 12:08:46 +08:00
|
|
|
"""
|
|
|
|
Returns the last data value for this key, or [] if it's an empty list;
|
|
|
|
raises KeyError if not found.
|
|
|
|
"""
|
|
|
|
try:
|
2007-11-26 02:10:45 +08:00
|
|
|
list_ = super(MultiValueDict, self).__getitem__(key)
|
2005-11-30 12:08:46 +08:00
|
|
|
except KeyError:
|
2013-09-17 04:20:29 +08:00
|
|
|
raise MultiValueDictKeyError(repr(key))
|
2005-11-30 12:08:46 +08:00
|
|
|
try:
|
|
|
|
return list_[-1]
|
|
|
|
except IndexError:
|
|
|
|
return []
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-03-29 01:31:04 +08:00
|
|
|
def __setitem__(self, key, value):
|
2007-11-26 02:10:45 +08:00
|
|
|
super(MultiValueDict, self).__setitem__(key, [value])
|
2006-03-29 01:31:04 +08:00
|
|
|
|
|
|
|
def __copy__(self):
|
2010-10-28 04:39:20 +08:00
|
|
|
return self.__class__([
|
|
|
|
(k, v[:])
|
|
|
|
for k, v in self.lists()
|
|
|
|
])
|
2006-03-29 01:31:04 +08:00
|
|
|
|
2006-06-05 08:46:18 +08:00
|
|
|
def __deepcopy__(self, memo=None):
|
2007-10-19 09:26:09 +08:00
|
|
|
if memo is None:
|
|
|
|
memo = {}
|
2006-03-29 01:31:04 +08:00
|
|
|
result = self.__class__()
|
|
|
|
memo[id(self)] = result
|
|
|
|
for key, value in dict.items(self):
|
2007-10-19 09:26:09 +08:00
|
|
|
dict.__setitem__(result, copy.deepcopy(key, memo),
|
|
|
|
copy.deepcopy(value, memo))
|
2006-03-29 01:31:04 +08:00
|
|
|
return result
|
2009-12-12 14:00:59 +08:00
|
|
|
|
2009-03-31 13:14:13 +08:00
|
|
|
def __getstate__(self):
|
|
|
|
obj_dict = self.__dict__.copy()
|
2014-12-07 05:00:09 +08:00
|
|
|
obj_dict['_data'] = {k: self.getlist(k) for k in self}
|
2009-03-31 13:14:13 +08:00
|
|
|
return obj_dict
|
2009-12-12 14:00:59 +08:00
|
|
|
|
2009-03-31 13:14:13 +08:00
|
|
|
def __setstate__(self, obj_dict):
|
|
|
|
data = obj_dict.pop('_data', {})
|
|
|
|
for k, v in data.items():
|
|
|
|
self.setlist(k, v)
|
|
|
|
self.__dict__.update(obj_dict)
|
2009-12-12 14:00:59 +08:00
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def get(self, key, default=None):
|
2007-11-26 02:14:18 +08:00
|
|
|
"""
|
2007-12-02 03:22:05 +08:00
|
|
|
Returns the last data value for the passed key. If key doesn't exist
|
2007-11-26 02:14:18 +08:00
|
|
|
or value is an empty list, then default is returned.
|
|
|
|
"""
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
|
|
|
val = self[key]
|
2005-11-30 12:08:46 +08:00
|
|
|
except KeyError:
|
2005-07-13 09:25:57 +08:00
|
|
|
return default
|
|
|
|
if val == []:
|
|
|
|
return default
|
|
|
|
return val
|
|
|
|
|
2011-05-22 23:05:29 +08:00
|
|
|
def getlist(self, key, default=None):
|
2007-11-26 02:14:18 +08:00
|
|
|
"""
|
2007-12-02 03:22:05 +08:00
|
|
|
Returns the list of values for the passed key. If key doesn't exist,
|
2011-05-22 23:05:29 +08:00
|
|
|
then a default value is returned.
|
2007-11-26 02:14:18 +08:00
|
|
|
"""
|
2005-07-13 09:25:57 +08:00
|
|
|
try:
|
2007-11-26 02:10:45 +08:00
|
|
|
return super(MultiValueDict, self).__getitem__(key)
|
2005-07-13 09:25:57 +08:00
|
|
|
except KeyError:
|
2012-02-10 02:41:20 +08:00
|
|
|
if default is None:
|
|
|
|
return []
|
|
|
|
return default
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def setlist(self, key, list_):
|
2007-11-26 02:10:45 +08:00
|
|
|
super(MultiValueDict, self).__setitem__(key, list_)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2005-11-30 12:08:46 +08:00
|
|
|
def setdefault(self, key, default=None):
|
|
|
|
if key not in self:
|
|
|
|
self[key] = default
|
2012-07-26 01:10:40 +08:00
|
|
|
# Do not return default here because __setitem__() may store
|
|
|
|
# another value -- QueryDict.__setitem__() does. Look it up.
|
2005-11-30 12:08:46 +08:00
|
|
|
return self[key]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2012-02-10 02:41:20 +08:00
|
|
|
def setlistdefault(self, key, default_list=None):
|
2005-11-30 12:08:46 +08:00
|
|
|
if key not in self:
|
2012-02-10 02:41:20 +08:00
|
|
|
if default_list is None:
|
|
|
|
default_list = []
|
2005-11-30 12:08:46 +08:00
|
|
|
self.setlist(key, default_list)
|
2012-07-26 01:10:40 +08:00
|
|
|
# Do not return default_list here because setlist() may store
|
|
|
|
# another value -- QueryDict.setlist() does. Look it up.
|
2005-11-30 12:08:46 +08:00
|
|
|
return self.getlist(key)
|
|
|
|
|
|
|
|
def appendlist(self, key, value):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""Appends an item to the internal list associated with key."""
|
2012-02-10 02:41:20 +08:00
|
|
|
self.setlistdefault(key).append(value)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
def _iteritems(self):
|
2008-08-16 10:17:55 +08:00
|
|
|
"""
|
|
|
|
Yields (key, value) pairs, where value is the last item in the list
|
|
|
|
associated with the key.
|
|
|
|
"""
|
2012-07-25 15:12:59 +08:00
|
|
|
for key in self:
|
|
|
|
yield key, self[key]
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
def _iterlists(self):
|
2009-03-31 14:30:07 +08:00
|
|
|
"""Yields (key, list) pairs."""
|
2012-07-25 15:12:59 +08:00
|
|
|
return six.iteritems(super(MultiValueDict, self))
|
2009-03-31 14:30:07 +08:00
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
def _itervalues(self):
|
2009-03-31 14:30:07 +08:00
|
|
|
"""Yield the last value on every key list."""
|
2012-07-25 15:12:59 +08:00
|
|
|
for key in self:
|
2009-03-31 14:30:07 +08:00
|
|
|
yield self[key]
|
2009-12-12 14:00:59 +08:00
|
|
|
|
2012-07-25 15:12:59 +08:00
|
|
|
if six.PY3:
|
|
|
|
items = _iteritems
|
|
|
|
lists = _iterlists
|
|
|
|
values = _itervalues
|
|
|
|
else:
|
|
|
|
iteritems = _iteritems
|
|
|
|
iterlists = _iterlists
|
|
|
|
itervalues = _itervalues
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
return list(self.iteritems())
|
|
|
|
|
|
|
|
def lists(self):
|
|
|
|
return list(self.iterlists())
|
|
|
|
|
|
|
|
def values(self):
|
|
|
|
return list(self.itervalues())
|
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def copy(self):
|
2010-10-28 04:39:20 +08:00
|
|
|
"""Returns a shallow copy of this object."""
|
2011-03-28 10:11:19 +08:00
|
|
|
return copy.copy(self)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
2006-09-01 05:51:19 +08:00
|
|
|
def update(self, *args, **kwargs):
|
2007-10-19 09:26:09 +08:00
|
|
|
"""
|
|
|
|
update() extends rather than replaces existing key lists.
|
|
|
|
Also accepts keyword args.
|
|
|
|
"""
|
2006-09-01 05:51:19 +08:00
|
|
|
if len(args) > 1:
|
2010-01-11 02:36:20 +08:00
|
|
|
raise TypeError("update expected at most 1 arguments, got %d" % len(args))
|
2006-09-01 05:51:19 +08:00
|
|
|
if args:
|
|
|
|
other_dict = args[0]
|
|
|
|
if isinstance(other_dict, MultiValueDict):
|
|
|
|
for key, value_list in other_dict.lists():
|
2012-02-10 02:41:20 +08:00
|
|
|
self.setlistdefault(key).extend(value_list)
|
2006-09-01 05:51:19 +08:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
for key, value in other_dict.items():
|
2012-02-10 02:41:20 +08:00
|
|
|
self.setlistdefault(key).append(value)
|
2006-09-01 05:51:19 +08:00
|
|
|
except TypeError:
|
2010-01-11 02:36:20 +08:00
|
|
|
raise ValueError("MultiValueDict.update() takes either a MultiValueDict or dictionary")
|
2012-07-25 15:12:59 +08:00
|
|
|
for key, value in six.iteritems(kwargs):
|
2012-02-10 02:41:20 +08:00
|
|
|
self.setlistdefault(key).append(value)
|
2005-11-30 12:08:46 +08:00
|
|
|
|
2011-06-17 00:34:55 +08:00
|
|
|
def dict(self):
|
|
|
|
"""
|
|
|
|
Returns current object as a dict with singular values.
|
|
|
|
"""
|
2014-12-07 05:00:09 +08:00
|
|
|
return {key: self[key] for key in self}
|
2011-06-17 00:34:55 +08:00
|
|
|
|
2007-08-12 20:02:08 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
class ImmutableList(tuple):
|
2007-08-12 20:02:08 +08:00
|
|
|
"""
|
2008-07-01 23:10:51 +08:00
|
|
|
A tuple-like object that raises useful errors when it is asked to mutate.
|
|
|
|
|
|
|
|
Example::
|
|
|
|
|
|
|
|
>>> a = ImmutableList(range(5), warning="You cannot mutate this.")
|
|
|
|
>>> a[3] = '4'
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
AttributeError: You cannot mutate this.
|
2007-08-12 20:02:08 +08:00
|
|
|
"""
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
def __new__(cls, *args, **kwargs):
|
|
|
|
if 'warning' in kwargs:
|
|
|
|
warning = kwargs['warning']
|
|
|
|
del kwargs['warning']
|
|
|
|
else:
|
|
|
|
warning = 'ImmutableList object is immutable.'
|
|
|
|
self = tuple.__new__(cls, *args, **kwargs)
|
|
|
|
self.warning = warning
|
|
|
|
return self
|
|
|
|
|
|
|
|
def complain(self, *wargs, **kwargs):
|
|
|
|
if isinstance(self.warning, Exception):
|
|
|
|
raise self.warning
|
|
|
|
else:
|
2010-01-11 02:36:20 +08:00
|
|
|
raise AttributeError(self.warning)
|
2008-07-01 23:10:51 +08:00
|
|
|
|
|
|
|
# All list mutation functions complain.
|
2013-10-22 21:31:43 +08:00
|
|
|
__delitem__ = complain
|
2008-07-01 23:10:51 +08:00
|
|
|
__delslice__ = complain
|
2013-10-22 21:31:43 +08:00
|
|
|
__iadd__ = complain
|
|
|
|
__imul__ = complain
|
|
|
|
__setitem__ = complain
|
2008-07-01 23:10:51 +08:00
|
|
|
__setslice__ = complain
|
2013-10-22 21:31:43 +08:00
|
|
|
append = complain
|
|
|
|
extend = complain
|
|
|
|
insert = complain
|
|
|
|
pop = complain
|
2013-10-23 18:09:29 +08:00
|
|
|
remove = complain
|
2013-10-22 21:31:43 +08:00
|
|
|
sort = complain
|
|
|
|
reverse = complain
|
2008-06-26 11:11:32 +08:00
|
|
|
|
2013-11-03 07:53:29 +08:00
|
|
|
|
2008-06-26 11:11:32 +08:00
|
|
|
class DictWrapper(dict):
|
|
|
|
"""
|
|
|
|
Wraps accesses to a dictionary so that certain values (those starting with
|
|
|
|
the specified prefix) are passed through a function before being returned.
|
|
|
|
The prefix is removed before looking up the real value.
|
|
|
|
|
|
|
|
Used by the SQL construction code to ensure that values are correctly
|
|
|
|
quoted before being used.
|
|
|
|
"""
|
|
|
|
def __init__(self, data, func, prefix):
|
|
|
|
super(DictWrapper, self).__init__(data)
|
|
|
|
self.func = func
|
|
|
|
self.prefix = prefix
|
|
|
|
|
|
|
|
def __getitem__(self, key):
|
|
|
|
"""
|
|
|
|
Retrieves the real value after stripping the prefix string (if
|
|
|
|
present). If the prefix is present, pass the value through self.func
|
|
|
|
before returning, otherwise return the raw value.
|
|
|
|
"""
|
|
|
|
if key.startswith(self.prefix):
|
|
|
|
use_func = True
|
|
|
|
key = key[len(self.prefix):]
|
|
|
|
else:
|
|
|
|
use_func = False
|
|
|
|
value = super(DictWrapper, self).__getitem__(key)
|
|
|
|
if use_func:
|
|
|
|
return self.func(value)
|
|
|
|
return value
|