Python 3 port:

/!\ this is a huge commit
/!\ starting from this commit, SàT is needs Python 3.6+
/!\ SàT maybe be instable or some feature may not work anymore, this will improve with time

This patch port backend, bridge and frontends to Python 3.

Roughly this has been done this way:

- 2to3 tools has been applied (with python 3.7)
- all references to python2 have been replaced with python3 (notably shebangs)
- fixed files not handled by 2to3 (notably the shell script)
- several manual fixes
- fixed issues reported by Python 3 that where not handled in Python 2
- replaced "async" with "async_" when needed (it's a reserved word from Python 3.7)
- replaced zope's "implements" with @implementer decorator
- temporary hack to handle data pickled in database, as str or bytes may be returned,
  to be checked later
- fixed hash comparison for password
- removed some code which is not needed anymore with Python 3
- deactivated some code which needs to be checked (notably certificate validation)
- tested with jp, fixed reported issues until some basic commands worked
- ported Primitivus (after porting dependencies like urwid satext)
- more manual fixes
This commit is contained in:
Goffi 2019-08-13 19:08:41 +02:00
commit fbfe96c505

View file

@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# SàT plugin for Dotclear Wiki Syntax
@ -40,9 +40,9 @@ PLUGIN_INFO = {
C.PI_DESCRIPTION: _("""Implementation of Dotclear wiki syntax"""),
}
NOTE_TPL = u"[{}]" # Note template
NOTE_A_REV_TPL = u"rev_note_{}"
NOTE_A_TPL = u"note_{}"
NOTE_TPL = "[{}]" # Note template
NOTE_A_REV_TPL = "rev_note_{}"
NOTE_A_TPL = "note_{}"
ESCAPE_CHARS_BASE = r"(?P<escape_char>[][{}%|\\/*#@{{}}~$-])"
ESCAPE_CHARS_EXTRA = (
r"!?_+'()"
@ -90,7 +90,7 @@ wiki_block_level_re = re.compile(
class DCWikiParser(object):
def __init__(self):
self._footnotes = None
for i in xrange(5):
for i in range(5):
setattr(
self,
"parser_h{}_title".format(i),
@ -108,7 +108,7 @@ class DCWikiParser(object):
try:
div_elt = xml_tools.ElementParser()(wrapped_html)
except domish.ParserError as e:
log.warning(u"Error while parsing HTML content, ignoring it: {}".format(e))
log.warning("Error while parsing HTML content, ignoring it: {}".format(e))
return
children = list(div_elt.elements())
if len(children) == 1 and children[0].name == "div":
@ -132,7 +132,7 @@ class DCWikiParser(object):
string = string[depth:].lstrip()
for i in xrange(depth + 1):
for i in range(depth + 1):
list_elt = getattr(parent, list_type)
if not list_elt:
parent = parent.addElement(list_type)
@ -165,7 +165,7 @@ class DCWikiParser(object):
if p_elt is None:
p_elt = blockquote_elt.addElement("p")
else:
string = u"\n" + string
string = "\n" + string
self._parse(string, p_elt)
@ -189,7 +189,7 @@ class DCWikiParser(object):
self._parse(string, del_elt)
def parser_link(self, string, parent):
url_data = string.split(u"|")
url_data = string.split("|")
a_elt = parent.addElement("a")
length = len(url_data)
if length == 1:
@ -206,10 +206,10 @@ class DCWikiParser(object):
if length >= 4:
a_elt["title"] = url_data[3]
if length > 4:
log.warning(u"too much data for url, ignoring extra data")
log.warning("too much data for url, ignoring extra data")
def parser_image(self, string, parent):
image_data = string.split(u"|")
image_data = string.split("|")
img_elt = parent.addElement("img")
for idx, attribute in enumerate(("src", "alt", "position", "longdesc")):
@ -231,19 +231,19 @@ class DCWikiParser(object):
"style"
] = "display:block; margin-left:auto; margin-right:auto"
else:
log.warning(u"bad position argument for image, ignoring it")
log.warning("bad position argument for image, ignoring it")
def parser_anchor(self, string, parent):
a_elt = parent.addElement("a")
a_elt["id"] = string
def parser_acronym(self, string, parent):
acronym, title = string.split(u"|", 1)
acronym, title = string.split("|", 1)
acronym_elt = parent.addElement("acronym", content=acronym)
acronym_elt["title"] = title
def parser_inline_quote(self, string, parent):
quote_data = string.split(u"|")
quote_data = string.split("|")
quote = quote_data[0]
q_elt = parent.addElement("q", content=quote)
for idx, attribute in enumerate(("lang", "cite"), 1):
@ -263,12 +263,12 @@ class DCWikiParser(object):
sup_elt["class"] = "note"
a_elt = sup_elt.addElement("a", content=note_txt)
a_elt["id"] = NOTE_A_REV_TPL.format(idx)
a_elt["href"] = u"#{}".format(NOTE_A_TPL.format(idx))
a_elt["href"] = "#{}".format(NOTE_A_TPL.format(idx))
p_elt = domish.Element((None, "p"))
a_elt = p_elt.addElement("a", content=note_txt)
a_elt["id"] = NOTE_A_TPL.format(idx)
a_elt["href"] = u"#{}".format(NOTE_A_REV_TPL.format(idx))
a_elt["href"] = "#{}".format(NOTE_A_REV_TPL.format(idx))
self._parse(string, p_elt)
# footnotes are actually added at the end of the parsing
self._footnotes.append(p_elt)
@ -287,7 +287,7 @@ class DCWikiParser(object):
try:
parser = getattr(self, "parser_{}".format(match.lastgroup))
except AttributeError:
log.warning(u"No parser found for {}".format(match.lastgroup))
log.warning("No parser found for {}".format(match.lastgroup))
# parent.addContent(string)
continue
parser(matched, parent)
@ -313,7 +313,7 @@ class XHTMLParser(object):
self.flags = None
self.toto = 0
self.footnotes = None # will hold a map from url to buffer id
for i in xrange(1, 6):
for i in range(1, 6):
setattr(
self,
"parser_h{}".format(i),
@ -333,11 +333,11 @@ class XHTMLParser(object):
except KeyError:
self.parserGeneric(elt, buf)
else:
buf.append(u"~~{}~~".format(id_))
buf.append("~~{}~~".format(id_))
return
link_data = [url]
name = unicode(elt)
name = str(elt)
if name != url:
link_data.insert(0, name)
@ -346,46 +346,46 @@ class XHTMLParser(object):
if lang is not None:
link_data.append(lang)
elif title is not None:
link_data.appand(u"")
link_data.appand("")
if title is not None:
link_data.append(title)
buf.append(u"[")
buf.append(u"|".join(link_data))
buf.append(u"]")
buf.append("[")
buf.append("|".join(link_data))
buf.append("]")
def parser_acronym(self, elt, buf):
try:
title = elt["title"]
except KeyError:
log.debug(u"Acronyme without title, using generic parser")
log.debug("Acronyme without title, using generic parser")
self.parserGeneric(elt, buf)
return
buf.append(u"??{}|{}??".format(unicode(elt), title))
buf.append("??{}|{}??".format(str(elt), title))
def parser_blockquote(self, elt, buf):
# we remove wrapping <p> to avoid empty line with "> "
children = list(
[child for child in elt.children if unicode(child).strip() not in ("", "\n")]
[child for child in elt.children if str(child).strip() not in ("", "\n")]
)
if len(children) == 1 and children[0].name == "p":
elt = children[0]
tmp_buf = []
self.parseChildren(elt, tmp_buf)
blockquote = u"> " + u"\n> ".join(u"".join(tmp_buf).split("\n"))
blockquote = "> " + "\n> ".join("".join(tmp_buf).split("\n"))
buf.append(blockquote)
def parser_br(self, elt, buf):
buf.append(u"%%%")
buf.append("%%%")
def parser_code(self, elt, buf):
buf.append(u"@@")
buf.append("@@")
self.parseChildren(elt, buf)
buf.append(u"@@")
buf.append("@@")
def parser_del(self, elt, buf):
buf.append(u"--")
buf.append("--")
self.parseChildren(elt, buf)
buf.append(u"--")
buf.append("--")
def parser_div(self, elt, buf):
if elt.getAttribute("class") == "footnotes":
@ -394,9 +394,9 @@ class XHTMLParser(object):
self.parseChildren(elt, buf, block=True)
def parser_em(self, elt, buf):
buf.append(u"''")
buf.append("''")
self.parseChildren(elt, buf)
buf.append(u"''")
buf.append("''")
def parser_h6(self, elt, buf):
# XXX: <h6/> heading is not managed by wiki syntax
@ -406,13 +406,13 @@ class XHTMLParser(object):
self._parse(elt, buf)
def parser_hr(self, elt, buf):
buf.append(u"\n----\n")
buf.append("\n----\n")
def parser_img(self, elt, buf):
try:
url = elt["src"]
except KeyError:
log.warning(u"Ignoring <img/> without src")
log.warning("Ignoring <img/> without src")
return
image_data = [url]
@ -433,24 +433,24 @@ class XHTMLParser(object):
if alt:
image_data.append(alt)
elif position or desc:
image_data.append(u"")
image_data.append("")
if position:
image_data.append(position)
elif desc:
image_data.append(u"")
image_data.append("")
if desc:
image_data.append(desc)
buf.append(u"((")
buf.append(u"|".join(image_data))
buf.append(u"))")
buf.append("((")
buf.append("|".join(image_data))
buf.append("))")
def parser_ins(self, elt, buf):
buf.append(u"++")
buf.append("++")
self.parseChildren(elt, buf)
buf.append(u"++")
buf.append("++")
def parser_li(self, elt, buf):
flag = None
@ -461,11 +461,11 @@ class XHTMLParser(object):
if current_flag is None:
current_flag = flag
if flag == current_flag:
bullets.append(u"*" if flag == FLAG_UL else u"#")
bullets.append("*" if flag == FLAG_UL else "#")
else:
break
if flag != current_flag and buf[-1] == u" ":
if flag != current_flag and buf[-1] == " ":
# this trick is to avoid a space when we switch
# from (un)ordered to the other type on the same row
# e.g. *# unorder + ordered item
@ -473,29 +473,29 @@ class XHTMLParser(object):
buf.extend(bullets)
buf.append(u" ")
buf.append(" ")
self.parseChildren(elt, buf)
buf.append(u"\n")
buf.append("\n")
def parser_ol(self, elt, buf):
self.parserList(elt, buf, FLAG_OL)
def parser_p(self, elt, buf):
self.parseChildren(elt, buf)
buf.append(u"\n\n")
buf.append("\n\n")
def parser_pre(self, elt, buf):
pre = u"".join(
pre = "".join(
[
child.toXml() if domish.IElement.providedBy(child) else unicode(child)
child.toXml() if domish.IElement.providedBy(child) else str(child)
for child in elt.children
]
)
pre = u" " + u"\n ".join(pre.split("\n"))
pre = " " + "\n ".join(pre.split("\n"))
buf.append(pre)
def parser_q(self, elt, buf):
quote_data = [unicode(elt)]
quote_data = [str(elt)]
lang = elt.getAttribute("lang")
cite = elt.getAttribute("url")
@ -503,27 +503,27 @@ class XHTMLParser(object):
if lang:
quote_data.append(lang)
elif cite:
quote_data.append(u"")
quote_data.append("")
if cite:
quote_data.append(cite)
buf.append(u"{{")
buf.append(u"|".join(quote_data))
buf.append(u"}}")
buf.append("{{")
buf.append("|".join(quote_data))
buf.append("}}")
def parser_span(self, elt, buf):
self.parseChildren(elt, buf, block=True)
def parser_strong(self, elt, buf):
buf.append(u"__")
buf.append("__")
self.parseChildren(elt, buf)
buf.append(u"__")
buf.append("__")
def parser_sup(self, elt, buf):
# sup is mainly used for footnotes, so we check if we have an anchor inside
children = list(
[child for child in elt.children if unicode(child).strip() not in ("", "\n")]
[child for child in elt.children if str(child).strip() not in ("", "\n")]
)
if (
len(children) == 1
@ -538,10 +538,10 @@ class XHTMLParser(object):
self.parserGeneric(elt, buf)
return
# this looks like a footnote
buf.append(u"$$")
buf.append(u" ") # placeholder
buf.append("$$")
buf.append(" ") # placeholder
self.footnotes[note_id] = len(buf) - 1
buf.append(u"$$")
buf.append("$$")
else:
self.parserGeneric(elt, buf)
@ -559,14 +559,14 @@ class XHTMLParser(object):
break
if idx == 0:
raise exceptions.InternalError(u"flag has been removed by an other parser")
raise exceptions.InternalError("flag has been removed by an other parser")
def parserHeading(self, elt, buf, level):
buf.append((6 - level) * u"!")
buf.append((6 - level) * "!")
for child in elt.children:
# we ignore other elements for a Hx title
self.parserText(child, buf)
buf.append(u"\n")
buf.append("\n")
def parserFootnote(self, elt, buf):
for elt in elt.elements():
@ -575,13 +575,13 @@ class XHTMLParser(object):
a_elt = elt.a
if a_elt is None:
log.warning(
u"<p/> element doesn't contain <a/> in footnote, ignoring it"
"<p/> element doesn't contain <a/> in footnote, ignoring it"
)
continue
try:
note_idx = self.footnotes[a_elt["id"]]
except KeyError:
log.warning(u"Note id doesn't match any known note, ignoring it")
log.warning("Note id doesn't match any known note, ignoring it")
# we create a dummy element to parse all children after the <a/>
dummy_elt = domish.Element((None, "note"))
a_idx = elt.children.index(a_elt)
@ -589,13 +589,13 @@ class XHTMLParser(object):
note_buf = []
self.parseChildren(dummy_elt, note_buf)
# now we can replace the placeholder
buf[note_idx] = u"".join(note_buf)
buf[note_idx] = "".join(note_buf)
def parserText(self, txt, buf, keep_whitespaces=False):
txt = unicode(txt)
txt = str(txt)
if not keep_whitespaces:
# we get text and only let one inter word space
txt = u" ".join(txt.split())
txt = " ".join(txt.split())
txt = re.sub(ESCAPE_CHARS, r"\\\1", txt)
if txt:
buf.append(txt)
@ -604,14 +604,14 @@ class XHTMLParser(object):
def parserGeneric(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(u"\n\n///html\n{}\n///\n\n".format(elt.toXml()))
buf.append("\n\n///html\n{}\n///\n\n".format(elt.toXml()))
def parseChildren(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"):
# we add separation if it isn't already there
buf.append(u" ")
buf.append(" ")
if domish.IElement.providedBy(child):
self._parse(child, buf)
first_visible = False
@ -642,14 +642,14 @@ class XHTMLParser(object):
self.footnotes = {}
buf = []
self._parse(elt, buf)
return u"".join(buf)
return "".join(buf)
def parseString(self, string):
wrapped_html = u"<div>{}</div>".format(string)
wrapped_html = "<div>{}</div>".format(string)
try:
div_elt = xml_tools.ElementParser()(wrapped_html)
except domish.ParserError as e:
log.warning(u"Error while parsing HTML content: {}".format(e))
log.warning("Error while parsing HTML content: {}".format(e))
return
children = list(div_elt.elements())
if len(children) == 1 and children[0].name == "div":
@ -661,7 +661,7 @@ class DCWikiSyntax(object):
SYNTAX_NAME = "wiki_dotclear"
def __init__(self, host):
log.info(_(u"Dotclear wiki syntax plugin initialization"))
log.info(_("Dotclear wiki syntax plugin initialization"))
self.host = host
self._dc_parser = DCWikiParser()
self._xhtml_parser = XHTMLParser()