* 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.
This commit is contained in:
Thomas Vander Stichele
2009-04-12 08:51:13 +00:00
parent 10647d18a1
commit f1d75142da
5 changed files with 52 additions and 14 deletions

View File

@@ -1,3 +1,14 @@
2009-04-12 Thomas Vander Stichele <thomas at apestaart dot org>
* 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 <thomas at apestaart dot org>
* morituri/common/task.py:

View File

@@ -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

View File

@@ -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')

View File

@@ -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

View File

@@ -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)