From 6ab298506df9718028212ee364c6106b1b23b8ec Mon Sep 17 00:00:00 2001 From: George Tsiamasiotis Date: Wed, 18 Sep 2024 10:26:27 +0300 Subject: [PATCH] Only use ruamel.yaml in frigate app. --- frigate/test/test_config.py | 3 ++- frigate/util/builtin.py | 31 +------------------------------ 2 files changed, 3 insertions(+), 31 deletions(-) diff --git a/frigate/test/test_config.py b/frigate/test/test_config.py index 5a5592b13..2e8152bb6 100644 --- a/frigate/test/test_config.py +++ b/frigate/test/test_config.py @@ -5,6 +5,7 @@ from unittest.mock import patch import numpy as np from pydantic import ValidationError +from ruamel.yaml.constructor import DuplicateKeyError from frigate.config import BirdseyeModeEnum, FrigateConfig from frigate.const import MODEL_CACHE_DIR @@ -1536,7 +1537,7 @@ class TestConfig(unittest.TestCase): - four """ - self.assertRaises(ValueError, lambda: load_yaml(raw_config)) + self.assertRaises(DuplicateKeyError, lambda: load_yaml(raw_config)) def test_object_filter_ratios_work(self): config = { diff --git a/frigate/util/builtin.py b/frigate/util/builtin.py index 42caf8d30..a01bc8983 100644 --- a/frigate/util/builtin.py +++ b/frigate/util/builtin.py @@ -9,14 +9,12 @@ import queue import re import shlex import urllib.parse -from collections import Counter from collections.abc import Mapping from pathlib import Path from typing import Any, Optional, Tuple import numpy as np import pytz -import yaml from ruamel.yaml import YAML from tzlocal import get_localzone from zoneinfo import ZoneInfoNotFoundError @@ -89,35 +87,8 @@ def deep_merge(dct1: dict, dct2: dict, override=False, merge_lists=False) -> dic return merged -class NoDuplicateKeysLoader(yaml.loader.SafeLoader): - """A yaml SafeLoader that disallows duplicate keys""" - - def construct_mapping(self, node, deep=False): - mapping = super().construct_mapping(node, deep=deep) - - if len(node.value) != len(mapping): - # There's a duplicate key somewhere. Find it. - duplicate_keys = [ - key - for key, count in Counter( - self.construct_object(key, deep=deep) for key, _ in node.value - ) - if count > 1 - ] - - # This might be possible if PyYAML's construct_mapping() changes the node - # afterwards for some reason? I don't see why, but better safe than sorry. - assert len(duplicate_keys) > 0 - - raise ValueError( - "Key redefinitions are not allowed: " + ", ".join(duplicate_keys) - ) - - return mapping - - def load_yaml(raw_config: str) -> dict: - return yaml.load(raw_config, NoDuplicateKeysLoader) + return YAML().load(raw_config) def clean_camera_user_pass(line: str) -> str: