Go to the documentation of this file.00001
00002 """
00003 server side implementation of callback event
00004 """
00005
00006 from collections import OrderedDict
00007 from enum import Enum
00008
00009 class CallbackType(Enum):
00010
00011 '''
00012 The possible types of a Callback type.
00013
00014 :ivar Null:
00015 :ivar MonitoredItem:
00016
00017 '''
00018 Null = 0
00019 ItemSubscriptionCreated = 1
00020 ItemSubscriptionModified = 2
00021 ItemSubscriptionDeleted= 3
00022
00023
00024
00025 class Callback(object):
00026 def __init__(self):
00027 self.__name = None
00028
00029 def setName(self, name):
00030 self.__name = name
00031
00032 def getName(self):
00033 return self.__name
00034
00035
00036 class ServerItemCallback(Callback):
00037 def __init__(self, request_params, response_params):
00038 self.request_params = request_params
00039 self.response_params = response_params
00040
00041
00042 class CallbackSubscriberInterface(object):
00043 def getSubscribedEvents(self):
00044 raise NotImplementedError()
00045
00046
00047 class CallbackDispatcher(object):
00048 def __init__(self):
00049 self._listeners = {}
00050
00051 def dispatch(self, eventName, event=None):
00052 if event is None:
00053 event = Callback()
00054 elif not isinstance(event, Callback):
00055 raise ValueError('Unexpected event type given')
00056 event.setName(eventName)
00057 if eventName not in self._listeners:
00058 return event
00059 for listener in self._listeners[eventName].values():
00060 listener(event, self)
00061 return event
00062
00063 def addListener(self, eventName, listener, priority=0):
00064 if eventName not in self._listeners:
00065 self._listeners[eventName] = {}
00066 self._listeners[eventName][priority] = listener
00067 self._listeners[eventName] = OrderedDict(sorted(self._listeners[eventName].items(), key=lambda item: item[0]))
00068
00069 def removeListener(self, eventName, listener=None):
00070 if eventName not in self._listeners:
00071 return
00072 if not listener:
00073 del self._listeners[eventName]
00074 else:
00075 for p, l in self._listeners[eventName].items():
00076 if l is listener:
00077 self._listeners[eventName].pop(p)
00078 return
00079
00080 def addSubscriber(self, subscriber):
00081 if not isinstance(subscriber, CallbackSubscriberInterface):
00082 raise ValueError('Unexpected subscriber type given')
00083 for eventName, params in subscriber.getSubscribedEvents().items():
00084 if isinstance(params, str):
00085 self.addListener(eventName, getattr(subscriber, params))
00086 elif isinstance(params, list):
00087 if not params:
00088 raise ValueError('Invalid params "{0!r}" for event "{1!s}"'.format(params, eventName))
00089 if len(params) <= 2 and isinstance(params[0], str):
00090 priority = params[1] if len(params) > 1 else 0
00091 self.addListener(eventName, getattr(subscriber, params[0]), priority)
00092 else:
00093 for listener in params:
00094 priority = listener[1] if len(listener) > 1 else 0
00095 self.addListener(eventName, getattr(subscriber, listener[0]), priority)
00096 else:
00097 raise ValueError('Invalid params for event "{0!s}"'.format(eventName))
00098
00099