comparison src/plugins/plugin_exp_lang_detect.py @ 2011:d95a6d553bec

plugin lang detect: added a parameter to (de)activate the detection
author Goffi <goffi@goffi.org>
date Sun, 17 Jul 2016 19:43:30 +0200
parents 4c5d8cd35690
children 1d3f73e065e1
comparison
equal deleted inserted replaced
2010:d2144d04065e 2011:d95a6d553bec
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _, D_
21 from sat.core.log import getLogger 21 from sat.core.log import getLogger
22 log = getLogger(__name__) 22 log = getLogger(__name__)
23 from sat.core import exceptions 23 from sat.core import exceptions
24 24
25 try: 25 try:
39 "main": "LangDetect", 39 "main": "LangDetect",
40 "handler": "no", 40 "handler": "no",
41 "description": _("""Detect and set message language when unknown""") 41 "description": _("""Detect and set message language when unknown""")
42 } 42 }
43 43
44 CATEGORY = D_(u"Misc")
45 NAME = u"lang_detect"
46 LABEL = D_(u"language detection")
47 PARAMS = """
48 <params>
49 <individual>
50 <category name="{category_name}">
51 <param name="{name}" label="{label}" type="bool" value="true" />
52 </category>
53 </individual>
54 </params>
55 """.format(category_name=CATEGORY,
56 name=NAME,
57 label=_(LABEL),
58 )
59
44 60
45 class LangDetect(object): 61 class LangDetect(object):
46 62
47 def __init__(self, host): 63 def __init__(self, host):
48 log.info(_(u"Language detection plugin initialization")) 64 log.info(_(u"Language detection plugin initialization"))
49 self.host = host 65 self.host = host
66 host.memory.updateParams(PARAMS)
50 host.trigger.add("MessageReceived", self.MessageReceivedTrigger) 67 host.trigger.add("MessageReceived", self.MessageReceivedTrigger)
51 host.trigger.add("messageSend", self.MessageSendTrigger) 68 host.trigger.add("messageSend", self.MessageSendTrigger)
52 69
53 def addLanguage(self, mess_data): 70 def addLanguage(self, mess_data):
54 message = mess_data['message'] 71 message = mess_data['message']
58 mess_data["message"] = {lang: msg} 75 mess_data["message"] = {lang: msg}
59 return mess_data 76 return mess_data
60 77
61 def MessageReceivedTrigger(self, client, message_elt, post_treat): 78 def MessageReceivedTrigger(self, client, message_elt, post_treat):
62 """ Check if source is linked and repeat message, else do nothing """ 79 """ Check if source is linked and repeat message, else do nothing """
63 post_treat.addCallback(self.addLanguage) 80
81 lang_detect = self.host.memory.getParamA(NAME, CATEGORY, profile_key=client.profile)
82 if lang_detect:
83 post_treat.addCallback(self.addLanguage)
64 return True 84 return True
65 85
66 def MessageSendTrigger(self, client, data, pre_xml_treatments, post_xml_treatments): 86 def MessageSendTrigger(self, client, data, pre_xml_treatments, post_xml_treatments):
67 self.addLanguage(data) 87 lang_detect = self.host.memory.getParamA(NAME, CATEGORY, profile_key=client.profile)
88 if lang_detect:
89 self.addLanguage(data)
68 return True 90 return True