Coverage for formkit_ninja / notifications.py: 0.00%

20 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-03-03 09:21 +0000

1""" 

2Notification helpers for optional integrations like Sentry. 

3""" 

4 

5from __future__ import annotations 

6 

7import importlib 

8from importlib.util import find_spec 

9from typing import Protocol 

10 

11 

12class Notifier(Protocol): 

13 """Protocol for sending notifications.""" 

14 

15 def notify(self, message: str) -> None: 

16 """Send a message to the notification backend.""" 

17 

18 

19class NullNotifier: 

20 """No-op notifier for when integrations are unavailable.""" 

21 

22 def notify(self, message: str) -> None: 

23 return None 

24 

25 

26class SentryNotifier: 

27 """Notifier that sends messages to sentry_sdk if available.""" 

28 

29 def __init__(self, sentry_sdk: object) -> None: 

30 self._sentry_sdk = sentry_sdk 

31 

32 def notify(self, message: str) -> None: 

33 if hasattr(self._sentry_sdk, "capture_message"): 

34 self._sentry_sdk.capture_message(message) 

35 

36 

37def get_default_notifier() -> Notifier: 

38 """Return the best available notifier for this runtime.""" 

39 if find_spec("sentry_sdk"): 

40 sentry_sdk = importlib.import_module("sentry_sdk") 

41 return SentryNotifier(sentry_sdk) 

42 return NullNotifier()