From de23b5c221d89a047807910edbc561992b1a88e3 Mon Sep 17 00:00:00 2001 From: NoDRM Date: Sat, 6 Aug 2022 20:09:30 +0200 Subject: [PATCH] Move SafeUnbuffered to own Python file --- DeDRM_plugin/__init__.py | 25 ++-------------------- DeDRM_plugin/convert2xml.py | 24 +-------------------- DeDRM_plugin/erdr2pml.py | 24 +-------------------- DeDRM_plugin/genbook.py | 24 +-------------------- DeDRM_plugin/ignoblekeyGenPassHash.py | 30 +-------------------------- DeDRM_plugin/ignoblekeyNookStudy.py | 27 ++---------------------- DeDRM_plugin/ineptepub.py | 30 +-------------------------- DeDRM_plugin/ineptpdf.py | 24 +-------------------- DeDRM_plugin/k4mobidedrm.py | 27 +----------------------- DeDRM_plugin/kindlekey.py | 24 +-------------------- DeDRM_plugin/kindlepid.py | 27 +----------------------- DeDRM_plugin/mobidedrm.py | 24 +-------------------- DeDRM_plugin/topazextract.py | 28 ++----------------------- DeDRM_plugin/utilities.py | 26 +++++++++++++++++++++++ 14 files changed, 42 insertions(+), 322 deletions(-) diff --git a/DeDRM_plugin/__init__.py b/DeDRM_plugin/__init__.py index ba9e2ec..eaf505a 100644 --- a/DeDRM_plugin/__init__.py +++ b/DeDRM_plugin/__init__.py @@ -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 diff --git a/DeDRM_plugin/convert2xml.py b/DeDRM_plugin/convert2xml.py index fe33eca..5cb9adb 100644 --- a/DeDRM_plugin/convert2xml.py +++ b/DeDRM_plugin/convert2xml.py @@ -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 diff --git a/DeDRM_plugin/erdr2pml.py b/DeDRM_plugin/erdr2pml.py index 6a20fdb..df25f86 100755 --- a/DeDRM_plugin/erdr2pml.py +++ b/DeDRM_plugin/erdr2pml.py @@ -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') diff --git a/DeDRM_plugin/genbook.py b/DeDRM_plugin/genbook.py index 21186b8..b0624fd 100644 --- a/DeDRM_plugin/genbook.py +++ b/DeDRM_plugin/genbook.py @@ -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 diff --git a/DeDRM_plugin/ignoblekeyGenPassHash.py b/DeDRM_plugin/ignoblekeyGenPassHash.py index e1cdba0..7d3bec7 100644 --- a/DeDRM_plugin/ignoblekeyGenPassHash.py +++ b/DeDRM_plugin/ignoblekeyGenPassHash.py @@ -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: diff --git a/DeDRM_plugin/ignoblekeyNookStudy.py b/DeDRM_plugin/ignoblekeyNookStudy.py index fd85660..ba78934 100644 --- a/DeDRM_plugin/ignoblekeyNookStudy.py +++ b/DeDRM_plugin/ignoblekeyNookStudy.py @@ -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: diff --git a/DeDRM_plugin/ineptepub.py b/DeDRM_plugin/ineptepub.py index 094cb8c..c4f8dd1 100644 --- a/DeDRM_plugin/ineptepub.py +++ b/DeDRM_plugin/ineptepub.py @@ -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: diff --git a/DeDRM_plugin/ineptpdf.py b/DeDRM_plugin/ineptpdf.py index 7e6cd81..64e21f8 100755 --- a/DeDRM_plugin/ineptpdf.py +++ b/DeDRM_plugin/ineptpdf.py @@ -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') diff --git a/DeDRM_plugin/k4mobidedrm.py b/DeDRM_plugin/k4mobidedrm.py index 3ddb9e8..bbd229e 100644 --- a/DeDRM_plugin/k4mobidedrm.py +++ b/DeDRM_plugin/k4mobidedrm.py @@ -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: diff --git a/DeDRM_plugin/kindlekey.py b/DeDRM_plugin/kindlekey.py index 0ce800d..304a891 100644 --- a/DeDRM_plugin/kindlekey.py +++ b/DeDRM_plugin/kindlekey.py @@ -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 diff --git a/DeDRM_plugin/kindlepid.py b/DeDRM_plugin/kindlepid.py index ba80e9b..aea3509 100644 --- a/DeDRM_plugin/kindlepid.py +++ b/DeDRM_plugin/kindlepid.py @@ -16,32 +16,7 @@ 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: diff --git a/DeDRM_plugin/mobidedrm.py b/DeDRM_plugin/mobidedrm.py index 902edd1..fe761c4 100755 --- a/DeDRM_plugin/mobidedrm.py +++ b/DeDRM_plugin/mobidedrm.py @@ -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') diff --git a/DeDRM_plugin/topazextract.py b/DeDRM_plugin/topazextract.py index f65d25a..3455cf3 100644 --- a/DeDRM_plugin/topazextract.py +++ b/DeDRM_plugin/topazextract.py @@ -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: diff --git a/DeDRM_plugin/utilities.py b/DeDRM_plugin/utilities.py index cd08a66..5537349 100644 --- a/DeDRM_plugin/utilities.py +++ b/DeDRM_plugin/utilities.py @@ -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