From 77308f40d4100751db766ee2209f2975cf3f7928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9d=C3=A9ric=20Hurier=20=28Fmind=29?= Date: Sat, 14 Dec 2024 10:34:20 +0100 Subject: [PATCH] fix(notifications): print and warn when notification module is not available --- src/bikes/io/services.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/bikes/io/services.py b/src/bikes/io/services.py index 5743bef..fe75b72 100644 --- a/src/bikes/io/services.py +++ b/src/bikes/io/services.py @@ -8,6 +8,7 @@ import contextlib as ctx import sys import typing as T +import warnings import loguru import mlflow @@ -113,14 +114,27 @@ def notify(self, title: str, message: str) -> None: message (str): message of the notification. """ if self.enable: - notification.notify( - title=title, - message=message, - app_name=self.app_name, - timeout=self.timeout, - ) + try: + notification.notify( + title=title, + message=message, + app_name=self.app_name, + timeout=self.timeout, + ) + except NotImplementedError: + warnings.warn("Notifications are not supported on this system.", RuntimeWarning) + self._print(title=title, message=message) else: - print(f"[{self.app_name}] {title}: {message}") + self._print(title=title, message=message) + + def _print(self, title: str, message: str) -> None: + """Print a notification to the system. + + Args: + title (str): title of the notification. + message (str): message of the notification. + """ + print(f"[{self.app_name}] {title}: {message}") class MlflowService(Service):