renamed tools.misc to tools.trigger

This commit is contained in:
Goffi 2015-03-19 19:44:37 +01:00
commit 76065758df
6 changed files with 13 additions and 13 deletions

View file

@ -22,7 +22,7 @@ log = getLogger(__name__)
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 @@ class QuickApp(object):
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:

View file

@ -32,7 +32,7 @@ log = getLogger(__name__)
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 @@ class SAT(service.Service):
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()

View file

@ -23,7 +23,7 @@ log = getLogger(__name__)
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 @@ class ExportCommandProtocol(protocol.ProcessProtocol):
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 @@ class CommandExport(object):
_continue &= process.boolOption("continue")
exclusive |= process.boolOption("exclusive")
if exclusive:
raise SkipOtherTriggers
raise trigger.SkipOtherTriggers
return _continue
return True
@ -144,7 +144,7 @@ class CommandExport(object):
if not profile:
log.warning("Unknown profile [%s]" % (profile,))
return
for target in targets:
try:
_jid = jid.JID(target)

View file

@ -24,7 +24,7 @@ log = getLogger(__name__)
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 @@ class Exp_Parrot(object):
#
# 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 """

View file

@ -33,7 +33,7 @@ except ImportError:
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 @@ class XEP_0033(object):
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):

94
src/tools/trigger.py Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
"""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