blob: ca23e5fa0bed6298a6cfb9407273c56d62fa9ba8 (
plain)
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
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/1/8 12:36
# @author : yinjinagyi
# @File : Config.py
# @Function: read config23.10.yaml file and manage all configurations
import yaml
import os
import sys
import LoggingTool
sys.path.append('..')
from tool.Functions import get_project_path
logger = LoggingTool.Logger().getLogger()
class Config:
def __init__(self):
# read version information from version.txt, and choose yaml file according to version
with open(get_project_path() + '/version.txt', 'r') as f:
version = f.read().strip()
# 如果文件不存在则报错,日志写入日志文件
if not os.path.exists(get_project_path() + '/config{}.yaml'.format(version)):
logger.error('config{}.yaml file does not exist'.format(version))
sys.exit()
self.config = self.read_yaml(get_project_path() + '/config{}.yaml'.format(version))
# read yaml file
def read_yaml(self, yaml_file):
try:
with open(yaml_file, 'r', encoding='utf-8') as f:
config = yaml.load(f.read(), Loader=yaml.FullLoader)
return config
except Exception as e:
print(e)
sys.exit()
if __name__ == '__main__':
config = Config().config
print(config)
print(config['time_zone'])
print(config['sql']['cyberghost']['sql_tsg'])
|