publish-meetups/publish_meetups/utils/config.py

57 lines
1.1 KiB
Python

import logging
import toml
import platformdirs
from . import PROGRAM_DATA_DIR, PROGRAM_NAME
logger = logging.getLogger("config")
_config: dict = {
"active_feeds": [
"mastodon",
"twitter",
],
"ics_url": "",
"mastodon": {},
"twitter": {},
"lemmy": {},
}
CONFIG_FILE = PROGRAM_DATA_DIR / f"{PROGRAM_NAME}.toml"
logger.info(f"Config file: {CONFIG_FILE}")
def read():
global _config, CONFIG_FILE
if CONFIG_FILE.exists():
with CONFIG_FILE.open("r", encoding="utf-8") as f:
_config.update(toml.load(f))
def write():
global _config, CONFIG_FILE
with CONFIG_FILE.open("w", encoding="utf-8") as f:
toml.dump(_config, f)
def get_field(__name: str, default=None):
global _config
return _config.get(__name, default)
def set_field(__name: str, __value, update_dict=False):
global _config
if update_dict and isinstance(__value, dict) and isinstance(_config.get(__name), dict):
_config[__name].update(__value)
else:
_config[__name] = __value
write()
read()