forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
68 lines (54 loc) · 1.26 KB
/
__init__.py
File metadata and controls
68 lines (54 loc) · 1.26 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
67
68
# encoding: utf-8
__author__ = 'zhanghe'
import os
# print os.getcwd()
# print os.path.dirname(os.path.abspath(__file__)) # 应该使用这种方式
def get_env():
"""
获取运行环境
"""
file_name = '/'.join((os.path.dirname(os.path.abspath(__file__)), 'env.conf'))
with open(file_name) as f:
env = f.read()
if env:
return env.strip()
else:
return ''
def get_config(key=None):
"""
获取配置文件
:return:
"""
config_db = None
config_proxy = None
env = get_env()
if env == 'dev':
import dev
config_db = dev.db
config_proxy = dev.proxy
if env == 'online':
import online
config_db = online.db
config_proxy = online.proxy
if key is None:
return {'db': config_db, 'proxy': config_proxy}
if key == 'db':
return config_db
if key == 'proxy':
return config_proxy
def set_config():
pass
# 配置项目
db = get_config('db')
proxy = get_config('proxy')
if __name__ == '__main__':
print get_config()
print get_config('db')
"""
环境切换
$ echo 'dev' > test/config/env.conf
$ echo 'online' > test/config/env.conf
使用配置
from config import db
from config import proxy
"""