-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
56 lines (44 loc) · 1.56 KB
/
utils.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from builtins import str
from future import standard_library
import logger
standard_library.install_aliases()
import platform
import subprocess
log = logger.getLogger(__name__)
def utf8_data_to_file(f, data):
if hasattr(data, 'decode'):
f.write(data.decode('utf-8'))
else:
f.write(data)
def notify_command_osx(msg, msg_type, t=None):
command = ['/usr/bin/osascript', '-e']
tpl = "display notification \"{}\" {} with title \"musicbox\""
sound = 'sound name \"/System/Library/Sounds/Ping.aiff\"' if msg_type else ''
command.append(tpl.format(msg, sound).encode('UTF-8'))
return command
def notify_command_linux(msg, t=None):
command = ['/usr/bin/notify-send']
command.append(msg.encode('UTF-8'))
if t:
command.extend(['-t', str(t)])
command.extend(['-h', 'int:transient:1'])
return command
def notify(msg, msg_type=0, t=None):
"Show system notification with duration t (ms)"
if platform.system() == 'Darwin':
command = notify_command_osx(msg, msg_type, t)
else:
command = notify_command_linux(msg, t)
try:
subprocess.call(command)
except OSError as e:
log.warning('Sending notification error.')
if __name__ == "__main__":
notify("I'm test 1", msg_type=1, t=1000)
notify("I'm test 2", msg_type=0, t=1000)