forked from M1ch43lV/3cqsbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
80 lines (67 loc) · 2.56 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
import configparser
import sys
class Config:
def __init__(self, datadir, program):
self.config = configparser.ConfigParser()
self.dataset = self.config.read(f"{datadir}/{program}.ini")
if self.dataset == []:
self.dataset = self.config.read("config.ini")
self.fixstrings = ["account_name", "prefix", "subprefix", "suffix"]
self.datadir = datadir
self.program = program
def get(self, attribute, defaultvalue="", section: str = None):
data = ""
if len(self.dataset) != 1:
sys.tracebacklimit = 0
sys.exit(
f"Cannot read {self.datadir}/{self.program}.ini or config.ini! - Please make sure it exists in the folder where 3cqsbot.py is executed."
)
sections = self.config.sections()
if section == None:
for section in sections:
if self.config.has_option(section, attribute):
raw_value = self.config[section].get(attribute)
if raw_value:
if attribute in self.fixstrings:
data = raw_value
else:
data = self.check_type(raw_value)
break
elif self.config.has_option(section, attribute):
raw_value = self.config[section].get(attribute)
if raw_value:
if attribute in self.fixstrings:
data = raw_value
else:
data = self.check_type(raw_value)
if data == "" and str(defaultvalue):
data = defaultvalue
elif data == "" and defaultvalue == "" and not attribute == "botid":
sys.tracebacklimit = 0
sys.exit(
"Make sure that section ["
+ section
+ "] is defined and mandatory attribute '"
+ attribute
+ "' is set. Please check the readme for configuration. Exiting script!"
)
return data
def isfloat(self, element):
try:
float(element)
return True
except ValueError:
return False
def check_type(self, raw_value):
data = ""
if raw_value.isdigit():
data = int(raw_value)
elif raw_value.lower() == "true":
data = True
elif raw_value.lower() == "false":
data = False
elif self.isfloat(raw_value):
data = float(raw_value)
else:
data = str(raw_value)
return data