Procházet zdrojové kódy

refactor: clean up preamble with a ArgsList instead of a named parameter

Sam Jaffe před 1 měsícem
rodič
revize
3121480d10
2 změnil soubory, kde provedl 7 přidání a 30 odebrání
  1. 1 1
      src/cipy/action.py
  2. 6 29
      src/cipy/runner.py

+ 1 - 1
src/cipy/action.py

@@ -123,7 +123,7 @@ class Script(Action):
 
     @final
     @cipy.runner.ipc
-    @cipy.runner.preamble(extra=[(_GREEN, lambda s: s.lines), ("", _shell_log)])
+    @cipy.runner.preamble((_GREEN, lambda s: s.lines), ("", _shell_log))
     def run(self, context: Context) -> Status:
         with tempfile.NamedTemporaryFile(suffix=self.shell.extension()) as script:
             script.write(self.script.encode("utf-8"))

+ 6 - 29
src/cipy/runner.py

@@ -18,7 +18,6 @@ from cipy.common import Context, Status, _validate
 Action = TypeVar("Action", bound=cipy.common.Action)
 type Run[Action] = Callable[[Action, Context], Status]
 type GetLines[Action] = Callable[[Action], str | Iterable[str]]
-type ExtraFormatting[Action] = list[tuple[str, GetLines[Action]]]
 
 
 @contextmanager
@@ -86,44 +85,22 @@ def logging_group(self: Action, message: str, *args: Any) -> Iterator[None]:
 
 @overload
 def preamble(
-    func: Run[Action],
-    *,
-    extra: ExtraFormatting[Action] | None = None,
+    func: Run[Action], *extra: tuple[str, GetLines[Action]]
 ) -> Run[Action]: ...
 
 
 @overload
 def preamble(
-    func: ExtraFormatting[Action],
-    *,
-    extra: ExtraFormatting[Action] | None = None,
-) -> Callable[
-    [Run[Action]],
-    Run[Action],
-]: ...
-
-
-@overload
-def preamble(
-    func: None = None, *, extra: ExtraFormatting[Action] | None = None
+    func: tuple[str, GetLines[Action]], *extra: tuple[str, GetLines[Action]]
 ) -> Callable[[Run[Action]], Run[Action]]: ...
 
 
 def preamble(
-    func: Run[Action] | ExtraFormatting[Action] | None = None,
-    *,
-    extra: ExtraFormatting[Action] | None = None,
+    func: Run[Action] | tuple[str, GetLines[Action]],
+    *extra: tuple[str, GetLines[Action]],
 ):
-    if isinstance(func, list):
-        assert extra is None
-        return lambda f: preamble(f, extra=func)
-
-    if func is None:
-        assert extra is not None
-        return lambda f: preamble(f, extra=extra)
-
-    if extra is None:
-        extra = []
+    if isinstance(func, tuple):
+        return lambda f: preamble(f, func, *extra)
 
     @functools.wraps(func)
     def wrapper(self: Action, context: Context) -> Status: