Skip to content

Commit

Permalink
Merge branch 'main' of github.com:memory-formation/dmf-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
pablomm committed Sep 14, 2024
2 parents addef95 + 8b59afb commit d1249b4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
24 changes: 16 additions & 8 deletions dmf/alerts/alerts.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Optional, TYPE_CHECKING, Tuple, Union

import warnings

from ..utils.typing import Literal
from ..env import env
Expand Down Expand Up @@ -58,6 +58,11 @@ def send_message(
"""
backend = get_backend()

if not backend:
warnings.warn("No alert backend is available. Message not sent.")
return

backend.send_message(
text=text, attachment=attachment, scheduled_time=scheduled_time
)
Expand Down Expand Up @@ -102,17 +107,20 @@ def send_alert(
Sending an error alert with an attachment::
send_alert(text="An error occurred. See the attached log file.", attachment="/path/to/log.txt", level="error")
send_alert(text="An error occurred. See the attached log file.", attachmenlogging.warning("No alert backend is available. Message not sent.")t="/path/to/log.txt", level="error")
"""
# Get or create the backend instance (always storing it globally)
backend = get_backend()
if not backend:
warnings.warn("No alert backend is available. Message not sent.")
return
backend.send_alert(text=text, attachment=attachment, params=params, level=level)


def get_backend(
alert_token: Optional[str] = None, store: bool = True
) -> "AlertBackend":
) -> Optional["AlertBackend"]:
"""
Get or create an AlertBackend instance. If the global backend is None or store is False, initialize it with resolved credentials.
Expand All @@ -135,7 +143,7 @@ def get_backend(

current_backend = TelegramBackend(token=alert_token)
else:
raise ValueError(f"Unsupported credential type: {credential_type}")
current_backend = None

# Store the backend if required
if store:
Expand All @@ -149,7 +157,7 @@ def get_backend(

def resolve_credentials(
alert_token: Optional[str] = None,
) -> Tuple[str, Literal["slack", "telegram"]]:
) -> Tuple[Optional[str], Optional[Literal["slack", "telegram"]]]:
"""
Resolve the credentials for the alert system. Look for environment variables, or use provided overrides.
Expand All @@ -159,15 +167,15 @@ def resolve_credentials(
alert_token = alert_token or env.getenv(ALERT_TOKEN)

if not alert_token:
raise ValueError(
f"Alert token must be provided or set in the {ALERT_TOKEN} environment variable."
)
return None, None

if alert_token.startswith("xoxb-"):
credential_type = "slack"
# Check if the token is a Telegram token format

elif len(alert_token.split(":")) == 2:
credential_type = "telegram"
else:
credential_type = None

return alert_token, credential_type
21 changes: 19 additions & 2 deletions dmf/alerts/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
from datetime import datetime
from pathlib import Path


from .alerts import send_alert

__all__ = ["alert"]
__all__ = ["alert", "DoNotAlert"]

class DoNotAlert(Exception):
"""Exception raised to prevent an alert from being sent."""
pass


def alert(
Expand All @@ -13,6 +18,7 @@ def alert(
output: bool = False,
input: bool = False,
max_length: int = 100,
disable: bool = False,
) -> Callable:
"""
Decorator that sends an alert after the execution of the decorated function.
Expand Down Expand Up @@ -64,6 +70,12 @@ def hello(name):
print(f"Hello {name}")
return Path("/path/to/{name}.pdf")
"""
# Quick disable of the alert decorator
if disable:
if func is None:
return lambda func: func
return func

if func is None:
# Used as @alert(output=True, input=True) with parentheses
def decorator(inner_func: Callable) -> Callable:
Expand All @@ -76,7 +88,6 @@ def decorator(inner_func: Callable) -> Callable:
# Used as @alert without parentheses
return _alert_decorator(func, output=output, input=input, max_length=max_length)


def _alert_decorator(
func: Callable,
output: bool,
Expand Down Expand Up @@ -118,6 +129,9 @@ def wrapper(*args, **kwargs):
text=message, level="success", params=params, attachment=attachment
)
return result
except DoNotAlert:
# Do not send an alert if the DoNotAlert exception is raised
return result
except Exception as e:
end_time = datetime.now()
message = f"Execution of function *{func.__name__}* failed with an error."
Expand Down Expand Up @@ -148,3 +162,6 @@ def _input_to_str(args, kwargs, input, max_length: int = 100) -> str:
if input is True or key in input:
input_str += f"{sep}*{key}*: _{_to_str(value, max_length)}_"
return input_str

# Assign the DoNotAlert exception to the alert decorator
setattr(alert, "DoNotAlert", DoNotAlert)

0 comments on commit d1249b4

Please sign in to comment.