radical-bot/lib/config.py

35 lines
1.1 KiB
Python
Raw Normal View History

2022-08-19 06:17:34 +00:00
import json
from pathlib import Path
from dataclasses import dataclass
@dataclass
class Config(object):
discord_token: str | None = None
discord_guild_id: int | None = None
2022-08-20 02:22:48 +00:00
discord_study_room_thread: int | None = None
2022-08-19 06:17:34 +00:00
git_branch: str | None = None
git_url: str | None = None
2022-08-20 02:22:48 +00:00
google_calendar_id: str | None = None
time_zone: str | None = None
2022-08-19 06:17:34 +00:00
def config_decoder(obj):
if '__type__' in obj and obj['__type__'] == "Config":
return Config(
discord_token=obj['discord_token'],
discord_guild_id=obj['discord_guild_id'],
2022-08-20 02:22:48 +00:00
discord_study_room_thread=obj['discord_study_room_thread'],
git_branch=obj['git_branch'],
2022-08-20 02:22:48 +00:00
git_url=obj['git_url'],
google_calendar_id=obj['google_calendar_id'],
time_zone=obj['time_zone']
2022-08-19 06:17:34 +00:00
)
def save_config(config: dict):
2022-08-19 06:17:34 +00:00
with Path("config.json").open("w+", encoding="utf-8") as fp:
json.dump(config, fp, indent=4)
def load_config():
2022-08-19 06:17:34 +00:00
with Path("config.json").open("r", encoding="utf-8") as fp:
config = json.load(fp, object_hook=config_decoder)
return config