microkodi/microkodi/config.py

78 lines
2.0 KiB
Python

from dataclasses import dataclass
from pathlib import Path
import json
from typing import Any
from microkodi.helpers import recursive_dict_merge
@dataclass
class Config:
host: str
port: int
# Path to the mpv binary
mpv: str
# Path to where the mpv IPC sock should be put
mpv_ipc_sock: str
# Extra args to pass to mpv
mpv_args: list[str]
# Path to the vlc binary
vlc: str
# Extra arguments to pass to vlc
vlc_args: list[str]
# Wallpapers to show
wallpapers: list[str]
# Additional scripts to load
scripts: dict[str, str]
# Player configuration
# URL scheme -> netloc (or '*' for fallback) -> fully-qualified player class
players: dict[str, dict[str, str]]
# The entire configuration file for use in user scripts
options: dict[str, Any]
# Enables the use of CEC
cec: bool
# Webhook to trigger to turn on power to the TV
tv_power_webhook: str | None
def load_config(config_path: Path | None) -> Config:
if config_path is None:
config_data = {}
else:
with open(config_path, "r") as f:
config_data = json.load(f)
return Config(
host=config_data.get("host", "0.0.0.0"),
port=config_data.get("port", 8080),
mpv=config_data.get("mpv", "/usr/bin/mpv"),
mpv_ipc_sock=config_data.get("mpv_ipc_sock", "/tmp/mpv.sock"),
mpv_args=config_data.get("mpv_args", []),
vlc=config_data.get("vlc", "/usr/bin/vlc"),
vlc_args=config_data.get("vlc_args", []),
wallpapers=config_data.get("wallpapers", []),
scripts=config_data.get("scripts", {}),
players=recursive_dict_merge(
{
"https": {
"*": "microkodi.programs.vlc.VlcProgram",
},
},
config_data.get("players", {}),
),
options=config_data.get("options", {}),
cec=config_data.get("cec", False),
tv_power_webhook=config_data.get("tv_power_webhook"),
)