Standardize handling of config files

This commit is contained in:
Nicolas Mowen 2024-12-11 17:13:41 -07:00
parent 6b12a45a95
commit 26507c4ad7
5 changed files with 21 additions and 39 deletions

View File

@ -28,6 +28,7 @@ from frigate.util.builtin import (
get_tz_modifiers, get_tz_modifiers,
update_yaml_from_url, update_yaml_from_url,
) )
from frigate.util.config import find_config_file
from frigate.util.services import ( from frigate.util.services import (
ffprobe_stream, ffprobe_stream,
get_nvidia_driver_info, get_nvidia_driver_info,
@ -147,13 +148,7 @@ def config(request: Request):
@router.get("/config/raw") @router.get("/config/raw")
def config_raw(): def config_raw():
config_file = os.environ.get("CONFIG_FILE", "/config/config.yml") config_file = find_config_file()
# Check if we can use .yaml instead of .yml
config_file_yaml = config_file.replace(".yml", ".yaml")
if os.path.isfile(config_file_yaml):
config_file = config_file_yaml
if not os.path.isfile(config_file): if not os.path.isfile(config_file):
return JSONResponse( return JSONResponse(
@ -198,13 +193,7 @@ def config_save(save_option: str, body: Any = Body(media_type="text/plain")):
# Save the config to file # Save the config to file
try: try:
config_file = os.environ.get("CONFIG_FILE", "/config/config.yml") config_file = find_config_file()
# Check if we can use .yaml instead of .yml
config_file_yaml = config_file.replace(".yml", ".yaml")
if os.path.isfile(config_file_yaml):
config_file = config_file_yaml
with open(config_file, "w") as f: with open(config_file, "w") as f:
f.write(new_config) f.write(new_config)
@ -253,13 +242,7 @@ def config_save(save_option: str, body: Any = Body(media_type="text/plain")):
@router.put("/config/set") @router.put("/config/set")
def config_set(request: Request, body: AppConfigSetBody): def config_set(request: Request, body: AppConfigSetBody):
config_file = os.environ.get("CONFIG_FILE", f"{CONFIG_DIR}/config.yml") config_file = find_config_file()
# Check if we can use .yaml instead of .yml
config_file_yaml = config_file.replace(".yml", ".yaml")
if os.path.isfile(config_file_yaml):
config_file = config_file_yaml
with open(config_file, "r") as f: with open(config_file, "r") as f:
old_raw_config = f.read() old_raw_config = f.read()

View File

@ -29,6 +29,7 @@ from frigate.util.builtin import (
) )
from frigate.util.config import ( from frigate.util.config import (
StreamInfoRetriever, StreamInfoRetriever,
find_config_file,
get_relative_coordinates, get_relative_coordinates,
migrate_frigate_config, migrate_frigate_config,
) )
@ -67,7 +68,6 @@ logger = logging.getLogger(__name__)
yaml = YAML() yaml = YAML()
DEFAULT_CONFIG_FILE = "/config/config.yml"
DEFAULT_CONFIG = """ DEFAULT_CONFIG = """
mqtt: mqtt:
enabled: False enabled: False
@ -638,16 +638,13 @@ class FrigateConfig(FrigateBaseModel):
@classmethod @classmethod
def load(cls, **kwargs): def load(cls, **kwargs):
config_path = os.environ.get("CONFIG_FILE", DEFAULT_CONFIG_FILE) config_path = find_config_file()
if not os.path.isfile(config_path):
config_path = config_path.replace("yml", "yaml")
# No configuration file found, create one. # No configuration file found, create one.
new_config = False new_config = False
if not os.path.isfile(config_path): if not os.path.isfile(config_path):
logger.info("No config file found, saving default config") logger.info("No config file found, saving default config")
config_path = DEFAULT_CONFIG_FILE config_path = config_path
new_config = True new_config = True
else: else:
# Check if the config file needs to be migrated. # Check if the config file needs to be migrated.

View File

@ -136,17 +136,17 @@ class Rknn(DetectionApi):
def check_config(self, config): def check_config(self, config):
if (config.model.width != 320) or (config.model.height != 320): if (config.model.width != 320) or (config.model.height != 320):
raise Exception( raise Exception(
"Make sure to set the model width and height to 320 in your config.yml." "Make sure to set the model width and height to 320 in your config."
) )
if config.model.input_pixel_format != "bgr": if config.model.input_pixel_format != "bgr":
raise Exception( raise Exception(
'Make sure to set the model input_pixel_format to "bgr" in your config.yml.' 'Make sure to set the model input_pixel_format to "bgr" in your config.'
) )
if config.model.input_tensor != "nhwc": if config.model.input_tensor != "nhwc":
raise Exception( raise Exception(
'Make sure to set the model input_tensor to "nhwc" in your config.yml.' 'Make sure to set the model input_tensor to "nhwc" in your config.'
) )
def detect_raw(self, tensor_input): def detect_raw(self, tensor_input):

View File

@ -2,7 +2,6 @@
import copy import copy
import logging import logging
import os
import queue import queue
import threading import threading
import time import time
@ -29,11 +28,11 @@ from frigate.const import (
AUTOTRACKING_ZOOM_EDGE_THRESHOLD, AUTOTRACKING_ZOOM_EDGE_THRESHOLD,
AUTOTRACKING_ZOOM_IN_HYSTERESIS, AUTOTRACKING_ZOOM_IN_HYSTERESIS,
AUTOTRACKING_ZOOM_OUT_HYSTERESIS, AUTOTRACKING_ZOOM_OUT_HYSTERESIS,
CONFIG_DIR,
) )
from frigate.ptz.onvif import OnvifController from frigate.ptz.onvif import OnvifController
from frigate.track.tracked_object import TrackedObject from frigate.track.tracked_object import TrackedObject
from frigate.util.builtin import update_yaml_file from frigate.util.builtin import update_yaml_file
from frigate.util.config import find_config_file
from frigate.util.image import SharedMemoryFrameManager, intersection_over_union from frigate.util.image import SharedMemoryFrameManager, intersection_over_union
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -328,13 +327,7 @@ class PtzAutoTracker:
self.autotracker_init[camera] = True self.autotracker_init[camera] = True
def _write_config(self, camera): def _write_config(self, camera):
config_file = os.environ.get("CONFIG_FILE", f"{CONFIG_DIR}/config.yml") config_file = find_config_file()
# Check if we can use .yaml instead of .yml
config_file_yaml = config_file.replace(".yml", ".yaml")
if os.path.isfile(config_file_yaml):
config_file = config_file_yaml
logger.debug( logger.debug(
f"{camera}: Writing new config with autotracker motion coefficients: {self.config.cameras[camera].onvif.autotracking.movement_weights}" f"{camera}: Writing new config with autotracker motion coefficients: {self.config.cameras[camera].onvif.autotracking.movement_weights}"

View File

@ -14,6 +14,15 @@ from frigate.util.services import get_video_properties
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
CURRENT_CONFIG_VERSION = "0.15-0" CURRENT_CONFIG_VERSION = "0.15-0"
DEFAULT_CONFIG_FILE = "/config/config.yml"
def find_config_file() -> str:
config_path = os.environ.get("CONFIG_FILE", DEFAULT_CONFIG_FILE)
if not os.path.isfile(config_path):
config_path = config_path.replace("yml", "yaml")
return config_path
def migrate_frigate_config(config_file: str): def migrate_frigate_config(config_file: str):