277
|
1 #!/usr/bin/python |
|
2 # -*- coding: utf-8 -*- |
|
3 |
|
4 """ |
|
5 SàT plugin for managing raw XML log |
|
6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org) |
|
7 |
|
8 This program is free software: you can redistribute it and/or modify |
|
9 it under the terms of the GNU General Public License as published by |
|
10 the Free Software Foundation, either version 3 of the License, or |
|
11 (at your option) any later version. |
|
12 |
|
13 This program is distributed in the hope that it will be useful, |
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
16 GNU General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
20 """ |
|
21 |
|
22 from logging import debug, info, error |
|
23 from twisted.words.protocols.jabber.xmlstream import XmlStream |
|
24 from twisted.words.xish import domish |
|
25 |
|
26 PLUGIN_INFO = { |
|
27 "name": "Raw XML log Plugin", |
|
28 "import_name": "XmlLog", |
|
29 "type": "Misc", |
|
30 "protocols": [], |
|
31 "dependencies": [], |
|
32 "main": "XmlLog", |
|
33 "handler": "no", |
|
34 "description": _("""Send raw XML logs to bridge""") |
|
35 } |
|
36 |
|
37 class LoggingXmlStream(XmlStream): |
|
38 """This class send the raw XML to the Bridge, for logging purpose""" |
|
39 |
|
40 def send(self, obj): |
|
41 if isinstance(obj,basestring): |
|
42 log=unicode(obj) |
|
43 elif isinstance(obj, domish.Element): |
|
44 log=obj.toXml() |
|
45 else: |
|
46 error(_('INTERNAL ERROR: Unmanaged XML type')) |
|
47 info('OUT data for %s: %s' % (self._profile, log)) |
|
48 self._host.bridge.XmlLog("OUT", log, self._profile) |
|
49 return XmlStream.send(self, obj) |
|
50 |
|
51 def dataReceived(self, data): |
|
52 info('IN data for %s: %s' % (self._profile, data.decode('utf-8'))) |
|
53 self._host.bridge.XmlLog("IN", data.decode('utf-8'), self._profile) |
|
54 return XmlStream.dataReceived(self, data) |
|
55 |
|
56 |
|
57 class XmlLog(): |
|
58 |
|
59 params = """ |
|
60 <params> |
|
61 <general> |
|
62 <category name="Debug"> |
|
63 <param name="Xml log" label="%(label_xmllog)s" value="false" type="bool" /> |
|
64 </category> |
|
65 </general> |
|
66 </params> |
|
67 """ % {"label_xmllog": _("Activate XML log")} |
|
68 |
|
69 def __init__(self, host): |
|
70 info(_("Plugin XML Log initialization")) |
|
71 self.host = host |
|
72 |
|
73 #parameters |
|
74 host.memory.importParams(self.params) |
|
75 |
|
76 #bridge |
|
77 host.bridge.addSignal("XmlLog", ".communication", signature='sss') #args: direction("IN" or "OUT"), xml_data, profile |
|
78 |
|
79 do_log = bool(self.host.memory.getParamA("Xml log", "Debug")) |
|
80 if do_log: |
|
81 info(_("XML log activated")) |
|
82 host.trigger.add("XML Initialized", self.logXml) |
|
83 |
|
84 def logXml(self, xmlstream, profile): |
|
85 xmlstream.__class__ = LoggingXmlStream |
|
86 xmlstream._profile = profile |
|
87 xmlstream._host = self.host |
|
88 return True |