-
-
Notifications
You must be signed in to change notification settings - Fork 62
/
entrypoint.py
71 lines (56 loc) · 2.18 KB
/
entrypoint.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
import os
from pathlib import Path
try:
import yaml
except ImportError:
import subprocess
subprocess.run(["pip", "install", "pyyaml"])
import yaml
config_path = Path('config')
def load_config(name: str) -> dict:
with open(config_path / f'{name}.yaml', 'r') as f:
return yaml.safe_load(f)
def save_config(name: str, config: dict) -> None:
with open(config_path / f'{name}.yaml', 'w') as f:
yaml.safe_dump(config, f, encoding='utf-8', allow_unicode=True)
def set_database():
config = load_config('database')
if 'ENABLE_MYSQL' in os.environ and os.environ['ENABLE_MYSQL']:
config['mode'] = 'mysql'
else:
return
if 'MYSQL_HOST' in os.environ and os.environ['MYSQL_HOST']:
config['config']['host'] = os.environ['MYSQL_HOST']
if 'MYSQL_PORT' in os.environ and os.environ['MYSQL_PORT']:
config['config']['port'] = os.environ['MYSQL_PORT']
if 'MYSQL_USER' in os.environ and os.environ['MYSQL_USER']:
config['config']['user'] = os.environ['MYSQL_USER']
if 'MYSQL_PASSWORD' in os.environ and os.environ['MYSQL_PASSWORD']:
config['config']['password'] = os.environ['MYSQL_PASSWORD']
save_config('database', config)
def set_prefix():
config = load_config('prefix')
if 'PREFIX' in os.environ and os.environ['PREFIX']:
if os.environ['PREFIX'].startswith('[') and os.environ['PREFIX'].endswith(']'):
prefixs = os.environ['PREFIX'][1:-1].split(',')
new = []
for prefix in prefixs:
# remove space
new.append(prefix.strip().replace('\'', '').replace('\"', '').replace('`', ''))
prefixs = new
else:
prefixs = [os.environ['PREFIX']]
config['prefix_keywords'] = prefixs
save_config('prefix', config)
def set_server():
config = load_config('server')
config['host'] = '0.0.0.0'
if 'AUTH' in os.environ and os.environ['AUTH']:
config['authKey'] = os.environ['AUTH']
save_config('server', config)
def main():
set_database()
set_prefix()
set_server()
if __name__ == '__main__':
main()