refactored to be simpler
This commit is contained in:
@@ -1,19 +1,9 @@
|
||||
from pathlib import Path
|
||||
import logging
|
||||
|
||||
import platformdirs
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
|
||||
PROGRAM_NAME: str = "publish-meetups"
|
||||
|
||||
|
||||
class Paths:
|
||||
CONFIG_PATH: Path = Path(platformdirs.user_config_path(appname=PROGRAM_NAME))
|
||||
|
||||
|
||||
Paths.CONFIG_PATH.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
|
||||
|
||||
__all__ = ["prompt", "CONFIG_PATH", "PROGRAM_NAME", "errors"]
|
||||
|
||||
|
||||
__all__ = ["prompt", "CONFIG_PATH", "PROGRAM_NAME", "errors", "config"]
|
||||
|
||||
57
publish_meetups/utils/config.py
Normal file
57
publish_meetups/utils/config.py
Normal file
@@ -0,0 +1,57 @@
|
||||
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()
|
||||
Reference in New Issue
Block a user