__init__.py
2.12 KB
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
"""
配置分为两部分:
1. 通用配置(common), 如token过期时间,不能发在源代码中, 存放在src/settings下,
2. 私密配置(secret),如帐号、密码, 不能放在源代码中
"""
import os
import sys
from django.core.management.color import color_style
from simple_config import Config, ConfigAttribute, converter
from . import _default_config
_SERVER_TYPE_LST = ['DEV', 'SIT', 'PRD']
def service_is_online(server_type):
return server_type == 'PRD'
class ProjectConfig(Config):
DEBUG = ConfigAttribute('DEBUG', get_converter=converter.boolean)
SERVICE_IS_ONLINE = ConfigAttribute(
'SERVER_TYPE', get_converter=service_is_online)
ALLOWED_HOSTS = ConfigAttribute(
'ALLOWED_HOSTS', get_converter=converter.Csv())
# ETCD_PORT = ConfigAttribute('ETCD_PORT', get_converter=int)
def load_config(self):
# 加载默认配置
self.from_object(_default_config)
self.load_secret_config()
self.load_common_config()
def load_common_config(self):
"""加载配置文件
"""
self.from_env_var('SERVER_TYPE')
assert self.SERVER_TYPE in _SERVER_TYPE_LST
config_file = self.get_common_config_filename(self.COMMON_CONF_DIR,
self.SERVER_TYPE)
if self.SERVER_TYPE == 'DEV' and not os.path.exists(config_file):
msg = '[SERVER_TYPE: %s] config_file: %s does not exist' % (
self.SERVER_TYPE, config_file)
config_file = self.get_common_config_filename(
self.COMMON_CONF_DIR, 'SIT')
msg = '%s, try to use %s as alternative\n' % (msg, config_file)
style = color_style()
sys.stdout.write(style.NOTICE(msg))
self.from_ini(config_file)
def load_secret_config(self):
"""加载私密配置
"""
self.from_ini(self.SECRET_CONF_FILE)
@staticmethod
def get_common_config_filename(conf_dir, server_type):
return os.path.join(conf_dir, '%s.ini' % server_type.lower())
config = ProjectConfig()
config.load_config()
sys.modules[__name__] = config