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 😉


# 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
   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):
         if prev and self.nc != None: prev.disconnect(self.nc)
         win = scr.get_active_window()
         if win:
            self.nc = win.connect("name_changed", self.name_handler)
      except KeyboardInterrupt: raise
   def name_handler(self, win):
      except KeyboardInterrupt: raise

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)

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!



  1. Hi,

    I am trying to use your hack of Hamster which will be quite usefull for tracking my progress on my thesis. However, I get the following error when trying to run it. Do you have any idea what is going wrong?

    Traceback (most recent call last):
    File “cree.py”, line 26, in handler
    File “cree.py”, line 18, in add_my_fact
    hamster.client.Storage.add_fact(self, fact)
    File “/usr/lib/python2.7/dist-packages/hamster/client.py”, line 172, in add_fact
    if not fact.activity:
    AttributeError: ‘str’ object has no attribute ‘activity’

    Best Regards,

  2. looks like the hamster API has changed a bit. you’ll need to change the line:
    hamster.client.Storage.add_fact(self, fact)
    hamster.client.Storage.add_fact(self, Fact(fact))

    and then we need to tell python where to get the ‘Fact’ method from. there’s been a few changes to this so the exact line you need will be one of these, depending on how old your hamster is…

    for hamster versions newer than 24 dec 2012, use:
    from hamster.lib import Fact

    for hamster versions between 5 dec 2010 and 24 dec 2012, use:
    from hamster.lib.stuff import Fact

    for hamster versions between 21 aug 2010 and 5 dec 2010, use:
    from hamster.utils.stuff import Fact

    you’ll need to add the right one of those after the “import hamster.client” line.

    fyi – you can see how the hamster command-line app does it by looking here:

    and you can see the changes made to that app over the months here:

    good luck (and with the thesis!) – let me know how you get on.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s