From 056fa47cf93fbdb199af8450b7c7fc0de1dea3bc Mon Sep 17 00:00:00 2001 From: you Date: Sat, 29 Jun 2024 19:15:13 +0200 Subject: [PATCH] Updates existing configurations by modifying the format of environment variables. --- frigate/util/config.py | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/frigate/util/config.py b/frigate/util/config.py index 0db523af6..04f6305dc 100644 --- a/frigate/util/config.py +++ b/frigate/util/config.py @@ -3,6 +3,7 @@ import asyncio import logging import os +import re import shutil from typing import Optional, Union @@ -13,7 +14,7 @@ from frigate.util.services import get_video_properties logger = logging.getLogger(__name__) -CURRENT_CONFIG_VERSION = 0.14 +CURRENT_CONFIG_VERSION = 0.15 def migrate_frigate_config(config_file: str): @@ -55,6 +56,13 @@ def migrate_frigate_config(config_file: str): os.path.join(EXPORT_DIR, file), os.path.join(EXPORT_DIR, new_name) ) + if previous_version < 0.15: + logger.info(f"Migrating frigate config from {previous_version} to 0.15...") + new_config = migrate_015(config) + with open(config_file, "w") as f: + yaml.dump(new_config, f) + previous_version = 0.15 + logger.info("Finished frigate config migration...") @@ -144,6 +152,36 @@ def migrate_014(config: dict[str, dict[str, any]]) -> dict[str, dict[str, any]]: return new_config +def migrate_015(config: dict[str, dict[str, any]]) -> dict[str, dict[str, any]]: + """Handle migrating frigate config to 0.15""" + + def update_placeholders(value): + pattern = re.compile(r"(\"|')?\{FRIGATE_[^\}]+\}(\"|')?") + if isinstance(value, str): + return pattern.sub(replacer, value) + elif isinstance(value, dict): + return {k: update_placeholders(v) for k, v in value.items()} + elif isinstance(value, list): + return [update_placeholders(v) for v in value] + return value + + def replacer(match): + matched_string = match.group(0) + # Remove surrounding quotes if they exist + if matched_string.startswith(("'", '"')): + matched_string = matched_string[1:] + if matched_string.endswith(("'", '"')): + matched_string = matched_string[:-1] + # Replace the prefix + return '${' + matched_string[1:] + + new_config = config.copy() + new_config = update_placeholders(new_config) + new_config["version"] = 0.15 + + return new_config + + def get_relative_coordinates( mask: Optional[Union[str, list]], frame_shape: tuple[int, int] ) -> Union[str, list]: