summaryrefslogtreecommitdiffstats
path: root/DeDRM_plugin/adobekey.py
diff options
context:
space:
mode:
Diffstat (limited to 'DeDRM_plugin/adobekey.py')
-rw-r--r--DeDRM_plugin/adobekey.py66
1 files changed, 6 insertions, 60 deletions
diff --git a/DeDRM_plugin/adobekey.py b/DeDRM_plugin/adobekey.py
index 50e2c71..4994dd0 100644
--- a/DeDRM_plugin/adobekey.py
+++ b/DeDRM_plugin/adobekey.py
@@ -45,29 +45,10 @@ import sys, os, struct, getopt
from base64 import b64decode
-# 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
+from argv_utils import unicode_argv
+
try:
from calibre.constants import iswindows, isosx
@@ -75,41 +56,6 @@ except:
iswindows = sys.platform.startswith('win')
isosx = sys.platform.startswith('darwin')
-def unicode_argv():
- if iswindows:
- # Uses shell32.GetCommandLineArgvW to get sys.argv as a list of Unicode
- # strings.
-
- # Versions 2.x of Python don't support Unicode in sys.argv on
- # Windows, with the underlying Windows API instead replacing multi-byte
- # characters with '?'. So use shell32.GetCommandLineArgvW to get sys.argv
- # as a list of Unicode strings and encode them as utf-8
-
- from ctypes import POINTER, byref, cdll, c_int, windll
- from ctypes.wintypes import LPCWSTR, LPWSTR
-
- GetCommandLineW = cdll.kernel32.GetCommandLineW
- GetCommandLineW.argtypes = []
- GetCommandLineW.restype = LPCWSTR
-
- CommandLineToArgvW = windll.shell32.CommandLineToArgvW
- CommandLineToArgvW.argtypes = [LPCWSTR, POINTER(c_int)]
- CommandLineToArgvW.restype = POINTER(LPWSTR)
-
- cmd = GetCommandLineW()
- argc = c_int(0)
- argv = CommandLineToArgvW(cmd, byref(argc))
- if argc.value > 0:
- # Remove Python executable and commands if present
- start = argc.value - len(sys.argv)
- return [argv[i] for i in
- range(start, argc.value)]
- # if we don't have any arguments at all, just pass back script name
- # this should never happen
- return ["adobekey.py"]
- else:
- argvencoding = sys.stdin.encoding or "utf-8"
- return [arg if (isinstance(arg, str) or isinstance(arg,unicode)) else str(arg, argvencoding) for arg in sys.argv]
class ADEPTError(Exception):
pass
@@ -507,7 +453,7 @@ def usage(progname):
def cli_main():
sys.stdout=SafeUnbuffered(sys.stdout)
sys.stderr=SafeUnbuffered(sys.stderr)
- argv=unicode_argv()
+ argv=unicode_argv("adobekey.py")
progname = os.path.basename(argv[0])
print("{0} v{1}\nCopyright © 2009-2020 i♥cabbages, Apprentice Harper et al.".format(progname,__version__))
@@ -585,7 +531,7 @@ def gui_main():
self.text.insert(tkinter.constants.END, text)
- argv=unicode_argv()
+ argv=unicode_argv("adobekey.py")
root = tkinter.Tk()
root.withdraw()
progpath, progname = os.path.split(argv[0])