forked from haikerapples/timetask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
46 lines (35 loc) · 1.12 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
# encoding:utf-8
import json
import logging
import os
class TimeTaskConfig(dict):
def __init__(self, d=None):
super().__init__()
if d is None:
d = {}
for k, v in d.items():
self[k] = v
def get(self, key, default=None):
try:
return self[key]
except KeyError as e:
return default
except Exception as e:
raise e
config = TimeTaskConfig()
def load_config():
global config
curdir = os.path.dirname(__file__)
config_path = os.path.join(curdir, "config.json")
if not os.path.exists(config_path):
logging.info("配置文件不存在,将使用config-template.json模板")
config_path = os.path.join(curdir, "config-template.json")
config_str = read_file(config_path)
logging.info("[timetask - INIT] config str: {}".format(config_str))
# 将json字符串反序列化为dict类型
config = TimeTaskConfig(json.loads(config_str))
def read_file(path):
with open(path, mode="r", encoding="utf-8") as f:
return f.read()
def conf():
return config