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