desktop-notifier 5.0.0__tar.gz → 5.0.0.dev1__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/src/desktop_notifier.egg-info → desktop_notifier-5.0.0.dev1}/PKG-INFO +1 -1
  2. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/pyproject.toml +1 -1
  3. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier/__init__.py +2 -3
  4. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier/main.py +90 -22
  5. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1/src/desktop_notifier.egg-info}/PKG-INFO +1 -1
  6. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier.egg-info/SOURCES.txt +1 -3
  7. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/tests/test_api.py +26 -0
  8. desktop_notifier-5.0.0/src/desktop_notifier/sync.py +0 -137
  9. desktop_notifier-5.0.0/tests/test_sync_api.py +0 -66
  10. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/LICENSE +0 -0
  11. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/README.md +0 -0
  12. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/setup.cfg +0 -0
  13. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier/base.py +0 -0
  14. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier/dbus.py +0 -0
  15. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier/dummy.py +0 -0
  16. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier/macos.py +0 -0
  17. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier/macos_support.py +0 -0
  18. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier/py.typed +0 -0
  19. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier/resources/__init__.py +0 -0
  20. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier/resources/python.png +0 -0
  21. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier/winrt.py +0 -0
  22. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier.egg-info/dependency_links.txt +0 -0
  23. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/src/desktop_notifier.egg-info/requires.txt +0 -0
  24. {desktop_notifier-5.0.0 → desktop_notifier-5.0.0.dev1}/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
3
+ Version: 5.0.0.dev1
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"
7
+ version = "5.0.0.dev1"
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
19
18
 
20
- __version__ = "5.0.0"
19
+
20
+ __version__ = "5.0.0.dev1"
21
21
  __author__ = "Sam Schott"
22
22
  __url__ = "https://github.com/samschott/desktop-notifier"
23
23
 
@@ -33,7 +33,6 @@ __all__ = [
33
33
  "Sound",
34
34
  "Attachment",
35
35
  "DesktopNotifier",
36
- "DesktopNotifierSync",
37
36
  "Capability",
38
37
  "DEFAULT_SOUND",
39
38
  "DEFAULT_ICON",
@@ -1,6 +1,6 @@
1
1
  # -*- coding: utf-8 -*-
2
2
  """
3
- Asynchronous desktop notification API
3
+ Desktop notifications for Windows, Linux, macOS, iOS and iPadOS.
4
4
  """
5
5
 
6
6
  from __future__ import annotations
@@ -15,6 +15,7 @@ from pathlib import Path
15
15
  from typing import (
16
16
  Type,
17
17
  Callable,
18
+ Coroutine,
18
19
  List,
19
20
  Any,
20
21
  TypeVar,
@@ -125,10 +126,11 @@ class DesktopNotifier:
125
126
  Linux, this should correspond to the application name in a desktop entry. On
126
127
  macOS, this argument is ignored and the app is identified by the bundle ID of
127
128
  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.
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).
132
134
  :param notification_limit: Maximum number of notifications to keep in the system's
133
135
  notification center. This may be ignored by some implementations.
134
136
  """
@@ -138,9 +140,11 @@ class DesktopNotifier:
138
140
  def __init__(
139
141
  self,
140
142
  app_name: str = "Python",
141
- app_icon: Icon | Path | str | None = DEFAULT_ICON,
143
+ app_icon: Path | str | Icon | None = DEFAULT_ICON,
142
144
  notification_limit: int | None = None,
143
145
  ) -> None:
146
+ impl_cls = get_implementation_class()
147
+
144
148
  if isinstance(app_icon, str):
145
149
  warnings.warn(
146
150
  message="Pass an Icon instance instead of a string. "
@@ -162,12 +166,28 @@ class DesktopNotifier:
162
166
 
163
167
  self.app_icon = app_icon
164
168
 
165
- impl_cls = get_implementation_class()
166
169
  self._impl = impl_cls(app_name, notification_limit)
167
170
  self._did_request_authorisation = False
168
171
 
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
+
169
176
  self._capabilities: frozenset[Capability] | None = None
170
177
 
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
+
171
191
  @property
172
192
  def app_name(self) -> str:
173
193
  """The application name"""
@@ -201,17 +221,11 @@ class DesktopNotifier:
201
221
  """
202
222
  Sends a desktop notification.
203
223
 
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).
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.
212
227
 
213
228
  :param notification: The notification to send.
214
- :returns: The passed notification instance with a unique identifier populated.
215
229
  """
216
230
  if not notification.icon:
217
231
  notification.icon = self.app_icon
@@ -245,9 +259,16 @@ class DesktopNotifier:
245
259
  """
246
260
  Sends a desktop notification
247
261
 
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`.
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).
251
272
 
252
273
  :returns: The scheduled notification instance.
253
274
  """
@@ -267,6 +288,52 @@ class DesktopNotifier:
267
288
  )
268
289
  return await self.send_notification(notification)
269
290
 
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
+
270
337
  @property
271
338
  def current_notifications(self) -> List[Notification]:
272
339
  """A list of all currently displayed notifications for this app"""
@@ -287,10 +354,11 @@ class DesktopNotifier:
287
354
  """
288
355
  await self._impl.clear_all()
289
356
 
290
- async def get_capabilities(self) -> frozenset[Capability]:
357
+ @property
358
+ def capabilities(self) -> frozenset[Capability]:
291
359
  """
292
- Returns which functionality is supported by the implementation.
360
+ Returns which functionality us supported by the implementation.
293
361
  """
294
362
  if not self._capabilities:
295
- self._capabilities = await self._impl.get_capabilities()
363
+ self._capabilities = self._run_coro_sync(self._impl.get_capabilities())
296
364
  return self._capabilities
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: desktop-notifier
3
- Version: 5.0.0
3
+ Version: 5.0.0.dev1
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,7 +9,6 @@ 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
13
12
  src/desktop_notifier/winrt.py
14
13
  src/desktop_notifier.egg-info/PKG-INFO
15
14
  src/desktop_notifier.egg-info/SOURCES.txt
@@ -18,5 +17,4 @@ src/desktop_notifier.egg-info/requires.txt
18
17
  src/desktop_notifier.egg-info/top_level.txt
19
18
  src/desktop_notifier/resources/__init__.py
20
19
  src/desktop_notifier/resources/python.png
21
- tests/test_api.py
22
- tests/test_sync_api.py
20
+ tests/test_api.py
@@ -110,6 +110,32 @@ 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
+
113
139
  @pytest.mark.asyncio
114
140
  @pytest.mark.skipif(
115
141
  sys.platform.startswith("win"),
@@ -1,137 +0,0 @@
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,66 +0,0 @@
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