]> sipb.mit.edu Git - snippets/.git/blobdiff - barn-growl/barn-growl.py
use time.strptime instead of parsing time ourselves
[snippets/.git] / barn-growl / barn-growl.py
index 546cd694863cc1a776a9de49de8781ee2518ddc7..5a2bf4a22e818ac9a1f5db690775da0db7bffaf4 100755 (executable)
@@ -12,6 +12,7 @@ import select
 import sys
 from abstfilter import AbstractConsumer
 import optparse
+import time
 
 class Notifier(AbstractConsumer):
     def __init__(self, usegrowl, usenotify, useprint):
@@ -21,6 +22,7 @@ class Notifier(AbstractConsumer):
             import pynotify
             pynotify.init("Zephyr")
             self.pings = {}
+            self.pynotify = pynotify
         self.useprint = useprint
         return
     def feed(self, s):
@@ -32,9 +34,10 @@ class Notifier(AbstractConsumer):
             zop = d['opcode'].lower()
             zsender = d['sender'].lower()
             zauth = d['auth'].lower() == 'yes'
-            ztime = ':'.join(d['time'].split(' ')[3].split(':')[0:2])
+            ztime = "%02d:%02d" % time.strptime(d['time'])[3:5]
             zmessage = d['message']
-            id = '%s/\n%s/\n%s\n %s' % (zclass, zinstance, zsender, ztime)
+            idtuple = (zclass, zinstance, zsender, ztime)
+            id = '%s/\n%s/\n%s\n %s' % idtuple
             if zop == 'ping':
                 header = '%s (%s)' % (id, zsender)
                 message = '...'
@@ -52,11 +55,15 @@ class Notifier(AbstractConsumer):
                 g.stdin.write(message)
                 g.stdin.close()
             if self.usenotify:
-                import pynotify
-                if id in self.pings:
-                    self.pings[id].close()
-                self.pings[id] = pynotify.Notification(header, message)
-                self.pings[id].show()
+                if idtuple in self.pings:
+                    self.pings[idtuple].update(header, message)
+                    self.pings[idtuple].show()
+                else:
+                    n = self.pynotify.Notification(header, message)
+                    n.show()
+                    if zop == 'ping':
+                        self.pings[idtuple] = n
+                self.pings = dict(filter(lambda ((c, i, s, time), v): time == idtuple[3], self.pings.items()))
     def close(self):
         return