]> sipb.mit.edu Git - snippets/.git/commitdiff
Rewrote to use optparser and added drivers for stdout, libnotify usage.
authorPeter Iannucci <iannucci@mit.edu>
Sun, 1 Mar 2009 01:40:32 +0000 (20:40 -0500)
committerPeter Iannucci <iannucci@mit.edu>
Sun, 1 Mar 2009 01:55:16 +0000 (20:55 -0500)
Bugfixes.

barn-growl/abstfilter.py
barn-growl/barn-growl.py
barn-growl/sexpr.py

index c8a6e119a5f0b35a9565bdfce7a181f63a378f1e..34755d5daa652653a58f627f07fb98ef5a983abd 100755 (executable)
@@ -3,6 +3,8 @@
 ##
 ##  abstfilter.py - A framework for cascade filters.
 ##
+##  from http://www.unixuser.org/~euske/python/index.html:
+##  The following files are in public domain except where otherwise noted. THESE FILES COME WITH ABSOLUTELY NO WARRANTY.
 
 
 ##  AbstractFeeder
index 6930f36990bfd06ed2ec242123e4bbfc8e695c6f..8a46cb917fdbfd0f02f6469ade2e5d2188dea3c6 100755 (executable)
@@ -1,18 +1,26 @@
 #!/usr/bin/env python
 
+"""
+Subscribes to zephyr via tzc and sends messages to notification drivers (growl or libnotify).
+"""
+
 import sexpr
 import os
+import subprocess
 import fcntl
 import select
 import sys
 from abstfilter import AbstractConsumer
+import optparse
 
-class Growler(AbstractConsumer):
-    def __init__(self):
+class Notifier(AbstractConsumer):
+    def __init__(self, usegrowl, usenotify, useprint):
+        self.usegrowl = usegrowl
+        self.usenotify = usenotify
+        self.useprint = useprint
         return
     def feed(self, s):
         if s is None or type(s) is type(''): return
-        print repr(s)
         d = dict([(ss[0], len(ss) > 2 and ss[2] or None) for ss in s])
         if d['tzcspew'] == 'message':
             zclass = d['class'].lower()
@@ -27,31 +35,63 @@ class Growler(AbstractConsumer):
                 header = '%s (%s)' % (id, zsender)
                 message = '...'
             elif zop == 'nil':
-                header = '%s (%s)' % (id, zmessage[0])
-                message = '%s' % zmessage[1]
+                header = '%s (%s)' % (id, len(zmessage) > 0 and zmessage[0] or zsender)
+                message = '%s' % (len(zmessage) > 1 and zmessage[1] or '')
             else:
                 return
-            g = os.popen("growlnotify -a MacZephyr -n zephyr -d '%s' -t '%s'" % (id, header), 'w')
-            g.write(message)
-            g.close()
+            if self.useprint:
+                print (id, header)
+                print message
+            if self.usegrowl:
+                growlnotify = ['growlnotify', '-a', 'MacZephyr', '-n', 'zephyr', '-d', id, '-t', header]
+                g = subprocess.Popen(growlnotify, stdin=subprocess.PIPE)
+                g.stdin.write(message)
+                g.stdin.close()
+            if self.usenotify:
+                notifysend = ['notify-send', header, message]
+                subprocess.call(notifysend)
     def close(self):
         return
 
 def main(argv):
-    if len(argv) < 2:
-        print """barn-growl v.0.0.1
+    parser = optparse.OptionParser(usage = '%prog [-s "username@machine"] (--growl | --notify | --print)',
+            description = __doc__.strip())
+    parser.add_option('-s', '--ssh',
+            type = 'string',
+            default = None,
+            dest = 'ssh',
+            help = 'optional remote host to run tzc')
+    parser.add_option('-g', '--growl',
+            action = 'store_true',
+            default = False,
+            dest = 'growl',
+            help = 'use growlnotify for output')
+    parser.add_option('-n', '--notify',
+            action = 'store_true',
+            default = False,
+            dest = 'notify',
+            help = 'use notify-send for output')
+    parser.add_option('-p', '--print',
+            action = 'store_true',
+            default = False,
+            dest = 'useprint',
+            help = 'use stdout for output')
+    opts, args = parser.parse_args()
 
-Usage:
-barn-growl USERNAME"""
-        return 0
+    usegrowl = opts.growl
+    usenotify = opts.notify
+    useprint = opts.useprint
+    if not usegrowl and not usenotify and not useprint:
+        parser.print_help(sys.stderr)
+        return 1
+    ssh = opts.ssh
 
-    username = argv[1]
-    principal = username
-    if principal.find("@") == -1:
-        principal += '@ATHENA.MIT.EDU'
-    bash = "/bin/bash -lc \"kdo %s ssh %s@linerva.mit.edu 'tzc -si'\" 2>/dev/null </dev/null" % (principal, username)
-    p = os.popen(bash)
-    r = sexpr.SExprReader(Growler())
+    if ssh is not None:
+        command = "ssh -K %s 'tzc -si'" % ssh
+    else:
+        command = "tzc -si"
+    p = os.popen(command)
+    r = sexpr.SExprReader(Notifier(usegrowl, usenotify, useprint))
 
     flags = fcntl.fcntl(p, fcntl.F_GETFL)
     fcntl.fcntl(p, fcntl.F_SETFL, flags | os.O_NONBLOCK)
index 0d09ef657842c806211c7f1940b1f6f35a0f2bc3..a00f9c59c5804d1e37812d6fd3a8394ca7595805 100644 (file)
@@ -4,6 +4,8 @@
 ##
 ##  * public domain *
 ##
+##  from http://www.unixuser.org/~euske/python/index.html:
+##  The following files are in public domain except where otherwise noted. THESE FILES COME WITH ABSOLUTELY NO WARRANTY.
 
 from abstfilter import AbstractFeeder, AbstractFilter, AbstractConsumer