Kaynağa Gözat

refactor: cleanup preamble logging

Sam Jaffe 1 ay önce
ebeveyn
işleme
72c5998c0f
2 değiştirilmiş dosya ile 17 ekleme ve 13 silme
  1. 6 6
      src/cipy/action.py
  2. 11 7
      src/cipy/runner.py

+ 6 - 6
src/cipy/action.py

@@ -110,20 +110,20 @@ class Script(Action):
     shell: Shell = Shell.DEFAULT
     name: str = ""
     script: str
-    _lines: list[str] = PrivateAttr()
+    lines: list[str] = []
 
     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]
+            self.name = self.lines[0]
 
-    def _shell_log(self) -> list[str]:
-        return ["shell: " + " ".join(self.shell.command("{0}"))]
+    def _shell_log(self) -> str:
+        return "shell: " + " ".join(self.shell.command("{0}"))
 
     @final
     @cipy.runner.ipc
-    @cipy.runner.preamble(extra=[(_GREEN, lambda s: s._lines), ("", _shell_log)])
+    @cipy.runner.preamble(extra=[(_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"))

+ 11 - 7
src/cipy/runner.py

@@ -7,7 +7,7 @@ import os
 import tempfile
 
 from contextlib import contextmanager
-from typing import Any, Callable, Iterable, Iterator, Literal, TypeVar, overload
+from typing import Any, Callable, Iterable, Iterator, TypeVar, overload
 
 from colored import Style
 from dotenv import dotenv_values
@@ -15,11 +15,9 @@ from dotenv import dotenv_values
 import cipy.common
 from cipy.common import Context, Status, _validate
 
-import cipy._logging as _logging
-
 Action = TypeVar("Action", bound=cipy.common.Action)
 type Run[Action] = Callable[[Action, Context], Status]
-type GetLines[Action] = Callable[[Action], Iterable[str]]
+type GetLines[Action] = Callable[[Action], str | Iterable[str]]
 type ExtraFormatting[Action] = list[tuple[str, GetLines[Action]]]
 
 
@@ -129,14 +127,20 @@ def preamble(
         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)
+
     @functools.wraps(func)
     def wrapper(self: Action, context: Context) -> Status:
         self.logger.info("##[group] Run %s", self.name)
         log_inputs(self)
         for color, get_lines in extra:
-            fmt = f"{color}%s{Style.reset}"
-            for line in get_lines(self):
-                self.logger.info(fmt, line)
+            log_extra(self, color, get_lines(self))
         self.logger.info("##[endgroup]")
         return func(self, context)