Skip to content

Commit c9bae8b

Browse files
authored
Provide easy add access to apprise.Apprise (#9)
* Easy "add" method * Improve documentation * reformat
1 parent 436207d commit c9bae8b

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Logprise is a Python package that seamlessly integrates [loguru](https://github.
77
- Unified logging interface that captures both standard logging and loguru logs
88
- Automatic notification delivery based on configurable log levels
99
- Batched notifications to prevent notification spam
10-
- Flexible configuration through apprise's extensive notification service support
10+
- Flexible configuration through apprises extensive notification service support
1111
- Periodic flushing of log messages at configurable intervals
1212
- Automatic capture of uncaught exceptions
1313
- Easy integration with existing Python applications
@@ -44,10 +44,17 @@ logger.error("This will trigger a notification") # Default is ERROR level
4444

4545
### Notification Services
4646

47-
Logprise uses Apprise for notifications, which supports a wide range of notification services. Create an `.apprise` file in one of the default configuration paths:
47+
Logprise uses Apprise for notifications, which supports a wide range of notification services. You can configure these in two ways:
4848

49-
- `~/.apprise`
50-
- `~/.config/apprise`
49+
#### 1. Configuration File
50+
51+
Create an `.apprise` file in one of the default configuration paths:
52+
53+
- `~/.apprise` \[or `%APPDATA%/Apprise/apprise`]
54+
- `~/.config/apprise` \[or `%LOCALAPPDATA%/Apprise/apprise`]
55+
- '/etc/apprise' \[or `%ALLUSERSPROFILE%/Apprise/apprise`]
56+
57+
*For more possible configuration file locations, please check: `DEFAULT_CONFIG_PATHS` in [apprises source code](https://github.com/caronc/apprise/blob/master/apprise/cli.py).*
5158

5259
Example configuration:
5360

@@ -57,6 +64,23 @@ tgram://bot_token/chat_id
5764
slack://tokenA/tokenB/tokenC/#channel
5865
```
5966

67+
#### 2. Programmatically Add Services
68+
69+
You can easily add more notification services programmatically:
70+
71+
```python
72+
from logprise import appriser
73+
74+
# Add a single URL
75+
appriser.add("mailto://user:pass@gmail.com")
76+
77+
# Add multiple URLs
78+
appriser.add(["tgram://bot_token/chat_id", "slack://tokenA/tokenB/tokenC/#channel"])
79+
80+
# Add URLs with tags
81+
appriser.add("discord://webhook_id/webhook_token", tag=["critical"])
82+
```
83+
6084
See [Apprise's configuration guide](https://github.com/caronc/apprise/wiki/config#cli) for the full list of supported services and their configuration.
6185

6286
### Notification Levels

logprise/__init__.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
if TYPE_CHECKING:
2020
import types
21-
from collections.abc import Callable
21+
from collections.abc import Callable, Iterable
22+
23+
from apprise import AppriseAsset, AppriseConfig, ConfigBase, NotifyBase
2224

2325
__all__ = ["appriser", "logger"]
2426

@@ -153,6 +155,25 @@ def _start_periodic_flush(self) -> None:
153155
self._flush_thread = threading.Thread(target=self._periodic_flush, daemon=True, name="logprise-flush")
154156
self._flush_thread.start()
155157

158+
def add(
159+
self,
160+
servers: str | dict | Iterable | ConfigBase | NotifyBase | AppriseConfig,
161+
asset: AppriseAsset = None,
162+
tag: list[str] | None = None,
163+
) -> bool:
164+
"""
165+
Adds one or more server URLs into our list.
166+
167+
This is a direct wrapper around the `apprise.Apprise.add()` method.
168+
For detailed documentation, see:
169+
https://github.com/caronc/apprise/wiki/Development_API#add-add-a-new-notification-service-by-urls
170+
171+
Returns:
172+
True if the server(s) were added successfully, False otherwise.
173+
"""
174+
175+
return self.apprise_obj.add(servers=servers, asset=asset, tag=tag)
176+
156177
def stop_periodic_flush(self) -> None:
157178
"""Stop the periodic flush thread."""
158179
if self._flush_thread and self._flush_thread.is_alive():

tests/test_adding_add_method.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from logprise import Appriser
2+
3+
4+
def test_add_method(mocker):
5+
"""Test that add method correctly passes through to apprise_obj.add"""
6+
appriser = Appriser()
7+
8+
# Create a mock for the underlying apprise_obj.add method
9+
mock_add = mocker.patch.object(appriser.apprise_obj, "add", return_value=True)
10+
11+
# Test with a simple string URL
12+
test_url = "mailto://user:pass@example.com"
13+
assert appriser.add(test_url) is True
14+
15+
# Verify add was called with correct parameters
16+
mock_add.assert_called_once_with(servers=test_url, asset=None, tag=None)
17+
18+
# Reset mock for next test
19+
mock_add.reset_mock()
20+
21+
# Test with a dictionary
22+
test_dict = {"urls": ["mailto://user:pass@example.com"]}
23+
test_tag = ["important"]
24+
assert appriser.add(test_dict, tag=test_tag) is True
25+
26+
# Verify add was called with correct parameters
27+
mock_add.assert_called_once_with(servers=test_dict, asset=None, tag=test_tag)
28+
29+
# Test return value pass-through (False case)
30+
mock_add.return_value = False
31+
mock_add.reset_mock()
32+
33+
assert appriser.add("invalid://url") is False
34+
mock_add.assert_called_once()

0 commit comments

Comments
 (0)