48 lines
1.8 KiB
Python
48 lines
1.8 KiB
Python
# core/config.py
|
|
import os
|
|
import sys
|
|
import yaml
|
|
|
|
DEFAULT_CONFIG = {
|
|
"port": None,
|
|
"baudrate": 115200,
|
|
"timeout": 5.0,
|
|
"crc_timeout_min_seconds": 2.0,
|
|
"crc_timeout_ms_per_100kb": 1.5,
|
|
}
|
|
|
|
def load_config(cli_args=None):
|
|
config = DEFAULT_CONFIG.copy()
|
|
|
|
cwd_config = os.path.join(os.getcwd(), "config.yaml")
|
|
script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
|
|
script_config = os.path.join(script_dir, "config.yaml")
|
|
|
|
yaml_path = cwd_config if os.path.exists(cwd_config) else script_config if os.path.exists(script_config) else None
|
|
|
|
if yaml_path:
|
|
try:
|
|
with open(yaml_path, "r", encoding="utf-8") as f:
|
|
yaml_data = yaml.safe_load(f)
|
|
if yaml_data and "serial" in yaml_data:
|
|
config["port"] = yaml_data["serial"].get("port", config["port"])
|
|
config["baudrate"] = yaml_data["serial"].get("baudrate", config["baudrate"])
|
|
config["timeout"] = yaml_data["serial"].get("timeout", config["timeout"])
|
|
config["crc_timeout_min_seconds"] = yaml_data["serial"].get(
|
|
"crc_timeout_min_seconds", config["crc_timeout_min_seconds"]
|
|
)
|
|
config["crc_timeout_ms_per_100kb"] = yaml_data["serial"].get(
|
|
"crc_timeout_ms_per_100kb", config["crc_timeout_ms_per_100kb"]
|
|
)
|
|
except Exception as e:
|
|
print(f"Fehler beim Laden der Konfigurationsdatei {yaml_path}: {e}")
|
|
|
|
if cli_args:
|
|
if getattr(cli_args, "port", None) is not None:
|
|
config["port"] = cli_args.port
|
|
if getattr(cli_args, "baudrate", None) is not None:
|
|
config["baudrate"] = cli_args.baudrate
|
|
if getattr(cli_args, "timeout", None) is not None:
|
|
config["timeout"] = cli_args.timeout
|
|
|
|
return config |