publish-meetups/publish_meetups/utils/config.py

56 lines
1.0 KiB
Python
Raw Normal View History

2024-02-13 20:25:47 +00:00
import logging
import toml
import platformdirs
2024-02-13 20:43:29 +00:00
from . import PROGRAM_DATA_DIR, PROGRAM_NAME
2024-02-13 20:25:47 +00:00
logger = logging.getLogger("config")
_config: dict = {
"active_feeds": [
"mastodon",
"twitter",
],
"mastodon": {},
"twitter": {},
"lemmy": {},
}
2024-02-13 20:43:29 +00:00
CONFIG_FILE = PROGRAM_DATA_DIR / f"{PROGRAM_NAME}.toml"
2024-02-13 20:25:47 +00:00
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()