annotate sat/plugins/plugin_misc_xmllog.py @ 2613:9680cd958529

plugin XEP-0045: fixed crash when muc service is not found
author Goffi <goffi@goffi.org>
date Thu, 21 Jun 2018 01:21:44 +0200
parents 26edcf3a30eb
children 56f94936df1e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1706
diff changeset
1 #!/usr/bin/env python2
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
4 # SàT plugin for managing raw XML log
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
5 # Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
7 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
10 # (at your option) any later version.
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
12 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
15 # GNU Affero General Public License for more details.
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 594
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
19
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 662
diff changeset
20 from sat.core.i18n import _
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
21 from sat.core.constants import Const as C
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 771
diff changeset
22 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 771
diff changeset
23 log = getLogger(__name__)
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.xish import domish
2440
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
25 from twisted.words.xish import xmlstream
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
26
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
27 PLUGIN_INFO = {
2145
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
28 C.PI_NAME: "Raw XML log Plugin",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
29 C.PI_IMPORT_NAME: "XmlLog",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
30 C.PI_TYPE: "Misc",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
31 C.PI_PROTOCOLS: [],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
32 C.PI_DEPENDENCIES: [],
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
33 C.PI_MAIN: "XmlLog",
33c8c4973743 core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents: 1934
diff changeset
34 C.PI_HANDLER: "no",
2440
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
35 C.PI_DESCRIPTION: _(u"""Send raw XML logs to bridge""")
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
36 }
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
37
2440
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
38 host = None
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
39
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
40 def send(self, obj):
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
41 global host
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
42 if isinstance(obj, basestring):
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
43 log = unicode(obj)
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
44 elif isinstance(obj, domish.Element):
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
45 log = obj.toXml()
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
46 else:
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
47 log.error(_(u'INTERNAL ERROR: Unmanaged XML type'))
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
48 host.bridge.xmlLog("OUT", log, self._profile)
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
49 return self._original_send(obj)
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
50
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
51
2440
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
52 def onElement(self, element):
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
53 global host
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
54 host.bridge.xmlLog("IN", element.toXml(), self._profile)
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
55 return self._original_onElement(element)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
56
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
57
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
58 class XmlLog(object):
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
59
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
60 params = """
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
61 <params>
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
62 <general>
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
63 <category name="Debug">
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
64 <param name="Xml log" label="%(label_xmllog)s" value="false" type="bool" />
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
65 </category>
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
66 </general>
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
67 </params>
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """ % {"label_xmllog": _("Activate XML log")}
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
69
2440
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
70 def __init__(self, host_):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 771
diff changeset
71 log.info(_("Plugin XML Log initialization"))
2440
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
72 global host
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
73 host = host_
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
74
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
75 #parameters
662
4f747d7fde8c core: importParams renamed to updateParams: it now updates the parameter instead of appending children if it find an existing one.
Goffi <goffi@goffi.org>
parents: 609
diff changeset
76 host.memory.updateParams(self.params)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
77
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
78 #bridge
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
79 host.bridge.addSignal("xmlLog", ".plugin", signature='sss') # args: direction("IN" or "OUT"), xml_data, profile
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
80
2440
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
81 self.do_log = host.memory.getParamA("Xml log", "Debug")
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
82 if self.do_log:
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
83 XmlStream = xmlstream.XmlStream
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
84 XmlStream._original_send = XmlStream.send
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
85 XmlStream._original_onElement = XmlStream.onElement
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
86 XmlStream.send = send
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
87 XmlStream.onElement = onElement
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
88 XmlStream._profile = ''
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
89 host.trigger.add("XML Initialized", self.setProfile)
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
90 log.info(_(u"XML log activated"))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
91
2440
51d346e283fd plugin XML Log: Monkey patch is done on the whole XmlStream class, making it available as soon as the stream start + long received message are sent once fully parsed (not cut anymore)
Goffi <goffi@goffi.org>
parents: 2145
diff changeset
92 def setProfile(self, xmlstream, profile):
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
93 xmlstream._profile = profile
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
94 return True