Debian Codegraph @

plyer/platforms/linux/notification.py

raw symbols SCIP
1 '''
2 Module of Linux API for plyer.notification.
3 '''
4
5 import warnings
6 import subprocess
7 from plyer.facades import Notification
8 from plyer.utils import whereis_exe
9 import os
10
11
12 class NotifyDesktopPortals(Notification):
13 '''
14 Implementation of xdg-desktop-portals API.
15 '''
16
17 def _notify(self, **kwargs):
18 title = kwargs.get("title", "title")
19 body = kwargs.get("message", "body")
20
21 subprocess.run([
22 "gdbus", "call", "--session", "--dest",
23 "org.freedesktop.portal.Desktop",
24 "--object-path", "/org/freedesktop/portal/desktop", "--method",
25 "org.freedesktop.portal.Notification.AddNotification", "",
26 "{'title': <'" + title + "'>, 'body': <'" + body + "'>}"
27 ], stdout=subprocess.DEVNULL)
28
29
30 class NotifySendNotification(Notification):
31 '''
32 Implementation of Linux notification API
33 using notify-send binary.
34 '''
35 def _notify(self, **kwargs):
36 icon = kwargs.get('icon', '')
37 title = kwargs.get('title', 'title')
38 hint = kwargs.get('hint', 'string::')
39 message = kwargs.get('message', 'body')
40 category = kwargs.get('category', '')
41 app_name = kwargs.get('app_name', '')
42 urgency = kwargs.get('urgency', 'normal')
43 expire_time = kwargs.get('expire_time', '0')
44
45 notify_send_args = (title,
46 message,
47 "-i", icon,
48 "-h", hint,
49 "-u", urgency,
50 "-c", category,
51 "-a", app_name,
52 "-t", expire_time)
53
54 subprocess.call(["notify-send", *notify_send_args])
55
56
57 class NotifyDbus(Notification):
58 '''
59 Implementation of Linux notification API
60 using dbus library and dbus-python wrapper.
61 '''
62
63 def _notify(self, **kwargs):
64 summary = kwargs.get('title', "title")
65 body = kwargs.get('message', "body")
66 app_name = kwargs.get('app_name', '')
67 app_icon = kwargs.get('app_icon', '')
68 timeout = kwargs.get('timeout', 10)
69 actions = kwargs.get('actions', [])
70 hints = kwargs.get('hints', {})
71 replaces_id = kwargs.get('replaces_id', 0)
72
73 _bus_name = 'org.freedesktop.Notifications'
74 _object_path = '/org/freedesktop/Notifications'
75 _interface_name = _bus_name
76
77 import dbus
78 session_bus = dbus.SessionBus()
79 obj = session_bus.get_object(_bus_name, _object_path)
80 interface = dbus.Interface(obj, _interface_name)
81 interface.Notify(
82 app_name, replaces_id, app_icon,
83 summary, body, actions,
84 hints, timeout * 1000
85 )
86
87
88 def instance():
89 '''
90 Instance for facade proxy.
91 '''
92 if os.path.isdir("/app"):
93 # Flatpak
94 return NotifyDesktopPortals()
95 try:
96 import dbus # noqa: F401
97 return NotifyDbus()
98 except ImportError:
99 msg = ("The Python dbus package is not installed.\n"
100 "Try installing it with your distribution's package manager, "
101 "it is usually called python-dbus or python3-dbus, but you "
102 "might have to try dbus-python instead, e.g. when using pip.")
103 warnings.warn(msg)
104
105 if whereis_exe('notify-send'):
106 return NotifySendNotification()
107 warnings.warn("notify-send not found.")
108 return Notification()