#!/usr/bin/python import getopt, sys from os.path import basename from pynotify import * def send_notification(config): init(config['appName']) if len(config['message']) < 1: config['message'] = sys.stdin.read() n = Notification(config['title'], config['message'], config['imagePath']) if config['priority'] == 0: n.set_urgency(URGENCY_LOW) elif config['priority'] == 2: n.set_urgency(URGENCY_CRITICAL) if config['sticky']: n.set_timeout(EXPIRES_NEVER) n.show() def usage(): print "Usage: %s [-h] [-s] [-n name] [-i imagepath] [-p priority] [-t title] [-m message]" % program print "Options:" print "\t-h\tDisplay this help" print "\t-s\tMake the notification sticky" print "\t-n\tSet the name of the application that sends the notification" print "\t\t[Default: %s]" % program print "\t-i\tSpecify the icon to use on the notification" print "\t\tEither a URI specifying the icon file name" print "\t\tor a 'stock' icon name (e.g. 'dialog-warning')" print "\t-p\tSpecify the priority (0, 1, 2) [Default: 1]" print "\t-t\tSpecify the title of the notification [Default: %s]" % program print "\t-m\tSpecify the message to the following instead of using stdin" sys.exit(2) def main(): # Defaults config = { 'sticky': False, 'appName': program, 'imagePath': '', 'priority': 1, 'title': program, 'message': '' } try: opts, args = getopt.getopt(sys.argv[1:], "hn:si:p:t:m:") except getopt.GetoptError: usage() for o, a in opts: if o == '-h': usage() if o == '-s': config['sticky'] = True if o == '-n': config['appName'] = a if o == '-i': config['imagePath'] = a if o == '-p': config['priority'] = a if o == '-t': config['title'] = a if o == '-m': config['message'] = a send_notification(config) if __name__ == '__main__': program = basename(sys.argv[0]) try: main() except KeyboardInterrupt: pass