Mercurial > libervia-backend
annotate sat/bridge/bridge_constructor/constructors/mediawiki/constructor.py @ 3044:691283719bb2
core (patches): updated TLS patches:
- updated patches to handle TLS after implementation in Twisted 19.7.0. A patch is still
needed for Wokkel.
- minimum required version of Twisted is now 19.7.0
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 01 Oct 2019 22:49:10 +0200 |
parents | ab2696e34d29 |
children | 9d0df638c8b4 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
2 # -*- coding: utf-8 -*- |
2085 | 3 |
4 # SàT: a XMPP client | |
2771 | 5 # Copyright (C) 2009-2019 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 def __init__(self, bridge_template, options): | |
28 base_constructor.Constructor.__init__(self, bridge_template, options) | |
29 self.core_template = "mediawiki_template.tpl" | |
30 self.core_dest = "mediawiki.wiki" | |
31 | |
32 def _addTextDecorations(self, text): | |
33 """Add text decorations like coloration or shortcuts""" | |
34 | |
35 def anchor_link(match): | |
36 link = match.group(1) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
37 # we add anchor_link for [method_name] syntax: |
2085 | 38 if link in self.bridge_template.sections(): |
39 return "[[#%s|%s]]" % (link, link) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
40 print("WARNING: found an anchor link to an unknown method") |
2085 | 41 return link |
42 | |
43 return re.sub(r"\[(\w+)\]", anchor_link, text) | |
44 | |
45 def _wikiParameter(self, name, sig_in): | |
46 """Format parameters with the wiki syntax | |
47 @param name: name of the function | |
48 @param sig_in: signature in | |
49 @return: string of the formated parameters""" | |
50 arg_doc = self.getArgumentsDoc(name) | |
51 arg_default = self.getDefault(name) | |
52 args_str = self.getArguments(sig_in) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
53 args = args_str.split(", ") if args_str else [] # ugly but it works :) |
2085 | 54 wiki = [] |
55 for i in range(len(args)): | |
56 if i in arg_doc: | |
57 name, doc = arg_doc[i] | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
58 doc = "\n:".join(doc.rstrip("\n").split("\n")) |
2085 | 59 wiki.append("; %s: %s" % (name, self._addTextDecorations(doc))) |
60 else: | |
61 wiki.append("; arg_%d: " % i) | |
62 if i in arg_default: | |
63 wiki.append(":''DEFAULT: %s''" % arg_default[i]) | |
64 return "\n".join(wiki) | |
65 | |
66 def _wikiReturn(self, name): | |
67 """Format return doc with the wiki syntax | |
68 @param name: name of the function | |
69 """ | |
70 arg_doc = self.getArgumentsDoc(name) | |
71 wiki = [] | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
72 if "return" in arg_doc: |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
73 wiki.append("\n|-\n! scope=row | return value\n|") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
74 wiki.append( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
75 "<br />\n".join( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
76 self._addTextDecorations(arg_doc["return"]).rstrip("\n").split("\n") |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
78 ) |
2085 | 79 return "\n".join(wiki) |
80 | |
81 def generateCoreSide(self): | |
82 signals_part = [] | |
83 methods_part = [] | |
84 sections = self.bridge_template.sections() | |
85 sections.sort() | |
86 for section in sections: | |
87 function = self.getValues(section) | |
3028 | 88 print(("Adding %s %s" % (section, function["type"]))) |
2085 | 89 async_msg = """<br />'''This method is asynchronous'''""" |
90 deprecated_msg = """<br />'''<font color="#FF0000">/!\ WARNING /!\ : This method is deprecated, please don't use it !</font>'''""" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
91 signature_signal = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
92 """\ |
2085 | 93 ! scope=row | signature |
94 | %s | |
95 |-\ | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
96 """ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
97 % function["sig_in"] |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
98 ) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
99 signature_method = """\ |
2085 | 100 ! scope=row | signature in |
101 | %s | |
102 |- | |
103 ! scope=row | signature out | |
104 | %s | |
105 |-\ | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
106 """ % ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
107 function["sig_in"], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
108 function["sig_out"], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
109 ) |
2085 | 110 completion = { |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
111 "signature": signature_signal |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
112 if function["type"] == "signal" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
113 else signature_method, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
114 "sig_out": function["sig_out"] or "", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
115 "category": function["category"], |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
116 "name": section, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
117 "doc": self.getDoc(section) or "FIXME: No description available", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
118 "async": async_msg if "async" in self.getFlags(section) else "", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
119 "deprecated": deprecated_msg |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
120 if "deprecated" in self.getFlags(section) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
121 else "", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
122 "parameters": self._wikiParameter(section, function["sig_in"]), |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
123 "return": self._wikiReturn(section) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
124 if function["type"] == "method" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
125 else "", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
126 } |
2085 | 127 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
128 dest = signals_part if function["type"] == "signal" else methods_part |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
129 dest.append( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
130 """\ |
2085 | 131 == %(name)s == |
132 ''%(doc)s'' | |
133 %(deprecated)s | |
134 %(async)s | |
135 {| class="wikitable" style="text-align:left; width:80%%;" | |
136 ! scope=row | category | |
137 | %(category)s | |
138 |- | |
139 %(signature)s | |
140 ! scope=row | parameters | |
141 | | |
142 %(parameters)s%(return)s | |
143 |} | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
144 """ |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
145 % completion |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
146 ) |
2085 | 147 |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
148 # at this point, signals_part, and methods_part should be filled, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
149 # we just have to place them in the right part of the template |
2085 | 150 core_bridge = [] |
151 template_path = self.getTemplatePath(self.core_template) | |
152 try: | |
153 with open(template_path) as core_template: | |
154 for line in core_template: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
155 if line.startswith("##SIGNALS_PART##"): |
2085 | 156 core_bridge.extend(signals_part) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
157 elif line.startswith("##METHODS_PART##"): |
2085 | 158 core_bridge.extend(methods_part) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
159 elif line.startswith("##TIMESTAMP##"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
160 core_bridge.append("Generated on %s" % datetime.now()) |
2085 | 161 else: |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
162 core_bridge.append(line.replace("\n", "")) |
2085 | 163 except IOError: |
3028 | 164 print(("Can't open template file [%s]" % template_path)) |
2085 | 165 sys.exit(1) |
166 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
167 # now we write to final file |
2085 | 168 self.finalWrite(self.core_dest, core_bridge) |