Jelajahi Sumber

refactor: use colored to provide color codes instead of manually doing it

Sam Jaffe 1 bulan lalu
induk
melakukan
1433660dce
4 mengubah file dengan 11 tambahan dan 25 penghapusan
  1. 1 0
      pyproject.toml
  2. 6 21
      src/cipy/_logging.py
  3. 2 1
      src/cipy/action.py
  4. 2 3
      src/cipy/runner.py

+ 1 - 0
pyproject.toml

@@ -5,6 +5,7 @@ description = "Python based tool for operating CI Pipelines"
 readme = "README.md"
 requires-python = ">=3.14"
 dependencies = [
+    "colored>=2.3.2",
     "pydantic>=2.12.5",
     "python-dotenv>=1.2.2",
 ]

+ 6 - 21
src/cipy/_logging.py

@@ -5,21 +5,7 @@ import time
 
 from typing import Callable, ClassVar, Final
 
-GREY: Final[str] = "\x1b[38;20m"
-YELLOW: Final[str] = "\x1b[33;20m"
-BLUE: Final[str] = "\x1b[34;20m"
-CYAN: Final[str] = "\x1b[36;20m"
-RED: Final[str] = "\x1b[31;20m"
-
-BOLD_RED: Final[str] = "\x1b[31;1m"
-BOLD_PURPLE: Final[str] = "\x1b[35;1m"
-
-RESET: Final[str] = "\x1b[0m"
-
-
-def colored(code: str | None, fmt: str) -> str:
-    """Apply a given color code"""
-    return f"{code}{fmt}{RESET}" if code else fmt
+from colored import Fore, Style
 
 
 class CIFormatter(logging.Formatter):
@@ -30,11 +16,10 @@ class CIFormatter(logging.Formatter):
     )
 
     DEFAULT_COLOR_CODES: Final[ClassVar[dict[int, str]]] = {
-        logging.DEBUG: BOLD_PURPLE,
-        logging.INFO: GREY,
-        logging.WARNING: YELLOW,
-        logging.ERROR: RED,
-        logging.CRITICAL: BOLD_RED,
+        logging.DEBUG: Fore.deep_pink_4a + Style.BOLD,
+        logging.WARNING: Fore.yellow,
+        logging.ERROR: Fore.red,
+        logging.CRITICAL: Fore.red + Style.BOLD,
     }
 
     converter: Callable[[float | None], time.struct_time] = time.gmtime
@@ -57,7 +42,7 @@ class CIFormatter(logging.Formatter):
 
     def _dispatch(self, level: int) -> logging.Formatter:
         fmt = logging.Formatter(
-            fmt=colored(self._color_codes.get(level), self._format),
+            fmt=self._color_codes.get(level, "") + self._format + Style.reset,
             datefmt=self.datefmt,
         )
         fmt.converter = self.converter

+ 2 - 1
src/cipy/action.py

@@ -7,6 +7,7 @@ import tempfile
 from textwrap import dedent
 from typing import Any, final
 
+from colored import Fore, Style
 from pydantic import Field, PrivateAttr
 
 import cipy.runner
@@ -116,7 +117,7 @@ class Script(Action):
 
     @final
     @cipy.runner.ipc
-    @cipy.runner.preamble(extra=[("CYAN", lambda s: s._lines)])
+    @cipy.runner.preamble(extra=[(f"{Fore.dark_sea_green_4b}{Style.BOLD}", lambda s: s._lines)])
     def run(self, context: Context) -> Status:
         with tempfile.NamedTemporaryFile(suffix=self.shell.extension()) as script:
             script.write(self.script.encode("utf-8"))

+ 2 - 3
src/cipy/runner.py

@@ -9,6 +9,7 @@ import tempfile
 from contextlib import contextmanager
 from typing import Any, Callable, Iterable, Iterator, Literal, TypeVar, overload
 
+from colored import Style
 from dotenv import dotenv_values
 
 import cipy.common
@@ -123,9 +124,7 @@ def preamble(
     def wrapper(self: Action, context: Context) -> Status:
         self.logger.info("##[group] Run %s", self.name)
         for color, get_lines in extra:
-            fmt = "%s"
-            if color in _logging.__dict__:
-                fmt = f"{_logging.__dict__[color]}%s{_logging.RESET}"
+            fmt = f"{color}%s{Style.reset}"
             for line in get_lines(self):
                 self.logger.info(fmt, line)
         self.logger.info("##[endgroup]")