]> xmof Git - DeDRM.git/commitdiff
Move SafeUnbuffered to own Python file
authorNoDRM <no_drm123@protonmail.com>
Sat, 6 Aug 2022 18:09:30 +0000 (20:09 +0200)
committerNoDRM <no_drm123@protonmail.com>
Sat, 6 Aug 2022 18:09:30 +0000 (20:09 +0200)
14 files changed:
DeDRM_plugin/__init__.py
DeDRM_plugin/convert2xml.py
DeDRM_plugin/erdr2pml.py
DeDRM_plugin/genbook.py
DeDRM_plugin/ignoblekeyGenPassHash.py
DeDRM_plugin/ignoblekeyNookStudy.py
DeDRM_plugin/ineptepub.py
DeDRM_plugin/ineptpdf.py
DeDRM_plugin/k4mobidedrm.py
DeDRM_plugin/kindlekey.py
DeDRM_plugin/kindlepid.py
DeDRM_plugin/mobidedrm.py
DeDRM_plugin/topazextract.py
DeDRM_plugin/utilities.py

index ba9e2ec27468f48a8ef095c0c5781bccd37a5bda..eaf505a24f10b770cecd1e2def2a633ea9b8b547 100644 (file)
@@ -134,29 +134,8 @@ except:
     config_dir = ""
 
 
-# Wrap a stream so that output gets flushed immediately
-# and also make sure that any unicode strings get safely
-# encoded using "replace" before writing them.
-class SafeUnbuffered:
-    def __init__(self, stream):
-        self.stream = stream
-        self.encoding = stream.encoding
-        if self.encoding == None:
-            self.encoding = "utf-8"
-    def write(self, data):
-        if isinstance(data,str) or isinstance(data,unicode):
-            # str for Python3, unicode for Python2
-            data = data.encode(self.encoding,"replace")
-        try:
-            buffer = getattr(self.stream, 'buffer', self.stream)
-            # self.stream.buffer for Python3, self.stream for Python2
-            buffer.write(data)
-            buffer.flush()
-        except:
-            # We can do nothing if a write fails
-            raise
-    def __getattr__(self, attr):
-        return getattr(self.stream, attr)
+from utilities import SafeUnbuffered
+
 
 PLUGIN_NAME = __version.PLUGIN_NAME
 PLUGIN_VERSION = __version.PLUGIN_VERSION
index fe33ecabf980496a763994726de94a996313bc7a..5cb9adb344b7e21feac53a3e860bac23b2140134 100644 (file)
@@ -5,29 +5,7 @@
 # For use with Topaz Scripts Version 2.6
 # Python 3, September 2020
 
-# Wrap a stream so that output gets flushed immediately
-# and also make sure that any unicode strings get
-# encoded using "replace" before writing them.
-class SafeUnbuffered:
-    def __init__(self, stream):
-        self.stream = stream
-        self.encoding = stream.encoding
-        if self.encoding == None:
-            self.encoding = "utf-8"
-    def write(self, data):
-        if isinstance(data,str) or isinstance(data,unicode):
-            # str for Python3, unicode for Python2
-            data = data.encode(self.encoding,"replace")
-        try:
-            buffer = getattr(self.stream, 'buffer', self.stream)
-            # self.stream.buffer for Python3, self.stream for Python2
-            buffer.write(data)
-            buffer.flush()
-        except:
-            # We can do nothing if a write fails
-            raise
-    def __getattr__(self, attr):
-        return getattr(self.stream, attr)
+from utilities import SafeUnbuffered
 
 import sys
 import csv
index 6a20fdb9cf6c7e9ab03f087763c11b5181ba1f8f..df25f86c648bb506793e981319c0b5c6aee00fe7 100755 (executable)
@@ -79,29 +79,7 @@ except ImportError:
 
 #@@CALIBRE_COMPAT_CODE@@
 
-# Wrap a stream so that output gets flushed immediately
-# and also make sure that any unicode strings get
-# encoded using "replace" before writing them.
-class SafeUnbuffered:
-    def __init__(self, stream):
-        self.stream = stream
-        self.encoding = stream.encoding
-        if self.encoding == None:
-            self.encoding = "utf-8"
-    def write(self, data):
-        if isinstance(data,str) or isinstance(data,unicode):
-            # str for Python3, unicode for Python2
-            data = data.encode(self.encoding,"replace")
-        try:
-            buffer = getattr(self.stream, 'buffer', self.stream)
-            # self.stream.buffer for Python3, self.stream for Python2
-            buffer.write(data)
-            buffer.flush()
-        except:
-            # We can do nothing if a write fails
-            raise
-    def __getattr__(self, attr):
-        return getattr(self.stream, attr)
+from utilities import SafeUnbuffered
 
 iswindows = sys.platform.startswith('win')
 isosx = sys.platform.startswith('darwin')
