|
|
|
from typing import Any
|
|
|
|
import json
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
from hrochobot.utils.json_templates import TEMPLATES
|
|
|
|
|
|
|
|
EXAMPLE_DATA = "data.example"
|
|
|
|
DATA_FOLDER = "TODO"
|
|
|
|
|
|
|
|
def load_json(filename: str):
|
|
|
|
filename += ".json"
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
return json.loads(TEMPLATES[os.path.basename(filename)])
|
|
|
|
|
|
|
|
with open(filename) as f:
|
|
|
|
content = json.load(f)
|
|
|
|
return content
|
|
|
|
|
|
|
|
def dump_json(filename: str, data: Any):
|
|
|
|
filename += ".json"
|
|
|
|
path = os.path.dirname(filename)
|
|
|
|
if not os.path.exists(path):
|
|
|
|
os.makedirs(path)
|
|
|
|
|
|
|
|
with open(filename, "w") as f:
|
|
|
|
json.dump(data, f)
|
|
|
|
|
|
|
|
def load_data(filename: str):
|
|
|
|
return load_json(os.path.join(DATA_FOLDER, filename))
|
|
|
|
|
|
|
|
def dump_data(filename: str, data: Any):
|
|
|
|
return dump_json(os.path.join(DATA_FOLDER, filename), data)
|
|
|
|
|
|
|
|
def load_guild_data(guild: int, filename: str):
|
|
|
|
return load_data(os.path.join(str(guild), filename))
|
|
|
|
|
|
|
|
def dump_guild_data(guild: int, filename: str, data: Any):
|
|
|
|
return dump_data(os.path.join(str(guild), filename), data)
|