-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
66 lines (49 loc) · 1.63 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
import ruamel.yaml
import logging
import platform
yaml = ruamel.yaml.YAML(typ='rt')
DEFAULTS_GENERAL = {
'port': 5000,
'log_level': logging.INFO,
'use_pulseaudio': True if platform.system() == 'linux' else False
}
DEFAULTS_STREAM = {
'device': 'pulse' if platform.system() == 'linux' else None,
'rate': 44100,
'channels': 2,
'max_segments': 5,
'name': None,
'segment_duration': 5,
'format': 'paInt16'
}
class PDConfig:
yaml_tag = 'PocketDiscoConfiguration'
def __init__(self):
for k in DEFAULTS_GENERAL.keys():
self.__dict__[k] = DEFAULTS_GENERAL[k]
self.streams = [PDStreamConfig()]
@classmethod
def from_file(cls, path):
with open(path, 'r') as f:
cfg = yaml.load(f)
if type(cfg) != PDConfig:
raise ValueError('Invalid Configuration!')
for k in DEFAULTS_GENERAL.keys():
if not hasattr(cfg, k):
cfg.__dict__[k] = DEFAULTS_GENERAL[k]
if not hasattr(cfg, 'streams'):
raise ValueError('Invalid Configuration!')
if len(cfg.streams) == 0:
raise ValueError('No streams were configured!')
for s in cfg.streams:
for k in DEFAULTS_STREAM.keys():
if not hasattr(s, k):
s.__dict__[k] = DEFAULTS_STREAM[k]
return cfg
class PDStreamConfig:
yaml_tag = 'stream'
def __init__(self):
for k in DEFAULTS_STREAM.keys():
self.__dict__[k] = DEFAULTS_STREAM[k]
yaml.register_class(PDConfig)
yaml.register_class(PDStreamConfig)