Add @module annotation

This commit is contained in:
Joscha 2025-11-04 02:39:11 +01:00
parent 16722906a9
commit 5bb1105f38
2 changed files with 33 additions and 1 deletions

View file

@ -1,10 +1,11 @@
from . import file, modules, util from . import file, modules, util
from .orchestrator import Module, Orchestrator from .orchestrator import Module, Orchestrator, module
__all__: list[str] = [ __all__: list[str] = [
"Module", "Module",
"Orchestrator", "Orchestrator",
"file", "file",
"module",
"modules", "modules",
"util", "util",
] ]

View file

@ -3,6 +3,7 @@ from __future__ import annotations
import getpass import getpass
import socket import socket
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from typing import Callable, Concatenate
from rich import print from rich import print
from rich.console import Console from rich.console import Console
@ -20,6 +21,36 @@ class Module(ABC):
def realize(self) -> None: ... def realize(self) -> None: ...
def _snake_to_camel(s: str) -> str:
return "".join(s.capitalize() for s in s.split("_"))
# Annotate a function with @module to turn it into a class implementing Module.
def module[**P](
func: Callable[Concatenate[Orchestrator, P], None],
) -> Callable[Concatenate[Orchestrator, P], None]:
def __init__(
self,
orchestrator: Orchestrator,
*args: P.args,
**kwargs: P.kwargs,
) -> None:
super(self.__class__, self).__init__(orchestrator)
self.args = args
self.kwargs = kwargs
def realize(self) -> None:
# pyrefly: ignore
return func(self.orchestrator, *self.args, **self.kwargs)
# pyrefly: ignore
return type(
_snake_to_camel(func.__name__),
(Module,),
{"__init__": __init__, "realize": realize},
)
class Orchestrator: class Orchestrator:
def __init__(self, name: str = "pasch", dry_run: bool = False) -> None: def __init__(self, name: str = "pasch", dry_run: bool = False) -> None:
self.name = name self.name = name