diff sat/plugins/plugin_misc_text_syntaxes.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 33d75cd3c371
children 00dbc3370d35
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_text_syntaxes.py	Fri Apr 07 15:18:39 2023 +0200
+++ b/sat/plugins/plugin_misc_text_syntaxes.py	Sat Apr 08 13:54:42 2023 +0200
@@ -196,7 +196,7 @@
             "syntaxes": self.syntaxes,
         }
 
-        self.addSyntax(
+        self.add_syntax(
             self.SYNTAX_XHTML,
             lambda xhtml: defer.succeed(xhtml),
             lambda xhtml: defer.succeed(xhtml),
@@ -204,10 +204,10 @@
         )
         # TODO: text => XHTML should add <a/> to url like in frontends
         #       it's probably best to move sat_frontends.tools.strings to sat.tools.common or similar
-        self.addSyntax(
+        self.add_syntax(
             self.SYNTAX_TEXT,
             lambda text: escape(text),
-            lambda xhtml: self._removeMarkups(xhtml),
+            lambda xhtml: self._remove_markups(xhtml),
             [TextSyntaxes.OPT_HIDDEN],
         )
         try:
@@ -217,7 +217,7 @@
             # XXX: we disable raw HTML parsing by default, to avoid parsing error
             #      when the user is not aware of markdown and HTML
             class EscapeHTML(Extension):
-                def extendMarkdown(self, md):
+                def extend_markdown(self, md):
                     md.preprocessors.deregister('html_block')
                     md.inlinePatterns.deregister('html')
 
@@ -226,7 +226,7 @@
                 h.body_width = 0  # do not truncate the lines, it breaks the long URLs
                 return h.handle(html)
 
-            self.addSyntax(
+            self.add_syntax(
                 self.SYNTAX_MARKDOWN,
                 partial(markdown.markdown,
                         extensions=[
@@ -251,22 +251,22 @@
                 "You can download/install them from https://pythonhosted.org/Markdown/ "
                 "and https://github.com/Alir3z4/html2text/"
             )
-        host.bridge.addMethod(
-            "syntaxConvert",
+        host.bridge.add_method(
+            "syntax_convert",
             ".plugin",
             in_sign="sssbs",
             out_sign="s",
             async_=True,
             method=self.convert,
         )
-        host.bridge.addMethod(
-            "syntaxGet", ".plugin", in_sign="s", out_sign="s", method=self.getSyntax
+        host.bridge.add_method(
+            "syntax_get", ".plugin", in_sign="s", out_sign="s", method=self.get_syntax
         )
-        if xml_tools.cleanXHTML is None:
+        if xml_tools.clean_xhtml is None:
             log.debug("Installing cleaning method")
-            xml_tools.cleanXHTML = self.cleanXHTML
+            xml_tools.clean_xhtml = self.clean_xhtml
 
-    def _updateParamOptions(self):
+    def _update_param_options(self):
         data_synt = self.syntaxes
         default_synt = TextSyntaxes.default_syntax
         syntaxes = []
@@ -284,23 +284,23 @@
             options.append('<option value="%s" %s/>' % (syntax, selected))
 
         self.params_data["options"] = "\n".join(options)
-        self.host.memory.updateParams(self.params % self.params_data)
+        self.host.memory.update_params(self.params % self.params_data)
 
-    def getCurrentSyntax(self, profile):
+    def get_current_syntax(self, profile):
         """ Return the selected syntax for the given profile
 
         @param profile: %(doc_profile)s
         @return: profile selected syntax
         """
-        return self.host.memory.getParamA(NAME, CATEGORY, profile_key=profile)
+        return self.host.memory.param_get_a(NAME, CATEGORY, profile_key=profile)
 
-    def _logError(self, failure, action="converting syntax"):
+    def _log_error(self, failure, action="converting syntax"):
         log.error(
             "Error while {action}: {failure}".format(action=action, failure=failure)
         )
         return failure
 
-    def cleanStyle(self, styles_raw: str) -> str:
+    def clean_style(self, styles_raw: str) -> str:
         """"Clean unsafe CSS styles
 
         Remove styles not in the whitelist, or where the value doesn't match the regex
@@ -327,7 +327,7 @@
             ["%s: %s" % (key_, value_) for key_, value_ in cleaned_styles]
         )
 
-    def cleanClasses(self, classes_raw: str) -> str:
+    def clean_classes(self, classes_raw: str) -> str:
         """Remove any non whitelisted class
 
         @param classes_raw: classes set on an element
@@ -335,7 +335,7 @@
         """
         return " ".join(SAFE_CLASSES.intersection(classes_raw.split()))
 
-    def cleanXHTML(self, xhtml):
+    def clean_xhtml(self, xhtml):
         """Clean XHTML text by removing potentially dangerous/malicious parts
 
         @param xhtml(unicode, lxml.etree._Element): raw HTML/XHTML text to clean
@@ -360,9 +360,9 @@
         )
         xhtml_elt = cleaner.clean_html(xhtml_elt)
         for elt in xhtml_elt.xpath("//*[@style]"):
-            elt.set("style", self.cleanStyle(elt.get("style")))
+            elt.set("style", self.clean_style(elt.get("style")))
         for elt in xhtml_elt.xpath("//*[@class]"):
-            elt.set("class", self.cleanClasses(elt.get("class")))
+            elt.set("class", self.clean_classes(elt.get("class")))
         # we remove self-closing elements for non-void elements
         for element in xhtml_elt.iter(tag=etree.Element):
             if not element.text:
@@ -389,11 +389,11 @@
         # TODO: a way for parser to return parsing errors/warnings
 
         if syntax_from == _SYNTAX_CURRENT:
-            syntax_from = self.getCurrentSyntax(profile)
+            syntax_from = self.get_current_syntax(profile)
         else:
             syntax_from = syntax_from.lower().strip()
         if syntax_to == _SYNTAX_CURRENT:
-            syntax_to = self.getCurrentSyntax(profile)
+            syntax_to = self.get_current_syntax(profile)
         else:
             syntax_to = syntax_to.lower().strip()
         syntaxes = self.syntaxes
@@ -411,7 +411,7 @@
         # TODO: keep only body element and change it to a div here ?
 
         if safe:
-            d.addCallback(self.cleanXHTML)
+            d.addCallback(self.clean_xhtml)
 
         if TextSyntaxes.OPT_NO_THREAD in syntaxes[syntax_to]["flags"]:
             d.addCallback(syntaxes[syntax_to]["from"])
@@ -422,7 +422,7 @@
         d.addCallback(lambda text: text.rstrip())
         return d
 
-    def addSyntax(self, name, to_xhtml_cb, from_xhtml_cb, flags=None):
+    def add_syntax(self, name, to_xhtml_cb, from_xhtml_cb, flags=None):
         """Add a new syntax to the manager
 
         @param name: unique name of the syntax
@@ -456,9 +456,9 @@
         if TextSyntaxes.OPT_DEFAULT in flags:
             TextSyntaxes.default_syntax = key
 
-        self._updateParamOptions()
+        self._update_param_options()
 
-    def getSyntax(self, name):
+    def get_syntax(self, name):
         """get syntax key corresponding to a name
 
         @raise exceptions.NotFound: syntax doesn't exist
@@ -468,7 +468,7 @@
             return key
         raise exceptions.NotFound
 
-    def _removeMarkups(self, xhtml):
+    def _remove_markups(self, xhtml):
         """Remove XHTML markups from the given string.
 
         @param xhtml: the XHTML string to be cleaned