1
0
personal-data/personal_data/fetchers/__init__.py
Jon Michael Aanes f7ea7c90b5
All checks were successful
Run Python tests (through Pytest) / Test (push) Successful in 35s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 30s
Enforce loading of fetchers
2024-11-17 11:30:37 +01:00

37 lines
962 B
Python

import importlib
import logging
from collections.abc import Iterator
from pathlib import Path
logger = logging.getLogger(__name__)
def load_backend(name: str) -> object | None:
try:
return importlib.import_module(__name__ + '.' + name)
except Exception:
logger.exception('Backend %s failed loading, and have been disabled', name)
return None
def get_modules(backend_dir: Path) -> Iterator[str]:
for f in backend_dir.iterdir():
if f.is_file() and f.suffix == '.py':
name = f.parts[-1].removesuffix('.py')
if name != '__init__':
yield name
FETCHER_MODULES_LOADED = False
def load_fetcher_modules() -> None:
global FETCHER_MODULES_LOADED
if not FETCHER_MODULES_LOADED:
backend_dir = Path(__name__.replace(r'.', '/'))
for module in get_modules(backend_dir):
load_backend(module)
FETCHER_MODULES_LOADED = True
load_fetcher_modules()