2085
|
1 #!/usr/bin/env python2 |
|
2 #-*- coding: utf-8 -*- |
|
3 |
|
4 # SàT: a XMPP client |
2483
|
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org) |
2085
|
6 |
|
7 # This program is free software: you can redistribute it and/or modify |
|
8 # it under the terms of the GNU Affero General Public License as published by |
|
9 # the Free Software Foundation, either version 3 of the License, or |
|
10 # (at your option) any later version. |
|
11 |
|
12 # This program is distributed in the hope that it will be useful, |
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15 # GNU Affero General Public License for more details. |
|
16 |
|
17 # You should have received a copy of the GNU Affero General Public License |
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19 |
|
20 from sat.bridge.bridge_constructor import base_constructor |
|
21 import sys |
|
22 from datetime import datetime |
|
23 import re |
|
24 |
|
25 |
|
26 class MediawikiConstructor(base_constructor.Constructor): |
|
27 |
|
28 def __init__(self, bridge_template, options): |
|
29 base_constructor.Constructor.__init__(self, bridge_template, options) |
|
30 self.core_template = "mediawiki_template.tpl" |
|
31 self.core_dest = "mediawiki.wiki" |
|
32 |
|
33 def _addTextDecorations(self, text): |
|
34 """Add text decorations like coloration or shortcuts""" |
|
35 |
|
36 def anchor_link(match): |
|
37 link = match.group(1) |
|
38 #we add anchor_link for [method_name] syntax: |
|
39 if link in self.bridge_template.sections(): |
|
40 return "[[#%s|%s]]" % (link, link) |
|
41 print ("WARNING: found an anchor link to an unknown method") |
|
42 return link |
|
43 |
|
44 return re.sub(r"\[(\w+)\]", anchor_link, text) |
|
45 |
|
46 def _wikiParameter(self, name, sig_in): |
|
47 """Format parameters with the wiki syntax |
|
48 @param name: name of the function |
|
49 @param sig_in: signature in |
|
50 @return: string of the formated parameters""" |
|
51 arg_doc = self.getArgumentsDoc(name) |
|
52 arg_default = self.getDefault(name) |
|
53 args_str = self.getArguments(sig_in) |
|
54 args = args_str.split(', ') if args_str else [] # ugly but it works :) |
|
55 wiki = [] |
|
56 for i in range(len(args)): |
|
57 if i in arg_doc: |
|
58 name, doc = arg_doc[i] |
|
59 doc = '\n:'.join(doc.rstrip('\n').split('\n')) |
|
60 wiki.append("; %s: %s" % (name, self._addTextDecorations(doc))) |
|
61 else: |
|
62 wiki.append("; arg_%d: " % i) |
|
63 if i in arg_default: |
|
64 wiki.append(":''DEFAULT: %s''" % arg_default[i]) |
|
65 return "\n".join(wiki) |
|
66 |
|
67 def _wikiReturn(self, name): |
|
68 """Format return doc with the wiki syntax |
|
69 @param name: name of the function |
|
70 """ |
|
71 arg_doc = self.getArgumentsDoc(name) |
|
72 wiki = [] |
|
73 if 'return' in arg_doc: |
|
74 wiki.append('\n|-\n! scope=row | return value\n|') |
|
75 wiki.append('<br />\n'.join(self._addTextDecorations(arg_doc['return']).rstrip('\n').split('\n'))) |
|
76 return "\n".join(wiki) |
|
77 |
|
78 def generateCoreSide(self): |
|
79 signals_part = [] |
|
80 methods_part = [] |
|
81 sections = self.bridge_template.sections() |
|
82 sections.sort() |
|
83 for section in sections: |
|
84 function = self.getValues(section) |
|
85 print ("Adding %s %s" % (section, function["type"])) |
|
86 async_msg = """<br />'''This method is asynchronous'''""" |
|
87 deprecated_msg = """<br />'''<font color="#FF0000">/!\ WARNING /!\ : This method is deprecated, please don't use it !</font>'''""" |
|
88 signature_signal = \ |
|
89 """\ |
|
90 ! scope=row | signature |
|
91 | %s |
|
92 |-\ |
|
93 """ % function['sig_in'] |
|
94 signature_method = \ |
|
95 """\ |
|
96 ! scope=row | signature in |
|
97 | %s |
|
98 |- |
|
99 ! scope=row | signature out |
|
100 | %s |
|
101 |-\ |
|
102 """ % (function['sig_in'], function['sig_out']) |
|
103 completion = { |
|
104 'signature': signature_signal if function['type'] == "signal" else signature_method, |
|
105 'sig_out': function['sig_out'] or '', |
|
106 'category': function['category'], |
|
107 'name': section, |
|
108 'doc': self.getDoc(section) or "FIXME: No description available", |
|
109 'async': async_msg if "async" in self.getFlags(section) else "", |
|
110 'deprecated': deprecated_msg if "deprecated" in self.getFlags(section) else "", |
|
111 'parameters': self._wikiParameter(section, function['sig_in']), |
|
112 'return': self._wikiReturn(section) if function['type'] == 'method' else ''} |
|
113 |
|
114 dest = signals_part if function['type'] == "signal" else methods_part |
|
115 dest.append("""\ |
|
116 == %(name)s == |
|
117 ''%(doc)s'' |
|
118 %(deprecated)s |
|
119 %(async)s |
|
120 {| class="wikitable" style="text-align:left; width:80%%;" |
|
121 ! scope=row | category |
|
122 | %(category)s |
|
123 |- |
|
124 %(signature)s |
|
125 ! scope=row | parameters |
|
126 | |
|
127 %(parameters)s%(return)s |
|
128 |} |
|
129 """ % completion) |
|
130 |
|
131 #at this point, signals_part, and methods_part should be filled, |
|
132 #we just have to place them in the right part of the template |
|
133 core_bridge = [] |
|
134 template_path = self.getTemplatePath(self.core_template) |
|
135 try: |
|
136 with open(template_path) as core_template: |
|
137 for line in core_template: |
|
138 if line.startswith('##SIGNALS_PART##'): |
|
139 core_bridge.extend(signals_part) |
|
140 elif line.startswith('##METHODS_PART##'): |
|
141 core_bridge.extend(methods_part) |
|
142 elif line.startswith('##TIMESTAMP##'): |
|
143 core_bridge.append('Generated on %s' % datetime.now()) |
|
144 else: |
|
145 core_bridge.append(line.replace('\n', '')) |
|
146 except IOError: |
|
147 print ("Can't open template file [%s]" % template_path) |
|
148 sys.exit(1) |
|
149 |
|
150 #now we write to final file |
|
151 self.finalWrite(self.core_dest, core_bridge) |
|
152 |
|
153 |