-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathconfig_loader.py
More file actions
85 lines (69 loc) · 2.38 KB
/
config_loader.py
File metadata and controls
85 lines (69 loc) · 2.38 KB
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
from __future__ import annotations
from argparse import ArgumentParser, Namespace
from typing import Any, Dict
IGNORED_CONFIG_KEYS = {"platform", "auto_start", "custom_video_pipeline", "video_device", "video_format"}
CONFIG_ARG_ALIASES = {"stream_id": "streamid"}
VIDEO_SOURCE_OVERRIDE_ATTRS = (
"test",
"hdmi",
"camlink",
"z1",
"z1passthru",
"apple",
"v4l2",
"libcamera",
"rpicam",
"nvidiacsi",
"pipeline",
"video_pipeline",
"filesrc",
"filesrc2",
"pipein",
"novideo",
)
def _arg_is_default(args: Namespace, parser: ArgumentParser, attr: str) -> bool:
if not hasattr(args, attr):
return False
return getattr(args, attr) == parser.get_default(attr)
def _video_source_has_cli_override(args: Namespace, parser: ArgumentParser) -> bool:
for attr in VIDEO_SOURCE_OVERRIDE_ATTRS:
if hasattr(args, attr) and getattr(args, attr) != parser.get_default(attr):
return True
return False
def _apply_video_source_override(
args: Namespace,
parser: ArgumentParser,
value: Any,
config: Dict[str, Any],
) -> None:
if _video_source_has_cli_override(args, parser):
return
if value == "test" and _arg_is_default(args, parser, "test"):
args.test = True
elif value == "libcamera" and _arg_is_default(args, parser, "libcamera"):
args.libcamera = True
elif value == "v4l2" and _arg_is_default(args, parser, "v4l2"):
args.v4l2 = config.get("video_device", "/dev/video0")
elif value == "custom" and _arg_is_default(args, parser, "video_pipeline"):
custom_pipeline = config.get("custom_video_pipeline")
if custom_pipeline:
args.video_pipeline = custom_pipeline
def apply_config_overrides(
args: Namespace,
parser: ArgumentParser,
config: Dict[str, Any],
) -> Namespace:
for key, value in config.items():
if key in IGNORED_CONFIG_KEYS:
continue
if key == "audio_enabled":
if value is False and _arg_is_default(args, parser, "noaudio"):
args.noaudio = True
continue
if key == "video_source":
_apply_video_source_override(args, parser, value, config)
continue
target_key = CONFIG_ARG_ALIASES.get(key, key)
if _arg_is_default(args, parser, target_key):
setattr(args, target_key, value)
return args