Skip to content

Commit

Permalink
[fix]捕获通知命令无法找到的异常
Browse files Browse the repository at this point in the history
捕获通知命令无法找到的异常,避免 RaspberryPI 一类的系统无法找到通知
命令,报错影响程序界面。
同时为方便异常捕获,改为使用 subprocess 调用命令。
  • Loading branch information
vincent committed Mar 14, 2017
1 parent 4048068 commit 4b35e49
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions NEMbox/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
from __future__ import absolute_import
from builtins import str
from future import standard_library
from . import logger
standard_library.install_aliases()
import platform
import os
import subprocess

log = logger.getLogger(__name__)

def utf8_data_to_file(f, data):
if hasattr(data, 'decode'):
Expand All @@ -22,16 +24,19 @@ def utf8_data_to_file(f, data):


def notify_command_osx(msg, msg_type, t=None):
command = '/usr/bin/osascript -e "display notification \\\"{}\\\" {} with title \\\"musicbox\\\""'
sound = 'sound name \\\"/System/Library/Sounds/Ping.aiff\\\"' if msg_type else ''
return command.format(msg, sound)
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 "' + msg + '"'
command = ['/usr/bin/notify-send']
command.append(msg.encode('UTF-8'))
if t:
command += ' -t ' + str(t)
command += ' -h int:transient:1'
command.extend(['-t', str(t)])
command.extend(['-h', 'int:transient:1'])
return command


Expand All @@ -41,8 +46,10 @@ def notify(msg, msg_type=0, t=None):
command = notify_command_osx(msg, msg_type, t)
else:
command = notify_command_linux(msg, t)
os.system(command.encode('utf-8'))

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)
Expand Down

0 comments on commit 4b35e49

Please sign in to comment.