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.
This commit is contained in:
Goffi 2023-04-08 13:54:42 +02:00
commit 6a6f15707a

View file

@ -317,7 +317,7 @@ class XHTMLParser(object):
setattr(
self,
"parser_h{}".format(i),
lambda elt, buf, level=i: self.parserHeading(elt, buf, level),
lambda elt, buf, level=i: self.parser_heading(elt, buf, level),
)
def parser_a(self, elt, buf):
@ -331,7 +331,7 @@ class XHTMLParser(object):
# we don't want empty values
raise KeyError
except KeyError:
self.parserGeneric(elt, buf)
self.parser_generic(elt, buf)
else:
buf.append("~~{}~~".format(id_))
return
@ -358,7 +358,7 @@ class XHTMLParser(object):
title = elt["title"]
except KeyError:
log.debug("Acronyme without title, using generic parser")
self.parserGeneric(elt, buf)
self.parser_generic(elt, buf)
return
buf.append("??{}|{}??".format(str(elt), title))
@ -370,7 +370,7 @@ class XHTMLParser(object):
if len(children) == 1 and children[0].name == "p":
elt = children[0]
tmp_buf = []
self.parseChildren(elt, tmp_buf)
self.parse_children(elt, tmp_buf)
blockquote = "> " + "\n> ".join("".join(tmp_buf).split("\n"))
buf.append(blockquote)
@ -379,23 +379,23 @@ class XHTMLParser(object):
def parser_code(self, elt, buf):
buf.append("@@")
self.parseChildren(elt, buf)
self.parse_children(elt, buf)
buf.append("@@")
def parser_del(self, elt, buf):
buf.append("--")
self.parseChildren(elt, buf)
self.parse_children(elt, buf)
buf.append("--")
def parser_div(self, elt, buf):
if elt.getAttribute("class") == "footnotes":
self.parserFootnote(elt, buf)
self.parser_footnote(elt, buf)
else:
self.parseChildren(elt, buf, block=True)
self.parse_children(elt, buf, block=True)
def parser_em(self, elt, buf):
buf.append("''")
self.parseChildren(elt, buf)
self.parse_children(elt, buf)
buf.append("''")
def parser_h6(self, elt, buf):
@ -449,7 +449,7 @@ class XHTMLParser(object):
def parser_ins(self, elt, buf):
buf.append("++")
self.parseChildren(elt, buf)
self.parse_children(elt, buf)
buf.append("++")
def parser_li(self, elt, buf):
@ -474,14 +474,14 @@ class XHTMLParser(object):
buf.extend(bullets)
buf.append(" ")
self.parseChildren(elt, buf)
self.parse_children(elt, buf)
buf.append("\n")
def parser_ol(self, elt, buf):
self.parserList(elt, buf, FLAG_OL)
self.parser_list(elt, buf, FLAG_OL)
def parser_p(self, elt, buf):
self.parseChildren(elt, buf)
self.parse_children(elt, buf)
buf.append("\n\n")
def parser_pre(self, elt, buf):
@ -513,11 +513,11 @@ class XHTMLParser(object):
buf.append("}}")
def parser_span(self, elt, buf):
self.parseChildren(elt, buf, block=True)
self.parse_children(elt, buf, block=True)
def parser_strong(self, elt, buf):
buf.append("__")
self.parseChildren(elt, buf)
self.parse_children(elt, buf)
buf.append("__")
def parser_sup(self, elt, buf):
@ -535,7 +535,7 @@ class XHTMLParser(object):
note_id = url[url.find("#") + 1 :]
if not note_id:
log.warning("bad link found in footnote")
self.parserGeneric(elt, buf)
self.parser_generic(elt, buf)
return
# this looks like a footnote
buf.append("$$")
@ -543,14 +543,14 @@ class XHTMLParser(object):
self.footnotes[note_id] = len(buf) - 1
buf.append("$$")
else:
self.parserGeneric(elt, buf)
self.parser_generic(elt, buf)
def parser_ul(self, elt, buf):
self.parserList(elt, buf, FLAG_UL)
self.parser_list(elt, buf, FLAG_UL)
def parserList(self, elt, buf, type_):
def parser_list(self, elt, buf, type_):
self.flags.append(type_)
self.parseChildren(elt, buf, block=True)
self.parse_children(elt, buf, block=True)
idx = 0
for flag in reversed(self.flags):
idx -= 1
@ -561,14 +561,14 @@ class XHTMLParser(object):
if idx == 0:
raise exceptions.InternalError("flag has been removed by an other parser")
def parserHeading(self, elt, buf, level):
def parser_heading(self, elt, buf, level):
buf.append((6 - level) * "!")
for child in elt.children:
# we ignore other elements for a Hx title
self.parserText(child, buf)
self.parser_text(child, buf)
buf.append("\n")
def parserFootnote(self, elt, buf):
def parser_footnote(self, elt, buf):
for elt in elt.elements():
# all children other than <p/> are ignored
if elt.name == "p":
@ -587,11 +587,11 @@ class XHTMLParser(object):
a_idx = elt.children.index(a_elt)
dummy_elt.children = elt.children[a_idx + 1 :]
note_buf = []
self.parseChildren(dummy_elt, note_buf)
self.parse_children(dummy_elt, note_buf)
# now we can replace the placeholder
buf[note_idx] = "".join(note_buf)
def parserText(self, txt, buf, keep_whitespaces=False):
def parser_text(self, txt, buf, keep_whitespaces=False):
txt = str(txt)
if not keep_whitespaces:
# we get text and only let one inter word space
@ -601,12 +601,12 @@ class XHTMLParser(object):
buf.append(txt)
return txt
def parserGeneric(self, elt, buf):
def parser_generic(self, elt, buf):
# as dotclear wiki syntax handle arbitrary XHTML code
# we use this feature to add elements that we don't know
buf.append("\n\n///html\n{}\n///\n\n".format(elt.toXml()))
def parseChildren(self, elt, buf, block=False):
def parse_children(self, elt, buf, block=False):
first_visible = True
for child in elt.children:
if not block and not first_visible and buf and buf[-1][-1] not in (" ", "\n"):
@ -616,7 +616,7 @@ class XHTMLParser(object):
self._parse(child, buf)
first_visible = False
else:
appended = self.parserText(child, buf)
appended = self.parser_text(child, buf)
if appended:
first_visible = False
@ -626,7 +626,7 @@ class XHTMLParser(object):
if style and elt_name not in ELT_WITH_STYLE:
# if we have style we use generic parser to put raw HTML
# to avoid losing it
parser = self.parserGeneric
parser = self.parser_generic
else:
try:
parser = getattr(self, "parser_{}".format(elt_name))
@ -634,7 +634,7 @@ class XHTMLParser(object):
log.debug(
"Can't find parser for {} element, using generic one".format(elt.name)
)
parser = self.parserGeneric
parser = self.parser_generic
parser(elt, buf)
def parse(self, elt):
@ -666,13 +666,13 @@ class DCWikiSyntax(object):
self._dc_parser = DCWikiParser()
self._xhtml_parser = XHTMLParser()
self._stx = self.host.plugins["TEXT_SYNTAXES"]
self._stx.addSyntax(
self.SYNTAX_NAME, self.parseWiki, self.parseXHTML, [self._stx.OPT_NO_THREAD]
self._stx.add_syntax(
self.SYNTAX_NAME, self.parse_wiki, self.parse_xhtml, [self._stx.OPT_NO_THREAD]
)
def parseWiki(self, wiki_stx):
def parse_wiki(self, wiki_stx):
div_elt = self._dc_parser.parse(wiki_stx)
return div_elt.toXml()
def parseXHTML(self, xhtml):
def parse_xhtml(self, xhtml):
return self._xhtml_parser.parseString(xhtml)