-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifier.py
More file actions
36 lines (24 loc) · 1.17 KB
/
Copy pathnotifier.py
File metadata and controls
36 lines (24 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""The neighboring Null Object cure, in the same domain.
``alert_email = None`` means notifications are explicitly disabled. Instead
of every caller branching on that, ``notifier_for`` hands back a NullNotifier
— a real object that intentionally does nothing — and callers stop checking.
"""
from __future__ import annotations
from patterns.python.sentinel_object.examples.layered_config.config import LayeredConfig
class EmailNotifier:
"""Fake outbound email; records sends so behavior is testable."""
def __init__(self, address: str) -> None:
self.address = address
self.sent: list[str] = []
def notify(self, message: str) -> None:
self.sent.append(f"to {self.address}: {message}")
class NullNotifier:
"""The Null Object: same interface, deliberate no-op, no None checks."""
def notify(self, message: str) -> None:
pass
def notifier_for(config: LayeredConfig) -> EmailNotifier | NullNotifier:
"""None (explicitly disabled) and unset both mean: the silent notifier."""
address = config.get("alert_email", default=None)
if isinstance(address, str):
return EmailNotifier(address)
return NullNotifier()