improved generators

This commit is contained in:
John Lancaster
2024-05-04 21:44:37 -05:00
parent 84312ba535
commit d34bf71b27

View File

@@ -1,4 +1,5 @@
import asyncio import asyncio
from io import StringIO
import logging import logging
from dataclasses import dataclass, field from dataclasses import dataclass, field
from datetime import datetime from datetime import datetime
@@ -174,14 +175,23 @@ class AppLoader:
} }
logger.debug(app_configs) logger.debug(app_configs)
async def gen_raw_app_configs(self, configs_to_load: Iterable[Path]): async def gen_raw_config_text(self, configs_to_load: Iterable[Path]):
for config_file in configs_to_load: for file in configs_to_load:
try: try:
with Path(config_file).open('r') as f: text = await file.read_text()
yield yaml.safe_load(f)
except Exception as e: except Exception as e:
logger.exception(e) # logger.exception(e)
logger.error(f'Error reading from {config_file}') 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):
try:
yield 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 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 for raw_full_cfg in self.gen_raw_app_configs(configs_to_load):