comparison sat/bridge/bridge_constructor/constructors/mediawiki/constructor.py @ 4037:524856bd7b19

massive refactoring to switch from camelCase to snake_case: historically, Libervia (SàT before) was using camelCase as allowed by PEP8 when using a pre-PEP8 code, to use the same coding style as in Twisted. However, snake_case is more readable and it's better to follow PEP8 best practices, so it has been decided to move on full snake_case. Because Libervia has a huge codebase, this ended with a ugly mix of camelCase and snake_case. To fix that, this patch does a big refactoring by renaming every function and method (including bridge) that are not coming from Twisted or Wokkel, to use fully snake_case. This is a massive change, and may result in some bugs.
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:54:42 +0200
parents 7550ae9cfbac
children
comparison
equal deleted inserted replaced
4036:c4464d7ae97b 4037:524856bd7b19
27 def __init__(self, bridge_template, options): 27 def __init__(self, bridge_template, options):
28 base_constructor.Constructor.__init__(self, bridge_template, options) 28 base_constructor.Constructor.__init__(self, bridge_template, options)
29 self.core_template = "mediawiki_template.tpl" 29 self.core_template = "mediawiki_template.tpl"
30 self.core_dest = "mediawiki.wiki" 30 self.core_dest = "mediawiki.wiki"
31 31
32 def _addTextDecorations(self, text): 32 def _add_text_decorations(self, text):
33 """Add text decorations like coloration or shortcuts""" 33 """Add text decorations like coloration or shortcuts"""
34 34
35 def anchor_link(match): 35 def anchor_link(match):
36 link = match.group(1) 36 link = match.group(1)
37 # we add anchor_link for [method_name] syntax: 37 # we add anchor_link for [method_name] syntax:
40 print("WARNING: found an anchor link to an unknown method") 40 print("WARNING: found an anchor link to an unknown method")
41 return link 41 return link
42 42
43 return re.sub(r"\[(\w+)\]", anchor_link, text) 43 return re.sub(r"\[(\w+)\]", anchor_link, text)
44 44
45 def _wikiParameter(self, name, sig_in): 45 def _wiki_parameter(self, name, sig_in):
46 """Format parameters with the wiki syntax 46 """Format parameters with the wiki syntax
47 @param name: name of the function 47 @param name: name of the function
48 @param sig_in: signature in 48 @param sig_in: signature in
49 @return: string of the formated parameters""" 49 @return: string of the formated parameters"""
50 arg_doc = self.getArgumentsDoc(name) 50 arg_doc = self.get_arguments_doc(name)
51 arg_default = self.getDefault(name) 51 arg_default = self.get_default(name)
52 args_str = self.getArguments(sig_in) 52 args_str = self.get_arguments(sig_in)
53 args = args_str.split(", ") if args_str else [] # ugly but it works :) 53 args = args_str.split(", ") if args_str else [] # ugly but it works :)
54 wiki = [] 54 wiki = []
55 for i in range(len(args)): 55 for i in range(len(args)):
56 if i in arg_doc: 56 if i in arg_doc:
57 name, doc = arg_doc[i] 57 name, doc = arg_doc[i]
58 doc = "\n:".join(doc.rstrip("\n").split("\n")) 58 doc = "\n:".join(doc.rstrip("\n").split("\n"))
59 wiki.append("; %s: %s" % (name, self._addTextDecorations(doc))) 59 wiki.append("; %s: %s" % (name, self._add_text_decorations(doc)))
60 else: 60 else:
61 wiki.append("; arg_%d: " % i) 61 wiki.append("; arg_%d: " % i)
62 if i in arg_default: 62 if i in arg_default:
63 wiki.append(":''DEFAULT: %s''" % arg_default[i]) 63 wiki.append(":''DEFAULT: %s''" % arg_default[i])
64 return "\n".join(wiki) 64 return "\n".join(wiki)
65 65
66 def _wikiReturn(self, name): 66 def _wiki_return(self, name):
67 """Format return doc with the wiki syntax 67 """Format return doc with the wiki syntax
68 @param name: name of the function 68 @param name: name of the function
69 """ 69 """
70 arg_doc = self.getArgumentsDoc(name) 70 arg_doc = self.get_arguments_doc(name)
71 wiki = [] 71 wiki = []
72 if "return" in arg_doc: 72 if "return" in arg_doc:
73 wiki.append("\n|-\n! scope=row | return value\n|") 73 wiki.append("\n|-\n! scope=row | return value\n|")
74 wiki.append( 74 wiki.append(
75 "<br />\n".join( 75 "<br />\n".join(
76 self._addTextDecorations(arg_doc["return"]).rstrip("\n").split("\n") 76 self._add_text_decorations(arg_doc["return"]).rstrip("\n").split("\n")
77 ) 77 )
78 ) 78 )
79 return "\n".join(wiki) 79 return "\n".join(wiki)
80 80
81 def generateCoreSide(self): 81 def generate_core_side(self):
82 signals_part = [] 82 signals_part = []
83 methods_part = [] 83 methods_part = []
84 sections = self.bridge_template.sections() 84 sections = self.bridge_template.sections()
85 sections.sort() 85 sections.sort()
86 for section in sections: 86 for section in sections:
112 if function["type"] == "signal" 112 if function["type"] == "signal"
113 else signature_method, 113 else signature_method,
114 "sig_out": function["sig_out"] or "", 114 "sig_out": function["sig_out"] or "",
115 "category": function["category"], 115 "category": function["category"],
116 "name": section, 116 "name": section,
117 "doc": self.getDoc(section) or "FIXME: No description available", 117 "doc": self.get_doc(section) or "FIXME: No description available",
118 "async": async_msg if "async" in self.getFlags(section) else "", 118 "async": async_msg if "async" in self.getFlags(section) else "",
119 "deprecated": deprecated_msg 119 "deprecated": deprecated_msg
120 if "deprecated" in self.getFlags(section) 120 if "deprecated" in self.getFlags(section)
121 else "", 121 else "",
122 "parameters": self._wikiParameter(section, function["sig_in"]), 122 "parameters": self._wiki_parameter(section, function["sig_in"]),
123 "return": self._wikiReturn(section) 123 "return": self._wiki_return(section)
124 if function["type"] == "method" 124 if function["type"] == "method"
125 else "", 125 else "",
126 } 126 }
127 127
128 dest = signals_part if function["type"] == "signal" else methods_part 128 dest = signals_part if function["type"] == "signal" else methods_part
146 ) 146 )
147 147
148 # at this point, signals_part, and methods_part should be filled, 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 149 # we just have to place them in the right part of the template
150 core_bridge = [] 150 core_bridge = []
151 template_path = self.getTemplatePath(self.core_template) 151 template_path = self.get_template_path(self.core_template)
152 try: 152 try:
153 with open(template_path) as core_template: 153 with open(template_path) as core_template:
154 for line in core_template: 154 for line in core_template:
155 if line.startswith("##SIGNALS_PART##"): 155 if line.startswith("##SIGNALS_PART##"):
156 core_bridge.extend(signals_part) 156 core_bridge.extend(signals_part)
163 except IOError: 163 except IOError:
164 print(("Can't open template file [%s]" % template_path)) 164 print(("Can't open template file [%s]" % template_path))
165 sys.exit(1) 165 sys.exit(1)
166 166
167 # now we write to final file 167 # now we write to final file
168 self.finalWrite(self.core_dest, core_bridge) 168 self.final_write(self.core_dest, core_bridge)