Split main.py in four.
[invirt/packages/invirt-web.git] / templates / webcommon.py
1 """Exceptions for the web interface."""
2
3 from sipb_xen_database import Machine
4
5 class MyException(Exception):
6     """Base class for my exceptions"""
7     pass
8
9 class InvalidInput(MyException):
10     """Exception for user-provided input is invalid but maybe in good faith.
11
12     This would include setting memory to negative (which might be a
13     typo) but not setting an invalid boot CD (which requires bypassing
14     the select box).
15     """
16     def __init__(self, err_field, err_value, expl=None):
17         MyException.__init__(self, expl)
18         self.err_field = err_field
19         self.err_value = err_value
20
21 class CodeError(MyException):
22     """Exception for internal errors or bad faith input."""
23     pass
24
25 import controls
26
27 class Global(object):
28     """Global state of the system, to avoid duplicate remctls to get state"""
29     def __init__(self, user):
30         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._uptimes
36     uptimes = property(__get_uptimes)
37
38     def clear(self):
39         """Clear the state so future accesses reload it."""
40         for attr in ('_uptimes', ):
41             if hasattr(self, attr):
42                 delattr(self, attr)
43
44 g = Global(None)