Source code for structum_lab.plugins.bootstrap.manager
# src/structum_lab/plugins/bootstrap/manager.py
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: 2025 PythonWoods
"""System bootstrapper orchestration.
This module provides the SystemBootstrapper class, which orchestrates
the execution of multiple validators during application startup.
"""
import logging
import sys
from structum_lab.validation import Validator
from .core import BootstrapContextImpl
log = logging.getLogger(__name__)
[docs]
class SystemBootstrapper:
"""
Orchestrates the system validation process.
"""
[docs]
def __init__(self) -> None:
"""Initialize the bootstrapper with an empty validator list."""
self._validators: list[Validator] = []
[docs]
def add_validator(self, validator: Validator) -> "SystemBootstrapper":
"""Add a validator to the bootstrap sequence.
Args:
validator: A validator instance implementing the Validator protocol.
Returns:
Self for method chaining.
"""
self._validators.append(validator)
return self
[docs]
def run(self) -> BootstrapContextImpl:
"""Run all validators and return the context."""
context = BootstrapContextImpl()
log.info(f"Starting system bootstrap with {len(self._validators)} validators...")
for validator in self._validators:
try:
validator.validate(context)
except Exception as e:
context.add_check(
"Bootstrap Internal Error",
False,
f"Validator {validator} crashed: {e}",
)
return context
[docs]
def run_or_exit(self) -> None:
"""Run validators and exit process if any fail."""
context = self.run()
if not context.is_valid():
print("\n❌ System Bootstrap Failed:\n", file=sys.stderr)
for error in context.errors:
print(f" - {error}", file=sys.stderr)
if context.warnings:
print("\n⚠️ Warnings:", file=sys.stderr)
for warn in context.warnings:
print(f" - {warn}", file=sys.stderr)
print("\nFix the above errors to proceed.", file=sys.stderr)
sys.exit(1)
# Log success
if context.warnings:
for warn in context.warnings:
log.warning(f"Bootstrap Warning: {warn}")
log.info("✅ System Bootstrap Completed Successfully")