added export config and user define config support
This commit is contained in:
@@ -3,30 +3,108 @@ from pathlib import Path
|
||||
|
||||
CONFIG_DIR = Path(__file__).parent
|
||||
|
||||
def load_config(config_name: str):
|
||||
def load_config(config_name_or_path):
|
||||
"""
|
||||
Load sensor configuration from JSON file.
|
||||
|
||||
Accepts either:
|
||||
1. A file path (absolute or relative) to a JSON config file
|
||||
2. A config name (without .json extension) to load from package configs directory
|
||||
|
||||
Args:
|
||||
config_name: Name of the config file (without .json extension)
|
||||
config_name_or_path: Either:
|
||||
- A file path string (e.g., "/path/to/config.json" or "./config.json")
|
||||
- A config name string (e.g., "prototype") to load from package configs
|
||||
|
||||
Returns:
|
||||
Dictionary containing the configuration
|
||||
|
||||
Raises:
|
||||
FileNotFoundError: If the config file doesn't exist
|
||||
ValueError: If the file path is not a JSON file
|
||||
"""
|
||||
config_path = CONFIG_DIR / f"{config_name}.json"
|
||||
# Convert to Path object for easier handling
|
||||
config_path = Path(config_name_or_path)
|
||||
|
||||
if not config_path.exists():
|
||||
# Check if it's an existing file (absolute or relative path)
|
||||
if config_path.exists() and config_path.is_file():
|
||||
# User provided a file path
|
||||
if config_path.suffix.lower() != '.json':
|
||||
raise ValueError(f"Config file must be a JSON file: {config_path}")
|
||||
|
||||
with open(config_path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
|
||||
# If it's an absolute path but doesn't exist, raise error immediately
|
||||
if config_path.is_absolute():
|
||||
raise FileNotFoundError(f"Config file not found at absolute path: {config_path}")
|
||||
|
||||
# Otherwise, treat it as a config name and look in package directory
|
||||
package_config_path = CONFIG_DIR / f"{config_name_or_path}.json"
|
||||
|
||||
if not package_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}"
|
||||
f"Config file not found: {config_name_or_path}\n"
|
||||
f"Tried as file path: {config_path} (not found)\n"
|
||||
f"Tried as package config: {package_config_path} (not found)\n"
|
||||
f"Available package configs: {available_str}"
|
||||
)
|
||||
|
||||
with open(config_path, 'r') as f:
|
||||
with open(package_config_path, 'r', encoding='utf-8') as f:
|
||||
return json.load(f)
|
||||
|
||||
def _format_json_value(value, indent_level=0, indent_str=" "):
|
||||
"""
|
||||
Recursively format JSON value with arrays/lists on single line.
|
||||
|
||||
Args:
|
||||
value: The value to format (dict, list, or primitive)
|
||||
indent_level: Current indentation level
|
||||
indent_str: String used for indentation (default: 4 spaces)
|
||||
|
||||
Returns:
|
||||
Formatted string representation
|
||||
"""
|
||||
if isinstance(value, dict):
|
||||
if not value:
|
||||
return "{}"
|
||||
items = []
|
||||
for k, v in value.items():
|
||||
key_str = json.dumps(k, ensure_ascii=False)
|
||||
value_str = _format_json_value(v, indent_level + 1, indent_str)
|
||||
items.append(f"{indent_str * (indent_level + 1)}{key_str}: {value_str}")
|
||||
return "{\n" + ",\n".join(items) + "\n" + indent_str * indent_level + "}"
|
||||
elif isinstance(value, list):
|
||||
if not value:
|
||||
return "[]"
|
||||
# Format arrays/lists on a single line
|
||||
items = [json.dumps(item, ensure_ascii=False) for item in value]
|
||||
return "[" + ", ".join(items) + "]"
|
||||
else:
|
||||
# Primitive types (string, number, bool, None)
|
||||
return json.dumps(value, ensure_ascii=False)
|
||||
|
||||
def export_config(config, path="./config/exported_config.json"):
|
||||
"""
|
||||
Export configuration to JSON file with readable formatting.
|
||||
Arrays/lists are kept on a single line, while nested objects are indented.
|
||||
|
||||
Args:
|
||||
config: Configuration dictionary to export
|
||||
path: Output file path (default: "./config/exported_config.json")
|
||||
|
||||
Returns:
|
||||
The exported config dictionary
|
||||
"""
|
||||
Path(path).parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
with open(path, 'w', encoding='utf-8') as f:
|
||||
formatted = _format_json_value(config)
|
||||
f.write(formatted)
|
||||
f.write('\n') # Add trailing newline
|
||||
|
||||
return config
|
||||
|
||||
Reference in New Issue
Block a user