added python tool inital version
This commit is contained in:
40
buzzer_tool/core/config.py
Normal file
40
buzzer_tool/core/config.py
Normal file
@@ -0,0 +1,40 @@
|
||||
# core/config.py
|
||||
import os
|
||||
import sys
|
||||
import yaml
|
||||
|
||||
DEFAULT_CONFIG = {
|
||||
"port": None,
|
||||
"baudrate": 115200,
|
||||
"timeout": 5.0
|
||||
}
|
||||
|
||||
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"])
|
||||
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
|
||||
Reference in New Issue
Block a user