From 20ca409666e74482c6afb5918a5e7b52bac7bb0f Mon Sep 17 00:00:00 2001 From: faissaloux Date: Thu, 11 Jul 2024 18:24:35 +0100 Subject: [PATCH] refactor ModeManager --- termspark/painter/mode_manager.py | 19 ++++++++----------- termspark/painter/modes/mode.py | 5 +++++ termspark/painter/modes/name.py | 4 ++++ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/termspark/painter/mode_manager.py b/termspark/painter/mode_manager.py index 2a7e9f1..8b9b12e 100644 --- a/termspark/painter/mode_manager.py +++ b/termspark/painter/mode_manager.py @@ -1,22 +1,19 @@ +from importlib import import_module from typing import Union -from .modes.hex import HEX -from .modes.name import Name -from .modes.rgb import RGB +from termspark.painter.modes.mode import Mode class ModeManager: - __mode: Union[RGB, HEX, Name] - def __init__(self, color: Union[str, tuple]): self._color = color - if RGB.check(color): - self.__mode = RGB(color) - elif HEX.check(color): - self.__mode = HEX(color) - else: - self.__mode = Name(color) + for mode in Mode.__subclasses__(): + _module = import_module(f"termspark.painter.modes.{mode.__name__.lower()}") + _class = getattr(_module, mode.__name__) + if _class.check(color): + self.__mode = _class(color) + break def format(self): return self.__mode.format() diff --git a/termspark/painter/modes/mode.py b/termspark/painter/modes/mode.py index 53b239f..92c06be 100644 --- a/termspark/painter/modes/mode.py +++ b/termspark/painter/modes/mode.py @@ -3,6 +3,11 @@ class Mode(ABC): + @staticmethod + @abstractmethod + def check(color: Union[str, tuple, None]) -> bool: + pass + @abstractmethod def format(self) -> Union[str, bool]: pass diff --git a/termspark/painter/modes/name.py b/termspark/painter/modes/name.py index 4d90a21..36108c6 100644 --- a/termspark/painter/modes/name.py +++ b/termspark/painter/modes/name.py @@ -10,5 +10,9 @@ def __init__(self, color: Union[str, tuple]): assert type(color) == str self.__color = getattr(Color, color.upper(), color) + @staticmethod + def check(color: Union[str, tuple, None]) -> bool: + return True + def format(self) -> Union[str, bool]: return RGB(self.__color).format()