Mercurial > libervia-backend
comparison sat/bridge/bridge_constructor/base_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 | c605a0d6506f |
children |
comparison
equal
deleted
inserted
replaced
4036:c4464d7ae97b | 4037:524856bd7b19 |
---|---|
72 except NoOptionError: | 72 except NoOptionError: |
73 value = None | 73 value = None |
74 function[option] = value | 74 function[option] = value |
75 return function | 75 return function |
76 | 76 |
77 def getDefault(self, name): | 77 def get_default(self, name): |
78 """Return default values of a function in a dict | 78 """Return default values of a function in a dict |
79 @param name: Name of the function to get | 79 @param name: Name of the function to get |
80 @return: dict, each key is the integer param number (no key if no default value)""" | 80 @return: dict, each key is the integer param number (no key if no default value)""" |
81 default_dict = {} | 81 default_dict = {} |
82 def_re = re.compile(r"param_(\d+)_default") | 82 def_re = re.compile(r"param_(\d+)_default") |
104 for option in self.bridge_template.options(name): | 104 for option in self.bridge_template.options(name): |
105 if option in C.DECLARATION_FLAGS: | 105 if option in C.DECLARATION_FLAGS: |
106 flags.append(option) | 106 flags.append(option) |
107 return flags | 107 return flags |
108 | 108 |
109 def getArgumentsDoc(self, name): | 109 def get_arguments_doc(self, name): |
110 """Return documentation of arguments | 110 """Return documentation of arguments |
111 @param name: Name of the function to get | 111 @param name: Name of the function to get |
112 @return: dict, each key is the integer param number (no key if no argument doc), value is a tuple (name, doc)""" | 112 @return: dict, each key is the integer param number (no key if no argument doc), value is a tuple (name, doc)""" |
113 doc_dict = {} | 113 doc_dict = {} |
114 option_re = re.compile(r"doc_param_(\d+)") | 114 option_re = re.compile(r"doc_param_(\d+)") |
129 if not value_match: | 129 if not value_match: |
130 raise ParseError("Invalid value for parameter doc [%i]" % idx) | 130 raise ParseError("Invalid value for parameter doc [%i]" % idx) |
131 doc_dict[idx] = (value_match.group(1), value_match.group(2)) | 131 doc_dict[idx] = (value_match.group(1), value_match.group(2)) |
132 return doc_dict | 132 return doc_dict |
133 | 133 |
134 def getDoc(self, name): | 134 def get_doc(self, name): |
135 """Return documentation of the method | 135 """Return documentation of the method |
136 @param name: Name of the function to get | 136 @param name: Name of the function to get |
137 @return: string documentation, or None""" | 137 @return: string documentation, or None""" |
138 if self.bridge_template.has_option(name, "doc"): | 138 if self.bridge_template.has_option(name, "doc"): |
139 return self.bridge_template.get(name, "doc") | 139 return self.bridge_template.get(name, "doc") |
140 return None | 140 return None |
141 | 141 |
142 def argumentsParser(self, signature): | 142 def arguments_parser(self, signature): |
143 """Generator which return individual arguments signatures from a global signature""" | 143 """Generator which return individual arguments signatures from a global signature""" |
144 start = 0 | 144 start = 0 |
145 i = 0 | 145 i = 0 |
146 | 146 |
147 while i < len(signature): | 147 while i < len(signature): |
174 break | 174 break |
175 i += 1 | 175 i += 1 |
176 yield signature[start:i] | 176 yield signature[start:i] |
177 start = i | 177 start = i |
178 | 178 |
179 def getArguments(self, signature, name=None, default=None, unicode_protect=False): | 179 def get_arguments(self, signature, name=None, default=None, unicode_protect=False): |
180 """Return arguments to user given a signature | 180 """Return arguments to user given a signature |
181 | 181 |
182 @param signature: signature in the short form (using s,a,i,b etc) | 182 @param signature: signature in the short form (using s,a,i,b etc) |
183 @param name: dictionary of arguments name like given by getArgumentsDoc | 183 @param name: dictionary of arguments name like given by get_arguments_doc |
184 @param default: dictionary of default values, like given by getDefault | 184 @param default: dictionary of default values, like given by get_default |
185 @param unicode_protect: activate unicode protection on strings (return strings as unicode(str)) | 185 @param unicode_protect: activate unicode protection on strings (return strings as unicode(str)) |
186 @return (str): arguments that correspond to a signature (e.g.: "sss" return "arg1, arg2, arg3") | 186 @return (str): arguments that correspond to a signature (e.g.: "sss" return "arg1, arg2, arg3") |
187 """ | 187 """ |
188 idx = 0 | 188 idx = 0 |
189 attr_string = [] | 189 attr_string = [] |
190 | 190 |
191 for arg in self.argumentsParser(signature): | 191 for arg in self.arguments_parser(signature): |
192 attr_string.append( | 192 attr_string.append( |
193 ( | 193 ( |
194 "str(%(name)s)%(default)s" | 194 "str(%(name)s)%(default)s" |
195 if (unicode_protect and arg == "s") | 195 if (unicode_protect and arg == "s") |
196 else "%(name)s%(default)s" | 196 else "%(name)s%(default)s" |
204 # give unicode(arg_1), unicode(arg_2), etc. if unicode_protect is set and arg is a string | 204 # give unicode(arg_1), unicode(arg_2), etc. if unicode_protect is set and arg is a string |
205 idx += 1 | 205 idx += 1 |
206 | 206 |
207 return ", ".join(attr_string) | 207 return ", ".join(attr_string) |
208 | 208 |
209 def getTemplatePath(self, template_file): | 209 def get_template_path(self, template_file): |
210 """return template path corresponding to file name | 210 """return template path corresponding to file name |
211 | 211 |
212 @param template_file(str): name of template file | 212 @param template_file(str): name of template file |
213 """ | 213 """ |
214 return os.path.join(self.constructor_dir, template_file) | 214 return os.path.join(self.constructor_dir, template_file) |
230 pass | 230 pass |
231 | 231 |
232 def generate(self, side): | 232 def generate(self, side): |
233 """generate bridge | 233 """generate bridge |
234 | 234 |
235 call generateCoreSide or generateFrontendSide if they exists | 235 call generate_core_side or generateFrontendSide if they exists |
236 else call generic self._generate method | 236 else call generic self._generate method |
237 """ | 237 """ |
238 try: | 238 try: |
239 if side == "core": | 239 if side == "core": |
240 method = self.generateCoreSide | 240 method = self.generate_core_side |
241 elif side == "frontend": | 241 elif side == "frontend": |
242 if not self.FRONTEND_ACTIVATE: | 242 if not self.FRONTEND_ACTIVATE: |
243 print("This constructor only handle core, please use core side") | 243 print("This constructor only handle core, please use core side") |
244 sys.exit(1) | 244 sys.exit(1) |
245 method = self.generateFrontendSide | 245 method = self.generateFrontendSide |
270 sections = self.bridge_template.sections() | 270 sections = self.bridge_template.sections() |
271 sections.sort() | 271 sections.sort() |
272 for section in sections: | 272 for section in sections: |
273 function = self.getValues(section) | 273 function = self.getValues(section) |
274 print(("Adding %s %s" % (section, function["type"]))) | 274 print(("Adding %s %s" % (section, function["type"]))) |
275 default = self.getDefault(section) | 275 default = self.get_default(section) |
276 arg_doc = self.getArgumentsDoc(section) | 276 arg_doc = self.get_arguments_doc(section) |
277 async_ = "async" in self.getFlags(section) | 277 async_ = "async" in self.getFlags(section) |
278 completion = { | 278 completion = { |
279 "sig_in": function["sig_in"] or "", | 279 "sig_in": function["sig_in"] or "", |
280 "sig_out": function["sig_out"] or "", | 280 "sig_out": function["sig_out"] or "", |
281 "category": "plugin" if function["category"] == "plugin" else "core", | 281 "category": "plugin" if function["category"] == "plugin" else "core", |
282 "name": section, | 282 "name": section, |
283 # arguments with default values | 283 # arguments with default values |
284 "args": self.getArguments( | 284 "args": self.get_arguments( |
285 function["sig_in"], name=arg_doc, default=default | 285 function["sig_in"], name=arg_doc, default=default |
286 ), | 286 ), |
287 "args_no_default": self.getArguments(function["sig_in"], name=arg_doc), | 287 "args_no_default": self.get_arguments(function["sig_in"], name=arg_doc), |
288 } | 288 } |
289 | 289 |
290 extend_method = getattr( | 290 extend_method = getattr( |
291 self, "{}_completion_{}".format(side, function["type"]) | 291 self, "{}_completion_{}".format(side, function["type"]) |
292 ) | 292 ) |
303 const_override = { | 303 const_override = { |
304 env[len(C.ENV_OVERRIDE) :]: v | 304 env[len(C.ENV_OVERRIDE) :]: v |
305 for env, v in os.environ.items() | 305 for env, v in os.environ.items() |
306 if env.startswith(C.ENV_OVERRIDE) | 306 if env.startswith(C.ENV_OVERRIDE) |
307 } | 307 } |
308 template_path = self.getTemplatePath(TEMPLATE) | 308 template_path = self.get_template_path(TEMPLATE) |
309 try: | 309 try: |
310 with open(template_path) as template: | 310 with open(template_path) as template: |
311 for line in template: | 311 for line in template: |
312 | 312 |
313 for part, extend_list in parts.items(): | 313 for part, extend_list in parts.items(): |
330 except IOError: | 330 except IOError: |
331 print(("can't open template file [{}]".format(template_path))) | 331 print(("can't open template file [{}]".format(template_path))) |
332 sys.exit(1) | 332 sys.exit(1) |
333 | 333 |
334 # now we write to final file | 334 # now we write to final file |
335 self.finalWrite(DEST, bridge) | 335 self.final_write(DEST, bridge) |
336 | 336 |
337 def finalWrite(self, filename, file_buf): | 337 def final_write(self, filename, file_buf): |
338 """Write the final generated file in [dest dir]/filename | 338 """Write the final generated file in [dest dir]/filename |
339 | 339 |
340 @param filename: name of the file to generate | 340 @param filename: name of the file to generate |
341 @param file_buf: list of lines (stings) of the file | 341 @param file_buf: list of lines (stings) of the file |
342 """ | 342 """ |