-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetting.py
81 lines (68 loc) · 2.41 KB
/
setting.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
81
#!/usr/bin/env python
# encoding:utf8
"""
Watch_Dogs
配置文件&日志
"""
import os
import sys
import json
import datetime
import logging
import logging.config
LOGGER_CONF_NAME = "logger.conf"
SETTING_JSON_NAME = "setting.json"
class Setting(object):
"""静态配置"""
# Singleton
_instance = None
def __new__(cls, *args, **kw):
"""单例模式"""
if not cls._instance:
cls._instance = super(Setting, cls).__new__(cls, *args, **kw)
return cls._instance
def __init__(self):
global LOGGER_CONF_NAME, SETTING_JSON_NAME
self.log_init_done = False
self.logger = None
now_path = os.path.abspath('.')
self.log_conf_path = os.path.join(now_path, LOGGER_CONF_NAME)
self.setting_json_path = os.path.join(now_path, SETTING_JSON_NAME)
if not os.path.exists(self.log_conf_path) or not os.path.exists(self.setting_json_path):
print "配置文件读取异常 : 请检查", os.path.basename(sys.argv[0]).split(".")[0], \
".py路径下是否有", LOGGER_CONF_NAME, SETTING_JSON_NAME
exit(-1)
self.log_init()
self.static_value_refresh()
PORT = 80
ALLOWED_REQUEST_ADDR_LIST = []
NET_MONITOR = False
# @staticmethod
def static_value_refresh(self):
"""静态值初始化/刷新"""
jsonFile = file(self.setting_json_path)
setting = json.load(jsonFile)
jsonFile.close()
# 读取参数
Setting.ALLOWED_REQUEST_ADDR_LIST = map(lambda u: u.encode("utf-8"), setting["allowed_request_addr"])
Setting.PORT = setting["port"]
Setting.NET_MONITOR = setting["net_monitor"]
if not Setting.ALLOWED_REQUEST_ADDR_LIST: # 若不填则默认允许所有地址发出请求
Setting.ALLOWED_REQUEST_ADDR_LIST = ["0.0.0.0"]
return setting
def log_init(self):
"""对象出初始化"""
# 只进行一次初始化
if not self.log_init_done:
self.log_init_done = True
logging.config.fileConfig(self.log_conf_path)
self.logger = logging.getLogger("main")
return self.logger
@staticmethod
def get_local_time():
"""获取本地时间"""
return str(datetime.datetime.now()).split(".")[0]
@staticmethod
def get_local_date():
"""获取本地日期"""
return str(datetime.datetime.now()).split(" ")[0]