1
# Copyright 2010-2011 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
14
def register_state(state):
15
"""Register a class to be an implementation for L{State}s.
17
The class's C{__state_name__} must be defined as a string and
18
C{__state_parent__} as a class.
20
if "__state_name__" not in state.__dict__:
21
state.__state_name__ = ""
22
if "__state_parent__" not in state.__dict__:
23
state.__state_parent__ = None
25
_state_types[state] = {}
26
if state.__state_parent__ is not None:
27
if state.__state_parent__ not in _state_types:
28
raise RuntimeError("%r state ancestor does not exist already"
29
% state.__state_parent__)
31
if state.__state_name__ in _state_types[state.__state_parent__]:
32
raise RuntimeError("%s state name already in ancestor %r"
33
% (state.__state_name__, state.__state_parent__))
35
_state_types[state.__state_parent__][state.__state_name__] = state
38
class StateMeta(type):
40
def __init__(cls, name, bases, dict):
41
super(StateMeta, cls).__init__(name, bases, dict)
47
__metaclass__ = StateMeta
49
__slots__ = ("parent",)
51
def __init__(self, parent=None):
58
while state is not None:
66
return cls(self.parent)
68
def getChild(self, name=""):
70
children = _state_types[cls]
72
raise Exception("State has no children: %s" % cls)
74
if name not in children:
75
raise Exception("State does not have child: %s" % name)
77
return children[name](self)
79
def handleEntry(self, result, *args):