# HG changeset patch # User Goffi # Date 1426790677 -3600 # Node ID 0befb14ecf628835d11c1146dbaf9c76b07cf5e9 # Parent 6d0e018098937b4f7dd62be440f518baf4576c43 renamed tools.misc to tools.trigger diff -r 6d0e01809893 -r 0befb14ecf62 frontends/src/quick_frontend/quick_app.py --- a/frontends/src/quick_frontend/quick_app.py Thu Mar 19 14:41:15 2015 +0100 +++ b/frontends/src/quick_frontend/quick_app.py Thu Mar 19 19:44:37 2015 +0100 @@ -22,7 +22,7 @@ from sat.core.i18n import _ from sat.core import exceptions -from sat.tools.misc import TriggerManager +from sat.tools import trigger from sat_frontends.tools import jid from sat_frontends.quick_frontend import quick_widgets @@ -223,7 +223,7 @@ self._listeners = {} # key: listener type ("avatar", "selected", etc), value: list of callbacks # triggers - self.trigger = TriggerManager() # trigger are used to change the default behaviour + self.trigger = trigger.TriggerManager() # trigger are used to change the default behaviour ## bridge ## try: diff -r 6d0e01809893 -r 0befb14ecf62 src/core/sat_main.py --- a/src/core/sat_main.py Thu Mar 19 14:41:15 2015 +0100 +++ b/src/core/sat_main.py Thu Mar 19 19:44:37 2015 +0100 @@ -32,7 +32,7 @@ from sat.core.constants import Const as C from sat.memory.memory import Memory from sat.memory.crypto import PasswordHasher -from sat.tools.misc import TriggerManager +from sat.tools import trigger from sat.stdui import ui_contact_list, ui_profile_manager from glob import glob from uuid import uuid4 @@ -72,7 +72,7 @@ self.plugins = {} self.memory = Memory(self) - self.trigger = TriggerManager() # trigger are used to change SàT behaviour + self.trigger = trigger.TriggerManager() # trigger are used to change SàT behaviour try: self.bridge = DBusBridge() diff -r 6d0e01809893 -r 0befb14ecf62 src/plugins/plugin_exp_command_export.py --- a/src/plugins/plugin_exp_command_export.py Thu Mar 19 14:41:15 2015 +0100 +++ b/src/plugins/plugin_exp_command_export.py Thu Mar 19 19:44:37 2015 +0100 @@ -23,7 +23,7 @@ from twisted.words.protocols.jabber import jid from twisted.internet import reactor, protocol -from sat.tools.misc import SkipOtherTriggers +from sat.tools import trigger from sat.tools.utils import clean_ustr PLUGIN_INFO = { @@ -57,10 +57,10 @@ log.info("connectionMade :)") def outReceived(self, data): - self.parent.host.sendMessage(self.target, self._clean(data), no_trigger=True, profile_key=self.profile) + self.parent.host.sendMessage(self.target, self._clean(data), no_trigger=True, profile_key=self.profile) def errReceived(self, data): - self.parent.host.sendMessage(self.target, self._clean(data), no_trigger=True, profile_key=self.profile) + self.parent.host.sendMessage(self.target, self._clean(data), no_trigger=True, profile_key=self.profile) def processEnded(self, reason): log.info (u"process finished: %d" % (reason.value.exitCode,)) @@ -124,7 +124,7 @@ _continue &= process.boolOption("continue") exclusive |= process.boolOption("exclusive") if exclusive: - raise SkipOtherTriggers + raise trigger.SkipOtherTriggers return _continue return True @@ -144,7 +144,7 @@ if not profile: log.warning("Unknown profile [%s]" % (profile,)) return - + for target in targets: try: _jid = jid.JID(target) diff -r 6d0e01809893 -r 0befb14ecf62 src/plugins/plugin_exp_parrot.py --- a/src/plugins/plugin_exp_parrot.py Thu Mar 19 14:41:15 2015 +0100 +++ b/src/plugins/plugin_exp_parrot.py Thu Mar 19 19:44:37 2015 +0100 @@ -24,7 +24,7 @@ from twisted.words.protocols.jabber import jid from sat.core.exceptions import UnknownEntityError -#from sat.tools.misc import SkipOtherTriggers +#from sat.tools import trigger PLUGIN_INFO = { "name": "Parrot Plugin", @@ -66,7 +66,7 @@ # # if mess_data['to'].userhostJID() in _links.values(): # log.debug("Parrot link detected, skipping other triggers") - # raise SkipOtherTriggers + # raise trigger.SkipOtherTriggers def MessageReceivedTrigger(self, message, post_treat, profile): """ Check if source is linked and repeat message, else do nothing """ diff -r 6d0e01809893 -r 0befb14ecf62 src/plugins/plugin_xep_0033.py --- a/src/plugins/plugin_xep_0033.py Thu Mar 19 14:41:15 2015 +0100 +++ b/src/plugins/plugin_xep_0033.py Thu Mar 19 19:44:37 2015 +0100 @@ -33,7 +33,7 @@ from twisted.words.xish import domish from twisted.internet import defer -from sat.tools.misc import TriggerManager +from sat.tools import trigger from time import time # TODO: fix Prosody "addressing" plugin to leave the concerned bcc according to the spec: @@ -72,7 +72,7 @@ log.info(_("Extended Stanza Addressing plugin initialization")) self.host = host self.internal_data = {} - host.trigger.add("sendMessage", self.sendMessageTrigger, TriggerManager.MIN_PRIORITY) + host.trigger.add("sendMessage", self.sendMessageTrigger, trigger.TriggerManager.MIN_PRIORITY) host.trigger.add("MessageReceived", self.messageReceivedTrigger) def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile): diff -r 6d0e01809893 -r 0befb14ecf62 src/tools/misc.py --- a/src/tools/misc.py Thu Mar 19 14:41:15 2015 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,94 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -# SAT: a jabber client -# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) - -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU Affero General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. - -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU Affero General Public License for more details. - -# You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see . - -"""Misc usefull classes""" - -from sat.core.i18n import _ -from sat.core.log import getLogger -log = getLogger(__name__) - - -class TriggerException(Exception): - pass - - -class SkipOtherTriggers(Exception): - """ Exception to raise if normal behaviour must be followed instead of - following triggers list """ - pass - - -class TriggerManager(object): - """This class manage triggers: code which interact to change the behaviour - of SàT""" - - try: # FIXME: to be removed when a better solution is found - MIN_PRIORITY = float('-inf') - MAX_PRIORITY = float('+inf') - except: # XXX: Pyjamas will bug if you specify ValueError here - # Pyjamas uses the JS Float class - MIN_PRIORITY = Number.NEGATIVE_INFINITY - MAX_PRIORITY = Number.POSITIVE_INFINITY - - def __init__(self): - self.__triggers = {} - - def add(self, point_name, callback, priority=0): - """Add a trigger to a point - @param point_name: name of the point when the trigger should be run - @param callback: method to call at the trigger point - @param priority: callback will be called in priority order, biggest - first - """ - if point_name not in self.__triggers: - self.__triggers[point_name] = [] - if priority != 0 and priority in [trigger_tuple[0] for trigger_tuple in self.__triggers[point_name]]: - if priority in (self.MIN_PRIORITY, self.MAX_PRIORITY): - log.warning(_("There is already a bound priority [%s]") % point_name) - else: - log.debug(_("There is already a trigger with the same priority [%s]") % point_name) - self.__triggers[point_name].append((priority, callback)) - self.__triggers[point_name].sort(key=lambda trigger_tuple: - trigger_tuple[0], reverse=True) - - def remove(self, point_name, callback): - """Remove a trigger from a point - @param point_name: name of the point when the trigger should be run - @param callback: method to remove, must exists in the trigger point""" - for trigger_tuple in self.__triggers[point_name]: - if trigger_tuple[1] == callback: - self.__triggers[point_name].remove(trigger_tuple) - return - raise TriggerException("Trying to remove an unexisting trigger") - - def point(self, point_name, *args, **kwargs): - """This put a trigger point - All the trigger for that point will be run - @param point_name: name of the trigger point - @return: True if the action must be continued, False else""" - if point_name not in self.__triggers: - return True - - for priority, trigger in self.__triggers[point_name]: - try: - if not trigger(*args, **kwargs): - return False - except SkipOtherTriggers: - break - return True diff -r 6d0e01809893 -r 0befb14ecf62 src/tools/trigger.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/trigger.py Thu Mar 19 19:44:37 2015 +0100 @@ -0,0 +1,94 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# SAT: a jabber client +# Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 Jérôme Poisson (goffi@goffi.org) + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. + +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +"""Misc usefull classes""" + +from sat.core.i18n import _ +from sat.core.log import getLogger +log = getLogger(__name__) + + +class TriggerException(Exception): + pass + + +class SkipOtherTriggers(Exception): + """ Exception to raise if normal behaviour must be followed instead of + following triggers list """ + pass + + +class TriggerManager(object): + """This class manage triggers: code which interact to change the behaviour + of SàT""" + + try: # FIXME: to be removed when a better solution is found + MIN_PRIORITY = float('-inf') + MAX_PRIORITY = float('+inf') + except: # XXX: Pyjamas will bug if you specify ValueError here + # Pyjamas uses the JS Float class + MIN_PRIORITY = Number.NEGATIVE_INFINITY + MAX_PRIORITY = Number.POSITIVE_INFINITY + + def __init__(self): + self.__triggers = {} + + def add(self, point_name, callback, priority=0): + """Add a trigger to a point + @param point_name: name of the point when the trigger should be run + @param callback: method to call at the trigger point + @param priority: callback will be called in priority order, biggest + first + """ + if point_name not in self.__triggers: + self.__triggers[point_name] = [] + if priority != 0 and priority in [trigger_tuple[0] for trigger_tuple in self.__triggers[point_name]]: + if priority in (self.MIN_PRIORITY, self.MAX_PRIORITY): + log.warning(_("There is already a bound priority [%s]") % point_name) + else: + log.debug(_("There is already a trigger with the same priority [%s]") % point_name) + self.__triggers[point_name].append((priority, callback)) + self.__triggers[point_name].sort(key=lambda trigger_tuple: + trigger_tuple[0], reverse=True) + + def remove(self, point_name, callback): + """Remove a trigger from a point + @param point_name: name of the point when the trigger should be run + @param callback: method to remove, must exists in the trigger point""" + for trigger_tuple in self.__triggers[point_name]: + if trigger_tuple[1] == callback: + self.__triggers[point_name].remove(trigger_tuple) + return + raise TriggerException("Trying to remove an unexisting trigger") + + def point(self, point_name, *args, **kwargs): + """This put a trigger point + All the trigger for that point will be run + @param point_name: name of the trigger point + @return: True if the action must be continued, False else""" + if point_name not in self.__triggers: + return True + + for priority, trigger in self.__triggers[point_name]: + try: + if not trigger(*args, **kwargs): + return False + except SkipOtherTriggers: + break + return True