From ca5d17571b91be1569c9b9fd151cc612d75c1f5e Mon Sep 17 00:00:00 2001 From: Guthrie McAfee Armstrong Date: Mon, 6 Jun 2022 21:35:27 -0400 Subject: [PATCH] Remove GlobalCommon, convert its methods (all static) to functions --- dangerzone/cli.py | 6 +- dangerzone/global_common.py | 502 ++++++++++---------- dangerzone/gui/__init__.py | 2 +- dangerzone/gui/{common.py => gui_common.py} | 0 dangerzone/gui/main_window.py | 4 +- 5 files changed, 254 insertions(+), 260 deletions(-) rename dangerzone/gui/{common.py => gui_common.py} (100%) diff --git a/dangerzone/cli.py b/dangerzone/cli.py index a68b813..2f20e1c 100644 --- a/dangerzone/cli.py +++ b/dangerzone/cli.py @@ -5,7 +5,7 @@ import click import colorama from colorama import Fore, Style # type: ignore -from .global_common import GlobalCommon +from .global_common import display_banner, install_container from .common import Common from .container import convert import dangerzone.util as dzutil @@ -23,7 +23,7 @@ def print_header(s): def cli_main(output_filename, ocr_lang, filename): colorama.init(autoreset=True) common = Common() - GlobalCommon.display_banner() + display_banner() # Validate filename valid = True @@ -85,7 +85,7 @@ def cli_main(output_filename, ocr_lang, filename): return # Ensure container is installed - GlobalCommon.install_container() + install_container() # Convert the document print_header("Converting document to safe PDF") diff --git a/dangerzone/global_common.py b/dangerzone/global_common.py index 305841c..f190c2d 100644 --- a/dangerzone/global_common.py +++ b/dangerzone/global_common.py @@ -6,260 +6,254 @@ import dangerzone import dangerzone.util as dzutil -class GlobalCommon(object): +def is_container_installed(): """ - The GlobalCommon class contains functionality shared throughout the app + See if the podman container is installed. Linux only. + """ + # Get the image id + with open(dzutil.get_resource_path("image-id.txt")) as f: + expected_image_id = f.read().strip() + + # See if this image is already installed + installed = False + found_image_id = subprocess.check_output( + [ + dzutil.CONTAINER_RUNTIME, + "image", + "list", + "--format", + "{{.ID}}", + dangerzone.util.CONTAINER_NAME, + ], + text=True, + startupinfo=dzutil.get_subprocess_startupinfo(), + ) + found_image_id = found_image_id.strip() + + if found_image_id == expected_image_id: + installed = True + elif found_image_id == "": + pass + else: + print("Deleting old dangerzone container image") + + try: + subprocess.check_output( + [dzutil.CONTAINER_RUNTIME, "rmi", "--force", found_image_id], + startupinfo=dzutil.get_subprocess_startupinfo(), + ) + except: + print("Couldn't delete old container image, so leaving it there") + + return installed + + +def install_container(): + """ + Make sure the podman container is installed. Linux only. + """ + if is_container_installed(): + return + + # Load the container into podman + print("Installing Dangerzone container image...") + + p = subprocess.Popen( + [dzutil.CONTAINER_RUNTIME, "load"], + stdin=subprocess.PIPE, + startupinfo=dzutil.get_subprocess_startupinfo(), + ) + + chunk_size = 10240 + compressed_container_path = dzutil.get_resource_path("container.tar.gz") + with gzip.open(compressed_container_path) as f: + while True: + chunk = f.read(chunk_size) + if len(chunk) > 0: + p.stdin.write(chunk) + else: + break + p.communicate() + + if not is_container_installed(): + print("Failed to install the container image") + return False + + print("Container image installed") + return True + + +def display_banner(): + """ + Raw ASCII art example: + ╭──────────────────────────╮ + │ ▄██▄ │ + │ ██████ │ + │ ███▀▀▀██ │ + │ ███ ████ │ + │ ███ ██████ │ + │ ███ ▀▀▀▀████ │ + │ ███████ ▄██████ │ + │ ███████ ▄█████████ │ + │ ████████████████████ │ + │ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ │ + │ │ + │ Dangerzone v0.1.5 │ + │ https://dangerzone.rocks │ + ╰──────────────────────────╯ """ - @staticmethod - def display_banner(): - """ - Raw ASCII art example: - ╭──────────────────────────╮ - │ ▄██▄ │ - │ ██████ │ - │ ███▀▀▀██ │ - │ ███ ████ │ - │ ███ ██████ │ - │ ███ ▀▀▀▀████ │ - │ ███████ ▄██████ │ - │ ███████ ▄█████████ │ - │ ████████████████████ │ - │ ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ │ - │ │ - │ Dangerzone v0.1.5 │ - │ https://dangerzone.rocks │ - ╰──────────────────────────╯ - """ - - print(Back.BLACK + Fore.YELLOW + Style.DIM + "╭──────────────────────────╮") - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ▄██▄ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ██████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███▀▀▀██ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███ ████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███ ██████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███ ▀▀▀▀████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███████ ▄██████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ███████ ▄█████████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ████████████████████ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Fore.LIGHTYELLOW_EX - + Style.NORMAL - + " ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print(Back.BLACK + Fore.YELLOW + Style.DIM + "│ │") - left_spaces = (15 - len(dzutil.VERSION) - 1) // 2 - right_spaces = left_spaces - if left_spaces + len(dzutil.VERSION) + 1 + right_spaces < 15: - right_spaces += 1 - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Style.RESET_ALL - + Back.BLACK - + Fore.LIGHTWHITE_EX - + Style.BRIGHT - + f"{' '*left_spaces}Dangerzone v{dzutil.VERSION}{' '*right_spaces}" - + Fore.YELLOW - + Style.DIM - + "│" - ) - print( - Back.BLACK - + Fore.YELLOW - + Style.DIM - + "│" - + Style.RESET_ALL - + Back.BLACK - + Fore.LIGHTWHITE_EX - + " https://dangerzone.rocks " - + Fore.YELLOW - + Style.DIM - + "│" - ) - print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯") - - @staticmethod - def install_container(): - """ - Make sure the podman container is installed. Linux only. - """ - if GlobalCommon.is_container_installed(): - return - - # Load the container into podman - print("Installing Dangerzone container image...") - - p = subprocess.Popen( - [dzutil.CONTAINER_RUNTIME, "load"], - stdin=subprocess.PIPE, - startupinfo=dzutil.get_subprocess_startupinfo(), - ) - - chunk_size = 10240 - compressed_container_path = dzutil.get_resource_path("container.tar.gz") - with gzip.open(compressed_container_path) as f: - while True: - chunk = f.read(chunk_size) - if len(chunk) > 0: - p.stdin.write(chunk) - else: - break - p.communicate() - - if not GlobalCommon.is_container_installed(): - print("Failed to install the container image") - return False - - print("Container image installed") - return True - - @staticmethod - def is_container_installed(): - """ - See if the podman container is installed. Linux only. - """ - # Get the image id - with open(dzutil.get_resource_path("image-id.txt")) as f: - expected_image_id = f.read().strip() - - # See if this image is already installed - installed = False - found_image_id = subprocess.check_output( - [ - dzutil.CONTAINER_RUNTIME, - "image", - "list", - "--format", - "{{.ID}}", - dangerzone.util.CONTAINER_NAME, - ], - text=True, - startupinfo=dzutil.get_subprocess_startupinfo(), - ) - found_image_id = found_image_id.strip() - - if found_image_id == expected_image_id: - installed = True - elif found_image_id == "": - pass - else: - print("Deleting old dangerzone container image") - - try: - subprocess.check_output( - [dzutil.CONTAINER_RUNTIME, "rmi", "--force", found_image_id], - startupinfo=dzutil.get_subprocess_startupinfo(), - ) - except: - print("Couldn't delete old container image, so leaving it there") - - return installed + print(Back.BLACK + Fore.YELLOW + Style.DIM + "╭──────────────────────────╮") + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ▄██▄ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ██████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███▀▀▀██ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███ ████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███ ██████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███ ▀▀▀▀████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███████ ▄██████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ███████ ▄█████████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ████████████████████ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Fore.LIGHTYELLOW_EX + + Style.NORMAL + + " ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀ " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print(Back.BLACK + Fore.YELLOW + Style.DIM + "│ │") + left_spaces = (15 - len(dzutil.VERSION) - 1) // 2 + right_spaces = left_spaces + if left_spaces + len(dzutil.VERSION) + 1 + right_spaces < 15: + right_spaces += 1 + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Style.RESET_ALL + + Back.BLACK + + Fore.LIGHTWHITE_EX + + Style.BRIGHT + + f"{' '*left_spaces}Dangerzone v{dzutil.VERSION}{' '*right_spaces}" + + Fore.YELLOW + + Style.DIM + + "│" + ) + print( + Back.BLACK + + Fore.YELLOW + + Style.DIM + + "│" + + Style.RESET_ALL + + Back.BLACK + + Fore.LIGHTWHITE_EX + + " https://dangerzone.rocks " + + Fore.YELLOW + + Style.DIM + + "│" + ) + print(Back.BLACK + Fore.YELLOW + Style.DIM + "╰──────────────────────────╯") diff --git a/dangerzone/gui/__init__.py b/dangerzone/gui/__init__.py index 495f72d..0a4e9cb 100644 --- a/dangerzone/gui/__init__.py +++ b/dangerzone/gui/__init__.py @@ -10,7 +10,7 @@ import uuid import colorama from .application import Application -from .common import GuiCommon +from .guicommon import GuiCommon from .main_window import MainWindow from .systray import SysTray diff --git a/dangerzone/gui/common.py b/dangerzone/gui/gui_common.py similarity index 100% rename from dangerzone/gui/common.py rename to dangerzone/gui/gui_common.py diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index 5c2229c..ce71e64 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -12,7 +12,7 @@ import dangerzone.util as dzutil from . import GuiCommon from ..common import Common from ..container import convert -from ..global_common import GlobalCommon +from ..global_common import install_container class MainWindow(QtWidgets.QMainWindow): @@ -97,7 +97,7 @@ class InstallContainerThread(QtCore.QThread): super(InstallContainerThread, self).__init__() def run(self): - GlobalCommon.install_container() + install_container() self.finished.emit()