dbus/test/python/test-service.py
Robert McQueen ac36149533 2005-11-04 Robert McQueen <robot101@debian.org>
* python/dbus_bindings.pyx, test/python/test-client.py: Fix
        marshalling of boolean values. Add some booleans to the values in
        the test client.

        * python/decorators.py, python/service.py: Add an 'async_callbacks'
        argument to the dbus.service.method decorator, which allows you to
        name arguments to take two callback functions for replying with
        return values or an exception.

        * test/python/test-client.py, test/python/test-service.py: Add test
        case using asynchronous method reply functions, both return values and
        errors, and from within both the function itself and from a mainloop
        callback.

        * python/decorators.py, python/service.py: Perform checking that the
        number of method/signal arguments matches the number of types in the
        signature at class loading time, not when you first introspect the
        class.

        * python/service.py: Remove debug print left by the last commit.
2005-11-04 12:18:00 +00:00

103 lines
3.3 KiB
Python
Executable file

#!/usr/bin/env python
import sys
import os
builddir = os.environ["DBUS_TOP_BUILDDIR"]
pydir = builddir + "/python"
sys.path.insert(0, pydir)
sys.path.insert(0, pydir + '/.libs')
import dbus
if not dbus.__file__.startswith(pydir):
raise Exception("DBus modules are not being picked up from the package")
import dbus.service
import dbus.glib
import gobject
import random
class TestInterface(dbus.service.Interface):
@dbus.service.method("org.freedesktop.DBus.TestSuiteInterface", in_signature='', out_signature='b')
def CheckInheritance(self):
return False
class TestObject(dbus.service.Object, TestInterface):
def __init__(self, bus_name, object_path="/org/freedesktop/DBus/TestSuitePythonObject"):
dbus.service.Object.__init__(self, bus_name, object_path)
""" Echo whatever is sent
"""
@dbus.service.method("org.freedesktop.DBus.TestSuiteInterface")
def Echo(self, arg):
return arg
@dbus.service.method("org.freedesktop.DBus.TestSuiteInterface")
def GetComplexArray(self):
ret = []
for i in range(0,100):
ret.append((random.randint(0,100), random.randint(0,100), str(random.randint(0,100))))
return dbus.Array(ret, signature="(uus)")
def returnValue(self, test):
if test == 0:
return ""
elif test == 1:
return "",""
elif test == 2:
return "","",""
elif test == 3:
return []
elif test == 4:
return {}
elif test == 5:
return ["",""]
elif test == 6:
return ["","",""]
@dbus.service.method("org.freedesktop.DBus.TestSuiteInterface", in_signature='u', out_signature='s')
def ReturnOneString(self, test):
return self.returnValue(test)
@dbus.service.method("org.freedesktop.DBus.TestSuiteInterface", in_signature='u', out_signature='ss')
def ReturnTwoStrings(self, test):
return self.returnValue(test)
@dbus.service.method("org.freedesktop.DBus.TestSuiteInterface", in_signature='u', out_signature='(ss)')
def ReturnStruct(self, test):
return self.returnValue(test)
@dbus.service.method("org.freedesktop.DBus.TestSuiteInterface", in_signature='u', out_signature='as')
def ReturnArray(self, test):
return self.returnValue(test)
@dbus.service.method("org.freedesktop.DBus.TestSuiteInterface", in_signature='u', out_signature='a{ss}')
def ReturnDict(self, test):
return self.returnValue(test)
def CheckInheritance(self):
return True
@dbus.service.method('org.freedesktop.DBus.TestSuiteInterface', in_signature='bbv', out_signature='v', async_callbacks=('return_cb', 'error_cb'))
def AsynchronousMethod(self, async, fail, variant, return_cb, error_cb):
try:
if async:
gobject.timeout_add(500, self.AsynchronousMethod, False, fail, variant, return_cb, error_cb)
return
else:
if fail:
raise RuntimeError
else:
return_cb(variant)
return False # do not run again
except Exception, e:
error_cb(e)
session_bus = dbus.SessionBus()
name = dbus.service.BusName("org.freedesktop.DBus.TestSuitePythonService", bus=session_bus)
object = TestObject(name)
loop = gobject.MainLoop()
loop.run()