Commits

Ronny Pfannschmidt committed 52a8f3f

create and test the api for dependency execution result reporting

  • Participants
  • Parent commits 80e29e6

Comments (0)

Files changed (2)

+"""
+    pu.tasks.util
+    ~~~~~~~~~~~~~
+
+    utility base classes and functions
+"""
+
+class CreateHelper(object):
+    def __init__(self, **kw):
+        for key in self.keys:
+            setattr(self, key, kw.pop(key))
+
+        self._debug_extra_kw = kw
+
+
+class HashHelper(object):
+
+    def __keytuple(self):
+        return tuple(getattr(self, key) for key in self.keys)
+
+    def __hash__(self):
+        return hash(self.__keytuple())
+
+    def __repr__(self):
+        return '<%s %s>' % (
+                type(self).__name__,
+                ' '.join('%s=%r' % k 
+                    for k in zip(self.keys, self.__keytuple())))
+    def __eq__(self, other):
+        return (isinstance(other, HashHelper)  # probably evil
+                and self.keys == other.keys
+                and self.__keytuple() == other.__keytuple())
+
+class ReportHelper(object):
+    def __dispatch(self, base, item):
+        name = getattr(item, 'category', item.__class__.__name__.lower())
+        method = 'on_%s_%s' % (name, base)
+        default_method = 'on_'+name
+        if hasattr(self, method):
+            return getattr(self, method)(item)
+        elif hasattr(self, default_method):
+            return getattr(self, default_method)(item)
+
+    def report_failure(self, dependency):
+        return self.__dispatch('failure', dependency)
+
+    def report_success(self, dependency):
+        return self.__dispatch('success', dependency)
+
+class TaskBase(CreateHelper, HashHelper, ReportHelper):
+    pass

tests/tasks/test_util.py

+import py
+from pu.tasks.util import TaskBase
+
+class Omg(TaskBase):
+    keys = ()
+
+class U(TaskBase):
+    keys = 'name', 'age'
+
+    def on_omg_failure(self, item):
+        return item
+
+    def on_u_success(self, item):
+        return item.name
+
+def test_base():
+    py.test.raises(KeyError, U, )
+
+    u1 = U(name='a', age=1)
+    u2 = U(name='a', age=1)
+    assert u1 == u2
+    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
+
+
+