This repository has been archived by the owner on Mar 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathconfig.py
142 lines (115 loc) · 4.2 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import getopt
import sys
import logging
# 定义配置文件目录
# linux
config_path = '/etc/dnspod/ddnsrc'
# windows
if os.name == 'nt':
config_path = 'ddnspod.cfg'
# 配置属性,及其默认参数
cfg = {}
# dnspod 登录配置
cfg["login_token"] = '' # 默认空参数,要求用户必填 格式 'token_id,token' # 登录token
# ddns 基本配置
cfg["sub_domain"] = '' # 默认空参数,要求用户必填 #格式 'www' # 子域名
cfg["domain"] = '' # 默认空参数,要求用户必填 #格式 'domain.com' # 域名
cfg["interval"] = '5' # 最小更新间隔
cfg["record_id"] = '{auto}' # 记录id,程序自动生成
cfg["current_ip"] = '{auto}' # 当前ip,程序自动生成
cfg["email"] = '' # 默认空参数,要求用户必填 #格式 '[email protected]'
# ip 池
cfg["ip_count"] = '1' # 此域名拥有的ip数量,默认为 1 , OpenWrt 玩家可能会有多个IP
cfg["ip_pool"] = '{auto}' # ip 池 ..,程序自动生成
cfg["last_update_time"] = '{auto}' # 上次更新成功时间戳,程序自动生成
def read_config():
# 后面读出的数据会覆盖前面的
read_config_from_file()
read_config_from_env()
read_config_from_argv()
def print_help():
max_key_len = max([len(key) for key in cfg.keys()])
print("ddns.py [-h|...]")
print("命令行方式调用,可用的参数如下:")
for name in cfg.keys():
print(' --%-' + str(max_key_len) + 's <value>' % name)
print("配置优先级: 命令行 > 环境变量 > 配置文件")
print("当前配置文件目录为:%s" % config_path)
def read_config_from_file():
try:
with open(config_path, 'rU') as fp:
for line in fp:
pair = [x.strip() for x in line.split('=')]
if pair[0] and pair[1]:
cfg[pair[0].lower()] = pair[1]
except:
pass
def read_config_from_env():
for key in cfg:
if os.getenv(key) is not None:
cfg[key] = os.getenv(key)
# 从命令行读取参数
def read_config_from_argv():
available_args = [x + "=" for x in cfg.keys()]
try:
opts, _ = getopt.getopt(sys.argv[1:], "h", available_args)
for opt, arg in opts:
if opt == '-h':
print_help()
sys.exit()
if opt.startswith('--'):
pair = [opt[2:], arg]
if pair[0] and pair[1]:
cfg[pair[0].lower()] = pair[1]
except getopt.GetoptError:
print_help()
sys.exit(1)
def save_config():
try:
save_config_to_env()
save_config_to_file()
except NotImplementedError as err:
logging.error("FAILED to save config:" + str(err))
# 不太清楚这个函数能干啥用 = = 写着玩。。。
def save_config_to_env():
for key in cfg:
os.environ[key] = cfg[key]
# 保存配置到文件… 这个函数现在会把配置文件里的注释也删掉……
def save_config_to_file():
max_key_len = max([len(key) for key in cfg.keys()])
try:
with open(config_path, "w+") as f:
f.writelines([
('%-'+str(max_key_len)+'s=%s\n') % (key, cfg[key])
for key in cfg.keys()
])
except IOError as err:
logging.error("FAILED to save config to file: " + str(err))
# 检查配置是否齐全
def check_config():
if not (
cfg['login_token'] and
cfg['domain'] and
cfg['sub_domain']):
logging.fatal('config error: need login info')
exit()
try:
if not(int(cfg["interval"])):
logging.fatal('interval error')
exit()
if not(int(cfg["ip_count"])):
logging.fatal('ip_count error')
exit()
except:
logging.fatal('config error')
exit()
logging.info('config checked')
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO,
format='%(levelname)-8s: %(message)s')
# 测试配置文件
logging.info("init cfg: %s" % cfg)
read_config()
logging.info("read cfg: %s" % cfg)
check_config()