wnck FTW!

well, yesterday’s hack turned out to be pretty useless.  effectively it just converted hamster into zeitgeist journal (or gnome activity monitor)  – which is okay until you start using an app which isn’t a zeitgeist data provider or which doesn’t happen to emit a signal for the activity you’re performing.

so, i thought i’d have a go at converting hamster into creeper instead.  thanks to creeper for showing me how it’s done.  i’ve called this cree.py 😉

#!/usr/bin/python

# a python version of creeper (a vala app) to monitor active windows
# actually turns hamster into creeper

# because the mainloop appears to catch exceptions
from traceback import print_exc
import hamster.client
class hamster_handler(hamster.client.Storage):
   def __init__(self):
      self.nc = None
      hamster.client.Storage.__init__(self)
      
   def add_fact(self, fact):
      # FIXME insert clever rules here
      fact = fact.replace(",", ";") # don't accidentally create descriptions
      fact = fact.replace("@", "(a)") # don't accidentally create categories
      hamster.client.Storage.add_fact(self, fact)

   def handler(self, scr, prev = None):
      try:
         if prev and self.nc != None: prev.disconnect(self.nc)
         win = scr.get_active_window()
         if win:
            self.add_fact(win.get_name())
            self.nc = win.connect("name_changed", self.name_handler)
      except KeyboardInterrupt: raise
      except:
         print_exc()
        
   def name_handler(self, win):
      try:
         self.add_fact(win.get_name())
      except KeyboardInterrupt: raise
      except:
         print_exc()

hh = hamster_handler()
from gobject import MainLoop
ml = MainLoop()
from wnck import screen_get_default
sc = screen_get_default()
sc.connect("active_window_changed", hh.handler)
sc.connect("window_stacking_changed", hh.handler)
hh.handler(sc)
ml.run()

this is working pretty well for me so far .. on fedora 14.  just tried on 13 and the hamster python library is too old i think.  ah well – yet another reason to upgrade!

Advertisements