58 lines
1.1 KiB
Python
58 lines
1.1 KiB
Python
|
import logging
|
||
|
|
||
|
import toml
|
||
|
import platformdirs
|
||
|
|
||
|
from . import PROGRAM_NAME
|
||
|
|
||
|
logger = logging.getLogger("config")
|
||
|
_config: dict = {
|
||
|
"active_feeds": [
|
||
|
"mastodon",
|
||
|
"twitter",
|
||
|
],
|
||
|
"mastodon": {},
|
||
|
"twitter": {},
|
||
|
"lemmy": {},
|
||
|
}
|
||
|
|
||
|
|
||
|
CONFIG_PATH = platformdirs.user_config_path(appname=PROGRAM_NAME)
|
||
|
CONFIG_PATH.mkdir(parents=True, exist_ok=True)
|
||
|
CONFIG_FILE = CONFIG_PATH / 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()
|