git-time-tracking/git_time_tracking/utils.py

42 lines
914 B
Python
Raw Normal View History

2024-05-29 10:26:15 +00:00
import atexit
import signal
from pathlib import Path
import toml
from rich.console import Console
from .__about__ import __name__
console: Console = Console()
2024-05-29 11:19:21 +00:00
SETTINGS_EXISTED = False
2024-05-29 10:26:15 +00:00
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():
2024-05-29 11:19:21 +00:00
global SETTINGS_EXISTED
2024-05-29 10:26:15 +00:00
if CONFIG_PATH.exists():
2024-05-29 11:19:21 +00:00
SETTINGS_EXISTED = True
2024-05-29 10:26:15 +00:00
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)