forked from dragonwolfsp/dwcom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
48 lines (41 loc) · 1.53 KB
/
config.py
File metadata and controls
48 lines (41 loc) · 1.53 KB
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
"""
config class for dwcom
"""
from threading import Thread
from conf import conf
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class Config:
def __init__(self):
self.serverConfigs = conf.servers()
self.watcher = ConfigWatcher('ttcom.conf', self.reloadConf)
self.observer = Observer()
self.observer.schedule(self.watcher, '.', recursive = False)
self.observer.start()
self.observerThread = Thread(target = self.observer.join, daemon = True)
self.observerThread.start()
def get(self, serverName: str, itemName: str):
try:
serverConfig = self.serverConfigs[serverName]
except ValueError as e:
print(e)
return None
try:
return self._convertConfigValue(serverConfig[itemName])
except KeyError:
return None
@staticmethod
def _convertConfigValue(configValue: str):
if configValue.isnumeric() and configValue != '1' and configValue != '0': return float(configValue)
match configValue.lower():
case 'y' | 'yes' | '1' | 'true': return True
case 'n' | 'no' | '0' | 'false': return False
case _: return configValue
def reloadConf(self):
self.serverConfigs = conf.servers()
class ConfigWatcher(FileSystemEventHandler):
def __init__(self, configPath, reloadFunc):
self.configPath = configPath
self.reloadFunc = reloadFunc
def on_modified(self, event):
self.reloadFunc()