summaryrefslogtreecommitdiffstats
path: root/DeDRM_plugin/argv_utils.py
diff options
context:
space:
mode:
authorApprentice Harper <[email protected]>2020-02-16 10:12:25 +0000
committerApprentice Harper <[email protected]>2020-02-16 10:12:25 +0000
commit92bf51bc8f201a2d5b1e8b90b8dc033606dbcfb0 (patch)
tree037f09f49a7d93d6b71dc36db4fba96fa31c5603 /DeDRM_plugin/argv_utils.py
parentef3c7f261c9049e7ee861c62f308ed82c5d757fb (diff)
Remove stand-alone apps. Only support the two plugins.
Diffstat (limited to 'DeDRM_plugin/argv_utils.py')
-rw-r--r--DeDRM_plugin/argv_utils.py88
1 files changed, 88 insertions, 0 deletions
diff --git a/DeDRM_plugin/argv_utils.py b/DeDRM_plugin/argv_utils.py
new file mode 100644
index 0000000..85ffaa4
--- /dev/null
+++ b/DeDRM_plugin/argv_utils.py
@@ -0,0 +1,88 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import sys, os
+import locale
+import codecs
+
+# get sys.argv arguments and encode them into utf-8
+def unicode_argv():
+ if sys.platform.startswith('win'):
+ # 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 '?'.
+
+
+ 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
+ xrange(start, argc.value)]
+ # if we don't have any arguments at all, just pass back script name
+ # this should never happen
+ return [u"DeDRM.py"]
+ else:
+ argvencoding = sys.stdin.encoding
+ if argvencoding == None:
+ argvencoding = "utf-8"
+ return [arg if (type(arg) == unicode) else unicode(arg,argvencoding) for arg in sys.argv]
+
+
+def add_cp65001_codec():
+ try:
+ codecs.lookup('cp65001')
+ except LookupError:
+ codecs.register(
+ lambda name: name == 'cp65001' and codecs.lookup('utf-8') or None)
+ return
+
+
+def set_utf8_default_encoding():
+ if sys.getdefaultencoding() == 'utf-8':
+ return
+
+ # Regenerate setdefaultencoding.
+ reload(sys)
+ sys.setdefaultencoding('utf-8')
+
+ for attr in dir(locale):
+ if attr[0:3] != 'LC_':
+ continue
+ aref = getattr(locale, attr)
+ try:
+ locale.setlocale(aref, '')
+ except locale.Error:
+ continue
+ try:
+ lang = locale.getlocale(aref)[0]
+ except (TypeError, ValueError):
+ continue
+ if lang:
+ try:
+ locale.setlocale(aref, (lang, 'UTF-8'))
+ except locale.Error:
+ os.environ[attr] = lang + '.UTF-8'
+ try:
+ locale.setlocale(locale.LC_ALL, '')
+ except locale.Error:
+ pass
+ return
+
+