View Single Post
Old 06-16-2020, 07:30 PM   #358
NiLuJe
BLAM!
NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.NiLuJe ought to be getting tired of karma fortunes by now.
 
NiLuJe's Avatar
 
Posts: 13,506
Karma: 26047202
Join Date: Jun 2010
Location: Paris, France
Device: Kindle 2i, 3g, 4, 5w, PW, PW2, PW5; Kobo H2O, Forma, Elipsa, Sage, C2E
Re: previous message.

This fixes it for me, as well as another snafu that broke it later on.

Code:
diff --git a/common.py b/common.py
index 36a989d..fe8a98c 100644
--- a/common.py
+++ b/common.py
@@ -31,32 +31,23 @@ from calibre.ebooks.oeb.polish.container import EpubContainer
 from calibre.ebooks.oeb.polish.container import OPF_NAMESPACES
 from calibre.ptempfile import PersistentTemporaryFile
 from calibre.utils.logging import ANSIStream
+from polyglot.builtins import is_py3, unicode_type
+from polyglot.io import PolyglotStringIO
 
 from lxml.etree import _Element
 
-try:
-    # Python 3
-    from io import StringIO
+if is_py3:
     from typing import Dict
     from typing import List
     from typing import Optional
     from typing import Union
 
-    unicode_type = str
-except ImportError:
-    # Python 2
-    from cStringIO import StringIO
-
-    # Ignore flake8 F821 (undefined name): type checking is done exclusvely in Python 3
-    # which does not define 'unicode'.
-    unicode_type = unicode  # noqa: F821
-
 KOBO_JS_RE = re.compile(r".*/?kobo.*?\.js$", re.IGNORECASE)
 XML_NAMESPACE = "http://www.w3.org/XML/1998/namespace"
 CONFIGDIR = os.path.join(config_dir, "plugins")  # type: str
 REFERENCE_KEPUB = os.path.join(CONFIGDIR, "reference.kepub.epub")  # type: str
 PLUGIN_VERSION = (3, 2, 9)
-PLUGIN_MINIMUM_CALIBRE_VERSION = (2, 60, 0)
+PLUGIN_MINIMUM_CALIBRE_VERSION = (3, 42, 0)
 
 
 class Logger:
@@ -74,7 +65,7 @@ class Logger:
         self._lock = Lock()
         # According to Kovid, calibre always uses UTF-8 for the Python 3 version
         self.preferred_encoding = (
-            "UTF-8" if sys.version_info.major > 2 else preferred_encoding
+            "UTF-8" if is_py3 else preferred_encoding
         )
         self.outputs = [ANSIStream()]
 
@@ -88,7 +79,7 @@ class Logger:
 
     def _tag_args(self, level, *args):
         now = time.localtime()
-        buf = StringIO()
+        buf = PolyglotStringIO()
         tagged_args = []
         for arg in args:
             prints(time.strftime("%Y-%m-%d %H:%M:%S", now), file=buf, end=" ")
diff --git a/container.py b/container.py
index 0c74a3d..7bddbfe 100644
--- a/container.py
+++ b/container.py
@@ -29,16 +29,13 @@ from calibre.ebooks.conversion.plugins.epub_input import IDPF_OBFUSCATION
 from calibre.ebooks.conversion.utils import HeuristicProcessor
 from calibre.ebooks.oeb.polish.container import EpubContainer
 from calibre.utils.smartypants import smartyPants
+from polyglot.builtins import is_py3
 
 from lxml import etree
 
-try:
-    # Python 3
+if is_py3:
     from typing import Dict
     from typing import Set
-except ImportError:
-    # Python 2
-    pass
 
 # Support load_translations() without forcing calibre 1.9+
 try:
diff --git a/conversion/kepub_input.py b/conversion/kepub_input.py
index 93dd1ec..25342d9 100644
--- a/conversion/kepub_input.py
+++ b/conversion/kepub_input.py
@@ -12,6 +12,7 @@ import sys
 
 from calibre.customize.conversion import OptionRecommendation
 from calibre.ebooks.conversion.plugins.epub_input import EPUBInput
+from polyglot.builtins import is_py3
 
 from calibre_plugins.kepubin import common
 
