#!/usr/bin/env python3 """ Configuration loader for Arduino UNO Q project """ import json import os from pathlib import Path class Config: def __init__(self, config_file="config.json"): self.config_file = Path(config_file) self.config = self.load_config() def load_config(self): """Load configuration from JSON file""" if not self.config_file.exists(): # Try to copy from example example_file = Path("config.example.json") if example_file.exists(): print(f"Creating config from example: {self.config_file}") import shutil shutil.copy(example_file, self.config_file) print("Please edit config.json with your personal settings") else: raise FileNotFoundError(f"Config file {self.config_file} not found") try: with open(self.config_file, 'r') as f: config = json.load(f) # Expand paths self.expand_paths(config) return config except json.JSONDecodeError as e: raise ValueError(f"Invalid JSON in config file: {e}") def expand_paths(self, config): """Expand ~ and relative paths""" if 'arduino_uno_q' in config and 'ssh_key' in config['arduino_uno_q']: ssh_key = config['arduino_uno_q']['ssh_key'] if ssh_key.startswith('~'): config['arduino_uno_q']['ssh_key'] = str(Path.home() / ssh_key[1:]) if 'paths' in config: for key, path in config['paths'].items(): if isinstance(path, str) and path.startswith('~'): config['paths'][key] = str(Path.home() / path[1:]) def get(self, key_path, default=None): """Get configuration value by dot notation""" keys = key_path.split('.') value = self.config for key in keys: if isinstance(value, dict) and key in value: value = value[key] else: return default return value def get_arduino_config(self): """Get Arduino UNO Q connection configuration""" return self.get('arduino_uno_q', {}) def get_build_config(self): """Get build configuration""" return self.get('build', {}) def get_paths(self): """Get paths configuration""" return self.get('paths', {}) def get_led_matrix_config(self): """Get LED Matrix configuration""" return self.get('led_matrix', {}) # Global config instance config = Config()