-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
80 lines (70 loc) · 2.65 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
from openttd_server import OpenTTDServer
from dbconfig import DbConfig
from general import GeneralConfig
from jsonhelper import JsonHelper
from os import path
class Config:
__default_filename = "config.json"
__full_path = ''
def __init__(self, config_path='', dump_default=False):
if config_path:
Config.__full_path = config_path
else:
Config.__full_path = path.join(Config.__default_filename)
if dump_default or not self.load():
self.config = Config.__default()
self.save()
@property
def servers(self):
return map(lambda x: OpenTTDServer(**x), self.config['config']['servers'])
@property
def database(self):
return DbConfig(**self.config['config']['database'])
@property
def general(self):
return GeneralConfig(**self.config['config']['general'])
def load(self):
try:
self.config = JsonHelper.from_json_file(Config.__full_path)
return True
except IOError:
return False
def save(self):
try:
JsonHelper.to_json_file(self.config, Config.__full_path)
except IOError:
pass
@staticmethod
def __default():
return {
"config":
{
"servers": [
{
"name": "server1",
"interface": "adminport",
"host": "localhost",
"port": 3977,
"password": ""
},
{
"name": "server2",
"interface": "ottdpoxy",
"url": "http://localhost:3017/stats",
"password": ""
}
],
"database": {
"type": "mysql",
"host": "localhost",
"port": 3306,
"username": "",
"password": "",
"database": "ottdstats"
},
"general": {
"daemon": False,
"interval": 30
}
}
}