index 21186b8c650cc1ef8e97d6e767145181789148c2..b0624fd9862667f54611d83401e7b31ab7987111 100644 (file)
@@ -4,29 +4,7 @@
 # Python 3 for calibre 5.0
 from __future__ import print_function
 
-# Wrap a stream so that output gets flushed immediately
-# and also make sure that any unicode strings get
-# encoded using "replace" before writing them.
-class SafeUnbuffered:
-    def __init__(self, stream):
-        self.stream = stream
-        self.encoding = stream.encoding
-        if self.encoding == None:
-            self.encoding = "utf-8"
-    def write(self, data):
-        if isinstance(data,str) or isinstance(data,unicode):
-            # str for Python3, unicode for Python2
-            data = data.encode(self.encoding,"replace")
-        try:
-            buffer = getattr(self.stream, 'buffer', self.stream)
-            # self.stream.buffer for Python3, self.stream for Python2
-            buffer.write(data)
-            buffer.flush()
-        except:
-            # We can do nothing if a write fails
-            raise
-    def __getattr__(self, attr):
-        return getattr(self.stream, attr)
+from utilities import SafeUnbuffered
 
 import sys
 import csv
index e1cdba05f8b647df37968fdb0f8ce1512d0c1864..7d3bec708b628f06e510cb43d6e7cb8f7ed2f20c 100644 (file)
@@ -50,35 +50,7 @@ try:
 except ImportError:
     from Crypto.Cipher import AES
 
-# Wrap a stream so that output gets flushed immediately
-# and also make sure that any unicode strings get
-# encoded using "replace" before writing them.
-class SafeUnbuffered:
-    def __init__(self, stream):
-        self.stream = stream
-        self.encoding = stream.encoding
-        if self.encoding == None:
-            self.encoding = "utf-8"
-    def write(self, data):
-        if isinstance(data,str) or isinstance(data,unicode):
-            # str for Python3, unicode for Python2
-            data = data.encode(self.encoding,"replace")
-        try:
-            buffer = getattr(self.stream, 'buffer', self.stream)
-            # self.stream.buffer for Python3, self.stream for Python2
-            buffer.write(data)
-            buffer.flush()
-        except:
-            # We can do nothing if a write fails
-            raise
-    def __getattr__(self, attr):
-        return getattr(self.stream, attr)
-
-try:
-    from calibre.constants import iswindows, isosx
-except:
-    iswindows = sys.platform.startswith('win')
-    isosx = sys.platform.startswith('darwin')
+from utilities import SafeUnbuffered
 
 def unicode_argv():
     if iswindows:
index fd85660e58579a0510e6b0c5221c6aa789b0a932..ba7893474fa394316af4210ffffa0cd5b0726b20 100644 (file)
@@ -27,35 +27,12 @@ import hashlib
 import getopt
 import re
 
-# Wrap a stream so that output gets flushed immediately
-# and also make sure that any unicode strings get
-# encoded using "replace" before writing them.
-class SafeUnbuffered:
-    def __init__(self, stream):
-        self.stream = stream
-        self.encoding = stream.encoding
-        if self.encoding == None:
-            self.encoding = "utf-8"
-    def write(self, data):
-        if isinstance(data,str) or isinstance(data,unicode):
-            # str for Python3, unicode for Python2
-            data = data.encode(self.encoding,"replace")
-        try:
-            buffer = getattr(self.stream, 'buffer', self.stream)
-            # self.stream.buffer for Python3, self.stream for Python2
-            buffer.write(data)
-            buffer.flush()
-        except:
-            # We can do nothing if a write fails
-            raise
-    def __getattr__(self, attr):
-        return getattr(self.stream, attr)
+from utilities import SafeUnbuffered
 
 try:
-    from calibre.constants import iswindows, isosx
+    from calibre.constants import iswindows
 except:
     iswindows = sys.platform.startswith('win')
-    isosx = sys.platform.startswith('darwin')
 
 def unicode_argv():
     if iswindows:
index 094cb8c89231ab956706de5812c87da2e0d5b1a1..c4f8dd19701c5b0c24fc55eb0cf6cf7fc6374174 100644 (file)
@@ -70,35 +70,7 @@ def unpad(data, padding=16):
 
     return data[:-pad_len]
 
