4 Subscribes to zephyr via tzc and sends messages to notification drivers (growl or libnotify).
13 from abstfilter import AbstractConsumer
16 class Notifier(AbstractConsumer):
17 def __init__(self, usegrowl, usenotify, useprint):
18 self.usegrowl = usegrowl
19 self.usenotify = usenotify
22 pynotify.init("Zephyr")
24 self.useprint = useprint
27 if s is None or type(s) is type(''): return
28 d = dict([(ss[0], len(ss) > 2 and ss[2] or None) for ss in s])
29 if d['tzcspew'] == 'message':
30 zclass = d['class'].lower()
31 zinstance = d['instance'].lower()
32 zop = d['opcode'].lower()
33 zsender = d['sender'].lower()
34 zauth = d['auth'].lower() == 'yes'
35 ztime = ':'.join(d['time'].split(' ')[3].split(':')[0:2])
36 zmessage = d['message']
37 id = '%s/\n%s/\n%s\n %s' % (zclass, zinstance, zsender, ztime)
39 header = '%s (%s)' % (id, zsender)
42 header = '%s (%s)' % (id, len(zmessage) > 0 and zmessage[0] or zsender)
43 message = '%s' % (len(zmessage) > 1 and zmessage[1] or '')
50 growlnotify = ['growlnotify', '-a', 'MacZephyr', '-n', 'zephyr', '-d', id, '-t', header]
51 g = subprocess.Popen(growlnotify, stdin=subprocess.PIPE)
52 g.stdin.write(message)
57 self.pings[id].close()
58 self.pings[id] = pynotify.Notification(header, message)
64 parser = optparse.OptionParser(usage = '%prog [-s "username@machine"] (--growl | --notify | --print)',
65 description = __doc__.strip())
66 parser.add_option('-s', '--ssh',
70 help = 'optional remote host to run tzc')
71 parser.add_option('-g', '--growl',
72 action = 'store_true',
75 help = 'use growlnotify for output')
76 parser.add_option('-n', '--notify',
77 action = 'store_true',
80 help = 'use notify-send for output')
81 parser.add_option('-p', '--print',
82 action = 'store_true',
85 help = 'use stdout for output')
86 opts, args = parser.parse_args()
89 usenotify = opts.notify
90 useprint = opts.useprint
91 if not usegrowl and not usenotify and not useprint:
92 parser.print_help(sys.stderr)
97 retval = subprocess.call(['which', 'tzc'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
99 print 'tzc not in path. Please add -s username@machine to specify remote host.'
103 command = "ssh -K %s 'tzc -si'" % ssh
106 p = os.popen(command)
107 r = sexpr.SExprReader(Notifier(usegrowl, usenotify, useprint))
109 flags = fcntl.fcntl(p, fcntl.F_GETFL)
110 fcntl.fcntl(p, fcntl.F_SETFL, flags | os.O_NONBLOCK)
114 [i,o,e] = select.select([p], [], [], 5)
115 if i: s = p.read(1024)
120 except KeyboardInterrupt:
124 if __name__ == "__main__":
125 sys.exit(main(sys.argv))