0

I was kindly directed to use GObject's "add_emission_hook" following a recent question on SO but I can't seem to find a usage example.

Does anyone have one to share, please?

Community
  • 1
  • 1
jldupont
  • 93,734
  • 56
  • 203
  • 318
  • I could only find a few examples with google's codesearch, http://google.com/codesearch?hl=en&lr=&q="add_emission_hook"+lang:python&sbtn=Search , or koders, http://www.koders.com/default.aspx?s="add_emission_hook"&submit=Search&la=Python&li=* , but maybe they can help you? – Alex Martelli Jan 18 '10 at 19:24
  • As far as I can tell, you use it the same way as «object».connect, except that you pass a class as the first parameter (the PyGTK version accepts an object as well; I think it automatically takes the class for you). So, for the particular example you mentioned, it would be gobject.add_emission_hook(Car, 'engine-started', myCallback). I've never actually used it though, so I might be wrong. – Johannes Sasongko Jan 18 '10 at 23:10

1 Answers1

0

After a discussion with helpful folks on IRC #pygtk, here is what I came up with:

import gobject

class Signals(gobject.GObject):

    __gsignals__ = {
        "lastfm_username_changed": (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, (gobject.TYPE_STRING,)) #@UndefinedVariable
    }

    def __init__(self):
        gobject.GObject.__init__(self)


class Bus(object):
     """
     Message Bus - Borg pattern
     """
    _signals=Signals()

    @classmethod
    def emit(cls, name, *pa, **kwa):
        cls._signals.emit(name, *pa, **kwa)

    @classmethod
    def add_emission_hook(cls, name, callback):
        gobject.add_emission_hook(cls._signals, name, callback)


if __name__=="__main__":

    def callback(self, data):
        print "callback: data=%s" % data

    Bus.add_emission_hook("lastfm_username_changed", callback)

    Bus.emit("lastfm_username_changed", "jldupont")
jldupont
  • 93,734
  • 56
  • 203
  • 318