[kupfer] scheduler: Add scheduler.Timer, and updateable timer



commit 0244ee424859748ed116df64cc1d894ea64c3f9d
Author: Ulrik Sverdrup <ulrik sverdrup gmail com>
Date:   Sat Aug 22 17:04:47 2009 +0200

    scheduler: Add scheduler.Timer, and updateable timer

 kupfer/scheduler.py |   23 +++++++++++++++++++++++
 1 files changed, 23 insertions(+), 0 deletions(-)
---
diff --git a/kupfer/scheduler.py b/kupfer/scheduler.py
index bd7ef4a..4e5431b 100644
--- a/kupfer/scheduler.py
+++ b/kupfer/scheduler.py
@@ -29,3 +29,26 @@ gobject.signal_new("loaded", Scheduler, gobject.SIGNAL_RUN_LAST,
 		gobject.TYPE_BOOLEAN, ())
 gobject.signal_new("finish", Scheduler, gobject.SIGNAL_RUN_LAST,
 		gobject.TYPE_BOOLEAN, ())
+
+class Timer (gobject.GObject):
+	def __init__(self, invalid_on_finish=True):
+		self._current_timer = -1
+		self._invalid_on_finish = invalid_on_finish
+		GetScheduler().connect("finish", self._on_finish)
+
+	def set(self, timeout_seconds, callback, *arguments):
+		"""Setup timer to call @timeout_seconds in the future.
+		If the timer was previously set, it is postponed
+		"""
+		self.invalidate()
+		self._current_timer = gobject.timeout_add_seconds(timeout_seconds,
+				callback, *arguments)
+	
+	def invalidate(self):
+		if self._current_timer > 0:
+			gobject.source_remove(self._current_timer)
+		self._current_timer = -1
+
+	def _on_finish(self, scheduler):
+		if self._invalid_on_finish:
+			self.invalidate()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]