diff --git a/directory_monitor.py b/directory_monitor.py index 889468e..b48f957 100644 --- a/directory_monitor.py +++ b/directory_monitor.py @@ -1,5 +1,6 @@ import asyncio from io import StringIO +import json import logging from dataclasses import dataclass, field from datetime import datetime @@ -15,7 +16,7 @@ logger = logging.getLogger(__name__) class AppConfig(BaseModel): module: str - class_: str = Field(validation_alias='class') + class_: str = Field(validation_alias='class', serialization_alias='class') model_config = ConfigDict(validate_assignment=True) @@ -173,33 +174,32 @@ class AppLoader: app_configs = { 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]): - for file in configs_to_load: + async def gen_raw_config_text(self, config_files: Iterable[Path]): + for file in config_files: try: - text = await file.read_text() + yield file, await file.read_text() except Exception as e: # logger.exception(e) logger.error(f'Error reading {file}') - else: - yield file, text - async def gen_raw_app_configs(self, configs_to_load: Iterable[Path]): - async for file, text in self.gen_raw_config_text(configs_to_load): + async def gen_raw_app_configs(self, config_files: Iterable[Path]): + async for file, text in self.gen_raw_config_text(config_files): try: - yield yaml.safe_load(StringIO(text)) + yield file, yaml.safe_load(StringIO(text)) except Exception as e: # logger.exception(e) logger.error(f'Error parsing YAML from {file}') - async def gen_valid_app_configs(self, configs_to_load: Iterable[Path]): - async for raw_full_cfg in self.gen_raw_app_configs(configs_to_load): + async def gen_valid_app_configs(self, config_files: Iterable[Path]): + async for file, raw_full_cfg in self.gen_raw_app_configs(config_files): for app_name, raw_cfg in raw_full_cfg.items(): try: valid_cfg = AppConfig.model_validate(raw_cfg) except ValidationError as e: - logger.error(f'Error validating "{app_name}"') + logger.error(f'Error validating "{app_name}" from {file}') logger.error(e) else: yield app_name, valid_cfg