2003-09-25 Seth Nickell <seth@gnome.org>
authorSeth Nickell <seth@gnome.org>
Thu, 25 Sep 2003 08:46:39 +0000 (08:46 +0000)
committerSeth Nickell <seth@gnome.org>
Thu, 25 Sep 2003 08:46:39 +0000 (08:46 +0000)
* python/dbus.py:
* python/dbus_bindings.pyx.in:

Handle return values.

* python/examples/example-client.py:
* python/examples/example-service.py:

Pass back return values from the service to the client.

ChangeLog
python/dbus.py
python/dbus_bindings.pyx.in
python/examples/example-client.py
python/examples/example-service.py

index 17b4fd4..36abf23 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,15 @@
+2003-09-25  Seth Nickell  <seth@gnome.org>
+
+       * python/dbus.py:
+       * python/dbus_bindings.pyx.in:
+
+       Handle return values.
+       
+       * python/examples/example-client.py:
+       * python/examples/example-service.py:
+
+       Pass back return values from the service to the client.
+       
 2003-09-24  Seth Nickell  <seth@gnome.org>
 
        * python/dbus.py:
index 55e5944..d09b3fe 100644 (file)
@@ -105,25 +105,20 @@ class RemoteMethod:
         self._method_name  = method_name
 
     def __call__(self, *args):
-        print ("Going to call object(%s).interface(%s).method(%s)"
-               % (self._object_path, self._interface, self._method_name))
-
         message = dbus_bindings.MethodCall(self._object_path, self._interface, self._method_name)
         message.set_destination(self._service_name)
         
         # Add the arguments to the function
         iter = message.get_iter()
         for arg in args:
-            print ("Adding arg %s" % (arg))
-            print ("Append success is %d" % (iter.append(arg)))
-            print ("Args now %s" % (message.get_args_list()))            
+            iter.append(arg)
 
         reply_message = self._connection.send_with_reply_and_block(message, 5000)
 
         args_tuple = reply_message.get_args_list()
-        if (len(args_tuple) == 0):
+        if len(args_tuple) == 0:
             return
-        elif (len(args_tuple) == 1):
+        elif len(args_tuple) == 1:
             return args_tuple[0]
         else:
             return args_tuple
@@ -174,20 +169,30 @@ class Object:
         target_method = self._method_name_to_method[target_method_name]
         args = message.get_args_list()
         
-        retval = target_method(*args)
-
-        reply = dbus_bindings.MethodReturn(message)
-        if retval != None:
-            reply.append(retval)
+        try:
+            retval = target_method(*args)
+        except Exception, e:
+            if e.__module__ == '__main__':
+                error_name = e.__class__
+            else:
+                error_name = e.__module__ + '.' + str(e.__class__)
+            error_contents = str(e)
+            reply = dbus_bindings.Error(message, error_name, error_contents)
+        else:
+            reply = dbus_bindings.MethodReturn(message)
+            if retval != None:
+                iter = reply.get_iter()
+                iter.append(retval)
+                
         self._connection.send(reply)
 
     def _build_method_dictionary(self, methods):
-        dictionary = {}
+        method_dict = {}
         for method in methods:
-            if dictionionary.has_key(method.__name__):
-                print ('WARNING: registering DBus Object methods, already have a method named %s' % (method.__name__)
-            dictionary[method.__name__] = method
-        return dictionary
+            if method_dict.has_key(method.__name__):
+                print ('WARNING: registering DBus Object methods, already have a method named %s' % (method.__name__))
+            method_dict[method.__name__] = method
+        return method_dict
         
 class RemoteService:
     """A remote service providing objects.
index 085a73f..8feb867 100644 (file)
@@ -63,8 +63,8 @@ class ConnectionError(Exception):
 
 cdef void cunregister_function_handler (DBusConnection *connection,
                                         void *user_data):
-    print ("cunregister_function_handler() called")
     tup = <object>user_data
+    assert (type(tup) == list)    
     function = tup[0]
     args = [Connection(_conn=<object>connection)]
     function(*args)
@@ -72,10 +72,8 @@ cdef void cunregister_function_handler (DBusConnection *connection,
 cdef DBusHandlerResult cmessage_function_handler (DBusConnection *connection,
                                                   DBusMessage *msg,
                                                   void *user_data):
-    print ("cmessage_function_handler() called")
     tup = <object>user_data
-    print (type(tup))
-    print (tup)
+    assert (type(tup) == list)
     function = tup[1]
     message = Message(_create=0)
     message._set_msg(<object>msg)
@@ -90,6 +88,7 @@ cdef DBusHandlerResult chandle_message_function_handler (DBusConnection *connect
                                                         DBusMessage *msg,
                                                         void *user_data):
     function = <object>user_data
+    assert (type(function) == function)
     messagein = Message(_create=0)
     messagein._set_msg(<object>msg)
     args = [Connection(_conn=<object>connection),
@@ -232,16 +231,12 @@ cdef class Connection:
 
         msg = message._get_msg()
 
-        print ("About to block")
-
         retval = dbus_connection_send_with_reply_and_block(
             <DBusConnection*>self.conn,
             <DBusMessage*>msg,
             <int>timeout_milliseconds,
             &error)
 
-        print ("done")
-
         if dbus_error_is_set(&error):
             raise DBusException, error.message
 
@@ -346,7 +341,7 @@ cdef class Connection:
 
         i = 0
         child_entries = []
-        print ("cchild_entries[0] is %d" % (<int>cchild_entries[0]))
+
         while (cchild_entries[i] != NULL):
             child_entries.append(cchild_entries[i])
             i = i + 1
@@ -607,10 +602,8 @@ cdef class Message:
         if message_type == MESSAGE_TYPE_METHOD_CALL:
             self.msg = dbus_message_new_method_call(cservice, path, interface, method)
         elif message_type == MESSAGE_TYPE_METHOD_RETURN:
-            print ("Doing this")
             cmsg = method_call._get_msg()
             self.msg = dbus_message_new_method_return(<DBusMessage*>cmsg)
-            print ("Done")
         elif message_type == MESSAGE_TYPE_SIGNAL:
             self.msg = dbus_message_new_signal(path, interface, name)
         elif message_type == MESSAGE_TYPE_ERROR:
index 24906b8..0038b2d 100644 (file)
@@ -4,6 +4,9 @@ import dbus
 
 bus = dbus.Bus()
 remote_service = bus.get_service("org.designfu.SampleService")
-remote_object = remote_service.get_object("/MyObject", "org.designfu.SampleInterface")
+remote_object = remote_service.get_object("/SomeObject",
+                                          "org.designfu.SampleInterface")
 
-remote_object.HelloWorld("Hello from example-client.py!")
+hello_reply = remote_object.HelloWorld("Hello from example-client.py!")
+
+print (hello_reply)
index eb55af4..88f6b50 100644 (file)
@@ -6,10 +6,11 @@ import gtk
 class MyObject(dbus.Object):
     def __init__(self):
         service = dbus.Service("org.designfu.SampleService")
-        dbus.Object("/MyObject", [self.HelloWorld], service)
+        dbus.Object("/SomeObject", [self.HelloWorld], service)
 
-    def HelloWorld(self, arg1):
-        print ("Hello World!: %s" % (arg1))
+    def HelloWorld(self, hello_message):
+        print (hello_message)
+        return "Hello from example-service.py"
 
 object = MyObject()