-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.py
56 lines (43 loc) · 1.66 KB
/
notify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# -*- coding: utf-8 -*-
"""
@author: Sam Schott (ss2151@cam.ac.uk)
(c) Sam Schott; This work is licensed under a Creative Commons
Attribution-NonCommercial-NoDerivs 2.0 UK: England & Wales License.
"""
import os
from enum import Enum
class SupportedImplementation(Enum):
notify_send = 'notify-send'
osascript = 'osascript'
class Notipy(object):
"""Send native OS notifications to user.
Relies on AppleScript on macOS and notify-send on linux, otherwise
falls back to stdout."""
enabled = True
def __init__(self):
self.implementation = self.__get_available_implementation()
def send(self, message, title="CustomXepr"):
if self.enabled:
self.__send_message(message, title)
else:
pass
def __send_message(self, message, title=""):
if self.implementation == SupportedImplementation.osascript:
os.system("osascript -e 'display notification \"{}\" with title \"{}\"'".format(
message, title))
elif self.implementation == SupportedImplementation.notify_send:
os.system('notify-send "{}" "{}"'.format(title, message))
else:
print('{}: {}'.format(title, message))
@staticmethod
def __command_exists(command):
return any(
os.access(os.path.join(path, command), os.X_OK)
for path in os.environ["PATH"].split(os.pathsep)
)
def __get_available_implementation(self):
if self.__command_exists('osascript'):
return SupportedImplementation.osascript
elif self.__command_exists('notify-send'):
return SupportedImplementation.notify_send
return None