Skip to content

Commit

Permalink
Only use ruamel.yaml in frigate app.
Browse files Browse the repository at this point in the history
  • Loading branch information
gtsiam committed Sep 18, 2024
1 parent c62c080 commit 6ab2985
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 31 deletions.
3 changes: 2 additions & 1 deletion frigate/test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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
Expand Down Expand Up @@ -1536,7 +1537,7 @@ def test_fails_duplicate_keys(self):
- 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 = {
Expand Down
31 changes: 1 addition & 30 deletions frigate/util/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 6ab2985

Please sign in to comment.