From f1d75142da179abf8d872949ed004e9b7e4006dd Mon Sep 17 00:00:00 2001 From: Thomas Vander Stichele Date: Sun, 12 Apr 2009 08:51:13 +0000 Subject: [PATCH] * examples/gtkcrc.py: * morituri/common/crc.py: * morituri/common/task.py: * morituri/image/image.py: Add a 'schedule' call to the TaskRunner class, so that we can abstract things like gobject.timeout_add and reactor.callLater Pass the runner to the task in Task.start() so a task can call schedule. --- ChangeLog | 11 +++++++++++ examples/gtkcrc.py | 8 +++++++- morituri/common/crc.py | 11 +++++------ morituri/common/task.py | 28 +++++++++++++++++++++++++--- morituri/image/image.py | 8 ++++---- 5 files changed, 52 insertions(+), 14 deletions(-) diff --git a/ChangeLog b/ChangeLog index fc66cc6..a96f263 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2009-04-12 Thomas Vander Stichele + + * examples/gtkcrc.py: + * morituri/common/crc.py: + * morituri/common/task.py: + * morituri/image/image.py: + Add a 'schedule' call to the TaskRunner class, so that we can + abstract things like gobject.timeout_add and reactor.callLater + Pass the runner to the task in Task.start() so a task can call + schedule. + 2009-04-12 Thomas Vander Stichele * morituri/common/task.py: diff --git a/examples/gtkcrc.py b/examples/gtkcrc.py index 4e1737b..86f8caa 100644 --- a/examples/gtkcrc.py +++ b/examples/gtkcrc.py @@ -55,7 +55,13 @@ class TaskProgress(gtk.VBox, task.TaskRunner): task.addListener(self) while gtk.events_pending(): gtk.main_iteration() - task.start() + task.start(self) + + def schedule(self, delta, callable, *args, **kwargs): + def c(): + callable(*args, **kwargs) + return False + gobject.timeout_add(int(delta * 1000L), c) def started(self, task): pass diff --git a/morituri/common/crc.py b/morituri/common/crc.py index bb29dad..4b6c85a 100644 --- a/morituri/common/crc.py +++ b/morituri/common/crc.py @@ -61,8 +61,8 @@ class CRCTask(task.Task): self.crc = None # result - def start(self): - task.Task.start(self) + def start(self, runner): + task.Task.start(self, runner) self._pipeline = gst.parse_launch(''' filesrc location="%s" ! decodebin ! audio/x-raw-int ! @@ -111,7 +111,7 @@ class CRCTask(task.Task): def play(): self._pipeline.set_state(gst.STATE_PLAYING) return False - gobject.timeout_add(0L, play) + self.runner.schedule(0, play) #self._pipeline.set_state(gst.STATE_PLAYING) self.debug('scheduled setting to play') @@ -143,8 +143,7 @@ class CRCTask(task.Task): framesDone = frame - self._frameStart progress = float(framesDone) / float((self._frameLength)) # marshall to the main thread - gobject.timeout_add(0L, self.setProgress, progress) - + self.runner.schedule(0, self.setProgress, progress) def do_crc_buffer(self, buffer, crc): """ @@ -156,7 +155,7 @@ class CRCTask(task.Task): # get the last one; FIXME: why does this not get to us before ? #self._new_buffer_cb(sink) self.debug('eos, scheduling stop') - gobject.timeout_add(0L, self.stop) + self.runner.schedule(0, self.stop) def stop(self): self.debug('stopping') diff --git a/morituri/common/task.py b/morituri/common/task.py index 60e515a..948d35f 100644 --- a/morituri/common/task.py +++ b/morituri/common/task.py @@ -36,12 +36,13 @@ class Task(object): progress = 0.0 increment = 0.01 running = False + runner = None _listeners = None ### subclass methods - def start(self): + def start(self, runner): """ Start the task. @@ -49,6 +50,7 @@ class Task(object): """ self.progress = 0.0 self.running = True + self.runner = runner self._notifyListeners('started') def stop(self): @@ -59,6 +61,7 @@ class Task(object): """ self.debug('stopping') self.running = False + self.runner = None self._notifyListeners('stopped') ### base class methods @@ -106,7 +109,19 @@ class TaskRunner: """ raise NotImplementedError - # listener callbacks + ### methods for tasks to call + def schedule(self, delta, callable, *args, **kwargs): + """ + Schedule a single future call. + + Subclasses should implement this. + + @type delta: float + @param delta: time in the future to schedule call for, in seconds. + """ + raise NotImplementedError + + ### listener callbacks def progressed(self, task, value): """ Implement me to be informed about progress. @@ -125,6 +140,7 @@ class TaskRunner: Implement me to be informed about the task starting. """ + class SyncRunner(TaskRunner): """ I run the task synchronously in a gobject MainLoop. @@ -136,9 +152,15 @@ class SyncRunner(TaskRunner): self._loop = gobject.MainLoop() self._task.addListener(self) - self._task.start() + self._task.start(self) self._loop.run() + def schedule(self, delta, callable, *args, **kwargs): + def c(): + callable(*args, **kwargs) + return False + gobject.timeout_add(int(delta * 1000L), c) + def progressed(self, task, value): if not self._verbose: return diff --git a/morituri/image/image.py b/morituri/image/image.py index 2dd07a3..05641ad 100644 --- a/morituri/image/image.py +++ b/morituri/image/image.py @@ -91,8 +91,8 @@ class AudioRipCRCTask(task.Task): self._tasks.append(crctask) - def start(self): - task.Task.start(self) + def start(self, runner): + task.Task.start(self, runner) self._next() def _next(self): @@ -103,7 +103,7 @@ class AudioRipCRCTask(task.Task): self.description = "CRC'ing track %2d of %d..." % ( self._track, self._tracks) task.addListener(self) - task.start() + task.start(self.runner) ### listener methods def started(self, task): @@ -119,4 +119,4 @@ class AudioRipCRCTask(task.Task): return # pick another - self.start() + self.start(self.runner)