annotate src/bridge/bridge_constructor/constructors/dbus-xml/constructor.py @ 2248:a81261cee29b

jp (output/template): initialize template rendered only if a rendering is actually needed
author Goffi <goffi@goffi.org>
date Sun, 21 May 2017 15:52:23 +0200
parents da4097de5a95
children 8b37a62336c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2085
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python2
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 #-*- coding: utf-8 -*-
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SàT: a XMPP client
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
5 # Copyright (C) 2009-2016 Jérôme Poisson (goffi@goffi.org)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
19
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
20 from sat.bridge.bridge_constructor import base_constructor
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from xml.dom import minidom
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import sys
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
23
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
24
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 class DbusXmlConstructor(base_constructor.Constructor):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 """Constructor for DBus XML syntaxt (used by Qt frontend)"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
27
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 def __init__(self, bridge_template, options):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 base_constructor.Constructor.__init__(self, bridge_template, options)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
30
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 self.template = "dbus_xml_template.xml"
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 self.core_dest = "org.goffi.sat.xml"
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 self.default_annotation = {'a{ss}': 'StringDict',
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 'a(sa{ss}as)': 'QList<Contact>',
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 'a{i(ss)}': 'HistoryT',
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 'a(sss)': 'QList<MenuT>',
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 'a{sa{s(sia{ss})}}': 'PresenceStatusT',
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 }
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
39
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 def generateCoreSide(self):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 try:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 doc = minidom.parse(self.getTemplatePath(self.template))
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 interface_elt = doc.getElementsByTagName('interface')[0]
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 except IOError:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 print ("Can't access template")
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 sys.exit(1)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 except IndexError:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 print ("Template error")
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 sys.exit(1)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
50
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 sections = self.bridge_template.sections()
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 sections.sort()
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 for section in sections:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 function = self.getValues(section)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 print ("Adding %s %s" % (section, function["type"]))
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 new_elt = doc.createElement('method' if function["type"] == 'method' else 'signal')
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 new_elt.setAttribute('name', section)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
58
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 idx = 0
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 args_doc = self.getArgumentsDoc(section)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 for arg in self.argumentsParser(function['sig_in'] or ''):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 arg_elt = doc.createElement('arg')
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 arg_elt.setAttribute('name', args_doc[idx][0] if idx in args_doc else "arg_%i" % idx)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 arg_elt.setAttribute('type', arg)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 _direction = 'in' if function["type"] == 'method' else 'out'
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 arg_elt.setAttribute('direction', _direction)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 new_elt.appendChild(arg_elt)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 if "annotation" in self.args.flags:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 if arg in self.default_annotation:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 annot_elt = doc.createElement("annotation")
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 annot_elt.setAttribute('name', "com.trolltech.QtDBus.QtTypeName.In%d" % idx)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 annot_elt.setAttribute('value', self.default_annotation[arg])
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 new_elt.appendChild(annot_elt)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 idx += 1
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
75
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 if function['sig_out']:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 arg_elt = doc.createElement('arg')
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 arg_elt.setAttribute('type', function['sig_out'])
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 arg_elt.setAttribute('direction', 'out')
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 new_elt.appendChild(arg_elt)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 if "annotation" in self.args.flags:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 if function['sig_out'] in self.default_annotation:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 annot_elt = doc.createElement("annotation")
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 annot_elt.setAttribute('name', "com.trolltech.QtDBus.QtTypeName.Out0")
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 annot_elt.setAttribute('value', self.default_annotation[function['sig_out']])
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 new_elt.appendChild(annot_elt)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 interface_elt.appendChild(new_elt)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 #now we write to final file
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 self.finalWrite(self.core_dest, [doc.toprettyxml()])