I have an little script which logs users that login to my Pidgin/MSN account
#!/usr/bin/env python
def log_names(buddy):
name = str(purple.PurpleBuddyGetName(buddy))
account = purple.PurpleAccountGetUsername(purple.PurpleBuddyGetAccount(buddy))
if account == u'dummy_account@hotmail.com':
try: log[name] += 1
except KeyError: log[name] = 1
log.sync()
import dbus, gobject, shelve
from dbus.mainloop.glib import DBusGMainLoop
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
log = shelve.open('pidgin.log')
obj = bus.get_object('im.pidgin.purple.PurpleService',
'/im/pidgin/purple/PurpleObject')
purple = dbus.Interface(obj, 'im.pidgin.purple.PurpleInterface')
bus.add_signal_receiver(log_names,
dbus_interface='im.pidgin.purple.PurpleInterface',
signal_name='BuddySignedOn')
loop = gobject.MainLoop()
loop.run()
I wanna add a simple interactive console to this which allows me to query the data from the log object, but I’m stuck at how I would implement it
Do I use threads of some kind or am I able to use some sort of call back within gobject.MainLoop()?
You should look in the direction of general GObject/GLib programming (this is where
gobject.MainLoop()is coming from). You could use threads, you could use event callbacks, whatever. For example, this is a simple ‘console’ using event callbacks. Add this just before theloop.run():If you are building a PyGTK application, you don’t have to call the mainloop specially for the dbus, because it will use the main application’s mainloop. There also other mainloops for other libraries available, for example dbus.mainloop.qt for PyQt4.