Mercurial > libervia-backend
annotate libervia/backend/bridge/bridge_constructor/constructors/mediawiki/constructor.py @ 4090:e9d800b105c1
cli (event): minor typo
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 08 Jun 2023 21:28:40 +0200 |
parents | 4b842c1fb686 |
children | 0d7bb4df2343 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2085 | 3 |
3480
7550ae9cfbac
Renamed the project from "Salut à Toi" to "Libervia":
Goffi <goffi@goffi.org>
parents:
3479
diff
changeset
|
4 # Libervia: an XMPP client |
3479 | 5 # Copyright (C) 2009-2021 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 | |
4071
4b842c1fb686
refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents:
4037
diff
changeset
|
20 from libervia.backend.bridge.bridge_constructor import base_constructor |
2085 | 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 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
32 def _add_text_decorations(self, text): |
2085 | 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 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
45 def _wiki_parameter(self, name, sig_in): |
2085 | 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""" | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
50 arg_doc = self.get_arguments_doc(name) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
51 arg_default = self.get_default(name) |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
52 args_str = self.get_arguments(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")) |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
59 wiki.append("; %s: %s" % (name, self._add_text_decorations(doc))) |
2085 | 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 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
66 def _wiki_return(self, name): |
2085 | 67 """Format return doc with the wiki syntax |
68 @param name: name of the function | |
69 """ | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
70 arg_doc = self.get_arguments_doc(name) |
2085 | 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( |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
76 self._add_text_decorations(arg_doc["return"]).rstrip("\n").split("\n") |
2624
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 | |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
81 def generate_core_side(self): |
2085 | 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, |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
117 "doc": self.get_doc(section) or "FIXME: No description available", |
2624
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 "", |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
122 "parameters": self._wiki_parameter(section, function["sig_in"]), |
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
123 "return": self._wiki_return(section) |
2624
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 = [] |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
151 template_path = self.get_template_path(self.core_template) |
2085 | 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 |
4037
524856bd7b19
massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents:
3480
diff
changeset
|
168 self.final_write(self.core_dest, core_bridge) |