mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-30 10:42:37 +02:00
Refactor GlobalCommon methods to be static
This commit is contained in:
parent
616ba55db7
commit
30065371e3
4 changed files with 45 additions and 39 deletions
|
@ -1,6 +1,11 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
import appdirs
|
||||||
|
|
||||||
|
CONTAINER_NAME = "dangerzone.rocks/dangerzone"
|
||||||
|
APPDATA_PATH = appdirs.user_config_dir("dangerzone")
|
||||||
|
|
||||||
if "DANGERZONE_MODE" in os.environ:
|
if "DANGERZONE_MODE" in os.environ:
|
||||||
mode = os.environ["DANGERZONE_MODE"]
|
mode = os.environ["DANGERZONE_MODE"]
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -1,17 +1,15 @@
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
import inspect
|
import inspect
|
||||||
import appdirs
|
|
||||||
import platform
|
import platform
|
||||||
import subprocess
|
import subprocess
|
||||||
import shutil
|
import shutil
|
||||||
import json
|
|
||||||
import gzip
|
import gzip
|
||||||
import colorama
|
import colorama
|
||||||
from colorama import Fore, Back, Style
|
from colorama import Fore, Back, Style
|
||||||
|
|
||||||
|
from . import CONTAINER_NAME
|
||||||
from .settings import Settings
|
from .settings import Settings
|
||||||
from .container import convert
|
|
||||||
|
|
||||||
|
|
||||||
class GlobalCommon(object):
|
class GlobalCommon(object):
|
||||||
|
@ -32,12 +30,6 @@ class GlobalCommon(object):
|
||||||
# Initialize terminal colors
|
# Initialize terminal colors
|
||||||
colorama.init(autoreset=True)
|
colorama.init(autoreset=True)
|
||||||
|
|
||||||
# App data folder
|
|
||||||
self.appdata_path = appdirs.user_config_dir("dangerzone")
|
|
||||||
|
|
||||||
# Container
|
|
||||||
self.container_name = "dangerzone.rocks/dangerzone"
|
|
||||||
|
|
||||||
# Languages supported by tesseract
|
# Languages supported by tesseract
|
||||||
self.ocr_languages = {
|
self.ocr_languages = {
|
||||||
"Afrikaans": "ar",
|
"Afrikaans": "ar",
|
||||||
|
@ -203,7 +195,7 @@ class GlobalCommon(object):
|
||||||
}
|
}
|
||||||
|
|
||||||
# Load settings
|
# Load settings
|
||||||
self.settings = Settings(self)
|
self.settings = Settings()
|
||||||
|
|
||||||
def display_banner(self):
|
def display_banner(self):
|
||||||
"""
|
"""
|
||||||
|
@ -380,13 +372,15 @@ class GlobalCommon(object):
|
||||||
)
|
)
|
||||||
print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯")
|
print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯")
|
||||||
|
|
||||||
def get_container_runtime(self):
|
@staticmethod
|
||||||
|
def get_container_runtime():
|
||||||
if platform.system() == "Linux":
|
if platform.system() == "Linux":
|
||||||
return shutil.which("podman")
|
return shutil.which("podman")
|
||||||
else:
|
else:
|
||||||
return shutil.which("docker")
|
return shutil.which("docker")
|
||||||
|
|
||||||
def get_resource_path(self, filename):
|
@staticmethod
|
||||||
|
def get_resource_path(filename):
|
||||||
if getattr(sys, "dangerzone_dev", False):
|
if getattr(sys, "dangerzone_dev", False):
|
||||||
# Look for resources directory relative to python file
|
# Look for resources directory relative to python file
|
||||||
prefix = os.path.join(
|
prefix = os.path.join(
|
||||||
|
@ -411,7 +405,8 @@ class GlobalCommon(object):
|
||||||
resource_path = os.path.join(prefix, filename)
|
resource_path = os.path.join(prefix, filename)
|
||||||
return resource_path
|
return resource_path
|
||||||
|
|
||||||
def get_subprocess_startupinfo(self):
|
@staticmethod
|
||||||
|
def get_subprocess_startupinfo():
|
||||||
if platform.system() == "Windows":
|
if platform.system() == "Windows":
|
||||||
startupinfo = subprocess.STARTUPINFO()
|
startupinfo = subprocess.STARTUPINFO()
|
||||||
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
||||||
|
@ -419,24 +414,25 @@ class GlobalCommon(object):
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def install_container(self):
|
@staticmethod
|
||||||
|
def install_container():
|
||||||
"""
|
"""
|
||||||
Make sure the podman container is installed. Linux only.
|
Make sure the podman container is installed. Linux only.
|
||||||
"""
|
"""
|
||||||
if self.is_container_installed():
|
if GlobalCommon.is_container_installed():
|
||||||
return
|
return
|
||||||
|
|
||||||
# Load the container into podman
|
# Load the container into podman
|
||||||
print("Installing Dangerzone container image...")
|
print("Installing Dangerzone container image...")
|
||||||
|
|
||||||
p = subprocess.Popen(
|
p = subprocess.Popen(
|
||||||
[self.get_container_runtime(), "load"],
|
[GlobalCommon.get_container_runtime(), "load"],
|
||||||
stdin=subprocess.PIPE,
|
stdin=subprocess.PIPE,
|
||||||
startupinfo=self.get_subprocess_startupinfo(),
|
startupinfo=GlobalCommon.get_subprocess_startupinfo(),
|
||||||
)
|
)
|
||||||
|
|
||||||
chunk_size = 10240
|
chunk_size = 10240
|
||||||
compressed_container_path = self.get_resource_path("container.tar.gz")
|
compressed_container_path = GlobalCommon.get_resource_path("container.tar.gz")
|
||||||
with gzip.open(compressed_container_path) as f:
|
with gzip.open(compressed_container_path) as f:
|
||||||
while True:
|
while True:
|
||||||
chunk = f.read(chunk_size)
|
chunk = f.read(chunk_size)
|
||||||
|
@ -446,34 +442,35 @@ class GlobalCommon(object):
|
||||||
break
|
break
|
||||||
p.communicate()
|
p.communicate()
|
||||||
|
|
||||||
if not self.is_container_installed():
|
if not GlobalCommon.is_container_installed():
|
||||||
print("Failed to install the container image")
|
print("Failed to install the container image")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
print("Container image installed")
|
print("Container image installed")
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def is_container_installed(self):
|
@staticmethod
|
||||||
|
def is_container_installed():
|
||||||
"""
|
"""
|
||||||
See if the podman container is installed. Linux only.
|
See if the podman container is installed. Linux only.
|
||||||
"""
|
"""
|
||||||
# Get the image id
|
# Get the image id
|
||||||
with open(self.get_resource_path("image-id.txt")) as f:
|
with open(GlobalCommon.get_resource_path("image-id.txt")) as f:
|
||||||
expected_image_id = f.read().strip()
|
expected_image_id = f.read().strip()
|
||||||
|
|
||||||
# See if this image is already installed
|
# See if this image is already installed
|
||||||
installed = False
|
installed = False
|
||||||
found_image_id = subprocess.check_output(
|
found_image_id = subprocess.check_output(
|
||||||
[
|
[
|
||||||
self.get_container_runtime(),
|
GlobalCommon.get_container_runtime(),
|
||||||
"image",
|
"image",
|
||||||
"list",
|
"list",
|
||||||
"--format",
|
"--format",
|
||||||
"{{.ID}}",
|
"{{.ID}}",
|
||||||
self.container_name,
|
CONTAINER_NAME,
|
||||||
],
|
],
|
||||||
text=True,
|
text=True,
|
||||||
startupinfo=self.get_subprocess_startupinfo(),
|
startupinfo=GlobalCommon.get_subprocess_startupinfo(),
|
||||||
)
|
)
|
||||||
found_image_id = found_image_id.strip()
|
found_image_id = found_image_id.strip()
|
||||||
|
|
||||||
|
@ -486,8 +483,8 @@ class GlobalCommon(object):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
subprocess.check_output(
|
subprocess.check_output(
|
||||||
[self.get_container_runtime(), "rmi", "--force", found_image_id],
|
[GlobalCommon.get_container_runtime(), "rmi", "--force", found_image_id],
|
||||||
startupinfo=self.get_subprocess_startupinfo(),
|
startupinfo=GlobalCommon.get_subprocess_startupinfo(),
|
||||||
)
|
)
|
||||||
except:
|
except:
|
||||||
print("Couldn't delete old container image, so leaving it there")
|
print("Couldn't delete old container image, so leaving it there")
|
||||||
|
|
|
@ -17,8 +17,6 @@ elif platform.system() == "Linux":
|
||||||
import getpass
|
import getpass
|
||||||
from xdg.DesktopEntry import DesktopEntry # type: ignore
|
from xdg.DesktopEntry import DesktopEntry # type: ignore
|
||||||
|
|
||||||
from ..settings import Settings
|
|
||||||
|
|
||||||
|
|
||||||
class GuiCommon(object):
|
class GuiCommon(object):
|
||||||
"""
|
"""
|
||||||
|
@ -41,11 +39,12 @@ class GuiCommon(object):
|
||||||
# Are we done waiting (for Docker Desktop to be installed, or for container to install)
|
# Are we done waiting (for Docker Desktop to be installed, or for container to install)
|
||||||
self.is_waiting_finished = False
|
self.is_waiting_finished = False
|
||||||
|
|
||||||
def get_window_icon(self):
|
@staticmethod
|
||||||
|
def get_window_icon():
|
||||||
if platform.system() == "Windows":
|
if platform.system() == "Windows":
|
||||||
path = self.global_common.get_resource_path("dangerzone.ico")
|
path = GlobalCommon.get_resource_path("dangerzone.ico")
|
||||||
else:
|
else:
|
||||||
path = self.global_common.get_resource_path("icon.png")
|
path = GlobalCommon.get_resource_path("icon.png")
|
||||||
return QtGui.QIcon(path)
|
return QtGui.QIcon(path)
|
||||||
|
|
||||||
def open_pdf_viewer(self, filename: str):
|
def open_pdf_viewer(self, filename: str):
|
||||||
|
@ -111,7 +110,12 @@ class GuiCommon(object):
|
||||||
|
|
||||||
class Alert(QtWidgets.QDialog):
|
class Alert(QtWidgets.QDialog):
|
||||||
def __init__(
|
def __init__(
|
||||||
self, gui_common, global_common, message, ok_text="Ok", extra_button_text=None
|
self,
|
||||||
|
gui_common: GuiCommon,
|
||||||
|
global_common: GlobalCommon,
|
||||||
|
message: str,
|
||||||
|
ok_text="Ok",
|
||||||
|
extra_button_text=None,
|
||||||
):
|
):
|
||||||
super(Alert, self).__init__()
|
super(Alert, self).__init__()
|
||||||
self.global_common = global_common
|
self.global_common = global_common
|
||||||
|
@ -121,8 +125,8 @@ class Alert(QtWidgets.QDialog):
|
||||||
self.setWindowIcon(self.gui_common.get_window_icon())
|
self.setWindowIcon(self.gui_common.get_window_icon())
|
||||||
self.setModal(True)
|
self.setModal(True)
|
||||||
|
|
||||||
flags = (
|
flags = ( # TODO Mypy: unsupported left operand type for | ("WindowType")
|
||||||
QtCore.Qt.CustomizeWindowHint
|
QtCore.Qt.CustomizeWindowHint # type: ignore
|
||||||
| QtCore.Qt.WindowTitleHint
|
| QtCore.Qt.WindowTitleHint
|
||||||
| QtCore.Qt.WindowSystemMenuHint
|
| QtCore.Qt.WindowSystemMenuHint
|
||||||
| QtCore.Qt.WindowCloseButtonHint
|
| QtCore.Qt.WindowCloseButtonHint
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import appdirs
|
|
||||||
|
from dangerzone import APPDATA_PATH
|
||||||
|
|
||||||
|
|
||||||
class Settings:
|
class Settings:
|
||||||
def __init__(self, common):
|
def __init__(self):
|
||||||
self.common = common
|
self.settings_filename = os.path.join(APPDATA_PATH, "settings.json")
|
||||||
self.settings_filename = os.path.join(self.common.appdata_path, "settings.json")
|
|
||||||
self.default_settings = {
|
self.default_settings = {
|
||||||
"save": True,
|
"save": True,
|
||||||
"ocr": True,
|
"ocr": True,
|
||||||
|
@ -47,6 +47,6 @@ class Settings:
|
||||||
self.save()
|
self.save()
|
||||||
|
|
||||||
def save(self):
|
def save(self):
|
||||||
os.makedirs(self.common.appdata_path, exist_ok=True)
|
os.makedirs(APPDATA_PATH, exist_ok=True)
|
||||||
with open(self.settings_filename, "w") as settings_file:
|
with open(self.settings_filename, "w") as settings_file:
|
||||||
json.dump(self.settings, settings_file, indent=4)
|
json.dump(self.settings, settings_file, indent=4)
|
||||||
|
|
Loading…
Reference in a new issue