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")