annotate src/bridge/bridge_constructor/constructors/mediawiki/constructor.py @ 2098:e0066920a661

primitivus, jp: dynamic bridge + fixed D-Bus bridge: Primitivus and jp can now load the bridge specified in sat.conf, but they are only working with D-Bus so far (need to change all sync calls to async). Exit code 3 is used for Bridge init error.
author Goffi <goffi@goffi.org>
date Sun, 18 Dec 2016 16:28:51 +0100
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 import sys
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from datetime import datetime
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 import re
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
24
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
25
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 class MediawikiConstructor(base_constructor.Constructor):
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 self.core_template = "mediawiki_template.tpl"
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 self.core_dest = "mediawiki.wiki"
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
32
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
33 def _addTextDecorations(self, text):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
34 """Add text decorations like coloration or shortcuts"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
35
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 def anchor_link(match):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 link = match.group(1)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 #we add anchor_link for [method_name] syntax:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 if link in self.bridge_template.sections():
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 return "[[#%s|%s]]" % (link, link)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 print ("WARNING: found an anchor link to an unknown method")
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 return link
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 return re.sub(r"\[(\w+)\]", anchor_link, text)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
46 def _wikiParameter(self, name, sig_in):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 """Format parameters with the wiki syntax
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 @param name: name of the function
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 @param sig_in: signature in
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 @return: string of the formated parameters"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 arg_doc = self.getArgumentsDoc(name)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 arg_default = self.getDefault(name)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 args_str = self.getArguments(sig_in)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 args = args_str.split(', ') if args_str else [] # ugly but it works :)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 wiki = []
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 for i in range(len(args)):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 if i in arg_doc:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 name, doc = arg_doc[i]
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 doc = '\n:'.join(doc.rstrip('\n').split('\n'))
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 wiki.append("; %s: %s" % (name, self._addTextDecorations(doc)))
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 else:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 wiki.append("; arg_%d: " % i)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
63 if i in arg_default:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 wiki.append(":''DEFAULT: %s''" % arg_default[i])
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 return "\n".join(wiki)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
66
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 def _wikiReturn(self, name):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """Format return doc with the wiki syntax
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 @param name: name of the function
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 """
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 arg_doc = self.getArgumentsDoc(name)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 wiki = []
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 if 'return' in arg_doc:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 wiki.append('\n|-\n! scope=row | return value\n|')
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 wiki.append('<br />\n'.join(self._addTextDecorations(arg_doc['return']).rstrip('\n').split('\n')))
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 return "\n".join(wiki)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
77
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
78 def generateCoreSide(self):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 signals_part = []
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
80 methods_part = []
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 sections = self.bridge_template.sections()
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 sections.sort()
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 for section in sections:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 function = self.getValues(section)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
85 print ("Adding %s %s" % (section, function["type"]))
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 async_msg = """<br />'''This method is asynchronous'''"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 deprecated_msg = """<br />'''<font color="#FF0000">/!\ WARNING /!\ : This method is deprecated, please don't use it !</font>'''"""
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 signature_signal = \
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
89 """\
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 ! scope=row | signature
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 | %s
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 |-\
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 """ % function['sig_in']
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
94 signature_method = \
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 """\
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 ! scope=row | signature in
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 | %s
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
98 |-
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 ! scope=row | signature out
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 | %s
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 |-\
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 """ % (function['sig_in'], function['sig_out'])
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 completion = {
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 'signature': signature_signal if function['type'] == "signal" else signature_method,
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 'sig_out': function['sig_out'] or '',
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 'category': function['category'],
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 'name': section,
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 'doc': self.getDoc(section) or "FIXME: No description available",
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
109 'async': async_msg if "async" in self.getFlags(section) else "",
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 'deprecated': deprecated_msg if "deprecated" in self.getFlags(section) else "",
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 'parameters': self._wikiParameter(section, function['sig_in']),
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 'return': self._wikiReturn(section) if function['type'] == 'method' else ''}
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
113
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 dest = signals_part if function['type'] == "signal" else methods_part
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
115 dest.append("""\
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 == %(name)s ==
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 ''%(doc)s''
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 %(deprecated)s
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 %(async)s
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 {| class="wikitable" style="text-align:left; width:80%%;"
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 ! scope=row | category
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 | %(category)s
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 |-
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 %(signature)s
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
125 ! scope=row | parameters
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 |
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 %(parameters)s%(return)s
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
128 |}
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 """ % completion)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
130
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 #at this point, signals_part, and methods_part should be filled,
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 #we just have to place them in the right part of the template
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 core_bridge = []
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 template_path = self.getTemplatePath(self.core_template)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 try:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 with open(template_path) as core_template:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 for line in core_template:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 if line.startswith('##SIGNALS_PART##'):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
139 core_bridge.extend(signals_part)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 elif line.startswith('##METHODS_PART##'):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 core_bridge.extend(methods_part)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 elif line.startswith('##TIMESTAMP##'):
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 core_bridge.append('Generated on %s' % datetime.now())
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 else:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 core_bridge.append(line.replace('\n', ''))
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 except IOError:
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 print ("Can't open template file [%s]" % template_path)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 sys.exit(1)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
149
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 #now we write to final file
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
151 self.finalWrite(self.core_dest, core_bridge)
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
152
da4097de5a95 bridge (constructor): refactoring:
Goffi <goffi@goffi.org>
parents:
diff changeset
153