mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-28 18:02:38 +02:00
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
This commit is contained in:
parent
0f4e6e9156
commit
ce57fc0449
7 changed files with 51 additions and 44 deletions
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
|
25
dangerzone/util.py
Normal file
25
dangerzone/util.py
Normal file
|
@ -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)
|
|
@ -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()
|
||||
|
|
13
tests/test_util.py
Normal file
13
tests/test_util.py
Normal file
|
@ -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}"
|
Loading…
Reference in a new issue