feat: config

This commit is contained in:
Hazel 2024-06-07 12:55:03 +02:00
parent 17f429ad7d
commit 872dfce140
3 changed files with 85 additions and 2 deletions

View File

@ -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."

View File

@ -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()

View File

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