From 872dfce140801b7539e15599cb19a52f54a0a418 Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Fri, 7 Jun 2024 12:55:03 +0200 Subject: [PATCH] feat: config --- pyproject.toml | 5 ++- update_applications/__main__.py | 20 ++++++++++- update_applications/utils.py | 62 +++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 update_applications/utils.py diff --git a/pyproject.toml b/pyproject.toml index 9da9830..9924490 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,9 @@ [project] name = "update-applications" -dependencies = [] +dependencies = [ + "toml~=0.10.2", + "rich~=13.7.1", +] dynamic = ["version"] authors = [] description = "If an application like hopscotch is not found in repositories, you can use this to still keep it updated, by either adding a download link for the newest version or a git repository with releases." diff --git a/update_applications/__main__.py b/update_applications/__main__.py index 798dffe..d1c975d 100644 --- a/update_applications/__main__.py +++ b/update_applications/__main__.py @@ -1,5 +1,23 @@ +import argparse +import datetime +import subprocess +from pathlib import Path + from .__about__ import __version__ +from .utils import (CONFIG_PATH, SETTINGS, SETTINGS_EXISTED, console, + edit_file, load_config, save_config) + + +def configure(): + save_config() + edit_file(CONFIG_PATH) + load_config() def cli(): - print("Hello, world!") + parser = argparse.ArgumentParser() + parser.add_argument("--config", action=argparse.BooleanOptionalAction, help="Configure the settings") + args = parser.parse_args() + + if not SETTINGS_EXISTED or args.config: + configure() diff --git a/update_applications/utils.py b/update_applications/utils.py new file mode 100644 index 0000000..68173ef --- /dev/null +++ b/update_applications/utils.py @@ -0,0 +1,62 @@ +import atexit +import os +import shutil +import signal +import subprocess +from pathlib import Path + +import toml +from rich.console import Console + +from .__about__ import __name__ + +console: Console = Console() + +SETTINGS_EXISTED = False +CONFIG_PATH = Path.home() / ".config" / __name__ / "config.toml" +SETTINGS = { +} + +def load_config(): + global SETTINGS_EXISTED + + if CONFIG_PATH.exists(): + SETTINGS_EXISTED = True + with CONFIG_PATH.open("r", encoding="utf-8") as f: + config = toml.load(f) + SETTINGS.update(config) + +load_config() + +def save_config(): + CONFIG_PATH.parent.mkdir(exist_ok=True, parents=True) + + with CONFIG_PATH.open("w", encoding="utf-8") as f: + toml.dump(SETTINGS, f) + + +atexit.register(save_config) +signal.signal(signal.SIGTERM, save_config) +signal.signal(signal.SIGINT, save_config) + + +def edit_file(file_name: Path): + if hasattr(os, "startfile"): + os.startfile(file_name) + elif shutil.which("xdg-open"): + subprocess.call(["xdg-open", file_name]) + elif "EDITOR" in os.environ: + subprocess.call([os.environ["EDITOR"], file_name]) + + # wait till the last changed is different or ctrl+c is pressed + console.print("Waiting till either the file is changed, or ctrl+c is pressed") + try: + last_change = os.path.getmtime(file_name) + while True: + if os.path.getmtime(file_name) != last_change: + break + + time.sleep(0.1) + except KeyboardInterrupt: + print("No changes were saved") +