1from __future__ import annotations
2
3from plain.packages import packages_registry
4
5from .core import Chore
6
7
8class ChoresRegistry:
9 def __init__(self) -> None:
10 self._chores: dict[str, type[Chore]] = {}
11
12 def register_chore(self, chore_class: type[Chore]) -> None:
13 """
14 Register a chore class.
15
16 Args:
17 chore_class: A Chore subclass to register
18 """
19 name = f"{chore_class.__module__}.{chore_class.__qualname__}"
20 self._chores[name] = chore_class
21
22 def import_modules(self) -> None:
23 """
24 Import modules from installed packages and app to trigger registration.
25 """
26 packages_registry.autodiscover_modules("chores", include_app=True)
27
28 def get_chores(self) -> list[type[Chore]]:
29 """
30 Get all registered chore classes.
31 """
32 return list(self._chores.values())
33
34
35chores_registry = ChoresRegistry()
36
37
38def register_chore(cls: type[Chore]) -> type[Chore]:
39 """
40 Decorator to register a chore class.
41
42 Usage:
43 @register_chore
44 class ClearExpired(Chore):
45 def run(self):
46 return "Done!"
47 """
48 chores_registry.register_chore(cls)
49 return cls