3 * 0: The device is inactive.
4 * ST_ACTIVE|ST_TRANSITION: The device is transitioning from inactive to active.
5 * ST_ACTIVE: The device is active.
6 * ST_TRANSITION: The device is transitioning from active to inactive.
12 class OnoffDevice(object):
13 """A device is a thing with two states, that can be asked to transition
14 from either state to the other. It can signal state changes to interested
17 @type notify: {int -> None}
18 @ivar notify: A set of functions taking a changed state.
21 @ivar state: is a read-only attribute to retrieve the current state
26 def changestate(self, state):
27 """Tell interested parties that the state has changed to the given
29 for func in self.notify:
33 """Ask the device to power on."""
34 raise NotImplementedError
37 """Ask the device to power off."""
38 raise NotImplementedError
41 """Release resources acquired by the constructor."""
44 class InvertedDevice(OnoffDevice):
45 """A device that swaps active and inactive states of a give device."""
46 def __init__(self, device):
47 OnoffDevice.__init__(self)
49 self.device.activate()
50 self.device.notify.add(self.changestate)
52 def changestate(self, state):
53 OnoffDevice.changestate(self, state ^ ST_ACTIVE)
57 return self.device.state ^ ST_ACTIVE
60 self.device.deactivate()
63 self.device.activate()
66 self.device.notify.remove(self.changestate)