add a sispmctl_device
[~helmut/onoff.git] / onoff / command.py
index c3dd4a6..87b9b26 100644 (file)
@@ -1,13 +1,33 @@
 import logging
 
-from gi.repository import GObject
-
 from .common import OnoffDevice, ST_ACTIVE, ST_TRANSITION
+from .gobject import spawn_child
 
 logger = logging.getLogger("onoff.command")
 
 class OnoffCommand(OnoffDevice):
+    """A device that is enabled and disabled by executing separate commands.
+    The transition period is the duration of the commands.
+    @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
+    @ivar target_state: is the state that we are currently transitioning to
+    """
     def __init__(self, oncommand, offcommand):
+        """
+
+        @type oncommand: [str]
+        @param command: an argument vector to be executed for activation.
+        @type offcommand: [str]
+        @param command: 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
@@ -27,11 +47,8 @@ class OnoffCommand(OnoffDevice):
                          (self.oncommand, "oncommand")][state]
         self.target_state = state
         logger.info("invoking %s %s", name, " ".join(command))
-        ret = GObject.spawn_async(command, flags=GObject.SPAWN_SEARCH_PATH | GObject.SPAWN_DO_NOT_REAP_CHILD)
-        self.pid = ret[0]
-        assert self.pid
+        self.pid, self.watch = spawn_child(command, self.process_died)
         logger.debug("started %s as pid %d", name, self.pid)
-        self.watch = GObject.child_watch_add(self.pid, self.process_died)
         self.changestate(self.state)
 
     def process_died(self, pid, condition):