Przeglądaj źródła

refactor: make preamble less obtuse by moving script logging out

Sam Jaffe 1 miesiąc temu
rodzic
commit
94f8961ada
2 zmienionych plików z 9 dodań i 37 usunięć
  1. 8 7
      src/cipy/action.py
  2. 1 30
      src/cipy/runner.py

+ 8 - 7
src/cipy/action.py

@@ -110,21 +110,22 @@ class Script(Action):
     shell: Shell = Shell.DEFAULT
     name: str = ""
     script: str
-    lines: list[str] = []
+    _lines: list[str] = PrivateAttr()
 
     def model_post_init(self, context: Any, /) -> None:
         self.script = dedent(self.script).strip("\n")
-        self.lines = self.script.splitlines()
+        self._lines = self.script.splitlines()
         if not self.name:
-            self.name = self.lines[0]
-
-    def _shell_log(self) -> str:
-        return "shell: " + " ".join(self.shell.command("{0}"))
+            self.name = self.shell.value + " script"
 
     @final
     @cipy.runner.ipc
-    @cipy.runner.preamble((_GREEN, lambda s: s.lines), ("", _shell_log))
     def run(self, context: Context) -> Status:
+        with cipy.runner.logging_group(self, self._lines[0]):
+            for line in self._lines:
+                self.logger.info("%s%s%s", Script._GREEN, line, Style.reset)
+            self.logger.info("shell: %s", " ".join(self.shell.command("{0}")))
+
         with tempfile.NamedTemporaryFile(suffix=self.shell.extension()) as script:
             script.write(self.script.encode("utf-8"))
             try:

+ 1 - 30
src/cipy/runner.py

@@ -17,7 +17,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]]
 
 
 @contextmanager
@@ -83,31 +82,12 @@ def logging_group(self: Action, message: str, *args: Any) -> Iterator[None]:
     self.logger.info("##[endgroup]")
 
 
-@overload
-def preamble(
-    func: Run[Action], *extra: tuple[str, GetLines[Action]]
-) -> Run[Action]: ...
-
-
-@overload
-def preamble(
-    func: tuple[str, GetLines[Action]], *extra: tuple[str, GetLines[Action]]
-) -> Callable[[Run[Action]], Run[Action]]: ...
-
-
-def preamble(
-    func: Run[Action] | tuple[str, GetLines[Action]],
-    *extra: tuple[str, GetLines[Action]],
-):
-    if isinstance(func, tuple):
-        return lambda f: preamble(f, func, *extra)
+def preamble(func: Run[Action]):
 
     @functools.wraps(func)
     def wrapper(self: Action, context: Context) -> Status:
         with logging_group(self, "Run %s", self.name):
             _log_inputs(self)
-            for color, get_lines in extra:
-                _log_extra(self, color, get_lines(self))
         return func(self, context)
 
     return wrapper
@@ -119,12 +99,3 @@ def _log_inputs(self: Action) -> None:
         self.logger.info("with:")
         for key, value in inputs:
             self.logger.info("  %s: %s", key, value)
-
-
-def _log_extra(self: Action, color: str, lines: str | Iterable[str]) -> None:
-    fmt = f"{color}%s{Style.reset}"
-    if isinstance(lines, str):
-        self.logger.info(fmt, lines)
-    else:
-        for line in lines:
-            self.logger.info(fmt, line)