From 1545d76803da31873e3dd956b34776ca37874c36 Mon Sep 17 00:00:00 2001 From: Derek Tracy Date: Wed, 24 Feb 2021 20:54:39 -0500 Subject: [PATCH] Support Python 2.7 and Python 3 winreg imports on Windows --- DeDRM_plugin/adobekey.py | 5 ++++- DeDRM_plugin/ignoblekey.py | 5 ++++- DeDRM_plugin/kindlekey.py | 6 +++++- DeDRM_plugin/simpleprefs.py | 5 ++++- Obok_plugin/obok/legacy_obok.py | 5 ++++- Obok_plugin/obok/obok.py | 5 ++++- .../DRM_Key_Scripts/Adobe_Digital_Editions/adobekey.pyw | 5 ++++- .../DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekey.pyw | 5 ++++- .../DRM_Key_Scripts/Kindle_for_Mac_and_PC/kindlekey.pyw | 5 ++++- Other_Tools/Kobo/obok.py | 5 ++++- 10 files changed, 41 insertions(+), 10 deletions(-) diff --git a/DeDRM_plugin/adobekey.py b/DeDRM_plugin/adobekey.py index fea2533..3565cf6 100644 --- a/DeDRM_plugin/adobekey.py +++ b/DeDRM_plugin/adobekey.py @@ -117,7 +117,10 @@ if iswindows: c_long, c_ulong from ctypes.wintypes import LPVOID, DWORD, BOOL - import winreg + try: + import winreg + except ImportError: + import _winreg as winreg def _load_crypto_libcrypto(): from ctypes.util import find_library diff --git a/DeDRM_plugin/ignoblekey.py b/DeDRM_plugin/ignoblekey.py index 0c3662e..ea9785a 100644 --- a/DeDRM_plugin/ignoblekey.py +++ b/DeDRM_plugin/ignoblekey.py @@ -101,7 +101,10 @@ def getNookLogFiles(): logFiles = [] found = False if iswindows: - import winreg + try: + import winreg + except ImportError: + import _winreg as winreg # some 64 bit machines do not have the proper registry key for some reason # or the python interface to the 32 vs 64 bit registry is broken diff --git a/DeDRM_plugin/kindlekey.py b/DeDRM_plugin/kindlekey.py index 68cea6b..8321923 100644 --- a/DeDRM_plugin/kindlekey.py +++ b/DeDRM_plugin/kindlekey.py @@ -195,7 +195,11 @@ if iswindows: create_unicode_buffer, create_string_buffer, CFUNCTYPE, addressof, \ string_at, Structure, c_void_p, cast - import winreg + try: + import winreg + except ImportError: + import _winreg as winreg + MAX_PATH = 255 kernel32 = windll.kernel32 advapi32 = windll.advapi32 diff --git a/DeDRM_plugin/simpleprefs.py b/DeDRM_plugin/simpleprefs.py index 65f2b59..bf8c5d4 100644 --- a/DeDRM_plugin/simpleprefs.py +++ b/DeDRM_plugin/simpleprefs.py @@ -20,7 +20,10 @@ class SimplePrefs(object): self.file2key[filename] = key self.target = target + 'Prefs' if sys.platform.startswith('win'): - import winreg + try: + import winreg + except ImportError: + import _winreg as winreg regkey = winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\") path = winreg.QueryValueEx(regkey, 'Local AppData')[0] prefdir = path + os.sep + self.target diff --git a/Obok_plugin/obok/legacy_obok.py b/Obok_plugin/obok/legacy_obok.py index caf24f2..9a9ae5d 100644 --- a/Obok_plugin/obok/legacy_obok.py +++ b/Obok_plugin/obok/legacy_obok.py @@ -42,7 +42,10 @@ class legacy_obok(object): pwsdid = '' try: if sys.platform.startswith('win'): - import winreg + try: + import winreg + except ImportError: + import _winreg as winreg regkey_browser = winreg.OpenKey(winreg.HKEY_CURRENT_USER, 'Software\\Kobo\\Kobo Desktop Edition\\Browser') cookies = winreg.QueryValueEx(regkey_browser, 'cookies') bytearrays = cookies[0] diff --git a/Obok_plugin/obok/obok.py b/Obok_plugin/obok/obok.py index ae8ff89..f680341 100644 --- a/Obok_plugin/obok/obok.py +++ b/Obok_plugin/obok/obok.py @@ -360,7 +360,10 @@ class KoboLibrary(object): if (self.kobodir == u""): if sys.platform.startswith('win'): - import winreg + try: + import winreg + except ImportError: + import _winreg as winreg if sys.getwindowsversion().major > 5: if 'LOCALAPPDATA' in os.environ.keys(): # Python 2.x does not return unicode env. Use Python 3.x diff --git a/Other_Tools/DRM_Key_Scripts/Adobe_Digital_Editions/adobekey.pyw b/Other_Tools/DRM_Key_Scripts/Adobe_Digital_Editions/adobekey.pyw index bc33567..cbc3038 100644 --- a/Other_Tools/DRM_Key_Scripts/Adobe_Digital_Editions/adobekey.pyw +++ b/Other_Tools/DRM_Key_Scripts/Adobe_Digital_Editions/adobekey.pyw @@ -129,7 +129,10 @@ if iswindows: c_long, c_ulong from ctypes.wintypes import LPVOID, DWORD, BOOL - import winreg + try: + import winreg + except ImportError: + import _winreg as winreg def _load_crypto_libcrypto(): from ctypes.util import find_library diff --git a/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekey.pyw b/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekey.pyw index 7365c94..f6a93eb 100644 --- a/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekey.pyw +++ b/Other_Tools/DRM_Key_Scripts/Barnes_and_Noble_ePubs/ignoblekey.pyw @@ -98,7 +98,10 @@ def getNookLogFiles(): logFiles = [] found = False if iswindows: - import winreg + try: + import winreg + except ImportError: + import _winreg as winreg # some 64 bit machines do not have the proper registry key for some reason # or the python interface to the 32 vs 64 bit registry is broken diff --git a/Other_Tools/DRM_Key_Scripts/Kindle_for_Mac_and_PC/kindlekey.pyw b/Other_Tools/DRM_Key_Scripts/Kindle_for_Mac_and_PC/kindlekey.pyw index 4837627..99b8d22 100644 --- a/Other_Tools/DRM_Key_Scripts/Kindle_for_Mac_and_PC/kindlekey.pyw +++ b/Other_Tools/DRM_Key_Scripts/Kindle_for_Mac_and_PC/kindlekey.pyw @@ -177,7 +177,10 @@ if iswindows: create_unicode_buffer, create_string_buffer, CFUNCTYPE, addressof, \ string_at, Structure, c_void_p, cast - import winreg + try: + import winreg + except ImportError: + import _winreg as winreg MAX_PATH = 255 kernel32 = windll.kernel32 advapi32 = windll.advapi32 diff --git a/Other_Tools/Kobo/obok.py b/Other_Tools/Kobo/obok.py index 98608c5..6f87114 100644 --- a/Other_Tools/Kobo/obok.py +++ b/Other_Tools/Kobo/obok.py @@ -346,7 +346,10 @@ class KoboLibrary(object): if (self.kobodir == u""): # step 4. we haven't found a device with serials, so try desktop apps if sys.platform.startswith('win'): - import winreg + try: + import winreg + except ImportError: + import _winreg as winreg if sys.getwindowsversion().major > 5: if 'LOCALAPPDATA' in os.environ.keys(): # Python 2.x does not return unicode env. Use Python 3.x