annotate src/plugins/plugin_misc_xmllog.py @ 277:05caa87196e6

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