Package gluon :: Module storage
[hide private]
[frames] | no frames]

Source Code for Module gluon.storage

  1  #!/usr/bin/env python 
  2  # -*- coding: utf-8 -*- 
  3   
  4  """ 
  5  This file is part of the web2py Web Framework 
  6  Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu> 
  7  License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html) 
  8   
  9  Provides: 
 10   
 11  - List; like list but returns None instead of IndexOutOfBounds 
 12  - Storage; like dictionary allowing also for `obj.foo` for `obj['foo']` 
 13  """ 
 14   
 15  import cPickle 
 16  import gluon.portalocker as portalocker 
 17   
 18  __all__ = ['List', 'Storage', 'Settings', 'Messages', 
 19             'StorageList', 'load_storage', 'save_storage'] 
 20   
 21  DEFAULT = lambda:0 
 22   
23 -class Storage(dict):
24 """ 25 A Storage object is like a dictionary except `obj.foo` can be used 26 in addition to `obj['foo']`, and setting obj.foo = None deletes item foo. 27 28 >>> o = Storage(a=1) 29 >>> print o.a 30 1 31 32 >>> o['a'] 33 1 34 35 >>> o.a = 2 36 >>> print o['a'] 37 2 38 39 >>> del o.a 40 >>> print o.a 41 None 42 """ 43 __slots__ = () 44 __setattr__ = dict.__setitem__ 45 __delattr__ = dict.__delitem__ 46 __getitem__ = dict.get 47 __getattr__ = dict.get 48 __repr__ = lambda self: '<Storage %s>' % dict.__repr__(self) 49 # http://stackoverflow.com/questions/5247250/why-does-pickle-getstate-accept-as-a-return-value-the-very-instance-it-requi 50 __getstate__ = lambda self: None 51 __copy__ = lambda self: Storage(self) 52
53 - def getlist(self, key):
54 """ 55 Return a Storage value as a list. 56 57 If the value is a list it will be returned as-is. 58 If object is None, an empty list will be returned. 59 Otherwise, [value] will be returned. 60 61 Example output for a query string of ?x=abc&y=abc&y=def 62 >>> request = Storage() 63 >>> request.vars = Storage() 64 >>> request.vars.x = 'abc' 65 >>> request.vars.y = ['abc', 'def'] 66 >>> request.vars.getlist('x') 67 ['abc'] 68 >>> request.vars.getlist('y') 69 ['abc', 'def'] 70 >>> request.vars.getlist('z') 71 [] 72 """ 73 value = self.get(key, []) 74 if value is None or isinstance(value, (list, tuple)): 75 return value 76 else: 77 return [value]
78
79 - def getfirst(self, key, default=None):
80 """ 81 Return the first or only value when given a request.vars-style key. 82 83 If the value is a list, its first item will be returned; 84 otherwise, the value will be returned as-is. 85 86 Example output for a query string of ?x=abc&y=abc&y=def 87 >>> request = Storage() 88 >>> request.vars = Storage() 89 >>> request.vars.x = 'abc' 90 >>> request.vars.y = ['abc', 'def'] 91 >>> request.vars.getfirst('x') 92 'abc' 93 >>> request.vars.getfirst('y') 94 'abc' 95 >>> request.vars.getfirst('z') 96 """ 97 values = self.getlist(key) 98 return values[0] if values else default
99
100 - def getlast(self, key, default=None):
101 """ 102 Returns the last or only single value when 103 given a request.vars-style key. 104 105 If the value is a list, the last item will be returned; 106 otherwise, the value will be returned as-is. 107 108 Simulated output with a query string of ?x=abc&y=abc&y=def 109 >>> request = Storage() 110 >>> request.vars = Storage() 111 >>> request.vars.x = 'abc' 112 >>> request.vars.y = ['abc', 'def'] 113 >>> request.vars.getlast('x') 114 'abc' 115 >>> request.vars.getlast('y') 116 'def' 117 >>> request.vars.getlast('z') 118 """ 119 values = self.getlist(key) 120 return values[-1] if values else default
121 122 PICKABLE = (str, int, long, float, bool, list, dict, tuple, set) 123 124
125 -class StorageList(Storage):
126 """ 127 like Storage but missing elements default to [] instead of None 128 """
129 - def __getitem__(self, key):
130 return self.__getattr__(key)
131
132 - def __getattr__(self, key):
133 if key in self: 134 return getattr(self, key) 135 else: 136 r = [] 137 setattr(self, key, r) 138 return r
139 140
141 -def load_storage(filename):
142 fp = None 143 try: 144 fp = portalocker.LockedFile(filename, 'rb') 145 storage = cPickle.load(fp) 146 finally: 147 if fp: 148 fp.close() 149 return Storage(storage)
150 151
152 -def save_storage(storage, filename):
153 fp = None 154 try: 155 fp = portalocker.LockedFile(filename, 'wb') 156 cPickle.dump(dict(storage), fp) 157 finally: 158 if fp: 159 fp.close()
160 161
162 -class Settings(Storage):
163 - def __setattr__(self, key, value):
164 if key != 'lock_keys' and self['lock_keys'] and key not in self: 165 raise SyntaxError('setting key \'%s\' does not exist' % key) 166 if key != 'lock_values' and self['lock_values']: 167 raise SyntaxError('setting value cannot be changed: %s' % key) 168 self[key] = value
169 170
171 -class Messages(Settings):
172 - def __init__(self, T):
173 Storage.__init__(self, T=T)
174
175 - def __getattr__(self, key):
176 value = self[key] 177 if isinstance(value, str): 178 return str(self.T(value)) 179 return value
180
181 -class FastStorage(dict):
182 """ 183 Eventually this should replace class Storage but causes memory leak 184 because of http://bugs.python.org/issue1469629 185 186 >>> s = FastStorage() 187 >>> s.a = 1 188 >>> s.a 189 1 190 >>> s['a'] 191 1 192 >>> s.b 193 >>> s['b'] 194 >>> s['b']=2 195 >>> s['b'] 196 2 197 >>> s.b 198 2 199 >>> isinstance(s,dict) 200 True 201 >>> dict(s) 202 {'a': 1, 'b': 2} 203 >>> dict(FastStorage(s)) 204 {'a': 1, 'b': 2} 205 >>> import pickle 206 >>> s = pickle.loads(pickle.dumps(s)) 207 >>> dict(s) 208 {'a': 1, 'b': 2} 209 >>> del s.b 210 >>> del s.a 211 >>> s.a 212 >>> s.b 213 >>> s['a'] 214 >>> s['b'] 215 """
216 - def __init__(self, *args, **kwargs):
217 dict.__init__(self, *args, **kwargs) 218 self.__dict__ = self
219
220 - def __getattr__(self, key):
221 return getattr(self, key) if key in self else None
222
223 - def __getitem__(self, key):
224 return dict.get(self, key, None)
225
226 - def copy(self):
227 self.__dict__ = {} 228 s = FastStorage(self) 229 self.__dict__ = self 230 return s
231
232 - def __repr__(self):
233 return '<Storage %s>' % dict.__repr__(self)
234
235 - def __getstate__(self):
236 return dict(self)
237
238 - def __setstate__(self, sdict):
239 dict.__init__(self, sdict) 240 self.__dict__ = self
241
242 - def update(self, *args, **kwargs):
243 dict.__init__(self, *args, **kwargs) 244 self.__dict__ = self
245 246
247 -class List(list):
248 """ 249 Like a regular python list but a[i] if i is out of bounds return None 250 instead of IndexOutOfBounds 251 """ 252
253 - def __call__(self, i, default=DEFAULT, cast=None, otherwise=None):
254 """ 255 request.args(0,default=0,cast=int,otherwise='http://error_url') 256 request.args(0,default=0,cast=int,otherwise=lambda:...) 257 """ 258 n = len(self) 259 if 0 <= i < n or -n <= i < 0: 260 value = self[i] 261 elif default is DEFAULT: 262 value = None 263 else: 264 value, cast = default, False 265 if cast: 266 try: 267 value = cast(value) 268 except (ValueError, TypeError): 269 from http import HTTP, redirect 270 if otherwise is None: 271 raise HTTP(404) 272 elif isinstance(otherwise, str): 273 redirect(otherwise) 274 elif callable(otherwise): 275 return otherwise() 276 else: 277 raise RuntimeError("invalid otherwise") 278 return value
279 280 281 if __name__ == '__main__': 282 import doctest 283 doctest.testmod() 284