]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/python/oils/event.py
added vars to store permission error info
[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(evt=None):
47         ''' Parses with parse_event.  If the resulting event is a non-success
48             event, it is converted to an exception and raised '''
49         evt = Event.parse_event(evt)
50         if evt and not evt.success:
51             raise evt.to_ex()
52
53
54 class EventException(osrf.ex.OSRFException):
55     ''' A throw-able exception wrapper for events '''
56     pass
57