-
Notifications
You must be signed in to change notification settings - Fork 64
/
storage_management.py
107 lines (88 loc) · 3.57 KB
/
storage_management.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import json
import os
from typing import Union
class JsonFileManager:
""" JsonFileManager class handles basic saving and loading of a json based settings file """
def __init__(self):
self.file_path = ""
self.settings = None
async def init(self) -> None:
""" Checks if the file exists, loads if it does, creates if it doesn't """
if await self.file_exists():
await self.load()
else:
await self.create_file()
async def create_file(self) -> None:
""" Create an empty JSON file, usually overwritten to provide config structure and defaults """
self.settings = {}
await self.write_file_to_disk()
async def file_exists(self) -> bool:
""" Checks if the file exists """
try:
open(self.file_path, "r")
return True
except FileNotFoundError:
return False
async def load(self) -> None:
""" Loads the json file from disk into self.settings """
self.settings = await self.load_local()
async def load_local(self) -> dict:
""" Returns the contents of the json file from disk, doesn't overwrite the stored settings. USE FOR READING VALUES ONLY! """
with open(self.file_path, "r") as r:
settings = json.load(r)
r.close()
return settings
async def write_file_to_disk(self) -> None:
""" Saves the contents of self.settings to disk """
with open(self.file_path, "w+") as w:
json.dump(self.settings, w, indent=4)
w.close()
class StorageManagement(JsonFileManager):
def __init__(self):
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
self.file_path = os.path.join(__location__, "settings.json")
self.settings = None
async def create_file(self) -> None:
with open(self.file_path, "w+") as w:
self.settings = w.read()
w.close()
self.settings = {
"guilds": {}
}
await self.write_file_to_disk()
async def has_guild(self, guild_id) -> bool:
guild_id = str(guild_id)
if self.settings["guilds"].get(guild_id) is not None:
return True
else:
return False
async def add_guild(self, guild_id) -> None:
guild_id = str(guild_id)
self.settings["guilds"][guild_id] = {
"muted_role_id": 0,
"log_channel_id": 0,
"mod_roles": [],
"muted_users": {},
"banned_users": {}
}
await self.write_file_to_disk()
await self.load()
class ConfigManagement(JsonFileManager):
""" Example custom config class to handle non guild-specific settings for customized features of the bot """
def __init__(self):
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
self.file_path = os.path.join(__location__, "custom_config.json")
self.settings = None
async def create_file(self) -> None:
self.settings = {
"some_key": "some_value"
}
await self.write_file_to_disk()
async def get_value(self, some_key) -> Union[str, None]:
""" Example function loading a key from the config file """
await self.load()
return self.settings.get(some_key)
async def set_value(self, some_key, some_value) -> None:
""" Example function setting a value to the config file and saving it to disk """
self.settings[some_key] = some_value
await self.write_file_to_disk()