mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-08-02 17:12:16 +03:00
Improve environment var handling (#22796)
* refactor env var handling - use shared helper - use left-to-right parser * add tests * formatting
This commit is contained in:
+71
-1
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
import re
|
||||
from pathlib import Path
|
||||
from typing import Annotated
|
||||
|
||||
@@ -15,8 +16,77 @@ if os.path.isdir(secrets_dir) and os.access(secrets_dir, os.R_OK):
|
||||
)
|
||||
|
||||
|
||||
# Matches a FRIGATE_* identifier following an opening brace.
|
||||
_FRIGATE_IDENT_RE = re.compile(r"FRIGATE_[A-Za-z0-9_]+")
|
||||
|
||||
|
||||
def substitute_frigate_vars(value: str) -> str:
|
||||
"""Substitute `{FRIGATE_*}` placeholders in *value*.
|
||||
|
||||
Reproduces the subset of `str.format()` brace semantics that Frigate's
|
||||
config has historically supported, while leaving unrelated brace content
|
||||
(e.g. ffmpeg `%{localtime\\:...}` expressions) untouched:
|
||||
|
||||
* `{{` and `}}` collapse to literal `{` / `}` (the documented escape).
|
||||
* `{FRIGATE_NAME}` is replaced from `FRIGATE_ENV_VARS`; an unknown name
|
||||
raises `KeyError` to preserve the existing "Invalid substitution"
|
||||
error path.
|
||||
* A `{` that begins `{FRIGATE_` but is not a well-formed
|
||||
`{FRIGATE_NAME}` placeholder raises `ValueError` (malformed
|
||||
placeholder). Callers that catch `KeyError` to allow unknown-var
|
||||
passthrough will still surface malformed syntax as an error.
|
||||
* Any other `{` or `}` is treated as a literal and passed through.
|
||||
"""
|
||||
out: list[str] = []
|
||||
i = 0
|
||||
n = len(value)
|
||||
while i < n:
|
||||
ch = value[i]
|
||||
if ch == "{":
|
||||
# Escaped literal `{{`.
|
||||
if i + 1 < n and value[i + 1] == "{":
|
||||
out.append("{")
|
||||
i += 2
|
||||
continue
|
||||
# Possible `{FRIGATE_*}` placeholder.
|
||||
if value.startswith("{FRIGATE_", i):
|
||||
ident_match = _FRIGATE_IDENT_RE.match(value, i + 1)
|
||||
if (
|
||||
ident_match is not None
|
||||
and ident_match.end() < n
|
||||
and value[ident_match.end()] == "}"
|
||||
):
|
||||
key = ident_match.group(0)
|
||||
if key not in FRIGATE_ENV_VARS:
|
||||
raise KeyError(key)
|
||||
out.append(FRIGATE_ENV_VARS[key])
|
||||
i = ident_match.end() + 1
|
||||
continue
|
||||
# Looks like a FRIGATE placeholder but is malformed
|
||||
# (no closing brace, illegal char, format spec, etc.).
|
||||
raise ValueError(
|
||||
f"Malformed FRIGATE_ placeholder near {value[i : i + 32]!r}"
|
||||
)
|
||||
# Plain `{` — pass through (e.g. `%{localtime\:...}`).
|
||||
out.append("{")
|
||||
i += 1
|
||||
continue
|
||||
if ch == "}":
|
||||
# Escaped literal `}}`.
|
||||
if i + 1 < n and value[i + 1] == "}":
|
||||
out.append("}")
|
||||
i += 2
|
||||
continue
|
||||
out.append("}")
|
||||
i += 1
|
||||
continue
|
||||
out.append(ch)
|
||||
i += 1
|
||||
return "".join(out)
|
||||
|
||||
|
||||
def validate_env_string(v: str) -> str:
|
||||
return v.format(**FRIGATE_ENV_VARS)
|
||||
return substitute_frigate_vars(v)
|
||||
|
||||
|
||||
EnvString = Annotated[str, AfterValidator(validate_env_string)]
|
||||
|
||||
Reference in New Issue
Block a user