From c8edea8c239a764ef1155e9502c967486c6e6e64 Mon Sep 17 00:00:00 2001 From: YisusChrist Date: Sat, 16 Nov 2024 18:12:11 +0100 Subject: [PATCH] refactor: update modules to use new core-helpers functionalities --- iltransfer/__main__.py | 18 ++++++++++++++---- iltransfer/cli.py | 27 +-------------------------- iltransfer/config.py | 12 +++++++++--- iltransfer/consts.py | 8 ++++---- iltransfer/file_processing.py | 2 +- 5 files changed, 29 insertions(+), 38 deletions(-) diff --git a/iltransfer/__main__.py b/iltransfer/__main__.py index 67467c9..2cebe88 100644 --- a/iltransfer/__main__.py +++ b/iltransfer/__main__.py @@ -12,13 +12,23 @@ """ from argparse import Namespace + from core_helpers.updates import check_updates +from core_helpers.utils import exit_session from rich import print from rich.traceback import install -from .cli import exit_session, get_parsed_args +from .cli import get_parsed_args from .config import configure_paths -from .consts import DEBUG, EXIT_FAILURE, EXIT_SUCCESS, GITHUB, PROFILE, VERSION +from .consts import ( + DEBUG, + EXIT_FAILURE, + EXIT_SUCCESS, + GITHUB, + LOG_PATH, + PROFILE, + VERSION, +) from .file_processing import find_and_move_folders from .logs import logger @@ -38,11 +48,11 @@ def main() -> None: src_path, dest_path = configure_paths(args) if not src_path.exists(): logger.error("Source path %s does not exist", src_path) - exit_session(EXIT_FAILURE) + exit_session(EXIT_FAILURE, LOG_PATH) find_and_move_folders(src_path, dest_path) - exit_session(EXIT_SUCCESS) + exit_session(EXIT_SUCCESS, LOG_PATH) if __name__ == "__main__": diff --git a/iltransfer/cli.py b/iltransfer/cli.py index 13d549c..c7184c4 100644 --- a/iltransfer/cli.py +++ b/iltransfer/cli.py @@ -1,13 +1,10 @@ """Command-line interface for the project.""" -import sys from argparse import Namespace from core_helpers.cli import ArgparseColorThemes, setup_parser -from rich import print -from .consts import DESC, EXIT_FAILURE, LOG_PATH, PACKAGE, VERSION -from .logs import logger +from .consts import DESC, PACKAGE, VERSION def get_parsed_args() -> Namespace: @@ -59,25 +56,3 @@ def get_parsed_args() -> Namespace: ) return parser.parse_args() - - -def exit_session(exit_value: int) -> None: - """ - Exit the program with the given exit value. - - Args: - exit_value (int): The POSIX exit value to exit with. - """ - logger.info("End of session") - # Check if the exit_value is a valid POSIX exit value - if not 0 <= exit_value <= 255: - exit_value = EXIT_FAILURE - - if exit_value == EXIT_FAILURE: - print( - "\n[red]There were errors during the execution of the script. " - f"Check the logs at '{LOG_PATH}' for more information.[/]" - ) - - # Exit the program with the given exit value - sys.exit(exit_value) diff --git a/iltransfer/config.py b/iltransfer/config.py index 5cb847b..3d2af75 100644 --- a/iltransfer/config.py +++ b/iltransfer/config.py @@ -7,10 +7,16 @@ from tkinter import filedialog from typing import Tuple +from core_helpers.utils import exit_session from rich import print -from .cli import exit_session -from .consts import CONFIG_FILE, DEFAULT_DEST_PATH, DEFAULT_SRC_PATH, EXIT_FAILURE +from .consts import ( + CONFIG_FILE, + DEFAULT_DEST_PATH, + DEFAULT_SRC_PATH, + EXIT_FAILURE, + LOG_PATH, +) from .logs import logger @@ -127,7 +133,7 @@ def configure_paths(args: Namespace) -> Tuple[Path, Path]: if not config_file.is_file(): logger.error("Configuration file '%s' does not exist", config_file) - exit_session(EXIT_FAILURE) + exit_session(EXIT_FAILURE, LOG_PATH) # Read both configuration file and command-line arguments config = ConfigParser() diff --git a/iltransfer/consts.py b/iltransfer/consts.py index 8a6159f..967875d 100644 --- a/iltransfer/consts.py +++ b/iltransfer/consts.py @@ -1,6 +1,6 @@ """Constants for the project.""" -from core_helpers.xdg_paths import get_user_path +from core_helpers.xdg_paths import PathType, get_user_path try: from importlib import metadata @@ -12,10 +12,10 @@ GITHUB: str = metadata.metadata(__package__ or __name__)["Home-page"] PACKAGE = metadata.metadata(__package__ or __name__)["Name"] -CONFIG_PATH = get_user_path(PACKAGE, "config") +CONFIG_PATH = get_user_path(PACKAGE, PathType.CONFIG) CONFIG_FILE = CONFIG_PATH / f"{PACKAGE}.ini" -DATA_PATH = get_user_path(PACKAGE, "data") -LOG_PATH = get_user_path(PACKAGE, "log") +DATA_PATH = get_user_path(PACKAGE, PathType.DATA) +LOG_PATH = get_user_path(PACKAGE, PathType.LOG) LOG_FILE = LOG_PATH / f"{PACKAGE}.log" VERSION = __version__ diff --git a/iltransfer/file_processing.py b/iltransfer/file_processing.py index b7b7fd7..e6cba62 100644 --- a/iltransfer/file_processing.py +++ b/iltransfer/file_processing.py @@ -4,7 +4,7 @@ from pathlib import Path from rich import print -from tqdm import tqdm +from tqdm import tqdm # type: ignore from .consts import DEBUG from .logs import logger