This commit is contained in:
John Lancaster
2024-05-04 21:52:02 -05:00
parent d34bf71b27
commit bd7bf3113f

View File

@@ -1,5 +1,6 @@
import asyncio import asyncio
from io import StringIO from io import StringIO
import json
import logging import logging
from dataclasses import dataclass, field from dataclasses import dataclass, field
from datetime import datetime from datetime import datetime
@@ -15,7 +16,7 @@ logger = logging.getLogger(__name__)
class AppConfig(BaseModel): class AppConfig(BaseModel):
module: str module: str
class_: str = Field(validation_alias='class') class_: str = Field(validation_alias='class', serialization_alias='class')
model_config = ConfigDict(validate_assignment=True) model_config = ConfigDict(validate_assignment=True)
@@ -173,33 +174,32 @@ class AppLoader:
app_configs = { app_configs = {
app_name: cfg async for app_name, cfg in self.gen_valid_app_configs(configs_to_load) app_name: cfg async for app_name, cfg in self.gen_valid_app_configs(configs_to_load)
} }
logger.debug(app_configs) app_configs = AppConfigs.dump_python(app_configs)
logger.debug(json.dumps(app_configs, indent=2))
async def gen_raw_config_text(self, configs_to_load: Iterable[Path]): async def gen_raw_config_text(self, config_files: Iterable[Path]):
for file in configs_to_load: for file in config_files:
try: try:
text = await file.read_text() yield file, await file.read_text()
except Exception as e: except Exception as e:
# logger.exception(e) # logger.exception(e)
logger.error(f'Error reading {file}') logger.error(f'Error reading {file}')
else:
yield file, text
async def gen_raw_app_configs(self, configs_to_load: Iterable[Path]): async def gen_raw_app_configs(self, config_files: Iterable[Path]):
async for file, text in self.gen_raw_config_text(configs_to_load): async for file, text in self.gen_raw_config_text(config_files):
try: try:
yield yaml.safe_load(StringIO(text)) yield file, yaml.safe_load(StringIO(text))
except Exception as e: except Exception as e:
# logger.exception(e) # logger.exception(e)
logger.error(f'Error parsing YAML from {file}') logger.error(f'Error parsing YAML from {file}')
async def gen_valid_app_configs(self, configs_to_load: Iterable[Path]): async def gen_valid_app_configs(self, config_files: Iterable[Path]):
async for raw_full_cfg in self.gen_raw_app_configs(configs_to_load): async for file, raw_full_cfg in self.gen_raw_app_configs(config_files):
for app_name, raw_cfg in raw_full_cfg.items(): for app_name, raw_cfg in raw_full_cfg.items():
try: try:
valid_cfg = AppConfig.model_validate(raw_cfg) valid_cfg = AppConfig.model_validate(raw_cfg)
except ValidationError as e: except ValidationError as e:
logger.error(f'Error validating "{app_name}"') logger.error(f'Error validating "{app_name}" from {file}')
logger.error(e) logger.error(e)
else: else:
yield app_name, valid_cfg yield app_name, valid_cfg