comparison libervia/backend/bridge/bridge_constructor/constructors/mediawiki/constructor.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/bridge/bridge_constructor/constructors/mediawiki/constructor.py@524856bd7b19
children
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # Libervia: an XMPP client
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
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 libervia.backend.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 _add_text_decorations(self, text):
33 """Add text decorations like coloration or shortcuts"""
34
35 def anchor_link(match):
36 link = match.group(1)
37 # we add anchor_link for [method_name] syntax:
38 if link in self.bridge_template.sections():
39 return "[[#%s|%s]]" % (link, link)
40 print("WARNING: found an anchor link to an unknown method")
41 return link
42
43 return re.sub(r"\[(\w+)\]", anchor_link, text)
44
45 def _wiki_parameter(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.get_arguments_doc(name)
51 arg_default = self.get_default(name)
52 args_str = self.get_arguments(sig_in)
53 args = args_str.split(", ") if args_str else [] # ugly but it works :)
54 wiki = []
55 for i in range(len(args)):
56 if i in arg_doc:
57 name, doc = arg_doc[i]
58 doc = "\n:".join(doc.rstrip("\n").split("\n"))
59 wiki.append("; %s: %s" % (name, self._add_text_decorations(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 _wiki_return(self, name):
67 """Format return doc with the wiki syntax
68 @param name: name of the function
69 """
70 arg_doc = self.get_arguments_doc(name)
71 wiki = []
72 if "return" in arg_doc:
73 wiki.append("\n|-\n! scope=row | return value\n|")
74 wiki.append(
75 "<br />\n".join(
76 self._add_text_decorations(arg_doc["return"]).rstrip("\n").split("\n")
77 )
78 )
79 return "\n".join(wiki)
80
81 def generate_core_side(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)
88 print(("Adding %s %s" % (section, function["type"])))
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>'''"""
91 signature_signal = (
92 """\
93 ! scope=row | signature
94 | %s
95 |-\
96 """
97 % function["sig_in"]
98 )
99 signature_method = """\
100 ! scope=row | signature in
101 | %s
102 |-
103 ! scope=row | signature out
104 | %s
105 |-\
106 """ % (
107 function["sig_in"],
108 function["sig_out"],
109 )
110 completion = {
111 "signature": signature_signal
112 if function["type"] == "signal"
113 else signature_method,
114 "sig_out": function["sig_out"] or "",
115 "category": function["category"],
116 "name": section,
117 "doc": self.get_doc(section) or "FIXME: No description available",
118 "async": async_msg if "async" in self.getFlags(section) else "",
119 "deprecated": deprecated_msg
120 if "deprecated" in self.getFlags(section)
121 else "",
122 "parameters": self._wiki_parameter(section, function["sig_in"]),
123 "return": self._wiki_return(section)
124 if function["type"] == "method"
125 else "",
126 }
127
128 dest = signals_part if function["type"] == "signal" else methods_part
129 dest.append(
130 """\
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 |}
144 """
145 % completion
146 )
147
148 # at this point, signals_part, and methods_part should be filled,
149 # we just have to place them in the right part of the template
150 core_bridge = []
151 template_path = self.get_template_path(self.core_template)
152 try:
153 with open(template_path) as core_template:
154 for line in core_template:
155 if line.startswith("##SIGNALS_PART##"):
156 core_bridge.extend(signals_part)
157 elif line.startswith("##METHODS_PART##"):
158 core_bridge.extend(methods_part)
159 elif line.startswith("##TIMESTAMP##"):
160 core_bridge.append("Generated on %s" % datetime.now())
161 else:
162 core_bridge.append(line.replace("\n", ""))
163 except IOError:
164 print(("Can't open template file [%s]" % template_path))
165 sys.exit(1)
166
167 # now we write to final file
168 self.final_write(self.core_dest, core_bridge)