source: trunk/packages/sipb-xen-www/code/webcommon.py @ 833

Last change on this file since 833 was 632, checked in by andersk, 16 years ago

Add overlord mode, accessible from xvm.mit.edu/overlord by
system:sipb-xen.

File size: 1.9 KB
RevLine 
[209]1"""Exceptions for the web interface."""
2
[535]3import time
[261]4from sipb_xen_database import Machine, MachineAccess
[209]5
6class MyException(Exception):
7    """Base class for my exceptions"""
8    pass
9
10class InvalidInput(MyException):
11    """Exception for user-provided input is invalid but maybe in good faith.
12
13    This would include setting memory to negative (which might be a
14    typo) but not setting an invalid boot CD (which requires bypassing
15    the select box).
16    """
17    def __init__(self, err_field, err_value, expl=None):
18        MyException.__init__(self, expl)
19        self.err_field = err_field
20        self.err_value = err_value
21
22class CodeError(MyException):
23    """Exception for internal errors or bad faith input."""
24    pass
25
26import controls
27
[261]28def cachedproperty(func):
29    name = '__cache_' + func.__name__ + '_' + str(id(func))
30    def getter(self):
31        try:
32            return getattr(self, name)
33        except AttributeError:
34            value = func(self)
35            setattr(self, name, value)
36            return value
37    return property(getter)
38
[572]39class State(object):
40    """State for a request"""
[632]41    def __init__(self, user, overlord=False):
[572]42        self.username = user
[632]43        self.overlord = overlord
[209]44
[632]45    def getMachines(self):
46        if self.overlord:
47            return Machine.select()
48        else:
49            return Machine.query().join('acl').select_by(user=self.username)
50
51    machines = cachedproperty(getMachines)
[554]52    xmlist_raw = cachedproperty(lambda self: controls.getList())
[535]53    xmlist = cachedproperty(lambda self:
[554]54                                dict((m, self.xmlist_raw[m.name])
55                                     for m in self.machines
56                                     if m.name in self.xmlist_raw))
[535]57
[209]58    def clear(self):
59        """Clear the state so future accesses reload it."""
[264]60        for attr in list(self.__dict__):
[261]61            if attr.startswith('__cache_'):
[209]62                delattr(self, attr)
Note: See TracBrowser for help on using the repository browser.