This repository has been archived by the owner on Sep 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global_config.py
99 lines (87 loc) · 2.94 KB
/
global_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
import os
# Nonstandard to avoid depend loop
from logger import BotLogger
syslog = BotLogger("system_log.txt")
def check(fn):
if os.path.exists(fn):
return True
else:
return False
class configboi:
def __init__(self, fn, logging=True):
self.config = {}
if not check(fn):
if logging:
syslog.log("Config", "No config found!")
else:
if logging:
syslog.log("Config", "----- Loading config values -----")
with open(fn) as f:
config_lines = f.read().split("\n")
for line in config_lines:
if line != "" and line != "\n":
if line[0] != "#":
bits = line.split(":")
key = bits[0]
val = bits[1]
if logging:
syslog.log("Config", "Added " + key + ": " + val)
self.config[key] = val
self.islogging = logging
def reloadconfig(self):
if not check(fn):
if self.logging:
syslog.log("Config", "No config found!")
else:
if self.logging:
syslog.log("Config", "----- Loading config values -----")
with open(fn) as f:
config_lines = f.read().split("\n")
for line in config_lines:
if line != "" and line != "\n":
if line[0] != "#":
bits = line.split(":")
key = bits[0]
val = bits[1]
if self.logging:
syslog.log("Config", "Added " + key + ": " + val)
self.config[key] = val
def get(self, key):
if key in self.config:
return self.config[key].replace("//", "://")
else:
return "Not found"
def getasint(self, key):
if key in self.config:
return int(self.config[key])
else:
return 0
def getasbool(self, key):
if key in self.config:
result = self.config[key]
if result == "true" or result == "True":
return True
else:
return False
else:
return False
def getaslist(self, key):
if key in self.config:
if "," in self.config[key]:
return self.config[key].split(",")
else:
return [self.config[key]]
else:
return None
def getasintlist(self, key):
if key in self.config:
if "," in self.config[key]:
data = self.config[key].split(",")
newdata = []
for item in data:
newdata.append(int(item))
return newdata
else:
return [int(self.config[key])]
else:
return [0]