-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathconfig.py
181 lines (139 loc) · 5.89 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import json
from copy import deepcopy
from os.path import expanduser
class conf_base(object):
def __init__(self, valid_fields, conf_dict):
# each item in the _valid_fields is a tuple represents
# (key, default_val, type, validate_regex, call_function)
self._valid_fields = valid_fields
self._conf = {}
self.load(conf_dict)
def load(self, conf_dict):
for field in self._valid_fields:
key = field[0]
dft_val = field[1]
val_type = field[2]
vld_regx = field[3]
func = field[4]
# More check can be made here
if key in conf_dict:
self._conf[key] = conf_dict[key] if func is None else func(conf_dict.get(key, dft_val))
self._conf[key] = int(self._conf[key]) if val_type == 'int' else self._conf[key]
elif dft_val is not None:
self._conf[key] = dft_val if func is None else func(conf_dict.get(key, dft_val))
def get_val(self, key):
return self._conf[key]
def __getitem__(self, key):
return self.get_val(key)
def set_val(self, key, val):
self._conf[key] = val
def __setitem__(self, key, val):
self.set_val(key, val)
def dict(self):
return self._conf
class ydl_conf(conf_base):
_valid_fields = [
#(key, default_val, type, validate_regex, call_function)
('proxy', None, 'string', None, None),
('format', None, 'string', None, None),
('ratelimit', 1048576, 'int', None, None),
('outtmpl', None, 'string', None, None),
]
_task_settable_fields = set(['format'])
def __init__(self, conf_dict={}):
self.logger = logging.getLogger('ydl_webui')
super(ydl_conf, self).__init__(self._valid_fields, conf_dict)
def merge_conf(self, task_conf_dict={}):
ret = deepcopy(self.dict())
for key, val in task_conf_dict.items():
if key not in self._task_settable_fields or val == '':
continue
ret[key] = val
return ret
class svr_conf(conf_base):
_valid_fields = [
#(key, default_val, type, validate_regex, call_function)
('host', '127.0.0.1', 'string', None, None),
('port', '5000', 'string', None, None),
]
def __init__(self, conf_dict={}):
self.logger = logging.getLogger('ydl_webui')
super(svr_conf, self).__init__(self._valid_fields, conf_dict)
class gen_conf(conf_base):
_valid_fields = [
#(key, default_val, type, validate_regex, call_function)
('download_dir', '~/Downloads/youtube-dl', 'string', '', expanduser),
('db_path', '~/.conf/ydl_webui.db', 'string', '', expanduser),
('log_size', 10, 'int', '', None),
]
def __init__(self, conf_dict={}):
self.logger = logging.getLogger('ydl_webui')
super(gen_conf, self).__init__(self._valid_fields, conf_dict)
class conf(object):
_valid_fields = set(('youtube_dl', 'server', 'general'))
ydl_conf = None
svr_conf = None
gen_conf = None
def __init__(self, conf_file, conf_dict={}, cmd_args={}):
self.logger = logging.getLogger('ydl_webui')
self.conf_file = conf_file
self.cmd_args = cmd_args
self.load(conf_dict)
def cmd_args_override(self):
_cat_dict = {'host': 'server',
'port': 'server'}
for key, val in self.cmd_args.items():
if key not in _cat_dict or val is None:
continue
sub_conf = self.get_val(_cat_dict[key])
sub_conf.set_val(key, val)
def load(self, conf_dict):
if not isinstance(conf_dict, dict):
self.logger.error("input parameter(conf_dict) is not an instance of dict")
return
for f in self._valid_fields:
if f == 'youtube_dl':
self.ydl_conf = ydl_conf(conf_dict.get(f, {}))
elif f == 'server':
self.svr_conf = svr_conf(conf_dict.get(f, {}))
elif f == 'general':
self.gen_conf = gen_conf(conf_dict.get(f, {}))
# override configurations by cmdline arguments
self.cmd_args_override()
def save2file(self):
if self.conf_file is not None:
try:
with open(self.conf_file, 'w') as f:
json.dump(self.dict(), f, indent=4)
except PermissionError:
return (False, 'permission error')
except FileNotFoundError:
return (False, 'can not find file')
else:
return (True, None)
def dict(self):
d = {}
for f in self._valid_fields:
if f == 'youtube_dl':
d[f] = self.ydl_conf.dict()
elif f == 'server':
d[f] = self.svr_conf.dict()
elif f == 'general':
d[f] = self.gen_conf.dict()
return d
def get_val(self, key):
if key not in self._valid_fields:
raise KeyError(key)
if key == 'youtube_dl':
return self.ydl_conf
elif key == 'server':
return self.svr_conf
elif key == 'general':
return self.gen_conf
else:
raise KeyError(key)
def __getitem__(self, key):
return self.get_val(key)