-# Wrap a stream so that output gets flushed immediately
-# and also make sure that any unicode strings get
-# encoded using "replace" before writing them.
-class SafeUnbuffered:
-    def __init__(self, stream):
-        self.stream = stream
-        self.encoding = stream.encoding
-        if self.encoding == None:
-            self.encoding = "utf-8"
-    def write(self, data):
-        if isinstance(data,str) or isinstance(data,unicode):
-            # str for Python3, unicode for Python2
-            data = data.encode(self.encoding,"replace")
-        try:
-            buffer = getattr(self.stream, 'buffer', self.stream)
-            # self.stream.buffer for Python3, self.stream for Python2
-            buffer.write(data)
-            buffer.flush()
-        except:
-            # We can do nothing if a write fails
-            raise
-    def __getattr__(self, attr):
-        return getattr(self.stream, attr)
-
-try:
-    from calibre.constants import iswindows, isosx
-except:
-    iswindows = sys.platform.startswith('win')
-    isosx = sys.platform.startswith('darwin')
+from utilities import SafeUnbuffered
 
 def unicode_argv():
     if iswindows:
index 7e6cd81bb8541fe0f300e5873f05e65a079bf94c..64e21f88905a7f9cf8b3f32e04f8bf7801302a98 100755 (executable)
@@ -93,29 +93,7 @@ def unpad(data, padding=16):
     return data[:-pad_len]
 
 
-# Wrap a stream so that output gets flushed immediately
-# and also make sure that any unicode strings get
-# encoded using "replace" before writing them.
-class SafeUnbuffered:
-    def __init__(self, stream):
-        self.stream = stream
-        self.encoding = stream.encoding
-        if self.encoding == None:
-            self.encoding = "utf-8"
-    def write(self, data):
-        if isinstance(data,str) or isinstance(data,unicode):
-            # str for Python3, unicode for Python2
-            data = data.encode(self.encoding,"replace")
-        try:
-            buffer = getattr(self.stream, 'buffer', self.stream)
-            # self.stream.buffer for Python3, self.stream for Python2
-            buffer.write(data)
-            buffer.flush()
-        except:
-            # We can do nothing if a write fails
-            raise
-    def __getattr__(self, attr):
-        return getattr(self.stream, attr)
+from utilities import SafeUnbuffered
 
 iswindows = sys.platform.startswith('win')
 isosx = sys.platform.startswith('darwin')
index 3ddb9e8a23c215e620f19da0310bbc9fd89dbef7..bbd229ece0178df11f8a386546dfa288f1454e31 100644 (file)
@@ -88,32 +88,7 @@ import kgenpids
 import androidkindlekey
 import kfxdedrm
 
-# Wrap a stream so that output gets flushed immediately
-# and also make sure that any unicode strings get
-# encoded using "replace" before writing them.
-class SafeUnbuffered:
-    def __init__(self, stream):
-        self.stream = stream
-        self.encoding = stream.encoding
-        if self.encoding == None:
-            self.encoding = "utf-8"
-    def write(self, data):
-        if isinstance(data,str) or isinstance(data,unicode):
-            # str for Python3, unicode for Python2
-            data = data.encode(self.encoding,"replace")
-        try:
-            buffer = getattr(self.stream, 'buffer', self.stream)
-            # self.stream.buffer for Python3, self.stream for Python2
-            buffer.write(data)
-            buffer.flush()
-        except:
-            # We can do nothing if a write fails
-            raise
-    def __getattr__(self, attr):
-        return getattr(self.stream, attr)
-
-iswindows = sys.platform.startswith('win')
-isosx = sys.platform.startswith('darwin')
+from utilities import SafeUnbuffered
 
 def unicode_argv():
     if iswindows:
index 0ce800d8a68e4a74ee28faeebb038098ca218d07..304a891e0dfaffd5763e43563f688c3653d19ba0 100644 (file)
@@ -62,29 +62,7 @@ except NameError:
 
 # Routines common to Mac and PC
 
-# Wrap a stream so that output gets flushed immediately
-# and also make sure that any unicode strings get
-# encoded using "replace" before writing them.
-class SafeUnbuffered:
-    def __init__(self, stream):
-        self.stream = stream
-        self.encoding = stream.encoding
-        if self.encoding == None:
-            self.encoding = "utf-8"
-    def write(self, data):
-        if isinstance(data,str) or isinstance(data,unicode):
-            # str for Python3, unicode for Python2
-            data = data.encode(self.encoding,"replace")
-        try:
-            buffer = getattr(self.stream, 'buffer', self.stream)
-            # self.stream.buffer for Python3, self.stream for Python2
-            buffer.write(data)
-            buffer.flush()
-        except:
-            # We can do nothing if a write fails
-            raise
-    def __getattr__(self, attr):
-        return getattr(self.stream, attr)
+from utilities import SafeUnbuffered
 
 try:
     from calibre.constants import iswindows, isosx
index ba80e9b68196ea0e7e3f2da9d5e6bfafc996cf28..aea3509aa43756b3d52b74dec493f5451d5df3ef 100644 (file)
 import sys
 import binascii
 
