2
2
import subprocess
3
3
import shlex
4
4
5
- from ..exceptions import BinaryNotFound , NotificationFailure , LinuxDbusException
5
+ from ..exceptions import BinaryNotFound
6
6
from ._base import BaseNotifier
7
7
8
8
try :
9
9
from jeepney import DBusAddress , new_method_call
10
10
from jeepney .io .blocking import open_dbus_connection
11
- import os
12
-
13
- # check if dbus is available
14
- _dbus_address = os .getenv ("DBUS_SESSION_BUS_ADDRESS" )
15
- if _dbus_address :
16
- logger .info ("Jeepney and Dbus is available. Using DBUS for notifications.." )
17
- USE_LEGACY = False
18
- else :
19
- logger .error (
20
- "Jeepney is available but DBUS is not. Using legacy notification instead."
21
- )
22
- USE_LEGACY = True
23
- except ImportError :
24
- logger .error ("DBUS suppport not installed. Using libnotify for notifications!" )
25
- USE_LEGACY = True
11
+ from shutil import which
26
12
13
+ NOTIFY = which ('notify-send' ) # alternatively: from ctypes.util import find_library
27
14
28
- class LinuxNotifierLibNotify (BaseNotifier ):
29
- def __init__ (self , ** kwargs ):
30
- """Main Linux Notification Class
15
+ if NOTIFY :
16
+ logger .info ("libnotify found, using it for notifications" )
17
+ else : # check if dbus is available
18
+ import os
19
+ _dbus_address = os .getenv ("DBUS_SESSION_BUS_ADDRESS" )
20
+ if _dbus_address :
21
+ logger .info ("Jeepney and Dbus is available. Using DBUS for notifications.." )
22
+ else :
23
+ raise ImportError
31
24
32
- This uses libnotify's tool of notfiy-send.
33
- I'll add support for (and probably use as first choice) sending
34
- through dbus.
25
+ APLAY = which ('aplay' )
35
26
36
- """
27
+ if APLAY == None :
28
+ logger .debug ("aplay binary not installed.. audio will not work!" )
37
29
38
- call_find_notify_send = self ._find_installed_notify_send ()
39
30
40
- if not call_find_notify_send :
41
- logger .error ("Unable to find notify-send." )
42
- raise BinaryNotFound ("notify-send" )
43
- if call_find_notify_send :
44
- self ._notify_send_binary = call_find_notify_send
31
+ except ImportError :
32
+ logger .error ("libnotify nor DBUS installed." )
45
33
46
- call_find_aplay = self ._find_installed_aplay ()
47
- if not call_find_aplay :
48
- # no Aplay is available.
49
- self ._aplay_binary = False
50
- else :
51
- self ._aplay_binary = call_find_aplay
52
34
53
- @staticmethod
54
- def _find_installed_aplay ():
55
- """Function to find the path for notify-send"""
56
- try :
57
- run_which_for_aplay = subprocess .check_output (["which" , "aplay" ])
58
- return run_which_for_aplay .decode ("utf-8" )
59
- except subprocess .CalledProcessError :
60
- logger .exception ("Unable to find aplay." )
61
- return False
62
- except Exception :
63
- logger .exception ("Unhandled exception for finding aplay." )
64
- return False
35
+ class LinuxNotifierLibNotify (BaseNotifier ):
36
+ def __init__ (self , ** kwargs ):
37
+ """Main Linux Notification Class
65
38
66
- @staticmethod
67
- def _find_installed_notify_send ():
68
- """Function to find the path for notify-send"""
69
- try :
70
- run_which_for_notify_send = subprocess .check_output (
71
- ["which" , "notify-send" ]
72
- )
73
- return run_which_for_notify_send .decode ("utf-8" )
74
- except subprocess .CalledProcessError :
75
- logger .exception ("Unable to find notify-send." )
76
- return False
77
- except Exception :
78
- logger .exception ("Unhandled exception for finding notify-send." )
79
- return False
39
+ This uses libnotify's tool of notfiy-send.
40
+ """
41
+ pass
80
42
81
43
def send_notification (
82
44
self ,
@@ -87,14 +49,13 @@ def send_notification(
87
49
** kwargs ,
88
50
):
89
51
try :
90
-
91
52
notification_title = " " if notification_title == "" else notification_title
92
53
notification_subtitle = (
93
54
" " if notification_subtitle == "" else notification_subtitle
94
55
)
95
56
96
57
generated_command = [
97
- self . _notify_send_binary . strip () ,
58
+ NOTIFY ,
98
59
notification_title ,
99
60
notification_subtitle ,
100
61
]
@@ -107,14 +68,17 @@ def send_notification(
107
68
f"--app-name={ shlex .quote (kwargs .get ('application_name' ))} "
108
69
)
109
70
71
+ if kwargs .get ('notification_urgency' ):
72
+ generated_command .extend (["-u" , kwargs .get ('notification_urgency' )])
73
+
110
74
logger .debug (f"Generated command: { generated_command } " )
111
75
if notification_audio :
112
76
113
- if self . _aplay_binary == False :
77
+ if APLAY == None :
114
78
raise BinaryNotFound ("aplay (Alsa)" )
115
79
116
80
subprocess .Popen (
117
- [self . _aplay_binary . strip () , notification_audio ],
81
+ [APLAY , notification_audio ],
118
82
stdout = subprocess .DEVNULL ,
119
83
stderr = subprocess .STDOUT ,
120
84
)
@@ -143,31 +107,6 @@ def __init__(self, **kwargs):
143
107
interface = "org.freedesktop.Notifications" ,
144
108
)
145
109
146
- call_find_aplay = self ._find_installed_aplay ()
147
- if not call_find_aplay :
148
- # no Aplay is available.
149
- self ._aplay_binary = False
150
- logger .debug ("aplay binary not installed.. audio will not work!" )
151
- else :
152
- self ._aplay_binary = call_find_aplay
153
-
154
- if kwargs .get ("linux_fallback_libnotify" ):
155
- self ._fallback_to_libnotify = True
156
- else :
157
- self ._fallback_to_libnotify = False
158
-
159
- @staticmethod
160
- def _find_installed_aplay ():
161
- """Function to find the path for notify-send"""
162
- try :
163
- run_which_for_aplay = subprocess .check_output (["which" , "aplay" ])
164
- return run_which_for_aplay .decode ("utf-8" )
165
- except subprocess .CalledProcessError :
166
- logger .exception ("Unable to find aplay." )
167
- return False
168
- except Exception :
169
- logger .exception ("Unhandled exception for finding aplay." )
170
- return False
171
110
172
111
def send_notification (
173
112
self ,
@@ -182,35 +121,23 @@ def send_notification(
182
121
logger .debug ("linux: opened dbus connection" )
183
122
except Exception :
184
123
logger .exception ("issue with opening DBUS connection!" )
185
- if self ._fallback_to_libnotify == True :
186
- logger .debug ("falling back to libnotify!" )
187
- return LinuxNotifierLibNotify ().send_notification (
188
- notification_title ,
189
- notification_subtitle ,
190
- notification_icon ,
191
- notification_audio ,
192
- ** kwargs ,
193
- )
194
- else :
195
- logger .exception (
196
- "there was an exception trying to open the dbus connection. fallback was not enabled, therefore this will return False."
197
- )
198
- return False
124
+ return False
199
125
200
126
try :
201
127
notification_title = " " if notification_title == "" else notification_title
202
128
notification_subtitle = (
203
129
" " if notification_subtitle == "" else notification_subtitle
204
130
)
131
+
205
132
if notification_audio :
206
133
# TODO: https://specifications.freedesktop.org/notification-spec/latest/ar01s09.html
207
134
# use sound param instead of relying on alsa?
208
135
209
- if self . _aplay_binary == False :
136
+ if APLAY == None :
210
137
raise BinaryNotFound ("aplay (Alsa)" )
211
138
212
139
subprocess .Popen (
213
- [self . _aplay_binary . strip () , notification_audio ],
140
+ [APLAY , notification_audio ],
214
141
stdout = subprocess .DEVNULL ,
215
142
stderr = subprocess .STDOUT ,
216
143
)
@@ -238,13 +165,4 @@ def send_notification(
238
165
239
166
except Exception :
240
167
logger .exception ("issue with sending through dbus!" )
241
- if self ._fallback_to_libnotify == True :
242
- logger .debug ("falling back to libnotify!" )
243
- return LinuxNotifierLibNotify ().send_notification (
244
- notification_title ,
245
- notification_subtitle ,
246
- notification_icon ,
247
- notification_audio ,
248
- ** kwargs ,
249
- )
250
168
return False
0 commit comments