From 1770c8daab2674309bf82cf483dfc274441e976a Mon Sep 17 00:00:00 2001 From: Nathan Shaaban <86252985+ctrlaltf24@users.noreply.github.com> Date: Wed, 23 Oct 2024 13:50:54 -0700 Subject: [PATCH] refactor: add type hints and comments Small handfull of comments on things I noticed while reading No functional changes --- ou_dedetai/cli.py | 2 +- ou_dedetai/config.py | 67 +++++++++++++++++++++-------------------- ou_dedetai/control.py | 8 +++-- ou_dedetai/installer.py | 3 +- ou_dedetai/network.py | 6 ++-- ou_dedetai/system.py | 11 +++++-- ou_dedetai/utils.py | 8 ++--- ou_dedetai/wine.py | 5 +-- 8 files changed, 61 insertions(+), 49 deletions(-) diff --git a/ou_dedetai/cli.py b/ou_dedetai/cli.py index af17cc20..a8d02e43 100644 --- a/ou_dedetai/cli.py +++ b/ou_dedetai/cli.py @@ -10,7 +10,7 @@ class CLI: def __init__(self): - self.running = True + self.running: bool = True self.choice_q = queue.Queue() self.input_q = queue.Queue() self.input_event = threading.Event() diff --git a/ou_dedetai/config.py b/ou_dedetai/config.py index b78d8c15..fdb178a2 100644 --- a/ou_dedetai/config.py +++ b/ou_dedetai/config.py @@ -3,6 +3,7 @@ import os import tempfile from datetime import datetime +from typing import Optional # Define app name variables. @@ -59,20 +60,20 @@ globals()[key] = os.getenv(key, default) # Set other run-time variables not set in the env. -ACTION = 'app' -APPIMAGE_FILE_PATH = None -authenticated = False -BADPACKAGES = None +ACTION: str = 'app' +APPIMAGE_FILE_PATH: Optional[str] = None +authenticated = False # FIXME: no references +BADPACKAGES: Optional[str] = None # FIXME: no references DEFAULT_CONFIG_PATH = os.path.expanduser(f"~/.config/FaithLife-Community/{name_binary}.json") # noqa: E501 -FLPRODUCTi = None -GUI = None -INSTALL_STEP = 0 -INSTALL_STEPS_COUNT = 0 +FLPRODUCTi: Optional[str] = None +GUI = None # FIXME: no references +INSTALL_STEP: int = 0 +INSTALL_STEPS_COUNT: int = 0 L9PACKAGES = None LEGACY_CONFIG_FILE = os.path.expanduser("~/.config/Logos_on_Linux/Logos_on_Linux.conf") # noqa: E501 LLI_AUTHOR = "Ferion11, John Goodman, T. H. Wright, N. Marti" LLI_CURRENT_VERSION = "4.0.0-beta.2" -LLI_LATEST_VERSION = None +LLI_LATEST_VERSION: Optional[str] = None LLI_TITLE = name_app LOG_LEVEL = logging.WARNING LOGOS_BLUE = '#0082FF' @@ -80,34 +81,34 @@ LOGOS_WHITE = '#FCFCFC' # LOGOS_WHITE = '#F7F7F7' LOGOS_DIR = os.path.dirname(LOGOS_EXE) if LOGOS_EXE else None # noqa: F821 -LOGOS_FORCE_ROOT = False -LOGOS_ICON_FILENAME = None -LOGOS_ICON_URL = None +LOGOS_FORCE_ROOT: bool = False +LOGOS_ICON_FILENAME: Optional[str] = None +LOGOS_ICON_URL: Optional[str] = None LOGOS_LATEST_VERSION_FILENAME = name_binary -LOGOS_LATEST_VERSION_URL = None -LOGOS9_RELEASES = None # used to save downloaded releases list +LOGOS_LATEST_VERSION_URL: Optional[str] = None +LOGOS9_RELEASES = None # used to save downloaded releases list # FIXME: not set #noqa: E501 LOGOS9_WINE64_BOTTLE_TARGZ_NAME = "wine64_bottle.tar.gz" LOGOS9_WINE64_BOTTLE_TARGZ_URL = f"https://github.com/ferion11/wine64_bottle_dotnet/releases/download/v5.11b/{LOGOS9_WINE64_BOTTLE_TARGZ_NAME}" # noqa: E501 -LOGOS10_RELEASES = None # used to save downloaded releases list -MYDOWNLOADS = None -OS_NAME = None -OS_RELEASE = None -PACKAGE_MANAGER_COMMAND_INSTALL = None -PACKAGE_MANAGER_COMMAND_REMOVE = None -PACKAGE_MANAGER_COMMAND_QUERY = None -PACKAGES = None -PASSIVE = None +LOGOS10_RELEASES = None # used to save downloaded releases list # FIXME: not set #noqa: E501 +MYDOWNLOADS: Optional[str] = None # FIXME: Should this use the tempfile module? +OS_NAME: Optional[str] = None +OS_RELEASE: Optional[str] = None +PACKAGE_MANAGER_COMMAND_INSTALL: Optional[list[str]] = None +PACKAGE_MANAGER_COMMAND_REMOVE: Optional[list[str]] = None +PACKAGE_MANAGER_COMMAND_QUERY: Optional[list[str]] = None +PACKAGES: Optional[str] = None +PASSIVE: Optional[bool] = None pid_file = f'/tmp/{name_binary}.pid' -PRESENT_WORKING_DIRECTORY = os.getcwd() -QUERY_PREFIX = None -REBOOT_REQUIRED = None -RECOMMENDED_WINE64_APPIMAGE_FULL_FILENAME = None -RECOMMENDED_WINE64_APPIMAGE_FULL_VERSION = None -RECOMMENDED_WINE64_APPIMAGE_FILENAME = None -RECOMMENDED_WINE64_APPIMAGE_VERSION = None -RECOMMENDED_WINE64_APPIMAGE_BRANCH = None -SUPERUSER_COMMAND = None -VERBUM_PATH = None +PRESENT_WORKING_DIRECTORY: str = os.getcwd() +QUERY_PREFIX: Optional[str] = None +REBOOT_REQUIRED: Optional[str] = None +RECOMMENDED_WINE64_APPIMAGE_FULL_FILENAME: Optional[str] = None +RECOMMENDED_WINE64_APPIMAGE_FULL_VERSION: Optional[str] = None +RECOMMENDED_WINE64_APPIMAGE_FILENAME: Optional[str] = None +RECOMMENDED_WINE64_APPIMAGE_VERSION: Optional[str] = None +RECOMMENDED_WINE64_APPIMAGE_BRANCH: Optional[str] = None +SUPERUSER_COMMAND: Optional[str] = None +VERBUM_PATH: Optional[str] = None WINETRICKS_URL = "https://raw.githubusercontent.com/Winetricks/winetricks/5904ee355e37dff4a3ab37e1573c56cffe6ce223/src/winetricks" # noqa: E501 WINETRICKS_VERSION = '20220411' wine_user = None diff --git a/ou_dedetai/control.py b/ou_dedetai/control.py index ce7bdd1a..d9ffa5d5 100644 --- a/ou_dedetai/control.py +++ b/ou_dedetai/control.py @@ -38,6 +38,7 @@ def restore(app=None): backup_and_restore(mode='restore', app=app) +# FIXME: consider moving this into it's own file/module. def backup_and_restore(mode='backup', app=None): data_dirs = ['Data', 'Documents', 'Users'] # Ensure BACKUPDIR is defined. @@ -268,8 +269,8 @@ def set_winetricks(): # Check if local winetricks version is up-to-date; if so, offer to # use it or to download; else, download it. local_winetricks_version = subprocess.check_output(["winetricks", "--version"]).split()[0] # noqa: E501 - if str(local_winetricks_version) >= "20220411": - if config.DIALOG == 'tk': + if str(local_winetricks_version) >= "20220411": #FIXME: consider using config.WINETRICKS_VERSION and != string comparision on versions is dodgy #noqa: E501 + if config.DIALOG == 'tk': #FIXME: CLI client not considered logging.info("Setting winetricks to the local binary…") config.WINETRICKSBIN = local_winetricks_path else: @@ -296,6 +297,7 @@ def set_winetricks(): ) return 0 else: + # FIXME: Should this call a function on the app object? msg.status("Installation canceled!") sys.exit(0) else: @@ -319,7 +321,7 @@ def set_winetricks(): return 0 -def download_winetricks(): +def download_winetricks(): #FIXME: unused, logic moved to system.install_winetricks msg.status("Downloading winetricks…") appdir_bindir = f"{config.INSTALLDIR}/data/bin" network.logos_reuse_download( diff --git a/ou_dedetai/installer.py b/ou_dedetai/installer.py index 34a4eb1c..9b26c793 100644 --- a/ou_dedetai/installer.py +++ b/ou_dedetai/installer.py @@ -677,6 +677,7 @@ def ensure_launcher_shortcuts(app=None): ensure_launcher_executable(app=app) config.INSTALL_STEP += 1 runmode = system.get_runmode() + # FIXME: why not do this all the time? if runmode == 'binary': update_install_feedback("Creating launcher shortcuts…", app=app) create_launcher_shortcuts() @@ -741,7 +742,7 @@ def create_wine_appimage_symlinks(app=None): p.symlink_to(f"./{config.APPIMAGE_LINK_SELECTION_NAME}") -def get_flproducti_name(product_name): +def get_flproducti_name(product_name) -> str: lname = product_name.lower() if lname == 'logos': return 'logos4' diff --git a/ou_dedetai/network.py b/ou_dedetai/network.py index 9c55b9a7..02e731ed 100644 --- a/ou_dedetai/network.py +++ b/ou_dedetai/network.py @@ -3,6 +3,7 @@ import logging import os import queue +from typing import Optional import requests import shutil import sys @@ -383,15 +384,16 @@ def get_latest_release_data(repository): return None -def get_first_asset_url(json_data): +def get_first_asset_url(json_data) -> Optional[str]: release_url = None if json_data: + # FIXME: Portential KeyError release_url = json_data.get('assets')[0].get('browser_download_url') logging.info(f"Release URL: {release_url}") return release_url -def get_tag_name(json_data): +def get_tag_name(json_data) -> Optional[str]: tag_name = None if json_data: tag_name = json_data.get('tag_name') diff --git a/ou_dedetai/system.py b/ou_dedetai/system.py index c2d3a40a..d499a25a 100644 --- a/ou_dedetai/system.py +++ b/ou_dedetai/system.py @@ -1,3 +1,4 @@ +from typing import Optional import distro import logging import os @@ -16,7 +17,7 @@ # TODO: Replace functions in control.py and wine.py with Popen command. -def run_command(command, retries=1, delay=0, **kwargs): +def run_command(command, retries=1, delay=0, **kwargs) -> Optional[subprocess.CompletedProcess[any]]: check = kwargs.get("check", True) text = kwargs.get("text", True) capture_output = kwargs.get("capture_output", True) @@ -259,7 +260,7 @@ def reboot(): sys.exit(0) -def t(command): +def t(command): # FIXME: unused, have_dep does the same thing if shutil.which(command) is not None: return True else: @@ -275,6 +276,7 @@ def tl(library): def get_dialog(): + # FIXME: wouldn't the cli still work without a DISPLAY? What about wayland? if not os.environ.get('DISPLAY'): msg.logos_error("The installer does not work unless you are running a display") # noqa: E501 @@ -481,6 +483,9 @@ def check_dialog_version(): if have_dep("dialog"): try: result = run_command(["dialog", "--version"]) + if result is None: + print("Failed to run the 'dialog' command.") # noqa: E501 + return None version_info = result.stdout.strip() if version_info.startswith("Version: "): version_info = version_info[len("Version: "):] @@ -493,7 +498,7 @@ def check_dialog_version(): def test_dialog_version(): - version = check_dialog_version() + version: Optional[str] = check_dialog_version() def parse_date(version): try: diff --git a/ou_dedetai/utils.py b/ou_dedetai/utils.py index 801342e6..042fe497 100644 --- a/ou_dedetai/utils.py +++ b/ou_dedetai/utils.py @@ -17,7 +17,7 @@ import tkinter as tk from packaging import version from pathlib import Path -from typing import List, Union +from typing import List, Optional, Union from . import config from . import msg @@ -225,7 +225,7 @@ def delete_symlink(symlink_path): logging.error(f"Error removing symlink: {e}") -def check_dependencies(app=None): +def check_dependencies(app=None): # FIXME: misnomer, isn't this an install_dependencies if config.TARGETVERSION: targetversion = int(config.TARGETVERSION) else: @@ -442,7 +442,7 @@ def get_winetricks_options(): # Check if local winetricks version is up-to-date. cmd = ["winetricks", "--version"] local_winetricks_version = subprocess.check_output(cmd).split()[0] - if str(local_winetricks_version) >= "20220411": + if str(local_winetricks_version) >= "20220411": #FIXME: consider using config.WINETRICKS_VERSION and != string comparision on versions is dodgy #noqa: E501 winetricks_options.insert(0, local_winetricks_path) else: logging.info("Local winetricks is too old.") @@ -509,7 +509,7 @@ def app_is_installed(): return config.LOGOS_EXE is not None and os.access(config.LOGOS_EXE, os.X_OK) # noqa: E501 -def find_installed_product(): +def find_installed_product() -> Optional[str]: if config.FLPRODUCT and config.WINEPREFIX: drive_c = Path(f"{config.WINEPREFIX}/drive_c/") name = config.FLPRODUCT diff --git a/ou_dedetai/wine.py b/ou_dedetai/wine.py index ac096385..c044171a 100644 --- a/ou_dedetai/wine.py +++ b/ou_dedetai/wine.py @@ -5,6 +5,7 @@ import signal import subprocess from pathlib import Path +from typing import Optional from . import config from . import msg @@ -16,8 +17,8 @@ def get_wine_user(): - path = config.LOGOS_EXE - normalized_path = os.path.normpath(path) + path: Optional[str] = config.LOGOS_EXE + normalized_path: str = os.path.normpath(path) path_parts = normalized_path.split(os.sep) config.wine_user = path_parts[path_parts.index('users') + 1]