annotate src/plugins/plugin_misc_xmllog.py @ 751:1def5b7edf9f

core, bridge: better GenericException handling
author Goffi <goffi@goffi.org>
date Tue, 17 Dec 2013 00:56:39 +0100
parents 4f747d7fde8c
children bfabeedbf32e
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
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
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from logging import debug, info, error
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from twisted.words.protocols.jabber.xmlstream import XmlStream
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.xish import domish
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
23
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
24 PLUGIN_INFO = {
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
25 "name": "Raw XML log Plugin",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
26 "import_name": "XmlLog",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
27 "type": "Misc",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
28 "protocols": [],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
29 "dependencies": [],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
30 "main": "XmlLog",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
31 "handler": "no",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
32 "description": _("""Send raw XML logs to bridge""")
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
33 }
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
34
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
35
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
36 class LoggingXmlStream(XmlStream):
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
37 """This class send the raw XML to the Bridge, for logging purpose"""
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
38
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
39 def send(self, obj):
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
40 if isinstance(obj, basestring):
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
41 log = unicode(obj)
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
42 elif isinstance(obj, domish.Element):
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
43 log = obj.toXml()
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
44 else:
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
45 error(_('INTERNAL ERROR: Unmanaged XML type'))
285
6422fcdd831c plugin XML Log: fixed dbus signal name
Goffi <goffi@goffi.org>
parents: 280
diff changeset
46 self._host.bridge.xmlLog("OUT", log, self._profile)
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
47 return XmlStream.send(self, obj)
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
48
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
49 def dataReceived(self, data):
285
6422fcdd831c plugin XML Log: fixed dbus signal name
Goffi <goffi@goffi.org>
parents: 280
diff changeset
50 self._host.bridge.xmlLog("IN", data.decode('utf-8'), self._profile)
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
51 return XmlStream.dataReceived(self, data)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
52
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
53
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
54 class XmlLog(object):
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
55
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
56 params = """
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
57 <params>
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
58 <general>
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
59 <category name="Debug">
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
60 <param name="Xml log" label="%(label_xmllog)s" value="false" type="bool" />
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
61 </category>
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 </params>
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
64 """ % {"label_xmllog": _("Activate XML log")}
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
65
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def __init__(self, host):
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
67 info(_("Plugin XML Log initialization"))
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
68 self.host = host
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
69
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
70 #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
71 host.memory.updateParams(self.params)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
72
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
73 #bridge
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
74 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
75
538
2c4016921403 core, frontends, bridgen plugins: fixed methods which were unproperly managing multi-profiles
Goffi <goffi@goffi.org>
parents: 480
diff changeset
76 do_log = self.host.memory.getParamA("Xml log", "Debug")
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
77 if do_log:
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
78 info(_("XML log activated"))
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
79 host.trigger.add("XML Initialized", self.logXml)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 538
diff changeset
80
277
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
81 def logXml(self, xmlstream, profile):
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
82 xmlstream.__class__ = LoggingXmlStream
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
83 xmlstream._profile = profile
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
84 xmlstream._host = self.host
05caa87196e6 new xml log plugin
Goffi <goffi@goffi.org>
parents:
diff changeset
85 return True