desktop-notifier 5.0.0.dev1__tar.gz → 5.0.0.dev2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. {desktop_notifier-5.0.0.dev1/src/desktop_notifier.egg-info → desktop_notifier-5.0.0.dev2}/PKG-INFO +1 -1
  2. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/pyproject.toml +1 -1
  3. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier/__init__.py +3 -2
  4. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier/main.py +22 -90
  5. desktop_notifier-5.0.0.dev2/src/desktop_notifier/sync.py +137 -0
  6. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2/src/desktop_notifier.egg-info}/PKG-INFO +1 -1
  7. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier.egg-info/SOURCES.txt +3 -1
  8. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/tests/test_api.py +0 -26
  9. desktop_notifier-5.0.0.dev2/tests/test_sync_api.py +66 -0
  10. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/LICENSE +0 -0
  11. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/README.md +0 -0
  12. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/setup.cfg +0 -0
  13. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier/base.py +0 -0
  14. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier/dbus.py +0 -0
  15. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier/dummy.py +0 -0
  16. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier/macos.py +0 -0
  17. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier/macos_support.py +0 -0
  18. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier/py.typed +0 -0
  19. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier/resources/__init__.py +0 -0
  20. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier/resources/python.png +0 -0
  21. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier/winrt.py +0 -0
  22. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier.egg-info/dependency_links.txt +0 -0
  23. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier.egg-info/requires.txt +0 -0
  24. {desktop_notifier-5.0.0.dev1 → desktop_notifier-5.0.0.dev2}/src/desktop_notifier.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: desktop-notifier
3
- Version: 5.0.0.dev1
3
+ Version: 5.0.0.dev2
4
4
  Summary: Python library for cross-platform desktop notifications
5
5
  Author-email: Sam Schott <sam.schott@outlook.com>
6
6
  License: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "desktop-notifier"
7
- version = "5.0.0.dev1"
7
+ version = "5.0.0.dev2"
8
8
  authors = [{name = "Sam Schott", email = "sam.schott@outlook.com"}]
9
9
  license = {text = "MIT"}
10
10
  description = "Python library for cross-platform desktop notifications"
@@ -15,9 +15,9 @@ from .main import (
15
15
  DEFAULT_SOUND,
16
16
  DEFAULT_ICON,
17
17
  )
18
+ from .sync import DesktopNotifierSync
18
19
 
19
-
20
- __version__ = "5.0.0.dev1"
20
+ __version__ = "5.0.0.dev2"
21
21
  __author__ = "Sam Schott"
22
22
  __url__ = "https://github.com/samschott/desktop-notifier"
23
23
 
@@ -33,6 +33,7 @@ __all__ = [
33
33
  "Sound",
34
34
  "Attachment",
35
35
  "DesktopNotifier",
36
+ "DesktopNotifierSync",
36
37
  "Capability",
37
38
  "DEFAULT_SOUND",
38
39
  "DEFAULT_ICON",
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  """
3
- Desktop notifications for Windows, Linux, macOS, iOS and iPadOS.
3
+ Asynchronous desktop notification API
4
4
  """
5
5
 
6
6
  from __future__ import annotations
@@ -15,7 +15,6 @@ from pathlib import Path
15
15
  from typing import (
16
16
  Type,
17
17
  Callable,
18
- Coroutine,
19
18
  List,
20
19
  Any,
21
20
  TypeVar,
@@ -126,11 +125,10 @@ class DesktopNotifier:
126
125
  Linux, this should correspond to the application name in a desktop entry. On
127
126
  macOS, this argument is ignored and the app is identified by the bundle ID of
128
127
  the sending program (e.g., Python).
129
- :param app_icon: Default icon to use for notifications. This should be either a URI
130
- string, a :class:`pathlib.Path` path, or a name in a freedesktop.org-compliant
131
- icon theme. If None, the icon of the calling application will be used if it
132
- can be determined. On macOS, this argument is ignored and the app icon is
133
- identified by the bundle ID of the sending program (e.g., Python).
128
+ :param app_icon: Default icon to use for notifications. This should be a
129
+ :class:`desktop_notifier.base.Icon` instance referencing either a file or a
130
+ named system icon. :class:`str` or :class:`pathlib.Path` are also accepted but
131
+ deprecated.
134
132
  :param notification_limit: Maximum number of notifications to keep in the system's
135
133
  notification center. This may be ignored by some implementations.
136
134
  """
@@ -140,11 +138,9 @@ class DesktopNotifier:
140
138
  def __init__(
141
139
  self,
142
140
  app_name: str = "Python",
143
- app_icon: Path | str | Icon | None = DEFAULT_ICON,
141
+ app_icon: Icon | Path | str | None = DEFAULT_ICON,
144
142
  notification_limit: int | None = None,
145
143
  ) -> None:
146
- impl_cls = get_implementation_class()
147
-
148
144
  if isinstance(app_icon, str):
149
145
  warnings.warn(
150
146
  message="Pass an Icon instance instead of a string. "
@@ -166,28 +162,12 @@ class DesktopNotifier:
166
162
 
167
163
  self.app_icon = app_icon
168
164
 
165
+ impl_cls = get_implementation_class()
169
166
  self._impl = impl_cls(app_name, notification_limit)
170
167
  self._did_request_authorisation = False
171
168
 
172
- # Use our own event loop for the sync API so that we don't interfere with any
173
- # other ansycio event loops / threads, etc.
174
- self._loop = default_event_loop_policy.new_event_loop()
175
-
176
169
  self._capabilities: frozenset[Capability] | None = None
177
170
 
178
- def _run_coro_sync(self, coro: Coroutine[None, None, T]) -> T:
179
- """
180
- Runs the given coroutine and returns the result synchronously. This is used as a
181
- wrapper to conveniently convert the async API calls to synchronous ones.
182
- """
183
- if self._loop.is_running():
184
- future = asyncio.run_coroutine_threadsafe(coro, self._loop)
185
- res = future.result()
186
- else:
187
- res = self._loop.run_until_complete(coro)
188
-
189
- return res
190
-
191
171
  @property
192
172
  def app_name(self) -> str:
193
173
  """The application name"""
@@ -221,11 +201,17 @@ class DesktopNotifier:
221
201
  """
222
202
  Sends a desktop notification.
223
203
 
224
- This method takes a fully constructed :class:`Notification` instance as input.
225
- Use :meth:`send` to provide separate notification properties such as ``title``,
226
- ``message``, etc., instead.
204
+ This method does not raise an exception when scheduling the notification fails
205
+ but logs warnings instead. If the notification was scheduled successfully, its
206
+ ``identifier`` will be set to the platform's native notification identifier.
207
+ Otherwise, the ``identifier`` will be ``None``.
208
+
209
+ Note that even a successfully scheduled notification may not be displayed to the
210
+ user, depending on their notification center settings (for instance if "do not
211
+ disturb" is enabled on macOS).
227
212
 
228
213
  :param notification: The notification to send.
214
+ :returns: The passed notification instance with a unique identifier populated.
229
215
  """
230
216
  if not notification.icon:
231
217
  notification.icon = self.app_icon
@@ -259,16 +245,9 @@ class DesktopNotifier:
259
245
  """
260
246
  Sends a desktop notification
261
247
 
262
- Arguments are the same as and will be passed on to :class:`Notification`.
263
-
264
- This method will always return a :class:`Notification` instance and will not
265
- raise an exception when scheduling the notification fails. If the notification
266
- was scheduled successfully, its ``identifier`` will be set to the platform's
267
- native notification identifier. Otherwise, the ``identifier`` will be ``None``.
268
-
269
- Note that even a successfully scheduled notification may not be displayed to the
270
- user, depending on their notification center settings (for instance if "do not
271
- disturb" is enabled on macOS).
248
+ This is a convenience function which creates a
249
+ :class:`desktop_notifier.base.Notification` with the provided arguments and then
250
+ calls :meth:`send_notification`.
272
251
 
273
252
  :returns: The scheduled notification instance.
274
253
  """
@@ -288,52 +267,6 @@ class DesktopNotifier:
288
267
  )
289
268
  return await self.send_notification(notification)
290
269
 
291
- def send_sync(
292
- self,
293
- title: str,
294
- message: str,
295
- urgency: Urgency = Urgency.Normal,
296
- icon: str | Icon | None = None,
297
- buttons: Sequence[Button] = (),
298
- reply_field: ReplyField | None = None,
299
- on_clicked: Callable[[], Any] | None = None,
300
- on_dismissed: Callable[[], Any] | None = None,
301
- attachment: str | Attachment | None = None,
302
- sound: bool | Sound | None = None,
303
- thread: str | None = None,
304
- timeout: int = -1,
305
- ) -> Notification:
306
- """
307
- Synchronous call of :meth:`send`, for use without an asyncio event loop.
308
-
309
- .. deprecated:: 5.0.0
310
- Use the async :func:`send` instead and schedule and block on its completion
311
- if required.
312
-
313
- :returns: The scheduled notification instance.
314
- """
315
- warnings.warn(
316
- message="'send_sync' is deprecated and will be removed in a future "
317
- "version. Use the async 'send' API instead",
318
- category=DeprecationWarning,
319
- )
320
-
321
- coro = self.send(
322
- title,
323
- message,
324
- urgency=urgency,
325
- icon=icon,
326
- buttons=buttons,
327
- reply_field=reply_field,
328
- on_clicked=on_clicked,
329
- on_dismissed=on_dismissed,
330
- attachment=attachment,
331
- sound=sound,
332
- thread=thread,
333
- timeout=timeout,
334
- )
335
- return self._run_coro_sync(coro)
336
-
337
270
  @property
338
271
  def current_notifications(self) -> List[Notification]:
339
272
  """A list of all currently displayed notifications for this app"""
@@ -354,11 +287,10 @@ class DesktopNotifier:
354
287
  """
355
288
  await self._impl.clear_all()
356
289
 
357
- @property
358
- def capabilities(self) -> frozenset[Capability]:
290
+ async def get_capabilities(self) -> frozenset[Capability]:
359
291
  """
360
- Returns which functionality us supported by the implementation.
292
+ Returns which functionality is supported by the implementation.
361
293
  """
362
294
  if not self._capabilities:
363
- self._capabilities = self._run_coro_sync(self._impl.get_capabilities())
295
+ self._capabilities = await self._impl.get_capabilities()
364
296
  return self._capabilities
@@ -0,0 +1,137 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Synchronous desktop notification API
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ # system imports
9
+ import asyncio
10
+ from typing import Callable, Coroutine, Any, Sequence, TypeVar, List
11
+
12
+ # local imports
13
+ from .main import DesktopNotifier
14
+ from .base import (
15
+ Capability,
16
+ Urgency,
17
+ Button,
18
+ ReplyField,
19
+ Icon,
20
+ Sound,
21
+ Attachment,
22
+ Notification,
23
+ DEFAULT_ICON,
24
+ )
25
+
26
+ __all__ = ["DesktopNotifierSync"]
27
+
28
+
29
+ T = TypeVar("T")
30
+
31
+
32
+ class DesktopNotifierSync:
33
+ """
34
+ A synchronous counterpart to :class:`desktop_notifier.main.DesktopNotifier`
35
+
36
+ .. warning::
37
+ Callbacks on interaction with the notification will not work on macOS or Linux
38
+ without a running event loop.
39
+ """
40
+
41
+ def __init__(
42
+ self,
43
+ app_name: str = "Python",
44
+ app_icon: Icon | None = DEFAULT_ICON,
45
+ notification_limit: int | None = None,
46
+ ) -> None:
47
+ self._async_api = DesktopNotifier(app_name, app_icon, notification_limit)
48
+ self._loop = asyncio.new_event_loop()
49
+
50
+ def _run_coro_sync(self, coro: Coroutine[None, None, T]) -> T:
51
+ # Make sure to always use the same loop because async queues, future, etc. are
52
+ # always bound to a loop.
53
+ if self._loop.is_running():
54
+ future = asyncio.run_coroutine_threadsafe(coro, self._loop)
55
+ res = future.result()
56
+ else:
57
+ res = self._loop.run_until_complete(coro)
58
+
59
+ return res
60
+
61
+ @property
62
+ def app_name(self) -> str:
63
+ """The application name"""
64
+ return self._async_api.app_name
65
+
66
+ @app_name.setter
67
+ def app_name(self, value: str) -> None:
68
+ """Setter: app_name"""
69
+ self._async_api.app_name = value
70
+
71
+ def request_authorisation(self) -> bool:
72
+ """See :meth:`desktop_notifier.main.DesktopNotifier.request_authorisation`"""
73
+ coro = self._async_api.request_authorisation()
74
+ return self._run_coro_sync(coro)
75
+
76
+ def has_authorisation(self) -> bool:
77
+ """See :meth:`desktop_notifier.main.DesktopNotifier.has_authorisation`"""
78
+ coro = self._async_api.has_authorisation()
79
+ return self._run_coro_sync(coro)
80
+
81
+ def send_notification(self, notification: Notification) -> Notification:
82
+ """See :meth:`desktop_notifier.main.DesktopNotifier.send_notification`"""
83
+ coro = self._async_api.send_notification(notification)
84
+ return self._run_coro_sync(coro)
85
+
86
+ def send(
87
+ self,
88
+ title: str,
89
+ message: str,
90
+ urgency: Urgency = Urgency.Normal,
91
+ icon: Icon | None = None,
92
+ buttons: Sequence[Button] = (),
93
+ reply_field: ReplyField | None = None,
94
+ on_clicked: Callable[[], Any] | None = None,
95
+ on_dismissed: Callable[[], Any] | None = None,
96
+ attachment: Attachment | None = None,
97
+ sound: Sound | None = None,
98
+ thread: str | None = None,
99
+ timeout: int = -1,
100
+ ) -> Notification:
101
+ """See :meth:`desktop_notifier.main.DesktopNotifier.send`"""
102
+ notification = Notification(
103
+ title,
104
+ message,
105
+ urgency=urgency,
106
+ icon=icon,
107
+ buttons=buttons,
108
+ reply_field=reply_field,
109
+ on_clicked=on_clicked,
110
+ on_dismissed=on_dismissed,
111
+ attachment=attachment,
112
+ sound=sound,
113
+ thread=thread,
114
+ timeout=timeout,
115
+ )
116
+ coro = self._async_api.send_notification(notification)
117
+ return self._run_coro_sync(coro)
118
+
119
+ @property
120
+ def current_notifications(self) -> List[Notification]:
121
+ """A list of all currently displayed notifications for this app"""
122
+ return self._async_api.current_notifications
123
+
124
+ def clear(self, notification: Notification) -> None:
125
+ """See :meth:`desktop_notifier.main.DesktopNotifier.notification`"""
126
+ coro = self._async_api.clear(notification)
127
+ return self._run_coro_sync(coro)
128
+
129
+ def clear_all(self) -> None:
130
+ """See :meth:`desktop_notifier.main.DesktopNotifier.clear_all`"""
131
+ coro = self._async_api.clear_all()
132
+ return self._run_coro_sync(coro)
133
+
134
+ def get_capabilities(self) -> frozenset[Capability]:
135
+ """See :meth:`desktop_notifier.main.DesktopNotifier.get_capabilities`"""
136
+ coro = self._async_api.get_capabilities()
137
+ return self._run_coro_sync(coro)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: desktop-notifier
3
- Version: 5.0.0.dev1
3
+ Version: 5.0.0.dev2
4
4
  Summary: Python library for cross-platform desktop notifications
5
5
  Author-email: Sam Schott <sam.schott@outlook.com>
6
6
  License: MIT
@@ -9,6 +9,7 @@ src/desktop_notifier/macos.py
9
9
  src/desktop_notifier/macos_support.py
10
10
  src/desktop_notifier/main.py
11
11
  src/desktop_notifier/py.typed
12
+ src/desktop_notifier/sync.py
12
13
  src/desktop_notifier/winrt.py
13
14
  src/desktop_notifier.egg-info/PKG-INFO
14
15
  src/desktop_notifier.egg-info/SOURCES.txt
@@ -17,4 +18,5 @@ src/desktop_notifier.egg-info/requires.txt
17
18
  src/desktop_notifier.egg-info/top_level.txt
18
19
  src/desktop_notifier/resources/__init__.py
19
20
  src/desktop_notifier/resources/python.png
20
- tests/test_api.py
21
+ tests/test_api.py
22
+ tests/test_sync_api.py
@@ -110,32 +110,6 @@ async def test_attachment_uri(notifier):
110
110
  )
111
111
 
112
112
 
113
- def test_send_sync(notifier):
114
- notification = notifier.send_sync(
115
- title="Julius Caesar",
116
- message="Et tu, Brute?",
117
- urgency=Urgency.Critical,
118
- buttons=[
119
- Button(
120
- title="Mark as read",
121
- on_pressed=lambda: print("Marked as read"),
122
- )
123
- ],
124
- reply_field=ReplyField(
125
- title="Reply",
126
- button_title="Send",
127
- on_replied=lambda text: print("Brutus replied:", text),
128
- ),
129
- on_clicked=lambda: print("Notification clicked"),
130
- on_dismissed=lambda: print("Notification dismissed"),
131
- sound=DEFAULT_SOUND,
132
- thread="test_notifications",
133
- timeout=5,
134
- )
135
- assert notification in notifier.current_notifications
136
- assert notification.identifier != ""
137
-
138
-
139
113
  @pytest.mark.asyncio
140
114
  @pytest.mark.skipif(
141
115
  sys.platform.startswith("win"),
@@ -0,0 +1,66 @@
1
+ import sys
2
+ import pytest
3
+
4
+ from desktop_notifier import Urgency, Button, ReplyField, DEFAULT_SOUND
5
+
6
+
7
+ def test_send(notifier_sync):
8
+ notification = notifier_sync.send(
9
+ title="Julius Caesar",
10
+ message="Et tu, Brute?",
11
+ urgency=Urgency.Critical,
12
+ buttons=[
13
+ Button(
14
+ title="Mark as read",
15
+ on_pressed=lambda: print("Marked as read"),
16
+ )
17
+ ],
18
+ reply_field=ReplyField(
19
+ title="Reply",
20
+ button_title="Send",
21
+ on_replied=lambda text: print("Brutus replied:", text),
22
+ ),
23
+ on_clicked=lambda: print("Notification clicked"),
24
+ on_dismissed=lambda: print("Notification dismissed"),
25
+ sound=DEFAULT_SOUND,
26
+ thread="test_notifications",
27
+ timeout=5,
28
+ )
29
+ assert notification in notifier_sync.current_notifications
30
+ assert notification.identifier != ""
31
+
32
+
33
+ @pytest.mark.skipif(
34
+ sys.platform.startswith("win"),
35
+ reason="Clearing individual notifications is broken on Windows",
36
+ )
37
+ def test_clear(notifier_sync):
38
+ n0 = notifier_sync.send(
39
+ title="Julius Caesar",
40
+ message="Et tu, Brute?",
41
+ )
42
+ n1 = notifier_sync.send(
43
+ title="Julius Caesar",
44
+ message="Et tu, Brute?",
45
+ )
46
+ assert n0 in notifier_sync.current_notifications
47
+ assert n1 in notifier_sync.current_notifications
48
+
49
+ notifier_sync.clear(n0)
50
+ assert n0 not in notifier_sync.current_notifications
51
+
52
+
53
+ def test_clear_all(notifier_sync):
54
+ n0 = notifier_sync.send(
55
+ title="Julius Caesar",
56
+ message="Et tu, Brute?",
57
+ )
58
+ n1 = notifier_sync.send(
59
+ title="Julius Caesar",
60
+ message="Et tu, Brute?",
61
+ )
62
+ assert n0 in notifier_sync.current_notifications
63
+ assert n1 in notifier_sync.current_notifications
64
+
65
+ notifier_sync.clear_all()
66
+ assert len(notifier_sync.current_notifications) == 0