porting into acq branch
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 17 Jan 2008 17:56:49 +0000 (17:56 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 17 Jan 2008 17:56:49 +0000 (17:56 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/branches/acq-experiment@8401 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/python/oils/event.py [new file with mode: 0644]

diff --git a/Open-ILS/src/python/oils/event.py b/Open-ILS/src/python/oils/event.py
new file mode 100644 (file)
index 0000000..db64481
--- /dev/null
@@ -0,0 +1,52 @@
+import osrf.ex
+
+class Event(object):
+    ''' Generic ILS event object '''
+
+
+    def __init__(self, evt_hash={}):
+        self.code = evt_hash.get('ilsevent') or -1 
+        self.text_code = evt_hash.get('textcode') or ''
+        self.desc = evt_hash.get('desc') or ''
+        self.payload = evt_hash.get('payload') or None
+        self.debug = evt_hash.get('stacktrace') or ''
+        self.servertime = evt_hash.get('servertime') or ''
+
+        self.success = False
+        if self.code == int(0):
+            self.success = True
+
+    def __str__(self):
+        return '%s: %s:%s -> %s' % (
+            self.__class__.__name__, self.code, self.text_code, self.desc)
+
+    # XXX eventually, add events file parsing...
+
+    def to_ex(self):
+        return EventException(unicode(self))
+        
+
+    @staticmethod
+    def parse_event(evt=None):
+        ''' If the provided evt object is a dictionary object that looks
+            like an ILS event, construct an Event object and return it.
+            Returns None otherwise.  '''
+
+        if evt and 'ilsevent' in evt and 'textcode' in evt:
+            return Event(evt)
+
+        return None
+
+    @staticmethod
+    def parse_and_raise(evt=None):
+        ''' Parses with parse_event.  If the resulting event is a non-success
+            event, it is converted to an exception and raised '''
+        evt = Event.parse_event(evt)
+        if evt and not evt.success:
+            raise evt.to_ex()
+
+
+class EventException(osrf.ex.OSRFException):
+    ''' A throw-able exception wrapper for events '''
+    pass
+