added a to_ex method to turn the event into an exception and a runtime raise_and_pars...
authorerickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 17 Jan 2008 17:57:34 +0000 (17:57 +0000)
committererickson <erickson@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Thu, 17 Jan 2008 17:57:34 +0000 (17:57 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@8402 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/python/oils/event.py

index e8f2481..db64481 100644 (file)
@@ -1,7 +1,9 @@
+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 ''
@@ -20,6 +22,10 @@ class Event(object):
 
     # 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
@@ -30,3 +36,17 @@ class Event(object):
             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
+