Commits

Ronny Pfannschmidt committed af7aa47

put the task helpers into a single class, prepare for signaling

Comments (0)

Files changed (1)

     utility base classes and functions
 """
 
-class CreateHelper(object):
+import Queue
+
+
+class TaskBase(object):
+    """
+    utility base class for implementing Tasks
+
+    """
+    keys = ()
+    requirements = ()
+    queue = None
+
     def __init__(self, **kw):
         for key in self.keys:
-            setattr(self, key, kw.pop(key))
+            setattr(self, key, kw[key])
 
-        self._debug_extra_kw = kw
+        self._kw = kw
 
-
-class HashHelper(object):
-
-    def __keytuple(self):
-        return tuple(getattr(self, key) for key in self.keys)
+    # hashing and equality
+    def _key(self):
+        return tuple((key, getattr(self, key)) for key in self.keys)
 
     def __hash__(self):
-        return hash(self.__keytuple())
+        return hash(self._key())
 
     def __repr__(self):
         return '<%s %s>' % (
                 type(self).__name__,
                 ' '.join('%s=%r' % k 
-                    for k in zip(self.keys, self.__keytuple())))
+                    for k in self._key()))
+
     def __eq__(self, other):
-        return (isinstance(other, HashHelper)  # probably evil
-                and self.keys == other.keys
-                and self.__keytuple() == other.__keytuple())
+        return (isinstance(other, TaskBase)
+                and self._key() == other._key())
 
-class ReportHelper(object):
-    def __dispatch(self, base, item):
+    # reporting dispatch
+    def _dispatch(self, base, item):
         name = getattr(item, 'category', item.__class__.__name__.lower())
         method = 'on_%s_%s' % (name, base)
         default_method = 'on_'+name
             return getattr(self, default_method)(item)
 
     def report_failure(self, dependency):
-        return self.__dispatch('failure', dependency)
+        return self._dispatch('failure', dependency)
 
     def report_success(self, dependency):
-        return self.__dispatch('success', dependency)
+        return self._dispatch('success', dependency)
 
-class TaskBase(CreateHelper, HashHelper, ReportHelper):
-    pass
+
+
+    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))
+        try:
+            return self.queue.get_nowait()
+        except Queue.Empty:
+            raise StopIteration
+
+
+