json works

This commit is contained in:
bluepulsar
2025-12-25 15:23:17 +08:00
parent 64f07fdfb9
commit 038941593e
7 changed files with 103 additions and 8 deletions
+34 -1
View File
@@ -4,11 +4,44 @@ from pathlib import Path
CONFIG_DIR = Path(__file__).parent
def load_config(config_name: str):
"""
Load sensor configuration from JSON file.
Args:
config_name: Name of the config file (without .json extension)
Returns:
Dictionary containing the configuration
Raises:
FileNotFoundError: If the config file doesn't exist
"""
config_path = CONFIG_DIR / f"{config_name}.json"
if not config_path.exists():
# List available configs for better error message
available = [f.stem for f in CONFIG_DIR.glob("*.json")]
available_str = ", ".join(available) if available else "none"
raise FileNotFoundError(
f"Config file not found: {config_path}\n"
f"Available configs: {available_str}"
)
with open(config_path, 'r') as f:
return json.load(f)
def save_config(config: dict, config_name: str):
"""
Save sensor configuration to JSON file.
Args:
config: Dictionary containing the configuration
config_name: Name of the config file (without .json extension)
Returns:
Path to the saved config file
"""
config_path = CONFIG_DIR / f"{config_name}.json"
with open(config_path, 'w') as f:
json.dump(config, f, indent=4)
json.dump(config, f, indent=4)
return config_path