Changed document exporter imports to use importlib.import_module(). eric7

Tue, 08 Nov 2022 12:19:25 +0100

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 08 Nov 2022 12:19:25 +0100
branch
eric7
changeset 9487
78cb053591c9
parent 9486
5a8179763e38
child 9488
44d723e53988

Changed document exporter imports to use importlib.import_module().

src/eric7/QScintilla/Exporters/ExporterHTML.py file | annotate | diff | comparison | revisions
src/eric7/QScintilla/Exporters/ExporterODT.py file | annotate | diff | comparison | revisions
src/eric7/QScintilla/Exporters/ExporterPDF.py file | annotate | diff | comparison | revisions
src/eric7/QScintilla/Exporters/ExporterRTF.py file | annotate | diff | comparison | revisions
src/eric7/QScintilla/Exporters/ExporterTEX.py file | annotate | diff | comparison | revisions
src/eric7/QScintilla/Exporters/__init__.py file | annotate | diff | comparison | revisions
--- a/src/eric7/QScintilla/Exporters/ExporterHTML.py	Tue Nov 08 11:50:50 2022 +0100
+++ b/src/eric7/QScintilla/Exporters/ExporterHTML.py	Tue Nov 08 12:19:25 2022 +0100
@@ -699,3 +699,17 @@
         foot = """\n</body>\n</html>\n"""
 
         return head + body + mermaid_initialize + foot
+
+
+def createExporter(editor, parent=None):
+    """
+    Function to instantiate an exporter object.
+
+    @param editor reference to the editor object
+    @type QScintilla.Editor.Editor
+    @param parent parent object of the exporter (defaults to None)
+    @type QObject (optional)
+    @return exporter object
+    @rtype ExporterHTML
+    """
+    return ExporterHTML(editor, parent=parent)
--- a/src/eric7/QScintilla/Exporters/ExporterODT.py	Tue Nov 08 11:50:50 2022 +0100
+++ b/src/eric7/QScintilla/Exporters/ExporterODT.py	Tue Nov 08 12:19:25 2022 +0100
@@ -72,3 +72,17 @@
                     """<p>The source could not be exported to""" """ <b>{0}</b>.</p>"""
                 ).format(filename),
             )
+
+
+def createExporter(editor, parent=None):
+    """
+    Function to instantiate an exporter object.
+
+    @param editor reference to the editor object
+    @type QScintilla.Editor.Editor
+    @param parent parent object of the exporter (defaults to None)
+    @type QObject (optional)
+    @return exporter object
+    @rtype ExporterODT
+    """
+    return ExporterODT(editor, parent=parent)
--- a/src/eric7/QScintilla/Exporters/ExporterPDF.py	Tue Nov 08 11:50:50 2022 +0100
+++ b/src/eric7/QScintilla/Exporters/ExporterPDF.py	Tue Nov 08 12:19:25 2022 +0100
@@ -11,7 +11,7 @@
 # Original code: Copyright 1998-2006 by Neil Hodgson <neilh@scintilla.org>
 
 from PyQt6.Qsci import QsciScintilla
-from PyQt6.QtGui import QFontInfo
+from PyQt6.QtGui import QColor, QFontInfo
 
 from eric7 import Preferences
 from eric7.EricGui.EricOverrideCursor import EricOverrideCursor
@@ -513,6 +513,9 @@
                             style.font |= 1
 
                         colour = lex.color(istyle)
+                        if colour.name() == "#ffffff":
+                            # map white to black for readability on paper
+                            colour = QColor("#000000")
                         style.fore = self.__getPDFRGB(colour)
                         self.pr.style[istyle] = style
 
@@ -635,3 +638,17 @@
                         """ <b>{0}</b>.</p><p>Reason: {1}</p>"""
                     ).format(filename, str(err)),
                 )
