diff sat/plugins/plugin_misc_text_syntaxes.py @ 3028:ab2696e34d29

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
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents a02ad4bc0a6d
children fee60f17ebac
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_text_syntaxes.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/plugins/plugin_misc_text_syntaxes.py	Tue Aug 13 19:08:41 2019 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 # SAT plugin for managing various text syntaxes
@@ -35,7 +35,7 @@
     from lxml import etree
 except ImportError:
     raise exceptions.MissingModule(
-        u"Missing module lxml, please download/install it from http://lxml.de/"
+        "Missing module lxml, please download/install it from http://lxml.de/"
     )
 
 log = getLogger(__name__)
@@ -218,38 +218,38 @@
                 partial(markdown.markdown,
                         extensions=[
                             EscapeHTML(),
-                            u'nl2br',
-                            u'codehilite',
-                            u'fenced_code',
-                            u'sane_lists',
-                            u'tables',
+                            'nl2br',
+                            'codehilite',
+                            'fenced_code',
+                            'sane_lists',
+                            'tables',
                             ],
                         extension_configs = {
-                            u"codehilite": {
-                                u"css_class": "highlight",
+                            "codehilite": {
+                                "css_class": "highlight",
                             }
                         }),
                 _html2text,
                 [TextSyntaxes.OPT_DEFAULT],
             )
         except ImportError:
-            log.warning(u"markdown or html2text not found, can't use Markdown syntax")
+            log.warning("markdown or html2text not found, can't use Markdown syntax")
             log.info(
-                u"You can download/install them from https://pythonhosted.org/Markdown/ and https://github.com/Alir3z4/html2text/"
+                "You can download/install them from https://pythonhosted.org/Markdown/ and https://github.com/Alir3z4/html2text/"
             )
         host.bridge.addMethod(
             "syntaxConvert",
             ".plugin",
             in_sign="sssbs",
             out_sign="s",
-            async=True,
+            async_=True,
             method=self.convert,
         )
         host.bridge.addMethod(
             "syntaxGet", ".plugin", in_sign="s", out_sign="s", method=self.getSyntax
         )
         if xml_tools.cleanXHTML is None:
-            log.debug(u"Installing cleaning method")
+            log.debug("Installing cleaning method")
             xml_tools.cleanXHTML = self.cleanXHTML
 
     def _updateParamOptions(self):
@@ -257,7 +257,7 @@
         default_synt = TextSyntaxes.default_syntax
         syntaxes = []
 
-        for syntax in data_synt.keys():
+        for syntax in list(data_synt.keys()):
             flags = data_synt[syntax]["flags"]
             if TextSyntaxes.OPT_HIDDEN not in flags:
                 syntaxes.append(syntax)
@@ -267,9 +267,9 @@
 
         for syntax in syntaxes:
             selected = 'selected="true"' if syntax == default_synt else ""
-            options.append(u'<option value="%s" %s/>' % (syntax, selected))
+            options.append('<option value="%s" %s/>' % (syntax, selected))
 
-        TextSyntaxes.params_data["options"] = u"\n".join(options)
+        TextSyntaxes.params_data["options"] = "\n".join(options)
         self.host.memory.updateParams(TextSyntaxes.params % TextSyntaxes.params_data)
 
     def getCurrentSyntax(self, profile):
@@ -280,9 +280,9 @@
         """
         return self.host.memory.getParamA(NAME, CATEGORY, profile_key=profile)
 
-    def _logError(self, failure, action=u"converting syntax"):
+    def _logError(self, failure, action="converting syntax"):
         log.error(
-            u"Error while {action}: {failure}".format(action=action, failure=failure)
+            "Error while {action}: {failure}".format(action=action, failure=failure)
         )
         return failure
 
@@ -320,13 +320,13 @@
         @return (unicode): cleaned XHTML
         """
 
-        if isinstance(xhtml, basestring):
+        if isinstance(xhtml, str):
             try:
                 xhtml_elt = html.fromstring(xhtml)
             except etree.ParserError as e:
                 if not xhtml.strip():
-                    return u""
-                log.error(u"Can't clean XHTML: {xhtml}".format(xhtml=xhtml))
+                    return ""
+                log.error("Can't clean XHTML: {xhtml}".format(xhtml=xhtml))
                 raise e
         elif isinstance(xhtml, html.HtmlElement):
             xhtml_elt = xhtml
@@ -345,8 +345,8 @@
                 if element.tag in VOID_ELEMENTS:
                     element.text = None
                 else:
-                    element.text = u''
-        return html.tostring(xhtml_elt, encoding=unicode, method="xml")
+                    element.text = ''
+        return html.tostring(xhtml_elt, encoding=str, method="xml")
 
     def convert(
         self, text, syntax_from, syntax_to=_SYNTAX_XHTML, safe=True, profile=None
@@ -413,7 +413,7 @@
         flags = flags if flags is not None else []
         if TextSyntaxes.OPT_HIDDEN in flags and TextSyntaxes.OPT_DEFAULT in flags:
             raise ValueError(
-                u"{} and {} are mutually exclusive".format(
+                "{} and {} are mutually exclusive".format(
                     TextSyntaxes.OPT_HIDDEN, TextSyntaxes.OPT_DEFAULT
                 )
             )
@@ -422,7 +422,7 @@
         key = name.lower().strip()
         if key in syntaxes:
             raise exceptions.ConflictError(
-                u"This syntax key already exists: {}".format(key)
+                "This syntax key already exists: {}".format(key)
             )
         syntaxes[key] = {
             "name": name,
@@ -453,4 +453,4 @@
         """
         cleaner = clean.Cleaner(kill_tags=["style"])
         cleaned = cleaner.clean_html(html.fromstring(xhtml))
-        return html.tostring(cleaned, encoding=unicode, method="text")
+        return html.tostring(cleaned, encoding=str, method="text")