Ronny Pfannschmidt avatar Ronny Pfannschmidt committed 848001a

start to use blinker

Comments (0)

Files changed (2)

 """
 
 import Queue
+from blinker import Namespace
 
+ns = Namespace()
+task_failed = ns.signal('task_failed')
+task_succeeded = ns.signal('task_succeeded')
+task_finished = ns.signal('task_finished')
 
 class TaskBase(object):
     """
     def __hash__(self):
         return hash(self._key())
 
+    def __eq__(self, other):
+        return (isinstance(other, TaskBase)
+                and self._key() == other._key())
+
     def __repr__(self):
         return '<%s %s>' % (
                 type(self).__name__,
                 ' '.join('%s=%r' % k 
                     for k in self._key()))
 
-    def __eq__(self, other):
-        return (isinstance(other, TaskBase)
-                and self._key() == other._key())
-
     # reporting dispatch
     def _dispatch(self, base, item):
+        task_succeeded.disconnect(
+                self._requirement_succeeded,
+                sender=item)
+        task_succeeded.disconnect(
+                self._requirement_failed,
+                sender=item)
+
         name = getattr(item, 'category', item.__class__.__name__.lower())
         method = 'on_%s_%s' % (name, base)
         default_method = 'on_'+name
         elif hasattr(self, default_method):
             return getattr(self, default_method)(item)
 
-    def report_failure(self, dependency):
-        return self._dispatch('failure', dependency)
+    def _requirement_succeeded(self, requirement):
+        return self._dispatch('success', requirement)
 
-    def report_success(self, dependency):
-        return self._dispatch('success', dependency)
-
-
+    def _requirement_failed(self, requirement):
+        return self._dispatch('failure', requirement)
 
     def next(self):
         if self.queue is None:
             self.queue = Queue.Queue()
-            evil_kw = zip(self.keys, self.__keytuple())
-            for req in self.requirements:
-                self.queue.put(req(**evil_kw))
+            if self.requirements:
+                self.requirements = Queue()
+                task = req(**self._kw)
+                task_succeeded.connect(self._requirement_succeeded, sender=task)
+                task_failed.connect(self._requirement_failed, sender=task)
+                self.queue.put_nowait(task)
         try:
             return self.queue.get_nowait()
         except Queue.Empty:

tests/tasks/test_util.py

 import py
-from pu.tasks.util import TaskBase
+from pu.tasks.util import TaskBase, task_succeeded, task_failed
 
 class Omg(TaskBase):
     keys = ()
     assert hash(u1) == hash(u2)
 
     o = Omg()
-    assert o.report_success(o) is None
-    assert u1.report_failure(o) is o
-    assert u1.report_success(u2) == 'a'
-    assert u1.report_success(o) is None
+    with task_succeeded.temporarily_connected_to(
+            o._requirement_succeeded,
+            sender=o):
+        result = task_succeeded.send(o)
+        assert result[0][1] is None
 
+    with task_failed.temporarily_connected_to(
+            u1._requirement_failed,
+            sender=o):
+        assert task_failed.send(o)[0][1] is o
 
+    with task_succeeded.temporarily_connected_to(
+            u1._requirement_succeeded,
+            sender=u2):
+        assert task_succeeded.send(u2)[0][1] == 'a'
 
+    with task_succeeded.temporarily_connected_to(
+            u1._requirement_succeeded,
+            sender=o):
+        assert task_succeeded.send(o)[0][1] is None
+
+
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.