1
0
personal-data/personal_data/fetchers/__init__.py
Jon Michael Aanes 30bbab2841
Some checks failed
Run Python tests (through Pytest) / Test (push) Failing after 34s
Verify Python project can be installed, loaded and have version checked / Test (push) Successful in 30s
Ruff
2024-11-17 17:09:41 +01:00

38 lines
963 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()