-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Config.py
117 lines (93 loc) · 3.19 KB
/
Config.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
108
109
110
111
112
113
114
115
116
117
import os
from dotenv import load_dotenv
from Logger import LogLevel, Logger
import json
import copy
load_dotenv()
class Config():
__HasLoaded = False
def __new__(cls):
if not hasattr(cls, 'instance'):
cls.instance = super(Config, cls).__new__(cls)
return cls.instance
def __init__(self):
self.Load()
def Load(self):
if (self.__HasLoaded):
return
Data = {}
with open(self.GetConfigFile(), "r") as config_file:
Data = json.load(config_file)
self.__dict__ = Data
self.__HasLoaded = True
Logger.Log(LogLevel.Notice, "Configuration Loaded!")
def Save(self):
StagingSave = copy.deepcopy(self.__dict__)
with open(self.GetConfigFile(), "wt") as config_file:
json.dump(StagingSave, config_file, indent=3)
def __getitem__(self, item):
return self.__dict__[item]
def IsValid(self, Key:str, ExpectType) -> bool:
try:
EntryValue = self[Key]
EntryValueType = type(EntryValue)
if (EntryValueType != ExpectType):
return False
if (EntryValueType == int):
if (EntryValue <= 0):
return False
else:
return True
elif (EntryValueType == str):
if (len(EntryValue) == 0):
return False
return True
return False
except(Exception):
return False
@staticmethod
def GetAllSubTokens():
if (not os.path.exists(Config.GetAPIKeysFile())):
return {}
with open(Config.GetAPIKeysFile(), "r") as crypto_file:
return json.load(crypto_file)
@staticmethod
def GetToken(ForInstance:int=-1) -> str:
if (ForInstance <= 0):
return os.getenv("DISCORD_TOKEN")
else:
CryptoKeys = Config.GetAllSubTokens()
InstanceStr:str = str(ForInstance)
if (CryptoKeys[InstanceStr] is None):
return ""
return CryptoKeys[InstanceStr]
@staticmethod
def GetNumberOfInstances() -> int:
CryptoKeys = Config.GetAllSubTokens()
return len(CryptoKeys)
@staticmethod
def GetDBFile() -> str:
return os.getenv("DATABASE_FILE")
@staticmethod
def GetConfigFile() -> str:
return os.getenv("CONFIG_FILE")
@staticmethod
def GetAPIKeysFile() -> str:
return os.getenv("API_KEYS")
@staticmethod
def GetBackupLocation() -> str:
return os.getenv("BACKUP_LOCATION")
# In this mode, bans do not actually process, nor do they send out to any users.
@staticmethod
def IsDevelopment() -> bool:
DevOption = os.getenv("DEVELOPMENT_MODE")
if (DevOption is None):
return False
if (DevOption.lower() == "false"):
return False
else:
return True
def __str__(self):
return f"{str(self.__dict__)}"
def Dump(self):
print(self)