def deregisterTrigger(self, id): """Deregister a given trigger at the given id. It proceeds to deregister the given trigger at the given id. :type id: int :param id: Id where the trigger is located. >>> nt = Notificator((1, 2, 3), 2) >>> def trigger(): pass >>> nt.registerTrigger(trigger) # doctest: +ELLIPSIS {'trigger': <function trigger at 0x...>, 'id': 1} >>> nt.triggerInfo[1] # doctest: +ELLIPSIS {'trigger': <function trigger at 0x...>, 'plist': {'after': <plist.PListFunction object at 0x...>, 'before': <plist.PListFunction object at 0x...>}, 'args': (), 'kwargs': {}} >>> nt.deregisterTrigger(1) >>> nt.triggerInfo[1] Traceback (most recent call last): ... KeyError: 1 >>> nt.deregisterTrigger(1) Traceback (most recent call last): ... DeregistrationException: Deregistration failed : Invalid id value: 1 :raise DeregistrationException: If the trigger was not deregistered for any reason. """ try: if __debug__: self.logger.debug('%s: deregister trigger with id=%d' % (info.FUNC(), id)) self._validateId(id) del self.triggerInfo[id] except InvalidIdException: raise DeregistrationException('Invalid id value: %s' % id)
def deregisterNotification(self, id, priority, isBeforeFlag, notification): """Deregister notification to a trigger identified by given id value. It deregisters the given notification with the given priority at the given position for a trigger which is identified by the given id value. >>> nt = Notificator((1, 2, 3), 2) >>> def trigger(): pass >>> nt.registerTrigger(trigger) # doctest: +ELLIPSIS {'trigger': <function trigger at 0x...>, 'id': 1} >>> def notif(): pass >>> nt.registerNotification(1, 1, True, True, notif) True >>> nt.deregisterNotification(1, 1, True, notif) # doctest: +ELLIPSIS <function notif at 0x...> >>> nt.deregisterNotification(2, 1, True, notif) Traceback (most recent call last): ... DeregistrationException: Deregistration failed : Invalid id value: 2 >>> nt.deregisterNotification(1, 1, True, 'NOTIF') Traceback (most recent call last): ... DeregistrationException: Deregistration failed : Invalid notification method: NOTIF >>> nt.deregisterNotification(1, 0, True, notif) Traceback (most recent call last): ... DeregistrationException: Deregistration failed : Invalid priority: 0 not in (1, 2, 3) :type id: int :param id: Identifies the trigger where the notification will be registered. :type priority: object :param priority: Notification priority. :type isBeforeFlag: boolean :param isBeforeFlag: If True, then Notification will be called before the trigger runs. If False, then Notification will be called after the trigger runs. :type notification: function :param notification: Notification to be registered. It should be a method call. :rtype: boolean :return: True if the notification was deregistered. :raise DeregistrationException: Notification was not deregistered properly. """ try: if __debug__: self.logger.debug( '%s: deregister %s notification %s, id=%d' % (info.FUNC(), BEFORE if isBeforeFlag else AFTER, notification, id)) self._validateId(id) self._validateMethodCall(notification) self._validatePriorityValue(priority) registrationSide = self._getRegistrationSideFromFlag(isBeforeFlag) triggerInfoPList = self.triggerInfo[id][PLIST] return triggerInfoPList[registrationSide].remove( {METHOD: notification}, priority) except InvalidIdException: raise DeregistrationException('Invalid id value: %s' % id) except InvalidMethodCallException, plist.InvalidEntryException: raise DeregistrationException('Invalid notification method: %s' % notification)
def registerNotification(self, id, priority, isBeforeFlag, inFrontFlag, notification, *args, **kwargs): """Register notification to a trigger identified by the given id value. It registers the given notification with the given priority at the given position for a trigger which is identified by the given id value. >>> nt = Notificator((1, 2, 3), 2) >>> def trigger(): pass >>> nt.registerTrigger(trigger) # doctest: +ELLIPSIS {'trigger': <function trigger at 0x...>, 'id': 1} >>> def notif(): pass >>> nt.registerNotification(1, 1, True, True, notif) True >>> nt.registerNotification(2, 1, True, True, notif) Traceback (most recent call last): ... RegistrationException: Registration failed : Invalid id value: 2 >>> nt.registerNotification(1, 1, True, True, 'NOTIF') Traceback (most recent call last): ... RegistrationException: Registration failed : Invalid notification method: NOTIF >>> nt.registerNotification(1, 0, True, True, notif) Traceback (most recent call last): ... RegistrationException: Registration failed : Invalid priority: 0 not in (1, 2, 3) :type id: int :param id: Identifies the trigger where the notification will be registered. :type priority: object :param priority: Notification priority. :type isBeforeFlag: boolean :param isBeforeFlag: If True, then Notification will be called before the trigger runs. If False, then Notification will be called after the trigger runs. :type inFrontFlag: boolean :param inFrontFlag: If True, then notification is registered at the front of the priority list. If False, then notification is registered at the back of the priority list. :type notification: function :param notification: Notification to be registered. It should be a method call. :type args: list :param args: List of parameters to pass when the notification is called. :type kwargs: dict :param kwargs: Dictionary of parameters to pass when the notification is called. :rtype: boolean :return: True if the notification was registered. :raise RegistrationException: Notification was not registered properly. """ try: if __debug__: self.logger.debug( '%s: register %s notification %s with args %s and kwargs %s, id=%d' % (info.FUNC(), BEFORE if isBeforeFlag else AFTER, notification, args, kwargs, id)) self._validateId(id) self._validateMethodCall(notification) self._validatePriorityValue(priority) registrationSide = self._getRegistrationSideFromFlag(isBeforeFlag) triggerInfoPList = self.triggerInfo[id][PLIST] addNotificationToListMethod =\ triggerInfoPList[registrationSide].addAtFront if inFrontFlag\ else triggerInfoPList[registrationSide].addAtBack addNotificationToListMethod( { METHOD: notification, ARGS: args, KWARGS: kwargs }, priority) return True except InvalidIdException: raise RegistrationException('Invalid id value: %s' % id) except InvalidMethodCallException: raise RegistrationException('Invalid notification method: %s' % notification) except plist.InvalidPriorityException: raise RegistrationException( 'Invalid priority: %s not in %s' % (priority, self.priorityValues.getList()))
def registerTrigger(self, triggerFunction, *args, **kwargs): """Register a new trigger function. It registers a new custom trigger function, where notification can be registered later on, and they will be called when the trigger function is called. >>> nt = Notificator((1, 2, 3), 2) >>> def trigger(): pass >>> nt.registerTrigger(trigger) # doctest: +ELLIPSIS {'trigger': <function trigger at 0x...>, 'id': 1} >>> nt.triggerInfo[1] # doctest: +ELLIPSIS {'trigger': <function trigger at 0x...>, 'plist': {'after': <plist.PListFunction object at 0x...>, 'before': <plist.PListFunction object at 0x...>}, 'args': (), 'kwargs': {}} :type triggerFunction: function :param triggerFunction: Custom trigger function to be registered. :type args: list :param args: Default list parameters when the trigger function is called. :type kwargs: dict :param kwargs: Default dictionary parameters when the trigger function is called. :rtype: dict :return: It return a dictionary with key 'id' with the id for the registered trigger and key 'trigger' with the wrapped up method that should be called now. :raise RegistrationException: Registration exception if the trigger method was not registered properly. """ try: self._validateMethodCall(triggerFunction) id = self._increaseId() ## TODO - MOVED - This has been moved to _validateMethodCall ## function. ## # This is required for testing with Mock functions, because ## # they are required to have a __name__ attribute in order to ## # work with @functools.wraps decorator. ## if isinstance(triggerFunction, mock.Mock): ## triggerFunction.__name__ = triggerFunction._mock_name # ================================================================= @functools.wraps(triggerFunction) def _wrapTrigger(*args, **kwargs): """Wrapped up the trigger function. It wraps up the custom trigger function, so notification can be called when the trigger function is executed. :type args: list :param args: Default list parameters when the trigger function is called. :type kwargs: dict :param kwargs: Default dictionary parameters when the trigger function is called. :rtype: function :return: Return trigger function return value. """ triggerInfo = self.triggerInfo[id] argsToUse = args if args else triggerInfo[ARGS] kwargsToUse = kwargs if kwargs else triggerInfo[KWARGS] if __debug__: self.logger.debug( 'triggerFunction %s, argsToUse %s, kwargsToUse %s' % (triggerInfo[TRIGGER], argsToUse, kwargsToUse)) triggerInfo[PLIST][BEFORE].callForAll(*argsToUse, **kwargsToUse) retvalue = triggerFunction(*argsToUse, **kwargsToUse) triggerInfo[PLIST][AFTER].callForAll(*argsToUse, **kwargsToUse) return retvalue self.triggerInfo[id] = { TRIGGER: _wrapTrigger, ARGS: args, KWARGS: kwargs, PLIST: { BEFORE: self._createPList('BEFORE'), AFTER: self._createPList('AFTER') } } if __debug__: self.logger.debug( '%s: register trigger %s with args %s and kwargs %s, id=%d' % (info.FUNC(), triggerFunction, args, kwargs, id)) return {ID: id, TRIGGER: _wrapTrigger} except InvalidMethodCallException: self.logger.error('%s: Invalid Trigger Function: %s' % (info.FUNC(), triggerFunction)) raise RegistrationException()
def runTrigger(self, id, *args, **kwargs): """Run the given trigger by the id. It runs the trigger with the given id, and it overrides the given args and kwargs. >>> nt = Notificator((1, 2, 3), 2) >>> def trigger1(): print 'the trigger1' >>> nt.registerTrigger(trigger1) # doctest: +ELLIPSIS {'trigger': <function trigger1 at 0x...>, 'id': 1} >>> nt.runTrigger(1) the trigger1 >>> def trigger2(x, y=0): print 'the trigger2 with (%s, %s)' % (x, y) >>> nt.registerTrigger(trigger2) # doctest: +ELLIPSIS {'trigger': <function trigger2 at 0x...>, 'id': 2} >>> nt.runTrigger(2, 'two', y=2) the trigger2 with (two, 2) >>> def trigger3(x, y=0): print 'the trigger3 with (%s, %s)' % (x, y) >>> nt.registerTrigger(trigger3, 'THREE', y=3) # doctest: +ELLIPSIS {'trigger': <function trigger3 at 0x...>, 'id': 3} >>> nt.runTrigger(3) the trigger3 with (THREE, 3) >>> def trigger4(): print 'the trigger4' >>> nt.registerTrigger(trigger4) # doctest: +ELLIPSIS {'trigger': <function trigger4 at 0x...>, 'id': 4} >>> def notif1(): print 'notif1' >>> nt.registerNotification(4, 1, True, True, notif1) True >>> nt.runTrigger(4) notif1 the trigger4 >>> nt.runTrigger(0) Traceback (most recent call last): ... TriggerException: Trigger failed : Invalid id value: 0 :type id: int :param id: Id where the trigger is located in the triggerInfo dictionary. :type args: list :param args: Override list parameters when the trigger function is called. :type kwargs: dict :param kwargs: Override dictionary parameters when the trigger function is called. :rtype: function :return: Trigger method return value. :raise TriggerException: If the trigger didn't run properly. """ try: if __debug__: self.logger.debug('%s: running trigger with id=%d' % (info.FUNC(), id)) self._validateId(id) triggerInfo = self.triggerInfo[id] triggerFunction = triggerInfo[TRIGGER] argsToUse = args if args else triggerInfo[ARGS] kwargsToUse = kwargs if kwargs else triggerInfo[KWARGS] return triggerFunction(*argsToUse, **kwargsToUse) except InvalidIdException: raise TriggerException('Invalid id value: %s' % id)