From adcb690662f0c79b32435ca85d237dbc498c2384 Mon Sep 17 00:00:00 2001 From: Safihre Date: Mon, 4 May 2020 18:19:38 +0200 Subject: [PATCH] Move apireg to utils directory --- .travis.yml | 2 +- SABnzbd.py | 2 +- appveyor.yml | 1 + sabnzbd/__init__.py | 6 +- sabnzbd/interface.py | 15 ++--- sabnzbd/utils/apireg.py | 148 ++++++++++++++++++++++++++++++++++++++++++++++++ tests/test_win_utils.py | 2 +- util/__init__.py | 0 util/apireg.py | 148 ------------------------------------------------ 9 files changed, 160 insertions(+), 164 deletions(-) create mode 100644 sabnzbd/utils/apireg.py delete mode 100644 util/__init__.py delete mode 100644 util/apireg.py diff --git a/.travis.yml b/.travis.yml index 51130c0..95e18b9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -43,6 +43,7 @@ install: fi; - python3 --version - python3 -m pip install --upgrade pip + - python3 -m pip install --upgrade wheel - python3 -m pip install --upgrade -r requirements.txt - python3 -m pip install --upgrade -r tests/requirements.txt @@ -55,7 +56,6 @@ script: scripts tools tests - util sabnzbd/utils sabnzbd/__init__.py sabnzbd/cfg.py diff --git a/SABnzbd.py b/SABnzbd.py index 9e4b054..ed249c8 100755 --- a/SABnzbd.py +++ b/SABnzbd.py @@ -79,7 +79,7 @@ try: import win32ts import pywintypes win32api.SetConsoleCtrlHandler(sabnzbd.sig_handler, True) - from util.apireg import get_connection_info, set_connection_info, del_connection_info + from sabnzbd.utils.apireg import get_connection_info, set_connection_info, del_connection_info except ImportError: if sabnzbd.WIN32: print("Sorry, requires Python module PyWin32.") diff --git a/appveyor.yml b/appveyor.yml index b45635c..f553142 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -17,6 +17,7 @@ install: - "SET PATH=%PYTHON%;%PYTHON%\\Scripts;%PATH%" - python --version - python -m pip install --upgrade pip + - python -m pip install --upgrade wheel - python -m pip install --upgrade -r requirements.txt - python -m pip install --upgrade -r tests/requirements.txt diff --git a/sabnzbd/__init__.py b/sabnzbd/__init__.py index 2e7b06d..478040a 100644 --- a/sabnzbd/__init__.py +++ b/sabnzbd/__init__.py @@ -41,7 +41,7 @@ KERNEL32 = None if os.name == "nt": WIN32 = True - from util.apireg import del_connection_info + from sabnzbd.utils.apireg import del_connection_info try: import ctypes @@ -168,7 +168,7 @@ COMPLETE_DIR_SPEED = 0 INTERNET_BANDWIDTH = 0 # Rendering of original command line arguments in Config -CMDLINE = " ".join(["\"%s\"" % arg for arg in sys.argv]) +CMDLINE = " ".join(['"%s"' % arg for arg in sys.argv]) __INITIALIZED__ = False __SHUTTING_DOWN__ = False @@ -189,8 +189,6 @@ def sig_handler(signum=None, frame=None): sabnzbd.zconfig.remove_server() finally: if sabnzbd.WIN32: - from util.apireg import del_connection_info - del_connection_info() if sabnzbd.WINTRAY: sabnzbd.WINTRAY.terminate = True diff --git a/sabnzbd/interface.py b/sabnzbd/interface.py index 0f87262..a11cbf4 100644 --- a/sabnzbd/interface.py +++ b/sabnzbd/interface.py @@ -25,7 +25,6 @@ from datetime import datetime import cherrypy import logging import urllib.request, urllib.parse, urllib.error -import json import re import hashlib import socket @@ -59,17 +58,15 @@ from sabnzbd.utils.diskspeed import diskspeedmeasure from sabnzbd.utils.getperformance import getpystone from sabnzbd.utils.internetspeed import internetspeed -from sabnzbd.constants import NORMAL_PRIORITY, MEBI, DEF_SKIN_COLORS, \ +from sabnzbd.constants import MEBI, DEF_SKIN_COLORS, \ DEF_STDCONFIG, DEF_MAIN_TMPL, DEFAULT_PRIORITY, CHEETAH_DIRECTIVES from sabnzbd.lang import list_languages from sabnzbd.api import list_scripts, list_cats, del_from_section, \ - api_handler, build_queue, build_status, \ - retry_job, retry_all_jobs, build_header, build_history, del_job_files, \ - format_bytes, report, del_hist_job, Ttemplate, build_queue_header, \ - _api_test_email, _api_test_notif - + api_handler, build_queue, build_status, retry_job, retry_all_jobs, \ + build_header, build_history, format_bytes, report, del_hist_job, Ttemplate, \ + build_queue_header ############################################################################## # Global constants @@ -506,8 +503,8 @@ class Wizard: def index(self, **kwargs): """ Show the language selection page """ if sabnzbd.WIN32: - import util.apireg - cfg.language.set(util.apireg.get_install_lng()) + from sabnzbd.utils.apireg import get_install_lng + cfg.language.set(get_install_lng()) logging.debug('Installer language code "%s"', cfg.language()) info = build_header(sabnzbd.WIZARD_DIR) diff --git a/sabnzbd/utils/apireg.py b/sabnzbd/utils/apireg.py new file mode 100644 index 0000000..5c169d4 --- /dev/null +++ b/sabnzbd/utils/apireg.py @@ -0,0 +1,148 @@ +#!/usr/bin/python3 -OO +# Copyright 2012-2020 The SABnzbd-Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +""" +util.apireg - Registration of API connection info +""" + +import winreg + + +def reg_info(user): + """ Return the reg key for API """ + if user: + # Normally use the USER part of the registry + section = winreg.HKEY_CURRENT_USER + keypath = r"Software\SABnzbd" + else: + # A Windows Service will use the service key instead + section = winreg.HKEY_LOCAL_MACHINE + keypath = r"SYSTEM\CurrentControlSet\Services\SABnzbd" + return section, keypath + + +def get_connection_info(user=True): + """ Return URL of the API running SABnzbd instance + 'user' == True will first try user's registry, otherwise system is used + """ + section, keypath = reg_info(user) + url = None + + try: + hive = winreg.ConnectRegistry(None, section) + key = winreg.OpenKey(hive, keypath + r"\api") + for i in range(0, winreg.QueryInfoKey(key)[1]): + name, value, val_type = winreg.EnumValue(key, i) + if name == "url": + url = value + + winreg.CloseKey(key) + except WindowsError: + pass + finally: + winreg.CloseKey(hive) + + # Nothing in user's registry, try system registry + if user and not url: + url = get_connection_info(user=False) + + return url + + +def set_connection_info(url, user=True): + """ Set API info in register """ + section, keypath = reg_info(user) + try: + hive = winreg.ConnectRegistry(None, section) + try: + winreg.CreateKey(hive, keypath) + except OSError: + pass + key = winreg.OpenKey(hive, keypath) + mykey = winreg.CreateKey(key, "api") + winreg.SetValueEx(mykey, "url", None, winreg.REG_SZ, url) + winreg.CloseKey(mykey) + winreg.CloseKey(key) + except WindowsError: + if user: + set_connection_info(url, user=False) + finally: + winreg.CloseKey(hive) + + +def del_connection_info(user=True): + """ Remove API info from register """ + section, keypath = reg_info(user) + try: + hive = winreg.ConnectRegistry(None, section) + key = winreg.OpenKey(hive, keypath) + winreg.DeleteKey(key, "api") + winreg.CloseKey(key) + except WindowsError: + if user: + del_connection_info(user=False) + finally: + winreg.CloseKey(hive) + + +def get_install_lng(): + """ Return language-code used by the installer """ + lng = 0 + try: + hive = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) + key = winreg.OpenKey(hive, r"Software\SABnzbd") + for i in range(0, winreg.QueryInfoKey(key)[1]): + name, value, val_type = winreg.EnumValue(key, i) + if name == "Installer Language": + lng = value + winreg.CloseKey(key) + except WindowsError: + pass + finally: + winreg.CloseKey(hive) + + if lng in LanguageMap: + return LanguageMap[lng] + return "en" + + +# Map from NSIS-codepage to our language-strings +LanguageMap = { + "1033": "en", + "1036": "fr", + "1031": "de", + "1043": "nl", + "1035": "fi", + "1045": "pl", + "1053": "sv", + "1030": "da", + "2068": "nb", + "1048": "ro", + "1034": "es", + "1046": "pr_BR", + "3098": "sr", + "1037": "he", + "1049": "ru", + "2052": "zh_CN", +} + + +if __name__ == "__main__": + print("URL = %s" % get_connection_info()) + print("Language = %s" % get_install_lng()) + # del_connection_info() + # set_connection_info('localhost', '8080', 'blabla', user=False) diff --git a/tests/test_win_utils.py b/tests/test_win_utils.py index b16e3da..be1fecc 100644 --- a/tests/test_win_utils.py +++ b/tests/test_win_utils.py @@ -25,7 +25,7 @@ import pytest if not sys.platform.startswith("win"): pytest.skip("Skipping Windows-only tests", allow_module_level=True) -import util.apireg as ar +import sabnzbd.utils.apireg as ar class TestAPIReg: diff --git a/util/__init__.py b/util/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/util/apireg.py b/util/apireg.py deleted file mode 100644 index 5c169d4..0000000 --- a/util/apireg.py +++ /dev/null @@ -1,148 +0,0 @@ -#!/usr/bin/python3 -OO -# Copyright 2012-2020 The SABnzbd-Team -# -# This program is free software; you can redistribute it and/or -# modify it under the terms of the GNU General Public License -# as published by the Free Software Foundation; either version 2 -# of the License, or (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program; if not, write to the Free Software -# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - -""" -util.apireg - Registration of API connection info -""" - -import winreg - - -def reg_info(user): - """ Return the reg key for API """ - if user: - # Normally use the USER part of the registry - section = winreg.HKEY_CURRENT_USER - keypath = r"Software\SABnzbd" - else: - # A Windows Service will use the service key instead - section = winreg.HKEY_LOCAL_MACHINE - keypath = r"SYSTEM\CurrentControlSet\Services\SABnzbd" - return section, keypath - - -def get_connection_info(user=True): - """ Return URL of the API running SABnzbd instance - 'user' == True will first try user's registry, otherwise system is used - """ - section, keypath = reg_info(user) - url = None - - try: - hive = winreg.ConnectRegistry(None, section) - key = winreg.OpenKey(hive, keypath + r"\api") - for i in range(0, winreg.QueryInfoKey(key)[1]): - name, value, val_type = winreg.EnumValue(key, i) - if name == "url": - url = value - - winreg.CloseKey(key) - except WindowsError: - pass - finally: - winreg.CloseKey(hive) - - # Nothing in user's registry, try system registry - if user and not url: - url = get_connection_info(user=False) - - return url - - -def set_connection_info(url, user=True): - """ Set API info in register """ - section, keypath = reg_info(user) - try: - hive = winreg.ConnectRegistry(None, section) - try: - winreg.CreateKey(hive, keypath) - except OSError: - pass - key = winreg.OpenKey(hive, keypath) - mykey = winreg.CreateKey(key, "api") - winreg.SetValueEx(mykey, "url", None, winreg.REG_SZ, url) - winreg.CloseKey(mykey) - winreg.CloseKey(key) - except WindowsError: - if user: - set_connection_info(url, user=False) - finally: - winreg.CloseKey(hive) - - -def del_connection_info(user=True): - """ Remove API info from register """ - section, keypath = reg_info(user) - try: - hive = winreg.ConnectRegistry(None, section) - key = winreg.OpenKey(hive, keypath) - winreg.DeleteKey(key, "api") - winreg.CloseKey(key) - except WindowsError: - if user: - del_connection_info(user=False) - finally: - winreg.CloseKey(hive) - - -def get_install_lng(): - """ Return language-code used by the installer """ - lng = 0 - try: - hive = winreg.ConnectRegistry(None, winreg.HKEY_CURRENT_USER) - key = winreg.OpenKey(hive, r"Software\SABnzbd") - for i in range(0, winreg.QueryInfoKey(key)[1]): - name, value, val_type = winreg.EnumValue(key, i) - if name == "Installer Language": - lng = value - winreg.CloseKey(key) - except WindowsError: - pass - finally: - winreg.CloseKey(hive) - - if lng in LanguageMap: - return LanguageMap[lng] - return "en" - - -# Map from NSIS-codepage to our language-strings -LanguageMap = { - "1033": "en", - "1036": "fr", - "1031": "de", - "1043": "nl", - "1035": "fi", - "1045": "pl", - "1053": "sv", - "1030": "da", - "2068": "nb", - "1048": "ro", - "1034": "es", - "1046": "pr_BR", - "3098": "sr", - "1037": "he", - "1049": "ru", - "2052": "zh_CN", -} - - -if __name__ == "__main__": - print("URL = %s" % get_connection_info()) - print("Language = %s" % get_install_lng()) - # del_connection_info() - # set_connection_info('localhost', '8080', 'blabla', user=False)