-# Wrap a stream so that output gets flushed immediately
-# and also make sure that any unicode strings get
-# encoded using "replace" before writing them.
-class SafeUnbuffered:
-    def __init__(self, stream):
-        self.stream = stream
-        self.encoding = stream.encoding
-        if self.encoding == None:
-            self.encoding = "utf-8"
-    def write(self, data):
-        if isinstance(data,str) or isinstance(data,unicode):
-            # str for Python3, unicode for Python2
-            data = data.encode(self.encoding,"replace")
-        try:
-            buffer = getattr(self.stream, 'buffer', self.stream)
-            # self.stream.buffer for Python3, self.stream for Python2
-            buffer.write(data)
-            buffer.flush()
-        except:
-            # We can do nothing if a write fails
-            raise
-    def __getattr__(self, attr):
-        return getattr(self.stream, attr)
-
-iswindows = sys.platform.startswith('win')
-isosx = sys.platform.startswith('darwin')
+from utilities import SafeUnbuffered
 
 def unicode_argv():
     if iswindows:
index 902edd156449cdd1e994aa2389a35bde43671207..fe761c4ae7de1113287fd1a50a4bbf6821c14b71 100755 (executable)
@@ -85,29 +85,7 @@ try:
 except:
     print("AlfCrypto not found. Using python PC1 implementation.")
 
-# Wrap a stream so that output gets flushed immediately
-# and also make sure that any unicode strings get
-# encoded using "replace" before writing them.
-class SafeUnbuffered:
-    def __init__(self, stream):
-        self.stream = stream
-        self.encoding = stream.encoding
-        if self.encoding == None:
-            self.encoding = "utf-8"
-    def write(self, data):
-        if isinstance(data,str) or isinstance(data,unicode):
-            # str for Python3, unicode for Python2
-            data = data.encode(self.encoding,"replace")
-        try:
-            buffer = getattr(self.stream, 'buffer', self.stream)
-            # self.stream.buffer for Python3, self.stream for Python2
-            buffer.write(data)
-            buffer.flush()
-        except:
-            # We can do nothing if a write fails
-            raise
-    def __getattr__(self, attr):
-        return getattr(self.stream, attr)
+from utilities import SafeUnbuffered
 
 iswindows = sys.platform.startswith('win')
 isosx = sys.platform.startswith('darwin')
index f65d25ac626f0406dd66bad953b44c1b6764e30c..3455cf3f873a66ea24b6d54889ba1268689abf6d 100644 (file)
@@ -23,33 +23,9 @@ from struct import pack
 from struct import unpack
 
 from alfcrypto import Topaz_Cipher
+from utilities import SafeUnbuffered
 
-# Wrap a stream so that output gets flushed immediately
-# and also make sure that any unicode strings get
-# encoded using "replace" before writing them.
-class SafeUnbuffered:
-    def __init__(self, stream):
-        self.stream = stream
-        self.encoding = stream.encoding
-        if self.encoding == None:
-            self.encoding = "utf-8"
-    def write(self, data):
-        if isinstance(data,str) or isinstance(data,unicode):
-            # str for Python3, unicode for Python2
-            data = data.encode(self.encoding,"replace")
-        try:
-            buffer = getattr(self.stream, 'buffer', self.stream)
-            # self.stream.buffer for Python3, self.stream for Python2
-            buffer.write(data)
-            buffer.flush()
-        except:
-            # We can do nothing if a write fails
-            raise
-    def __getattr__(self, attr):
-        return getattr(self.stream, attr)
-
-iswindows = sys.platform.startswith('win')
-isosx = sys.platform.startswith('darwin')
+from argv_utils import unicode_argv
 
 def unicode_argv():
     if iswindows:
index cd08a66571e6ad2d8eecd925cb063ba5b858f6db..5537349dc82c5cd9244989657a5857b2d371a5b5 100644 (file)
@@ -45,3 +45,29 @@ def parseCustString(keystuff):
         except:
             pass
     return userkeys
+
+
+# Wrap a stream so that output gets flushed immediately
+# and also make sure that any unicode strings get safely
+# encoded using "replace" before writing them.
+class SafeUnbuffered:
+    def __init__(self, stream):
+        self.stream = stream
+        self.encoding = stream.encoding
+        if self.encoding == None:
+            self.encoding = "utf-8"
+    def write(self, data):
+        if isinstance(data,str) or isinstance(data,unicode):
+            # str for Python3, unicode for Python2
+            data = data.encode(self.encoding,"replace")
+        try:
+            buffer = getattr(self.stream, 'buffer', self.stream)
+            # self.stream.buffer for Python3, self.stream for Python2
+            buffer.write(data)
+            buffer.flush()
+        except:
+            # We can do nothing if a write fails
+            raise
+    def __getattr__(self, attr):
+        return getattr(self.stream, attr)
+        
\ No newline at end of file