Changeset 261 for trunk/web/webcommon.py
- Timestamp:
- Jan 24, 2008, 9:34:53 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/web/webcommon.py
r236 r261 1 1 """Exceptions for the web interface.""" 2 2 3 from sipb_xen_database import Machine 3 from sipb_xen_database import Machine, MachineAccess 4 4 5 5 class MyException(Exception): … … 25 25 import controls 26 26 27 def cachedproperty(func): 28 name = '__cache_' + func.__name__ + '_' + str(id(func)) 29 def getter(self): 30 try: 31 return getattr(self, name) 32 except AttributeError: 33 value = func(self) 34 setattr(self, name, value) 35 return value 36 return property(getter) 37 27 38 class Global(object): 28 39 """Global state of the system, to avoid duplicate remctls to get state""" 29 40 def __init__(self, user): 30 41 self.user = user 31 32 def __get_uptimes(self):33 if not hasattr(self, '_uptimes'):34 self._uptimes = controls.getUptimes(Machine.select())35 return self._uptimes36 uptimes = property(__get_uptimes)42 43 machines = cachedproperty(lambda self: 44 [ma.machine for ma in 45 MachineAccess.select_by(user=self.user)]) 46 uptimes = cachedproperty(lambda self: 47 controls.getUptimes(self.machines)) 37 48 38 49 def clear(self): 39 50 """Clear the state so future accesses reload it.""" 40 for attr in ('_uptimes', ):41 if hasattr(self, attr):51 for attr in self.__dict__: 52 if attr.startswith('__cache_'): 42 53 delattr(self, attr) 43 54
Note: See TracChangeset
for help on using the changeset viewer.