git-time-tracking/git_time_tracking/utils.py

42 lines
914 B
Python

import atexit
import signal
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 = {
"git_directories": [],
"git_author": None,
"censor_author": False,
"date_format": r"%Y-%m-%d",
}
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)