new Storage class, thanks Oscar Benjamin
This commit is contained in:
@@ -1 +1 @@
|
|||||||
Version 2.00.0 (2012-08-21 09:52:19) dev
|
Version 2.00.0 (2012-08-21 11:34:52) dev
|
||||||
|
|||||||
+59
-84
@@ -25,7 +25,11 @@ class List(list):
|
|||||||
instead of IndexOutOfBounds
|
instead of IndexOutOfBounds
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __call__(self, i, default=None, cast=None, url_onerror=None):
|
def __call__(self, i, default=None, cast=None, otherwise=None):
|
||||||
|
"""
|
||||||
|
request.args(0,default=0,cast=int,otherwise='http://error_url')
|
||||||
|
request.args(0,default=0,cast=int,otherwise=lambda:...)
|
||||||
|
"""
|
||||||
n = len(self)
|
n = len(self)
|
||||||
if 0<=i<n or -n<=i<0:
|
if 0<=i<n or -n<=i<0:
|
||||||
value = self[i]
|
value = self[i]
|
||||||
@@ -36,14 +40,17 @@ class List(list):
|
|||||||
value = cast(value)
|
value = cast(value)
|
||||||
except (ValueError, TypeError):
|
except (ValueError, TypeError):
|
||||||
from http import HTTP, redirect
|
from http import HTTP, redirect
|
||||||
if url_onerror:
|
if otherwise is None:
|
||||||
redirect(url_onerror)
|
|
||||||
else:
|
|
||||||
raise HTTP(404)
|
raise HTTP(404)
|
||||||
|
elif isinstance(otherwise,str):
|
||||||
|
redirect(otherwise)
|
||||||
|
elif callable(otherwise):
|
||||||
|
return otherwise()
|
||||||
|
else:
|
||||||
|
raise RuntimeError, "invalid otherwise"
|
||||||
return value
|
return value
|
||||||
|
|
||||||
class Storage(dict):
|
class Storage(dict):
|
||||||
|
|
||||||
"""
|
"""
|
||||||
A Storage object is like a dictionary except `obj.foo` can be used
|
A Storage object is like a dictionary except `obj.foo` can be used
|
||||||
in addition to `obj['foo']`, and setting obj.foo = None deletes item foo.
|
in addition to `obj['foo']`, and setting obj.foo = None deletes item foo.
|
||||||
@@ -61,41 +68,22 @@ class Storage(dict):
|
|||||||
|
|
||||||
>>> del o.a
|
>>> del o.a
|
||||||
>>> print o.a
|
>>> print o.a
|
||||||
None
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
def __getattr__(self, key):
|
self.__dict__ = self
|
||||||
return dict.get(self, key, None)
|
dict.__init__(self, *args, **kwargs)
|
||||||
|
def __getattr__(self,key):
|
||||||
def __setattr__(self, key, value):
|
return getattr(self,key) if key in self else None
|
||||||
if value is None:
|
def __getitem__(self,key):
|
||||||
if key in self:
|
return dict.get(self,key,None)
|
||||||
del self[key]
|
def copy(self):
|
||||||
else:
|
return Storage(self)
|
||||||
self[key] = value
|
|
||||||
|
|
||||||
def __delattr__(self, key):
|
|
||||||
if key in self:
|
|
||||||
del self[key]
|
|
||||||
else:
|
|
||||||
raise AttributeError, "missing key=%s" % key
|
|
||||||
|
|
||||||
def __getitem__(self, key):
|
|
||||||
return dict.get(self, key, None)
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return '<Storage ' + dict.__repr__(self) + '>'
|
return '<Storage %s>' % self.__dict__
|
||||||
|
def getlist(self,key):
|
||||||
def __getstate__(self):
|
"""
|
||||||
return dict(self)
|
Return a Storage value as a list.
|
||||||
|
|
||||||
def __setstate__(self, value):
|
|
||||||
for (k, v) in value.items():
|
|
||||||
self[k] = v
|
|
||||||
|
|
||||||
def getlist(self, key):
|
|
||||||
"""Return a Storage value as a list.
|
|
||||||
|
|
||||||
If the value is a list it will be returned as-is.
|
If the value is a list it will be returned as-is.
|
||||||
If object is None, an empty list will be returned.
|
If object is None, an empty list will be returned.
|
||||||
@@ -112,17 +100,13 @@ class Storage(dict):
|
|||||||
['abc', 'def']
|
['abc', 'def']
|
||||||
>>> request.vars.getlist('z')
|
>>> request.vars.getlist('z')
|
||||||
[]
|
[]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
value = self.get(key, None)
|
value = getattr(self,key,[])
|
||||||
if isinstance(value, (list, tuple)):
|
return value if not value else \
|
||||||
return value
|
value if isinstance(value,(list,tuple)) else [value]
|
||||||
elif value is None:
|
def getfirst(self,key,default=None):
|
||||||
return []
|
"""
|
||||||
return [value]
|
Return the first or only value when given a request.vars-style key.
|
||||||
|
|
||||||
def getfirst(self, key):
|
|
||||||
"""Return the first or only value when given a request.vars-style key.
|
|
||||||
|
|
||||||
If the value is a list, its first item will be returned;
|
If the value is a list, its first item will be returned;
|
||||||
otherwise, the value will be returned as-is.
|
otherwise, the value will be returned as-is.
|
||||||
@@ -137,15 +121,13 @@ class Storage(dict):
|
|||||||
>>> request.vars.getfirst('y')
|
>>> request.vars.getfirst('y')
|
||||||
'abc'
|
'abc'
|
||||||
>>> request.vars.getfirst('z')
|
>>> request.vars.getfirst('z')
|
||||||
|
|
||||||
"""
|
"""
|
||||||
value = self.getlist(key)
|
values = self.getlist(default)
|
||||||
if len(value):
|
return values[0] if values else default
|
||||||
return value[0]
|
def getlast(self,key,default=None):
|
||||||
return None
|
"""
|
||||||
|
Returns the last or only single value when
|
||||||
def getlast(self, key):
|
given a request.vars-style key.
|
||||||
"""Returns the last or only single value when given a request.vars-style key.
|
|
||||||
|
|
||||||
If the value is a list, the last item will be returned;
|
If the value is a list, the last item will be returned;
|
||||||
otherwise, the value will be returned as-is.
|
otherwise, the value will be returned as-is.
|
||||||
@@ -160,33 +142,30 @@ class Storage(dict):
|
|||||||
>>> request.vars.getlast('y')
|
>>> request.vars.getlast('y')
|
||||||
'def'
|
'def'
|
||||||
>>> request.vars.getlast('z')
|
>>> request.vars.getlast('z')
|
||||||
|
|
||||||
"""
|
"""
|
||||||
value = self.getlist(key)
|
values = self.getlist(default)
|
||||||
if len(value):
|
return values[0] if values else default
|
||||||
return value[-1]
|
|
||||||
return None
|
|
||||||
|
|
||||||
def __getinitargs__(self):
|
|
||||||
return ()
|
|
||||||
|
|
||||||
def __getnewargs__(self):
|
# import _abcoll
|
||||||
return ()
|
# _abcoll.MutableMapping.register(Storage)
|
||||||
|
|
||||||
|
|
||||||
PICKABLE = (str,int,long,float,bool,list,dict,tuple,set)
|
PICKABLE = (str,int,long,float,bool,list,dict,tuple,set)
|
||||||
def PickleableStorage(data):
|
|
||||||
return Storage(dict((k,v) for (k,v) in data.items() if isinstance(v,PICKABLE)))
|
|
||||||
|
|
||||||
class StorageList(Storage):
|
class StorageList(Storage):
|
||||||
"""
|
"""
|
||||||
like Storage but missing elements default to [] instead of None
|
like Storage but missing elements default to [] instead of None
|
||||||
"""
|
"""
|
||||||
|
def __getitem__(self,key):
|
||||||
|
return self.__gteattr__(key)
|
||||||
def __getattr__(self, key):
|
def __getattr__(self, key):
|
||||||
if key in self:
|
if key in self:
|
||||||
return self[key]
|
return getattr(self,key)
|
||||||
else:
|
else:
|
||||||
self[key] = []
|
r = []
|
||||||
return self[key]
|
setattr(self,key,r)
|
||||||
|
return r
|
||||||
|
|
||||||
def load_storage(filename):
|
def load_storage(filename):
|
||||||
fp = None
|
fp = None
|
||||||
@@ -197,7 +176,6 @@ def load_storage(filename):
|
|||||||
if fp: fp.close()
|
if fp: fp.close()
|
||||||
return Storage(storage)
|
return Storage(storage)
|
||||||
|
|
||||||
|
|
||||||
def save_storage(storage, filename):
|
def save_storage(storage, filename):
|
||||||
fp = None
|
fp = None
|
||||||
try:
|
try:
|
||||||
@@ -207,35 +185,31 @@ def save_storage(storage, filename):
|
|||||||
if fp: fp.close()
|
if fp: fp.close()
|
||||||
|
|
||||||
class Settings(Storage):
|
class Settings(Storage):
|
||||||
|
|
||||||
def __setattr__(self, key, value):
|
def __setattr__(self, key, value):
|
||||||
if key != 'lock_keys' and self.get('lock_keys', None)\
|
if key != 'lock_keys' and self.lock_keys and not key in self:
|
||||||
and not key in self:
|
|
||||||
raise SyntaxError, 'setting key \'%s\' does not exist' % key
|
raise SyntaxError, 'setting key \'%s\' does not exist' % key
|
||||||
if key != 'lock_values' and self.get('lock_values', None):
|
if key != 'lock_values' and self.lock_values:
|
||||||
raise SyntaxError, 'setting value cannot be changed: %s' % key
|
raise SyntaxError, 'setting value cannot be changed: %s' % key
|
||||||
self[key] = value
|
Storage.__setattr__(self,key,value)
|
||||||
|
|
||||||
|
|
||||||
class Messages(Storage):
|
class Messages(Storage):
|
||||||
|
|
||||||
def __init__(self, T):
|
def __init__(self, T):
|
||||||
self['T'] = T
|
self.T = T
|
||||||
|
|
||||||
def __setattr__(self, key, value):
|
def __setattr__(self, key, value):
|
||||||
if key != 'lock_keys' and self.get('lock_keys', None)\
|
if key != 'lock_keys' and self.lock_keys and not key in self:
|
||||||
and not key in self:
|
|
||||||
raise SyntaxError, 'setting key \'%s\' does not exist' % key
|
raise SyntaxError, 'setting key \'%s\' does not exist' % key
|
||||||
if key != 'lock_values' and self.get('lock_values', None):
|
if key != 'lock_values' and self.lock_values:
|
||||||
raise SyntaxError, 'setting value cannot be changed: %s' % key
|
raise SyntaxError, 'setting value cannot be changed: %s' % key
|
||||||
self[key] = value
|
Storage.__setattr__(self,key,value)
|
||||||
|
|
||||||
def __getattr__(self, key):
|
def __getattr__(self, key):
|
||||||
value = self[key]
|
value = Storage.__getattr__(self,key)
|
||||||
if isinstance(value, str):
|
if isinstance(value, str):
|
||||||
return str(self['T'](value))
|
return str(self.T(value))
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import doctest
|
import doctest
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
@@ -244,3 +218,4 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user