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

python, hamster and zeitgeist FTW!

update: note that this turned out not to be all that useful – tracking active windows into hamster turned out better.

in response to gnome bug 639018 and my general desire to track automatically what i’ve done, i’ve made a python script which connects to the zeitgeist activity monitor and copies its messages to the hamster time tracker.  it goes like this:

#!/usr/bin/python

# monitor zeitgeist and do stuff
from zeitgeist.client import ZeitgeistClient
from zeitgeist.datamodel import TimeRange, Event
from gobject import MainLoop

import hamster.client
class hamster_handler(hamster.client.Storage):
   def handler(self, tr, ev):
      # because the mainloop appears to catch exceptions
      from traceback import print_exc
      from urlparse import urlparse
      try:
         # FIXME insert clever rules here
         app = urlparse(ev[0].actor).netloc
         desk = open("/usr/share/applications/" + app)
         comments = filter(lambda x: x.startswith("Comment[en_GB]="), desk)
         comment = comments[0].split("=")[1].strip()
         self.add_fact(comment + " - " + ev[0].subjects[0].text)
      except:
         print_exc()

hh = hamster_handler()
ml = MainLoop()
ZeitgeistClient().install_monitor(
    TimeRange.from_now(),
    [Event()],
    hh.handler,
    hh.handler)
ml.run()

It never ends until it’s killed so you’ll probably want to run it in the background – i’ve added it to my session ‘startup applications’.  if it doesn’t appear to be working then run it from the command line instead – you should see some error messages if it’s failing to update hamster.

on my fedora 14 system i only get updates for local text files, images and videos opened in gedit, EoG and totem.  on ubuntu i imagine you’ll get a lot more updates.  OTOH, on ubuntu the script will probably need some tweaking for the hard-coded paths and locale.

under surveillance

i’ve decided not to link this to my hamster-to-empathy updater – i don’t really want to broadcast a stream of every little thing i do .. particularly if my IM accounts include twitter and facebook status feeds.  🙂

python + dbus FTW!!!

was just thinking it’d be nice to be able to push a button to copy my current activity from Hamster to my work XMPP status in Empathy.  couldn’t find any such options or plugins for either app.  but with dbus (and dfeet) and python, how hard can it be?  turns out, this hard:

#!/usr/bin/python

# get status from hamster, write it to empathy

myjid = "firstname.surname@example.com/" # my XMPP node and domain - any XMPP resource

from dbus import SessionBus

def busAsPath(bus):
  return "/" + bus.replace(".", "/")

def jidToDbus(jid):
  return jid.replace(".", "_2e").replace("@", "_40").replace("/", "_2f")

ogh = "org.gnome.Hamster"
mybus = "org.freedesktop.Telepathy.Connection.gabble.jabber." + jidToDbus(myjid)
sb = SessionBus()

fact = sb.get_object(ogh, busAsPath(ogh)).GetCurrentFact()['name']
for bus in sb.list_names():
  if bus.startswith(mybus):
    sb.get_object(bus, busAsPath(bus)).SetPresence("available", fact,
    dbus_interface = "org.freedesktop.Telepathy.Connection.Interface.SimplePresence")

stuck that in a file, made a custom launcher for it on the gnome panel and put that next to hamster.  lovely.

this only sets the status on that one account. if you’ve got multiple accounts which can have their presence set then empathy won’t change what it displays in its status selector until they all match. easily done by removing the account-specific code and applying the SetPresence to every bus which startswith(“org.freedesktop.Telepathy.Connection.”) – then you need a try/pass to ignore any exceptions generated by attempting to SetPresence on accounts which don’t have one. 🙂

another desktopish thought

along with all the great stuff that’s on the way in gnome-shell – with all its thinking about desktop use cases etc, i think we should add a ‘restart session’ option which does a ‘logout and log back in’ without asking for a password.

mind you, that’s only needed when certain pieces of software get updated.  under those circumstances it would be better if those apps sorted themselves out silently.  but until that’s the case the option could just be available on the PackageKit “you need to log out and in again” notification.

screenshot of gnome shell overlay modewhile i’m thinking about gnome-shell, i think when you drag something from the sidebar to a desktop in overlay mode it shouldn’t automatically take you out of overlay mode – it should assume you’ll want to get more than one thing.

or should we just be able to multi-select from the sidebar?  can you do that already?  i don’t like multi-select anyway – means you have to use both mouse and keyboard.

maybe we just need a mechanism for defining a group of things and making that group appear as an object in the sidebar – perhaps just a ‘save task/workspace’ option somewhere.