-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
executable file
·81 lines (67 loc) · 2.74 KB
/
cli.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
#!/usr/bin/env python3
import sys
import logging
import os
import yaml
import click
from multiviewer import *
from encoder import *
logging.basicConfig(format='%(asctime)s %(name)s %(levelname)s %(message)s',
datefmt='%a, %d %b %Y %H:%M:%S +0000')
class PinballRig:
def __init__(self, config, mbps):
self.config = config
self.mbps = mbps
self.logger = logging.getLogger(f'{__name__}')
self.logger.setLevel(logging.DEBUG if self.config.get(
'local').get('debug') else logging.INFO)
def configure(self, config, profile):
try:
mv = uhdmcu.FourXOneUHD(config)
mv.apply(profile['mv'])
except Exception as exc:
self.logger.error(f'Unable to apply MV config: "{exc}"')
raise RigBroke from exc
if not config['local']['ignore_encoder'] or not os.path.exists(config['local']['serial_port']):
try:
enc = magewell.ProConvert(config, self.mbps)
enc.apply(profile['encoder'])
except Exception as exc:
self.logger.error(f'Unable to apply Encoder config: {exc}')
raise RigBroke from exc
class RigBroke(Exception):
pass
@click.command()
@click.option('--config', type=click.Path(exists=True),
default='config.yaml', help='config.yaml formatted file')
@click.option('--profile', default='default', help='Profile name in --config')
@click.option('--mbps', default=0, help='Throttle encoder bitrate to Mbit/s, overrides bitrate in `encoder` sections in --config. Use with caution.')
@click.option('--debug', default=False, help='Turn on very verbose logging and override --config flag.', is_flag=True)
def apply(config, profile, mbps, debug):
logger = logging.getLogger(f'{__name__}')
if debug:
logger.setLevel(logging.DEBUG)
with open(config, encoding="utf-8") as stream:
try:
cf = yaml.safe_load(stream)
except yaml.YAMLError as exc:
logger.error(f'Unable to read config file "{config}" with exception: "{exc}"')
sys.exit(1)
config = cf.get('config')
profiles = cf.get('profiles')
if debug:
config['local']['debug'] = True
if config and profiles.get(profile):
rig = PinballRig(config, mbps)
try:
rig.configure(config, profiles.get(profile))
logger.debug(f'Rig configured with profile: "{profile}"')
except RigBroke:
logger.error('Failed configuring the rig, check logs.')
sys.exit(1)
else:
logger.error(f'The `config` stanza or profile "{profile}" in the `profiles` \
stanza invalid in --config file')
sys.exit(1)
if __name__ == '__main__':
apply(None, None, None, None)