From d24ff71832cc1d52beb467f9a8e5e123b02935a8 Mon Sep 17 00:00:00 2001 From: Sori Lim Date: Wed, 6 Nov 2024 00:58:09 -0800 Subject: [PATCH] feat: create abstract messenger for extendability --- README.md | 5 +- example.py | 7 ++- ohmygod/__init__.py | 9 ++-- ohmygod/main.py | 22 +++++--- ohmygod/{message.py => messenger/buddha.py} | 46 +++++++++++++--- ohmygod/messenger/messenger.py | 58 +++++++++++++++++++++ pyproject.toml | 4 +- 7 files changed, 126 insertions(+), 25 deletions(-) rename ohmygod/{message.py => messenger/buddha.py} (82%) create mode 100644 ohmygod/messenger/messenger.py diff --git a/README.md b/README.md index cae3201..b6d3ae4 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ -# ohmygod - CLI Tool Powered by Buddha +# ohmygod - CLI Tool Powered by Gods -https://github.com/user-attachments/assets/9861c2c6-6bb2-4bc6-b575-e7c5dbbcd72b +ohmygod is an extension of `Console` class from [rich](<(https://github.com/Textualize/rich)>) library. If your program does not need a cute guardian god, you may want to use the original library instead. +https://github.com/user-attachments/assets/9861c2c6-6bb2-4bc6-b575-e7c5dbbcd72b ### Set up with Poetry diff --git a/example.py b/example.py index bf2f7cf..de6eb7a 100644 --- a/example.py +++ b/example.py @@ -1,7 +1,7 @@ -from ohmygod import OhMyGod, HolyMessage +from ohmygod import OhMyGod, Buddha import time -omg = OhMyGod() +omg = OhMyGod(Buddha) @omg.protect("> Praying for a long running process") def long_running_process(fail: bool): @@ -23,3 +23,6 @@ def long_running_process(fail: bool): # Show an error message if the process fails omg.clear() omg.error(str(e)) + +# Print using messages defined by the package +omg.print(omg.quotes.BLESSING) \ No newline at end of file diff --git a/ohmygod/__init__.py b/ohmygod/__init__.py index 2fd0791..2d0418d 100644 --- a/ohmygod/__init__.py +++ b/ohmygod/__init__.py @@ -1,10 +1,9 @@ """OH MY GOD package built upon rich console interface""" -__version__ = "0.1.5" - +__version__ = "0.2.0" from .main import OhMyGod -from .message import Message as HolyMessage -from .utils import Color as HolyColor +from .utils import Color +from .messenger.buddha import Buddha -__all__ = ["OhMyGod", "HolyMessage", "HolyColor"] +__all__ = ["OhMyGod", "Color", "Buddha"] diff --git a/ohmygod/main.py b/ohmygod/main.py index 9d873dd..0206c6c 100644 --- a/ohmygod/main.py +++ b/ohmygod/main.py @@ -6,19 +6,27 @@ import time from queue import Queue -from . import message as Message +from .messenger.buddha import Buddha +from .messenger.messenger import _Messenger as Messenger class OhMyGod(Console): """Console interface powered by Buddha""" - def __init__(self): + def __init__(self, messenger_cls: type[Messenger] = Buddha): super().__init__() + self.messenger = messenger_cls() self.bless() def bless(self): """Give a blessing to the program""" - self.print(Text(Message._BLESSING)) + self.print(Text(self.messenger.BLESSING)) + + + @property + def quotes(self): + """Retrieve importable messages for the messenger""" + return self.messenger.quotes def protect(self, message: str = ""): @@ -32,7 +40,7 @@ def pray(): with Live(auto_refresh=False, console=self) as live: state = 0 while not signal.is_set(): - prayer = Text(Message._PRAYER_ANIMATED[state % 2]) + prayer = Text(self.messenger.PRAYER_ANIMATED[state % 2]) dots = "." * (state % 4) live.update(prayer + message + dots) @@ -71,7 +79,7 @@ def success(self, message: str = ""): """Print a success message to the screen""" with Live(auto_refresh=False, console=self) as live: for i in range(3): - live.update(Text(Message._HURRAY_ANIMATED[i % 2] + message)) + live.update(Text(self.messenger.HURRAY_ANIMATED[i % 2] + message)) live.refresh() if i < 2: time.sleep(0.4 + 0.3 * i) @@ -79,10 +87,10 @@ def success(self, message: str = ""): def error(self, message: str = ""): """Print an error message to the screen""" - self.print(Text(Message._ERROR_COLORED), end="") + self.print(Text(self.messenger.ERROR_COLORED), end="") # Print characters in the error message one by one - for char in Message._ERROR_ANIMATION: + for char in self.messenger.ERROR_ANIMATION: self.print(Text(char, style="red"), end="") time.sleep(0.1) diff --git a/ohmygod/message.py b/ohmygod/messenger/buddha.py similarity index 82% rename from ohmygod/message.py rename to ohmygod/messenger/buddha.py index c7abff9..024e394 100644 --- a/ohmygod/message.py +++ b/ohmygod/messenger/buddha.py @@ -1,5 +1,7 @@ from rich.text import Text -from .utils import Color, _Message + +from .messenger import _Messenger +from ..utils import Color, _Message _BLESSING = _Message.from_str(r""" @@ -71,10 +73,40 @@ _ERROR = _ERROR_TEMPLATE.clean() + _ERROR_ANIMATION -class Message: - """Importable messages for the OhMyGod""" +class Buddha(_Messenger): + @property + def BLESSING(self): + return _BLESSING + + @property + def PRAYER_ANIMATED(self): + return _PRAYER_ANIMATED + + @property + def HURRAY_ANIMATED(self): + return _HURRAY_ANIMATED + + @property + def ERROR_COLORED(self): + return _ERROR_COLORED + + @property + def ERROR_ANIMATION(self): + return _ERROR_ANIMATION - BLESSING = Text(_BLESSING) - PRAYER = Text(_PRAYER) - HURRAY = Text(_HURRAY) - ERROR = Text(_ERROR) + class Quotes(_Messenger.Quotes): + @property + def BLESSING(self): + return Text(_BLESSING) + + @property + def PRAYER(self): + return Text(_PRAYER) + + @property + def HURRAY(self): + return Text(_HURRAY) + + @property + def ERROR(self): + return Text(_ERROR) diff --git a/ohmygod/messenger/messenger.py b/ohmygod/messenger/messenger.py new file mode 100644 index 0000000..525f7f3 --- /dev/null +++ b/ohmygod/messenger/messenger.py @@ -0,0 +1,58 @@ +from abc import ABC, abstractmethod +from rich.text import Text +from typing import List + +from ..utils import _Message + + +class _Messenger(ABC): + @property + @abstractmethod + def BLESSING(self) -> _Message: + pass + + @property + @abstractmethod + def PRAYER_ANIMATED(self) -> List[_Message]: + pass + + @property + @abstractmethod + def HURRAY_ANIMATED(self) -> List[_Message]: + pass + + @property + @abstractmethod + def ERROR_COLORED(self) -> _Message: + pass + + @property + @abstractmethod + def ERROR_ANIMATION(self) -> _Message: + pass + + @property + def quotes(self) -> 'Quotes': + """Importable messages for the OhMyGod""" + return self.Quotes() + + class Quotes(ABC): + @property + @abstractmethod + def BLESSING(self) -> Text: + pass + + @property + @abstractmethod + def PRAYER(self) -> Text: + pass + + @property + @abstractmethod + def HURRAY(self) -> Text: + pass + + @property + @abstractmethod + def ERROR(self) -> Text: + pass diff --git a/pyproject.toml b/pyproject.toml index d694b72..28632ab 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.poetry] name = "ohmygod" -version = "0.1.5" -description = "Rich CLI tool powered by Buddha" +version = "0.2.0" +description = "Rich CLI tool powered by gods" authors = ["Sori Lim "] license = "MIT" readme = "README.md"