Пример #1
0
class SXDBus(Plugin):
    key = 'dbus'

    def __init__(self, app):
        self.objects = {}
        if YAYDBUS:
            self.session_bus = SessionBus()
            app.add_fd_handler('read', self.session_bus, self.process_dbus)
        else:
            dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

            self.session_bus = dbus.SessionBus()
            self.name = service.BusName(DEFAULT_BUS_NAME, self.session_bus)

        if YAYDBUS:
            self.session_bus.request_name(DEFAULT_BUS_NAME)
            self.session_bus.request_name(DEFAULT_BUS_NAME+'.DBusService')
        self.register('dbus', functools.partial(DBusObject, app))

    def process_dbus(self):
        self.session_bus.receive_one()

    def register(self, name, cls, path=None):
        """ register a new dbus object """
        log.info('registering %s: %s', name, cls)
        
        if path is None:
            path = "/%s" % name

        if YAYDBUS:
            self.objects[name] = self.session_bus.add_object(
                    cls(self.session_bus, path),
            )
        else:
            self.objects[name] = cls(
                    conn=self.session_bus, 
                    object_path=path,
                    bus_name=self.name,
            )
Пример #2
0
        print

    @method('com.example.SimpleService')
    @annotate(i=UInt32, return_=UInt32)
    def ExpensiveCalculation(self, i):
        while i < 1000:
            i += 1
        return i

    @method('com.example.SimpleService')
    @annotate(something=Variant)
    def PrintSomething(self, something):
        print 'Something:', something

    @method('com.example.SimpleService', in_signature='v', out_signature='s')
    def Repr(self, value):
        return repr(value)

    @method('com.example.SimpleService')
    @annotate(dic=Dictionary(str, str), key=str, return_=str)
    def GetItem(self, dic, key):
        return dic[key]

bus = SessionBus()
bus.request_name('com.example.SimpleService')
obj = bus.add_object(TestObject(bus))

mainloop = Mainloop([bus])
mainloop.run()