mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-12 22:25:24 +03:00
Uses PyYAML's implicit resolver to handle all variable substitutions when loading the YAML file.
Environment variables must use the ${FRIGATE } format (add the $ symbol) and must not be enclosed in quotation marks.
This commit is contained in:
parent
0d63838f02
commit
b3b6c4d7fe
@ -5,6 +5,7 @@ import datetime
|
|||||||
import logging
|
import logging
|
||||||
import multiprocessing as mp
|
import multiprocessing as mp
|
||||||
import queue
|
import queue
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
import shlex
|
import shlex
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
@ -96,6 +97,18 @@ def load_config_with_no_duplicates(raw_config) -> dict:
|
|||||||
class PreserveDuplicatesLoader(yaml.loader.SafeLoader):
|
class PreserveDuplicatesLoader(yaml.loader.SafeLoader):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
env_var_pattern = re.compile(r'\$\{FRIGATE_[A-Z0-9_]+\}')
|
||||||
|
|
||||||
|
def env_var_constructor(loader, node):
|
||||||
|
value = loader.construct_scalar(node)
|
||||||
|
matches = env_var_pattern.findall(value)
|
||||||
|
for match in matches:
|
||||||
|
env_var_name = match[2:-1] # Strip '${' and '}'
|
||||||
|
env_var_value = os.getenv(env_var_name)
|
||||||
|
if env_var_value is not None:
|
||||||
|
value = value.replace(match, env_var_value)
|
||||||
|
return value
|
||||||
|
|
||||||
def map_constructor(loader, node, deep=False):
|
def map_constructor(loader, node, deep=False):
|
||||||
keys = [loader.construct_object(node, deep=deep) for node, _ in node.value]
|
keys = [loader.construct_object(node, deep=deep) for node, _ in node.value]
|
||||||
vals = [loader.construct_object(node, deep=deep) for _, node in node.value]
|
vals = [loader.construct_object(node, deep=deep) for _, node in node.value]
|
||||||
@ -110,6 +123,13 @@ def load_config_with_no_duplicates(raw_config) -> dict:
|
|||||||
data[key] = val
|
data[key] = val
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
PreserveDuplicatesLoader.add_implicit_resolver(
|
||||||
|
'!env_var', env_var_pattern, None
|
||||||
|
)
|
||||||
|
PreserveDuplicatesLoader.add_constructor(
|
||||||
|
'!env_var', env_var_constructor
|
||||||
|
)
|
||||||
|
|
||||||
PreserveDuplicatesLoader.add_constructor(
|
PreserveDuplicatesLoader.add_constructor(
|
||||||
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, map_constructor
|
yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, map_constructor
|
||||||
)
|
)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user