]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/python/oils/event.py
repaired logic error in reading event status. assuming existance of code and textcode
[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
7     def __init__(self, evt_hash={}):
8         self.code = int(evt_hash['ilsevent'])
9         self.text_code = evt_hash['textcode']
10         self.desc = evt_hash.get('desc') or ''
11         self.payload = evt_hash.get('payload')
12         self.debug = evt_hash.get('stacktrace') or ''
13         self.servertime = evt_hash.get('servertime') or ''
14
15         self.success = False
16         if self.code == 0:
17             self.success = True
18
19     def __str__(self):
20         return '%s: %s:%s -> %s' % (
21             self.__class__.__name__, self.code, self.text_code, self.desc)
22
23     # XXX eventually, add events file parsing...
24
25     def to_ex(self):
26         return EventException(unicode(self))
27         
28
29     @staticmethod
30     def parse_event(evt=None):
31         ''' If the provided evt object is a dictionary object that looks
32             like an ILS event, construct an Event object and return it.
33             Returns None otherwise.  '''
34
35         if isinstance(evt, dict) and 'ilsevent' in evt and 'textcode' in evt:
36             return Event(evt)
37
38         return None
39
40     @staticmethod
41     def parse_and_raise(evt=None):
42         ''' Parses with parse_event.  If the resulting event is a non-success
43             event, it is converted to an exception and raised '''
44         evt = Event.parse_event(evt)
45         if evt and not evt.success:
46             raise evt.to_ex()
47
48
49 class EventException(osrf.ex.OSRFException):
50     ''' A throw-able exception wrapper for events '''
51     pass
52