]>
Commit | Line | Data |
---|---|---|
e9647132 PI |
1 | #!/usr/bin/env python |
2 | ||
9d9c1ae1 PI |
3 | """ |
4 | Subscribes to zephyr via tzc and sends messages to notification drivers (growl or libnotify). | |
5 | """ | |
6 | ||
e9647132 PI |
7 | import sexpr |
8 | import os | |
9d9c1ae1 | 9 | import subprocess |
e9647132 PI |
10 | import fcntl |
11 | import select | |
12 | import sys | |
13 | from abstfilter import AbstractConsumer | |
9d9c1ae1 | 14 | import optparse |
e9647132 | 15 | |
9d9c1ae1 PI |
16 | class Notifier(AbstractConsumer): |
17 | def __init__(self, usegrowl, usenotify, useprint): | |
18 | self.usegrowl = usegrowl | |
19 | self.usenotify = usenotify | |
2deb9d01 GT |
20 | if usenotify: |
21 | import pynotify | |
22 | pynotify.init("Zephyr") | |
23 | self.pings = {} | |
f9d64b9d | 24 | self.pynotify = pynotify |
9d9c1ae1 | 25 | self.useprint = useprint |
e9647132 PI |
26 | return |
27 | def feed(self, s): | |
28 | if s is None or type(s) is type(''): return | |
e9647132 PI |
29 | d = dict([(ss[0], len(ss) > 2 and ss[2] or None) for ss in s]) |
30 | if d['tzcspew'] == 'message': | |
31 | zclass = d['class'].lower() | |
32 | zinstance = d['instance'].lower() | |
33 | zop = d['opcode'].lower() | |
34 | zsender = d['sender'].lower() | |
35 | zauth = d['auth'].lower() == 'yes' | |
36 | ztime = ':'.join(d['time'].split(' ')[3].split(':')[0:2]) | |
37 | zmessage = d['message'] | |
de9408d7 GT |
38 | idtuple = (zclass, zinstance, zsender, ztime) |
39 | id = '%s/\n%s/\n%s\n %s' % idtuple | |
e9647132 PI |
40 | if zop == 'ping': |
41 | header = '%s (%s)' % (id, zsender) | |
42 | message = '...' | |
43 | elif zop == 'nil': | |
9d9c1ae1 PI |
44 | header = '%s (%s)' % (id, len(zmessage) > 0 and zmessage[0] or zsender) |
45 | message = '%s' % (len(zmessage) > 1 and zmessage[1] or '') | |
e9647132 PI |
46 | else: |
47 | return | |
9d9c1ae1 PI |
48 | if self.useprint: |
49 | print (id, header) | |
50 | print message | |
51 | if self.usegrowl: | |
52 | growlnotify = ['growlnotify', '-a', 'MacZephyr', '-n', 'zephyr', '-d', id, '-t', header] | |
53 | g = subprocess.Popen(growlnotify, stdin=subprocess.PIPE) | |
54 | g.stdin.write(message) | |
55 | g.stdin.close() | |
56 | if self.usenotify: | |
de9408d7 GT |
57 | if idtuple in self.pings: |
58 | self.pings[idtuple].update(header, message) | |
59 | self.pings[idtuple].show() | |
60 | else: | |
61 | n = self.pynotify.Notification(header, message) | |
62 | n.show() | |
63 | if zop == 'ping': | |
64 | self.pings[idtuple] = n | |
65 | self.pings = dict(filter(lambda ((c, i, s, time), v): time == idtuple[3], self.pings.items())) | |
e9647132 PI |
66 | def close(self): |
67 | return | |
68 | ||
69 | def main(argv): | |
9d9c1ae1 PI |
70 | parser = optparse.OptionParser(usage = '%prog [-s "username@machine"] (--growl | --notify | --print)', |
71 | description = __doc__.strip()) | |
72 | parser.add_option('-s', '--ssh', | |
73 | type = 'string', | |
74 | default = None, | |
75 | dest = 'ssh', | |
76 | help = 'optional remote host to run tzc') | |
77 | parser.add_option('-g', '--growl', | |
78 | action = 'store_true', | |
79 | default = False, | |
80 | dest = 'growl', | |
81 | help = 'use growlnotify for output') | |
82 | parser.add_option('-n', '--notify', | |
83 | action = 'store_true', | |
84 | default = False, | |
85 | dest = 'notify', | |
86 | help = 'use notify-send for output') | |
87 | parser.add_option('-p', '--print', | |
88 | action = 'store_true', | |
89 | default = False, | |
90 | dest = 'useprint', | |
91 | help = 'use stdout for output') | |
92 | opts, args = parser.parse_args() | |
e9647132 | 93 | |
9d9c1ae1 PI |
94 | usegrowl = opts.growl |
95 | usenotify = opts.notify | |
96 | useprint = opts.useprint | |
97 | if not usegrowl and not usenotify and not useprint: | |
98 | parser.print_help(sys.stderr) | |
99 | return 1 | |
100 | ssh = opts.ssh | |
e9647132 | 101 | |
c0557112 PI |
102 | if ssh is None: |
103 | retval = subprocess.call(['which', 'tzc'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | |
104 | if retval: | |
105 | print 'tzc not in path. Please add -s username@machine to specify remote host.' | |
106 | return 1 | |
107 | ||
9d9c1ae1 PI |
108 | if ssh is not None: |
109 | command = "ssh -K %s 'tzc -si'" % ssh | |
110 | else: | |
111 | command = "tzc -si" | |
112 | p = os.popen(command) | |
113 | r = sexpr.SExprReader(Notifier(usegrowl, usenotify, useprint)) | |
e9647132 PI |
114 | |
115 | flags = fcntl.fcntl(p, fcntl.F_GETFL) | |
116 | fcntl.fcntl(p, fcntl.F_SETFL, flags | os.O_NONBLOCK) | |
117 | ||
c0557112 PI |
118 | try: |
119 | while 1: | |
120 | [i,o,e] = select.select([p], [], [], 5) | |
121 | if i: s = p.read(1024) | |
122 | else: s = '' | |
e9647132 | 123 | |
c0557112 PI |
124 | if s != '': |
125 | r.feed(s) | |
126 | except KeyboardInterrupt: | |
127 | pass | |
e9647132 PI |
128 | return 0 |
129 | ||
130 | if __name__ == "__main__": | |
131 | sys.exit(main(sys.argv)) |