You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
"""Adapter registry with decorator-based registration."""
|
|
from __future__ import annotations
|
|
|
|
from typing import TYPE_CHECKING, Callable, Type
|
|
|
|
if TYPE_CHECKING:
|
|
from gimme_job.models.manifest import SiteManifest
|
|
|
|
_REGISTRY: dict[str, Type] = {}
|
|
|
|
|
|
def register(site_id: str) -> Callable:
|
|
"""Decorator to register a custom adapter class for a site."""
|
|
def decorator(cls):
|
|
_REGISTRY[site_id] = cls
|
|
return cls
|
|
return decorator
|
|
|
|
|
|
def get_adapter(site_id: str, manifest: "SiteManifest"):
|
|
"""Return a custom adapter if registered, otherwise ManifestDrivenAdapter."""
|
|
from gimme_job.adapters.base import ManifestDrivenAdapter
|
|
|
|
if site_id in _REGISTRY:
|
|
return _REGISTRY[site_id](manifest)
|
|
return ManifestDrivenAdapter(manifest)
|
|
|
|
|
|
def list_adapters() -> list[str]:
|
|
return sorted(_REGISTRY.keys())
|
|
|
|
|
|
def _load_all_adapters() -> None:
|
|
"""Import all adapter modules so they register themselves."""
|
|
import importlib
|
|
import pkgutil
|
|
import gimme_job.adapters as pkg
|
|
|
|
for _, module_name, _ in pkgutil.iter_modules(pkg.__path__):
|
|
if module_name not in ("base", "registry"):
|
|
try:
|
|
importlib.import_module(f"gimme_job.adapters.{module_name}")
|
|
except Exception:
|
|
pass
|