-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathconfig.py
84 lines (65 loc) · 2.41 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
import configparser
_CFGFILE = 'config.cfg'
_DEFAULTS = {
'main': {
'snapshot_seek': '-1',
'converter': '',
'output_format': 'webm',
'snapshot_format': '',
},
'advanced': {
'converter_args': ('-c:v libvpx -b:v 2M -crf 10'
' -qmin 10 -qmax 42 -cpu-used 5'
' -v error'),
'converter_args_snap': '-frames:v 1 -v error',
'ffmpeg_args': '-vcodec copy -v error',
'ffmpeg_args_snap': '-vframes 1 -v error',
'snapshot_pos': 0.5,
'db_file': 'index.db',
'h_index_file': 'index00.bin',
},
}
_MANDATORY = {
'main': ('data_dir', 'output_dir'),
}
class Config(configparser.ConfigParser):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def configure(self, cfgfile=_CFGFILE):
self._load_defaults()
self._read_file(cfgfile)
self.validate()
def save(self, cfgfile=_CFGFILE):
self._write_file(cfgfile)
def _load_defaults(self):
self.read_dict(_DEFAULTS)
def _read_file(self, cfgfile):
self.read(cfgfile)
def _write_file(self, cfgfile):
with open(cfgfile, 'w') as f:
self.write(f)
def validate(self):
for sec, keys in _MANDATORY.items():
if sec not in self:
raise ConfigError(
'Section [{}] must be present in config file'
.format(sec)
)
for k in keys:
if k not in self[sec]:
raise ConfigError(
'Parameter `{}` must be present in section [{}]'
.format(k, sec)
)
if self.getboolean('main', 'analyze_motion'):
try:
import cv2 # noqa
import numpy # noqa
except ImportError:
raise Exception('analyze_motion requires OpenVC and numpy')
class ConfigError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
config = Config()