|
|
527b5645ed
|
refactor(phase2.3): Migrate to Pydantic V2 and modernize config management
Complete Pydantic V2 migration and config modernization:
api/models.py:
- Migrated @validator → @field_validator (Pydantic V2)
- Added @classmethod decorators (required in V2)
- Updated validator signatures: (cls, v, values) → (cls, v, info)
- Consolidated unique validators into one method
- Fixed: 3 PydanticDeprecatedSince20 warnings ✅
config/schema.py:
- Migrated from @dataclass → Pydantic BaseModel
- Added Field() with validation constraints:
* Port: ge=1, le=65535
* Floats: gt=0, ge=0
* Strings: min_length constraints
- Added descriptive field descriptions
- Better type safety and runtime validation
core/bootstrap.py:
- Replaced SimpleNamespace → MQTTConfig (Pydantic)
- Now returns properly typed Pydantic models
- Better IDE autocomplete and type checking
Benefits:
✅ No more Pydantic V1 deprecation warnings
✅ Runtime validation for all config fields
✅ Better error messages on invalid config
✅ Type-safe config throughout the application
✅ Automatic validation (port ranges, positive numbers, etc.)
✅ Prepared for Pydantic V3 migration
Migration verified:
- Config loading works: load_config('config.example.yaml') ✓
- Bootstrap works with Pydantic models ✓
- Field validation works (tested port ranges, thresholds) ✓
- All existing functionality preserved ✓
Test: python3 -c 'from core.bootstrap import bootstrap; bootstrap()'
✅ Works perfectly with new Pydantic V2 models
See OPTIMIZATION_PLAN.md Phase 2.3 for details.
|
2026-02-18 22:53:36 +01:00 |
|
|
|
eb3c49c8c4
|
refactor(phase2.2): Refactor main.py - Extract bootstrap and service_manager
Massive refactoring of main.py to improve code organization:
Files Created:
1. core/bootstrap.py (234 lines)
- BootstrapConfig: Configuration container
- parse_arguments(): CLI arg parsing
- load_bridge_config(): Config loading with persisted fallback
- setup_logger(): Structured logging setup
- get_mqtt_config(): MQTT config with env var fallback
- bootstrap(): Complete initialization orchestration
2. core/service_manager.py (369 lines)
- ServiceManager: Central service lifecycle coordinator
- Signal handlers for graceful shutdown
- Service initialization (Odoo, MQTT, Event Queue, etc.)
- Callbacks for config updates and MQTT reconnection
- Graceful shutdown sequencing
Files Modified:
- Now ultra-clean entry point
- Only orchestration logic remains
- 7-phase startup sequence clearly documented
2. core/__init__.py
- Export new modules for clean imports
Benefits:
- ✅ Single Responsibility: Each module has one clear purpose
- ✅ Testability: Services can be tested independently
- ✅ Readability: main.py is now self-documenting
- ✅ Maintainability: Easy to find and modify specific logic
- ✅ No global variables: All state in ServiceManager
- ✅ Type hints throughout
Architecture:
main.py (orchestration)
├── bootstrap() → config + logger
└── ServiceManager() → all services
├── Odoo Client
├── Event Queue
├── Status Monitor
├── MQTT Client
├── Device Manager
└── HTTP Config Server
Test: python3 main.py --config config.example.yaml
✅ Bridge starts successfully with all services
See OPTIMIZATION_PLAN.md Phase 2.2 for details.
|
2026-02-18 22:43:00 +01:00 |
|