@@ -66,7 +67,7 @@ class KEPUBInput(EPUBInput):
 
         try:
             zf = ZipFile(stream)
-            cwd = os.getcwdu() if sys.version_info.major == 2 else os.getcwd()
+            cwd = os.getcwdu() if not is_py3 else os.getcwd()
             zf.extractall(cwd)
         except Exception:
             log.exception(
@@ -100,7 +101,7 @@ class KEPUBInput(EPUBInput):
         if os.path.exists(encfile):
             raise DRMError(os.path.basename(path))
 
-        cwd = os.getcwdu() if sys.version_info.major == 2 else os.getcwd()
+        cwd = os.getcwdu() if not is_py3 else os.getcwd()
         opf = os.path.relpath(opf, cwd)
         parts = os.path.split(opf)
         opf = OPF(opf, os.path.dirname(os.path.abspath(opf)))
diff --git a/conversion/kepub_output.py b/conversion/kepub_output.py
index c9a185e..c33e704 100644
--- a/conversion/kepub_output.py
+++ b/conversion/kepub_output.py
@@ -18,6 +18,7 @@ from calibre.ebooks.metadata.book.base import Metadata
 from calibre.ebooks.metadata.book.base import NULL_VALUES
 
 from calibre_plugins.kepubout import common
+from calibre_plugins.kepubout.container import KEPubContainer
 
 
 # Support load_translations() without forcing calibre 1.9+
@@ -105,7 +106,7 @@ class KEPubOutput(OutputFormatPlugin):
             oeb_book, output, input_plugin, opts, common.log
         )
         common.log.debug("Done ePub conversion")
-        container = common.KEPubContainer(output, common.log)
+        container = KEPubContainer(output, common.log)
 
         if container.is_drm_encumbered:
             common.log.error("DRM-encumbered container, skipping conversion")
diff --git a/device/driver.py b/device/driver.py
index 203de39..8c69901 100644
--- a/device/driver.py
+++ b/device/driver.py
@@ -17,10 +17,11 @@ import re
 import shutil
 from datetime import datetime
 
-try:
-    # Python 3
+from polyglot.builtins import is_py3
+
+if is_py3:
     from configparser import SafeConfigParser
-except ImportError:
+else:
     from ConfigParser import SafeConfigParser
 
 from calibre.constants import config_dir
@@ -28,6 +29,7 @@ from calibre.devices.kobo.driver import KOBOTOUCH
 from calibre.ebooks.oeb.polish.errors import DRMError
 
 from calibre_plugins.kobotouch_extended import common
+from calibre_plugins.kobotouch_extended.container import KEPubContainer
 
 # Support load_translations() without forcing calibre 1.9+
 try:
@@ -212,7 +214,7 @@ class KOBOTOUCHEXTENDED(KOBOTOUCH):
         is_encumbered_book = False
         try:
             if container is None:
-                container = common.KEPubContainer(infile, common.log)
+                container = KEPubContainer(infile, common.log)
             else:
                 is_encumbered_book = container.is_drm_encumbered
         except DRMError:
diff --git a/metadata/writer.py b/metadata/writer.py
index a1903d6..9f59f9f 100644
--- a/metadata/writer.py
+++ b/metadata/writer.py
@@ -8,13 +8,7 @@ __docformat__ = "markdown en"
 
 import os
 
-try:
-    # Python 3
-    from io import StringIO
-except ImportError:
-    # Python 2
-    from cStringIO import StringIO
-
+from polyglot.io import PolyglotStringIO
 
 from calibre.customize.builtins import EPUBMetadataWriter
 from calibre.ebooks.metadata.epub import get_zip_reader
@@ -97,7 +91,7 @@ class KEPUBMetadataWriter(EPUBMetadataWriter):
                             found_cover = True
 
             if found_cover:
-                newopf = StringIO(reader.opf.render().decode("UTF-8"))
+                newopf = PolyglotStringIO(reader.opf.render().decode("UTF-8"))
                 if isinstance(reader.archive, LocalZipFile):
                     reader.archive.safe_replace(reader.container[OPF.MIMETYPE], newopf)
                 else:

Last edited by NiLuJe; 06-16-2020 at 07:52 PM. Reason: And with even more Polyglot usage
NiLuJe is offline   Reply With Quote