add desktop notification

This commit is contained in:
lilydjwg 2013-05-26 17:20:15 +08:00
parent ae66b40369
commit 53e87b9ebe
2 changed files with 99 additions and 0 deletions

92
lib/notify.py Normal file
View File

@ -0,0 +1,92 @@
'''
调用 libnotify
'''
__all__ = ["set", "show", "update", "set_timeout", "set_urgency"]
from ctypes import *
from threading import Lock
import atexit
NOTIFY_URGENCY_LOW = 0
NOTIFY_URGENCY_NORMAL = 1
NOTIFY_URGENCY_CRITICAL = 2
UrgencyLevel = {NOTIFY_URGENCY_LOW, NOTIFY_URGENCY_NORMAL, NOTIFY_URGENCY_CRITICAL}
libnotify = CDLL('libnotify.so')
gobj = CDLL('libgobject-2.0.so')
libnotify_lock = Lock()
libnotify_inited = False
class obj: pass
notify_st = obj()
def set(summary=None, body=None, icon_str=None):
with libnotify_lock:
init()
if summary is not None:
notify_st.summary = summary.encode()
notify_st.body = notify_st.icon_str = None
if body is not None:
notify_st.body = body.encode()
if icon_str is not None:
notify_st.icon_str = icon_str.encode()
libnotify.notify_notification_update(
notify_st.notify,
c_char_p(notify_st.summary),
c_char_p(notify_st.body),
c_char_p(notify_st.icon_str),
c_void_p()
)
def show():
libnotify.notify_notification_show(notify_st.notify, c_void_p())
def update(summary=None, body=None, icon_str=None):
if not any((summary, body)):
raise TypeError('at least one argument please')
set(summary, body, icon_str)
show()
def set_timeout(self, timeout):
'''set `timeout' in milliseconds'''
libnotify.notify_notification_set_timeout(notify_st.notify, int(timeout))
def set_urgency(self, urgency):
if urgency not in UrgencyLevel:
raise ValueError
libnotify.notify_notification_set_urgency(notify_st.notify, urgency)
def init():
global libnotify_inited
if libnotify_inited:
return
libnotify.notify_init('pynotify')
libnotify_inited = True
notify_st.notify = libnotify.notify_notification_new(
c_void_p(), c_void_p(), c_void_p(),
)
atexit.register(uninit)
def uninit():
global libnotify_inited
try:
if libnotify_inited:
gobj.g_object_unref(notify_st.notify)
libnotify.notify_uninit()
libnotify_inited = False
except AttributeError:
# libnotify.so 已被卸载
pass
if __name__ == '__main__':
from time import sleep
notify = __import__('__main__')
notify.set('This is a test', '测试一下。')
notify.show()
sleep(1)
notify.update(body='再测试一下。')

View File

@ -1,18 +1,22 @@
#!/usr/bin/env python3
# vim:fileencoding=utf-8
import os
import sys
import configparser
import logging
from functools import partial
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'lib'))
from tornado.ioloop import IOLoop
import tornado.options
from get_version import get_version
import notify
logger = logging.getLogger(__name__)
g_counter = 0
notifications = []
def task_inc():
global g_counter
@ -36,6 +40,9 @@ def load_config(*files):
def print_version(name, version):
print('%s: %s' % (name, version))
msg = '%s lastest version: %s' % (name, version)
notifications.append(msg)
notify.update('nvchecker', '\n'.join(notifications))
task_dec()
def get_versions(config):