From e6a908dbccd001295b6767a4e11b1d1fdcd3044b Mon Sep 17 00:00:00 2001 From: Romanin <60302782+romanin-rf@users.noreply.github.com> Date: Thu, 30 Nov 2023 00:07:13 +0200 Subject: [PATCH] Update 0.7.2.post6 - Add log on loading of SeaPlayer --- pyproject.toml | 2 +- seaplayer/objects/Log.py | 26 +++++++++++++++++------ seaplayer/plug/pipw.py | 5 +---- seaplayer/plug/pluginloader.py | 39 +++++++++++++++++++--------------- seaplayer/units.py | 2 +- 5 files changed, 45 insertions(+), 29 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 39f7224..596942e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "SeaPlayer" -version = "0.7.2.post5" +version = "0.7.2.post6" description = "SeaPlayer is a player that works in the terminal." repository = "https://github.com/romanin-rf/SeaPlayer" authors = ["Romanin "] diff --git a/seaplayer/objects/Log.py b/seaplayer/objects/Log.py index a49ff1a..b845c4a 100644 --- a/seaplayer/objects/Log.py +++ b/seaplayer/objects/Log.py @@ -1,9 +1,13 @@ +from rich.console import Console from textual.widgets import RichLog # > Typing from typing import TypeVar # > Local Import's from ..functions import rich_exception +# ! Vars +console = Console() + # ! Types RETURN = TypeVar('RETURN') @@ -20,11 +24,21 @@ def __init__(self, chap_max_width: int=8, enable_logging: bool=True, **kwargs): super().__init__(**kwargs) - def write_log(self, chap: str, msg: str, *, chap_color: str="green") -> None: + def write_log(self, chap: str, msg: str, *, chap_color: str="green", in_console: bool=False) -> None: if self.enable_logging: - self.write(f"[[{chap_color}]{chap.center(self.chap_max_width)}[/]]: {msg}", shrink=False) + text = f"[[{chap_color}]{chap.center(self.chap_max_width)}[/]]: {msg}" + self.write(text, shrink=False) + if in_console: + console.print(text) + + def info(self, msg: str, *, in_console: bool=False) -> None: + self.write_log("INFO", msg, chap_color="green", in_console=in_console) + + def error(self, msg: str, *, in_console: bool=False) -> None: + self.write_log("ERROR", msg, chap_color="red", in_console=in_console) + + def warn(self, msg: str, *, in_console: bool=False) -> None: + self.write_log("WARN", msg, chap_color="orange", in_console=in_console) - def info(self, msg: str) -> None: self.write_log("INFO", msg, chap_color="green") - def error(self, msg: str) -> None: self.write_log("ERROR", msg, chap_color="red") - def warn(self, msg: str) -> None: self.write_log("WARN", msg, chap_color="orange") - def exception(self, e: Exception) -> None: self.write_log("ERROR", rich_exception(e), chap_color="red") + def exception(self, e: Exception, *, in_console: bool=False) -> None: + self.write_log("ERROR", rich_exception(e), chap_color="red", in_console=in_console) diff --git a/seaplayer/plug/pipw.py b/seaplayer/plug/pipw.py index a3439cd..c4cc973 100644 --- a/seaplayer/plug/pipw.py +++ b/seaplayer/plug/pipw.py @@ -8,10 +8,7 @@ def __init__(self) -> None: self.python_path = sys.executable def __call__(self, *args: str) -> str: - return subprocess.check_output( - [self.python_path, "-m", "pip", *args], - stdin=subprocess.DEVNULL, stderr=subprocess.DEVNULL - ).decode(errors="ignore") + return subprocess.check_output([self.python_path, "-m", "pip", *args]).decode(errors="ignore") def install(self, *args: str) -> str: return self.__call__("install", *args) diff --git a/seaplayer/plug/pluginloader.py b/seaplayer/plug/pluginloader.py index 6b33f7a..af05637 100644 --- a/seaplayer/plug/pluginloader.py +++ b/seaplayer/plug/pluginloader.py @@ -4,6 +4,7 @@ import asyncio from pathlib import Path from pydantic import BaseModel +from rich.console import Console # > ImportLib from importlib.util import spec_from_file_location, module_from_spec # > Typing @@ -21,6 +22,9 @@ GLOB_PLUGINS_DEPS_SEARCH ) +# ! Vars +console = Console() + # ! Types class PluginModuleType(ModuleType): plugin_main: Type[PluginBase] @@ -123,7 +127,7 @@ def enable_plugin_by_name_id(self, name_id: str) -> None: # ! Plugin Loader Class class PluginLoader: __title__: str = "PluginLoader" - __version__: str = "0.2.0" + __version__: str = "0.2.6" __author__: str = "Romanin" __email__: str = "semina054@gmail.com" @@ -205,44 +209,45 @@ def load_plugin_info(path: str) -> PluginInfo: return PluginInfo.model_validate_json(data) def on_init(self) -> None: - self.app.info(f"{self.__title__} [#00ffee]v{self.__version__}[/#00ffee] from {self.__author__} ({self.__email__})") + self.app.info(f"{self.__title__} [#00ffee]v{self.__version__}[/#00ffee] from {self.__author__} ({self.__email__})", in_console=True) plugins_paths = list(self.search_plugins_paths()) - self.app.info(f"Found plugins : {repr([os.path.basename(os.path.dirname(i[0])) for i in plugins_paths])}") - self.app.info(f"Initialization plugins...") + self.app.info(f"Found plugins : {repr([os.path.basename(os.path.dirname(i[0])) for i in plugins_paths])}", in_console=True) + self.app.info(f"Initialization plugins...", in_console=True) for init_path, info_path, deps_path in plugins_paths: info = None try: info = self.load_plugin_info(info_path) if not self.config.exists_plugin(info): self.config.add_plugin(info) - self.app.info(f"{info.name} ({repr(info.name_id)}) > New plugin added to config!") + self.app.info(f"{info.name} ({repr(info.name_id)}) > New plugin added to config!", in_console=True) if self.config.is_enable_plugin(info): - self.app.info(f"{info.name} ({repr(info.name_id)}) > Plugin is [green]enabled[/green]!") + self.app.info(f"{info.name} ({repr(info.name_id)}) > Plugin is [green]enabled[/green]!", in_console=True) if deps_path is not None: - self.app.info(f"{info.name} ({repr(info.name_id)}) > Installing plugin dependencies...") - self.app.info(f"[#8700af]pip.install[/#8700af]:\n{pip.install_requirements(deps_path, True)}") - self.app.info(f"{info.name} ({repr(info.name_id)}) > Installed!") - self.app.info(f"{info.name} ({repr(info.name_id)}) > Importing in SeaPlayer...") + self.app.info(f"{info.name} ({repr(info.name_id)}) > Installing plugin dependencies...", in_console=True) + pip.install_requirements(deps_path, True) + self.app.info(f"{info.name} ({repr(info.name_id)}) > Installed!", in_console=True) + self.app.info(f"{info.name} ({repr(info.name_id)}) > Importing in SeaPlayer...", in_console=True) plugin_module = load_module(init_path) plugin = plugin_from_module(self.app, self, info, plugin_module) - self.app.info(f"{info.name} ({repr(info.name_id)}) > Imported!") + self.app.info(f"{info.name} ({repr(info.name_id)}) > Imported!", in_console=True) try: plugin.on_init() except: - self.app.error(f"Failed to do [green]`on_init`[/green] in: {plugin.info}") + self.app.error(f"Failed to do [green]`on_init`[/green] in: {plugin.info}", in_console=True) self.on_plugins.append(plugin) else: - self.app.info(f"{info.name} ({repr(info.name_id)}) > Plugin is [red]disabled[/red]!") + self.app.info(f"{info.name} ({repr(info.name_id)}) > Plugin is [red]disabled[/red]!", in_console=True) self.off_plugins.append(info) except Exception as e: self.error_plugins.append( (info_path, init_path) ) if info is not None: - self.app.error(f"Failed to load plugin: {repr(info)}") + self.app.error(f"Failed to load plugin: {repr(info)}", in_console=True) else: - self.app.error(f"Failed to load plugin: {repr(os.path.basename(os.path.dirname(info_path)))}") + self.app.error(f"Failed to load plugin: {repr(os.path.basename(os.path.dirname(info_path)))}", in_console=True) raise e - self.app.info(f"Plugins loaded ([green]ON [/green]) : {repr(self.on_plugins)}") - self.app.info(f"Plugins loaded ([red]OFF[/red]) : {repr(self.off_plugins)}") + self.app.info(f"Plugins loaded ([green]ON [/green]) : {repr(self.on_plugins)}", in_console=True) + self.app.info(f"Plugins loaded ([red]OFF[/red]) : {repr(self.off_plugins)}", in_console=True) + self.app.info(f"---", in_console=True) def on_run(self) -> None: for i in self.on_plugins: diff --git a/seaplayer/units.py b/seaplayer/units.py index 8b58003..3521925 100644 --- a/seaplayer/units.py +++ b/seaplayer/units.py @@ -6,7 +6,7 @@ # ! Metadata __title__ = "SeaPlayer" -__version__ = "0.7.2.post5" +__version__ = "0.7.2.post6" __author__ = "Romanin" __email__ = "semina054@gmail.com" __url__ = "https://github.com/romanin-rf/SeaPlayer"