]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/python/oils/event.py
augmented parse_and_raise to act as a pass-thru when the provided object is not an...
[working/Evergreen.git] / Open-ILS / src / python / oils / event.py
1 import osrf.ex
2
3 class Event(object):
4     ''' Generic ILS event object '''
5
6     def __init__(self, evt_hash={}):
7         self.code = int(evt_hash['ilsevent'])
8         self.text_code = evt_hash['textcode']
9         self.desc = evt_hash.get('desc') or ''
10         self.payload = evt_hash.get('payload')
11         self.debug = evt_hash.get('stacktrace') or ''
12         self.servertime = evt_hash.get('servertime') or ''
13         self.ilsperm = evt_hash.get('ilsperm')
14         self.ilspermloc = evt_hash.get('ilspermloc')
15
16         self.success = False
17         if self.code == 0:
18             self.success = True
19
20     def __str__(self):
21         if self.ilsperm:
22             return '%s: %s:%s -> %s %s@%s' % (
23                 self.__class__.__name__, self.code, self.text_code, self.desc, self.ilsperm, str(self.ilspermloc))
24         else:
25             return '%s: %s:%s -> %s' % (
26                 self.__class__.__name__, self.code, self.text_code, self.desc)
27
28     # XXX eventually, add events file parsing...
29
30     def to_ex(self):
31         return EventException(unicode(self))
32         
33
34     @staticmethod
35     def parse_event(evt=None):
36         ''' If the provided evt object is a dictionary object that looks
37             like an ILS event, construct an Event object and return it.
38             Returns None otherwise.  '''
39
40         if isinstance(evt, dict) and 'ilsevent' in evt and 'textcode' in evt:
41             return Event(evt)
42
43         return None
44
45     @staticmethod
46     def parse_and_raise(obj=None):
47         ''' Parses with parse_event.  If the resulting event is a non-success
48             event, it is converted to an exception and raised.  If the resulting
49             event is a success event, the event object is returned.  If the
50             object is not an event, the original original object is returned 
51             unchanged. '''
52         evt = Event.parse_event(obj)
53         if evt:
54             if evt.success:
55                 return evt
56             raise evt.to_ex()
57         return obj
58
59
60 class EventException(osrf.ex.OSRFException):
61     ''' A throw-able exception wrapper for events '''
62     pass
63