abstract away ScheduledFunction
authorHelmut Grohne <helmut@subdivi.de>
Sat, 19 Aug 2017 19:41:33 +0000 (21:41 +0200)
committerHelmut Grohne <helmut@subdivi.de>
Sat, 19 Aug 2017 19:41:33 +0000 (21:41 +0200)
Rather than work with GObject.timeout_add and GObject.source_remove
directly, wrap them up in a ScheduledFunction to ease usage.

onoff/common.py
onoff/dbusutils.py
onoff/gobject.py
onoff/process.py

index df32990..2aa79b6 100644 (file)
@@ -8,7 +8,7 @@ Defined states:
 
 import logging
 
-from gi.repository import GObject
+from .gobject import ScheduledFunction
 
 logger = logging.getLogger("onoff.common")
 
@@ -100,12 +100,11 @@ class ThrottledDevice(OnoffDevice):
 
     def _schedule_transition(self, delay, func):
         assert self.transition is None
-        self.transition = GObject.timeout_add(int(1000 * delay), func)
+        self.transition = ScheduledFunction(delay, func)
 
     def _cancel_transition(self):
         assert self.transition is not None
-        ret = GObject.source_remove(self.transition)
-        assert ret
+        self.transition.cancel()
         self.transition = None
 
     def changestate(self, state):
index e0312cb..0477ed1 100644 (file)
@@ -7,6 +7,8 @@ import dbus
 import dbus.service
 from gi.repository import GObject
 
+from .gobject import ScheduledFunction
+
 logger = logging.getLogger("onoff.dbusutils")
 
 object_prefix = "/de/subdivi/onoff0"
@@ -115,7 +117,7 @@ class OnoffControl(dbus.service.Object):
     def activatetime(self, duration):
         """Activate the device for a given number of seconds."""
         logger.info("activatetime %d", duration)
-        GObject.timeout_add(duration * 1000, self.unuse)
+        ScheduledFunction(duration, self.unuse)
         return self.use()
 
     @dbus.service.method(domain, in_signature="", out_signature="qh")
index 73ed7d2..a2f9428 100644 (file)
@@ -18,3 +18,15 @@ def spawn_child(command, callback):
     assert pid
     GObject.child_watch_add(pid, callback)
     return pid
+
+class ScheduledFunction(object):
+    def __init__(self, interval, function):
+        """
+        @type interval: float
+        @param interval: seconds
+        """
+        self.event = GObject.timeout_add(int(1000 * interval), function)
+
+    def cancel(self):
+        ret = GObject.source_remove(self.event)
+        assert ret
index fca1d2d..92757e3 100644 (file)
@@ -2,10 +2,8 @@ import logging
 import os
 import signal
 
-from gi.repository import GObject
-
 from .common import ST_ACTIVE, ST_TRANSITION, OnoffDevice
-from .gobject import spawn_child
+from .gobject import spawn_child, ScheduledFunction
 
 logger = logging.getLogger("onoff.process")
 
@@ -16,8 +14,8 @@ class OnoffProcess(OnoffDevice):
     @type pid: int or None
     @ivar pid: is either None if there is no process or the pid of the
             spawned process
-    @ivar starting: is either None or a GObject event source id of the
-            callback sigalling the end of the activation transition.
+    @ivar starting: is either None or a ScheduledFunction representing the
+            callback signalling the end of the activation transition.
     @type killed: bool
     @ivar killed: indicates whether the termination signal has been sent
             to the spawned process.
@@ -54,16 +52,14 @@ class OnoffProcess(OnoffDevice):
         logger.info("starting command %s", " ".join(self.command))
         self.pid = spawn_child(self.command, self.process_died)
         logger.debug("started as pid %d", self.pid)
-        self.starting = GObject.timeout_add(int(1000 * self.start_wait),
-                                            self.process_started)
+        self.starting = ScheduledFunction(self.start_wait, self.process_started)
         self.changestate(self.state)
 
     def cancel_start_wait(self):
         if self.starting is None:
             return
         logger.debug("cancelling start notification")
-        ret = GObject.source_remove(self.starting)
-        assert ret
+        self.starting.cancel()
         self.starting = None
 
     def process_died(self, pid, condition):