use the enum module to represent states
[~helmut/onoff.git] / onoff / command.py
index 87b9b26..7a40bbd 100644 (file)
@@ -1,6 +1,6 @@
 import logging
 
-from .common import OnoffDevice, ST_ACTIVE, ST_TRANSITION
+from .common import OnoffDevice, OnoffState
 from .gobject import spawn_child
 
 logger = logging.getLogger("onoff.command")
@@ -11,8 +11,6 @@ class OnoffCommand(OnoffDevice):
     @type pid: int or None
     @ivar pid: is either None or the pid of a transition command as long as
             it lives.
-    @ivar watch: is either None or a GObject event source id of the
-            callback waiting for the termination of the spawned process.
     @type desired_state: bool
     @ivar desired_state: is the state that should be transitioned to
     @type target_state: bool
@@ -22,24 +20,23 @@ class OnoffCommand(OnoffDevice):
         """
 
         @type oncommand: [str]
-        @param command: an argument vector to be executed for activation.
+        @param oncommand: an argument vector to be executed for activation.
         @type offcommand: [str]
-        @param command: an argument vector to be executed for deactivation.
+        @param offcommand: an argument vector to be executed for deactivation.
         @note: For both commands the first element is used as executable and
                 looked up in $PATH.
         """
         OnoffDevice.__init__(self)
         self.oncommand = oncommand
         self.offcommand = offcommand
-        self.desired_state = 0 # bit mask of just ST_ACTIVE
+        self.desired_state = OnoffState.inactive
         self.pid = None
-        self.watch = None
-        self.target_state = 0 # state the last command targeted
+        self.target_state = OnoffState.inactive  # last command target state
 
     @property
     def state(self):
         if self.pid is not None:
-            return self.desired_state | ST_TRANSITION
+            return self.desired_state | OnoffState.transition
         return self.desired_state
 
     def transition(self, state):
@@ -47,31 +44,31 @@ class OnoffCommand(OnoffDevice):
                          (self.oncommand, "oncommand")][state]
         self.target_state = state
         logger.info("invoking %s %s", name, " ".join(command))
-        self.pid, self.watch = spawn_child(command, self.process_died)
+        self.pid = spawn_child(command, self.process_died)
         logger.debug("started %s as pid %d", name, self.pid)
         self.changestate(self.state)
 
     def process_died(self, pid, condition):
         assert self.pid == pid
-        assert self.watch is not None
-        self.watch = None
         self.pid = None
-        logger.info("command %d targeting %d completed", pid, self.target_state)
+        logger.info("command %d targeting %s completed", pid,
+                    self.target_state.name)
         if self.desired_state == self.target_state:
             self.changestate(self.state)
         else:
-            logger.info("desired state changed to %d during invocation " +
-                        "targeting %d", self.desired_state, self.target_state)
+            logger.info("desired state changed to %s during invocation " +
+                        "targeting %s", self.desired_state.name,
+                        self.target_state.name)
             self.transition(self.desired_state)
 
     def activate(self):
-        if self.desired_state != ST_ACTIVE:
-            self.desired_state = ST_ACTIVE
+        if self.desired_state != OnoffState.active:
+            self.desired_state = OnoffState.active
             if self.pid is None:
                 self.transition(self.desired_state)
 
     def deactivate(self):
-        if self.desired_state != 0:
-            self.desired_state = 0
+        if self.desired_state != OnoffState.inactive:
+            self.desired_state = OnoffState.inactive
             if self.pid is None:
                 self.transition(self.desired_state)