From ce57fc04497904dc92aea0237df40cc2c081604f Mon Sep 17 00:00:00 2001 From: deeplow Date: Wed, 14 Sep 2022 15:09:00 +0100 Subject: [PATCH] move get_resource_path to util.py static methods that are used application-wide should belong to the utilities python file. inspired by @gmarmstrong's PR #166 on refactoring global_common methods to be static and have a dzutil.py --- dangerzone/container.py | 2 ++ dangerzone/global_common.py | 28 ++++------------------------ dangerzone/gui/common.py | 9 ++++----- dangerzone/gui/main_window.py | 9 +++------ dangerzone/util.py | 25 +++++++++++++++++++++++++ tests/test_global_common.py | 9 --------- tests/test_util.py | 13 +++++++++++++ 7 files changed, 51 insertions(+), 44 deletions(-) create mode 100644 dangerzone/util.py create mode 100644 tests/test_util.py diff --git a/dangerzone/container.py b/dangerzone/container.py index 193d109..405d947 100644 --- a/dangerzone/container.py +++ b/dangerzone/container.py @@ -9,6 +9,8 @@ from typing import Callable, List, Optional import appdirs +from .util import get_resource_path + # What container tech is used for this platform? if platform.system() == "Linux": container_tech = "podman" diff --git a/dangerzone/global_common.py b/dangerzone/global_common.py index 57fe241..3dbe254 100644 --- a/dangerzone/global_common.py +++ b/dangerzone/global_common.py @@ -14,6 +14,7 @@ from colorama import Back, Fore, Style from .container import convert from .settings import Settings +from .util import get_resource_path log = logging.getLogger(__name__) @@ -26,7 +27,7 @@ class GlobalCommon(object): def __init__(self) -> None: # Version try: - with open(self.get_resource_path("version.txt")) as f: + with open(get_resource_path("version.txt")) as f: self.version = f.read().strip() except FileNotFoundError: # In dev mode, in Windows, get_resource_path doesn't work properly for the container, but luckily @@ -394,27 +395,6 @@ class GlobalCommon(object): raise Exception(f"{runtime_name} is not installed") return runtime - def get_resource_path(self, filename: str) -> str: - if getattr(sys, "dangerzone_dev", False): - # Look for resources directory relative to python file - project_root = pathlib.Path(__file__).parent.parent - prefix = project_root.joinpath("share") - else: - if platform.system() == "Darwin": - bin_path = pathlib.Path(sys.executable) - app_path = bin_path.parent.parent - prefix = app_path.joinpath("Resources", "share") - elif platform.system() == "Linux": - prefix = pathlib.Path(sys.prefix).joinpath("share", "dangerzone") - elif platform.system() == "Windows": - exe_path = pathlib.Path(sys.executable) - dz_install_path = exe_path.parent - prefix = dz_install_path.joinpath("share") - else: - raise NotImplementedError(f"Unsupported system {platform.system()}") - resource_path = prefix.joinpath(filename) - return str(resource_path) - def get_subprocess_startupinfo(self): # type: ignore [no-untyped-def] if platform.system() == "Windows": startupinfo = subprocess.STARTUPINFO() @@ -440,7 +420,7 @@ class GlobalCommon(object): ) chunk_size = 10240 - compressed_container_path = self.get_resource_path("container.tar.gz") + compressed_container_path = get_resource_path("container.tar.gz") with gzip.open(compressed_container_path) as f: while True: chunk = f.read(chunk_size) @@ -463,7 +443,7 @@ class GlobalCommon(object): See if the podman container is installed. Linux only. """ # Get the image id - with open(self.get_resource_path("image-id.txt")) as f: + with open(get_resource_path("image-id.txt")) as f: expected_image_id = f.read().strip() # See if this image is already installed diff --git a/dangerzone/gui/common.py b/dangerzone/gui/common.py index 0d7e8ea..956481f 100644 --- a/dangerzone/gui/common.py +++ b/dangerzone/gui/common.py @@ -19,6 +19,7 @@ elif platform.system() == "Linux": from ..global_common import GlobalCommon from ..settings import Settings +from ..util import get_resource_path log = logging.getLogger(__name__) @@ -48,9 +49,9 @@ class GuiCommon(object): def get_window_icon(self) -> QtGui.QIcon: if platform.system() == "Windows": - path = self.global_common.get_resource_path("dangerzone.ico") + path = get_resource_path("dangerzone.ico") else: - path = self.global_common.get_resource_path("icon.png") + path = get_resource_path("icon.png") return QtGui.QIcon(path) def open_pdf_viewer(self, filename: str) -> None: @@ -141,9 +142,7 @@ class Alert(QtWidgets.QDialog): logo = QtWidgets.QLabel() logo.setPixmap( - QtGui.QPixmap.fromImage( - QtGui.QImage(self.global_common.get_resource_path("icon.png")) - ) + QtGui.QPixmap.fromImage(QtGui.QImage(get_resource_path("icon.png"))) ) label = QtWidgets.QLabel() diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index 08bb7c3..5a6a5b4 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -13,6 +13,7 @@ from PySide2 import QtCore, QtGui, QtWidgets from ..common import Common from ..container import convert from ..global_common import GlobalCommon +from ..util import get_resource_path from .common import GuiCommon log = logging.getLogger(__name__) @@ -39,9 +40,7 @@ class MainWindow(QtWidgets.QMainWindow): # Header logo = QtWidgets.QLabel() logo.setPixmap( - QtGui.QPixmap.fromImage( - QtGui.QImage(self.global_common.get_resource_path("icon.png")) - ) + QtGui.QPixmap.fromImage(QtGui.QImage(get_resource_path("icon.png"))) ) header_label = QtWidgets.QLabel("dangerzone") header_label.setFont(self.gui_common.fixed_font) @@ -569,9 +568,7 @@ class ConvertWidget(QtWidgets.QWidget): # Label self.error_image = QtWidgets.QLabel() self.error_image.setPixmap( - QtGui.QPixmap.fromImage( - QtGui.QImage(self.global_common.get_resource_path("error.png")) - ) + QtGui.QPixmap.fromImage(QtGui.QImage(get_resource_path("error.png"))) ) self.error_image.hide() diff --git a/dangerzone/util.py b/dangerzone/util.py new file mode 100644 index 0000000..776d9fe --- /dev/null +++ b/dangerzone/util.py @@ -0,0 +1,25 @@ +import pathlib +import platform +import sys + + +def get_resource_path(filename: str) -> str: + if getattr(sys, "dangerzone_dev", False): + # Look for resources directory relative to python file + project_root = pathlib.Path(__file__).parent.parent + prefix = project_root.joinpath("share") + else: + if platform.system() == "Darwin": + bin_path = pathlib.Path(sys.executable) + app_path = bin_path.parent.parent + prefix = app_path.joinpath("Resources", "share") + elif platform.system() == "Linux": + prefix = pathlib.Path(sys.prefix).joinpath("share", "dangerzone") + elif platform.system() == "Windows": + exe_path = pathlib.Path(sys.executable) + dz_install_path = exe_path.parent + prefix = dz_install_path.joinpath("share") + else: + raise NotImplementedError(f"Unsupported system {platform.system()}") + resource_path = prefix.joinpath(filename) + return str(resource_path) diff --git a/tests/test_global_common.py b/tests/test_global_common.py index c1bc1af..37d4ee3 100644 --- a/tests/test_global_common.py +++ b/tests/test_global_common.py @@ -7,8 +7,6 @@ from strip_ansi import strip_ansi # type: ignore import dangerzone.global_common -VERSION_FILE_NAME = "version.txt" - @pytest.fixture def global_common(): @@ -16,13 +14,6 @@ def global_common(): class TestGlobalCommon: - def test_get_resource_path(self, global_common): - share_dir = Path("share").resolve() - resource_path = Path(global_common.get_resource_path(VERSION_FILE_NAME)).parent - assert share_dir.samefile( - resource_path - ), f"{share_dir} is not the same file as {resource_path}" - @pytest.mark.skipif(platform.system() != "Windows", reason="Windows-specific") def test_get_subprocess_startupinfo(self, global_common): startupinfo = global_common.get_subprocess_startupinfo() diff --git a/tests/test_util.py b/tests/test_util.py new file mode 100644 index 0000000..042fd07 --- /dev/null +++ b/tests/test_util.py @@ -0,0 +1,13 @@ +from pathlib import Path + +import dangerzone.util as util + +VERSION_FILE_NAME = "version.txt" + + +def test_get_resource_path(): + share_dir = Path("share").resolve() + resource_path = Path(util.get_resource_path(VERSION_FILE_NAME)).parent + assert share_dir.samefile( + resource_path + ), f"{share_dir} is not the same file as {resource_path}"