Video-Summary/Application/Config.py

57 lines
1.6 KiB
Python
Raw Normal View History

2022-10-11 19:35:37 +00:00
import json
import os
from typing import Any, Optional
from Application.Logger import get_logger
logger = get_logger(__name__)
2022-10-11 19:35:37 +00:00
class Config:
c = {
"min_area": 300,
2022-01-09 19:25:44 +00:00
"max_area": 900000,
2022-10-11 19:35:37 +00:00
"threshold": 7,
"resizeWidth": 700,
2022-01-09 19:25:44 +00:00
"inputPath": None,
2020-10-11 15:09:49 +00:00
"outputPath": None,
2022-01-09 19:25:44 +00:00
"maxLayerLength": 5000,
"minLayerLength": 40,
2020-12-18 20:27:19 +00:00
"tolerance": 20,
"maxLength": None,
2020-12-22 12:58:47 +00:00
"ttolerance": 60,
"videoBufferLength": 250,
2020-12-18 20:27:19 +00:00
"LayersPerContour": 220,
2022-01-09 19:25:44 +00:00
"avgNum": 10,
}
2020-10-11 15:09:49 +00:00
def __init__(self, config_path: Optional[str]):
"""
Initialize configuration from file or use defaults.
Args:
config_path: Path to JSON configuration file. If None or invalid, uses defaults.
"""
if config_path and os.path.isfile(config_path):
logger.info(f"Using supplied configuration at {config_path}")
try:
with open(config_path) as file:
self.c = json.load(file)
except (json.JSONDecodeError, IOError) as e:
logger.error(f"Failed to parse config file: {e}")
logger.warning("Falling back to default configuration")
2022-10-11 19:35:37 +00:00
else:
logger.info("Using default configuration")
2022-10-11 19:35:37 +00:00
logger.info("Current Configuration:")
2020-11-08 15:28:47 +00:00
for key, value in self.c.items():
logger.info(f" {key}: {value}")
2022-01-09 19:25:44 +00:00
def __getitem__(self, key: str) -> Any:
2020-10-16 08:36:52 +00:00
if key not in self.c:
return None
2022-01-09 19:25:44 +00:00
return self.c[key]
def __setitem__(self, key: str, value: Any) -> None:
2020-10-11 15:09:49 +00:00
self.c[key] = value