+
+
+def createExporter(editor, parent=None):
+    """
+    Function to instantiate an exporter object.
+
+    @param editor reference to the editor object
+    @type QScintilla.Editor.Editor
+    @param parent parent object of the exporter (defaults to None)
+    @type QObject (optional)
+    @return exporter object
+    @rtype ExporterPDF
+    """
+    return ExporterPDF(editor, parent=parent)
--- a/src/eric7/QScintilla/Exporters/ExporterRTF.py	Tue Nov 08 11:50:50 2022 +0100
+++ b/src/eric7/QScintilla/Exporters/ExporterRTF.py	Tue Nov 08 12:19:25 2022 +0100
@@ -461,3 +461,17 @@
         )
 
         return styles, fontsize
+
+
+def createExporter(editor, parent=None):
+    """
+    Function to instantiate an exporter object.
+
+    @param editor reference to the editor object
+    @type QScintilla.Editor.Editor
+    @param parent parent object of the exporter (defaults to None)
+    @type QObject (optional)
+    @return exporter object
+    @rtype ExporterRTF
+    """
+    return ExporterRTF(editor, parent=parent)
--- a/src/eric7/QScintilla/Exporters/ExporterTEX.py	Tue Nov 08 11:50:50 2022 +0100
+++ b/src/eric7/QScintilla/Exporters/ExporterTEX.py	Tue Nov 08 12:19:25 2022 +0100
@@ -295,3 +295,17 @@
                         """ <b>{0}</b>.</p><p>Reason: {1}</p>"""
                     ).format(filename, str(err)),
                 )
+
+
+def createExporter(editor, parent=None):
+    """
+    Function to instantiate an exporter object.
+
+    @param editor reference to the editor object
+    @type QScintilla.Editor.Editor
+    @param parent parent object of the exporter (defaults to None)
+    @type QObject (optional)
+    @return exporter object
+    @rtype ExporterTEX
+    """
+    return ExporterTEX(editor, parent=parent)
--- a/src/eric7/QScintilla/Exporters/__init__.py	Tue Nov 08 11:50:50 2022 +0100
+++ b/src/eric7/QScintilla/Exporters/__init__.py	Tue Nov 08 12:19:25 2022 +0100
@@ -7,6 +7,9 @@
 Package implementing exporters for various file formats.
 """
 
+import contextlib
+import importlib
+
 from PyQt6.QtCore import QCoreApplication
 
 
@@ -38,26 +41,18 @@
     @return reference to the instanciated exporter object
         (QScintilla.Exporter.Exporter)
     """
-    try:
-        if exporterFormat == "HTML":
-            from .ExporterHTML import ExporterHTML  # __IGNORE_WARNING_I101__
-
-            return ExporterHTML(editor)
-        elif exporterFormat == "PDF":
-            from .ExporterPDF import ExporterPDF  # __IGNORE_WARNING_I101__
-
-            return ExporterPDF(editor)
-        elif exporterFormat == "RTF":
-            from .ExporterRTF import ExporterRTF  # __IGNORE_WARNING_I101__
+    exporterMapping = {
+        "HTML": "ExporterHTML",
+        "ODT": "ExporterODT",
+        "PDF": "ExporterPDF",
+        "RTF": "ExporterRTF",
+        "TeX": "ExporterTEX",
+    }
+    with contextlib.suppress(ImportError):
+        if exporterFormat in exporterMapping:
+            mod = importlib.import_module(
+                "eric7.QScintilla.Exporters.{0}".format(exporterMapping[exporterFormat])
+            )
+            return mod.createExporter(editor)
 
-            return ExporterRTF(editor)
-        elif exporterFormat == "TeX":
-            from .ExporterTEX import ExporterTEX  # __IGNORE_WARNING_I101__
-
-            return ExporterTEX(editor)
-        elif exporterFormat == "ODT":
-            from .ExporterODT import ExporterODT  # __IGNORE_WARNING_I101__
-
-            return ExporterODT(editor)
-    except ImportError:
-        return None
+    return None

eric ide

mercurial