2004-05-30 Seth Nickell <seth@gnome.org>
authorSeth Nickell <seth@gnome.org>
Sun, 30 May 2004 08:20:58 +0000 (08:20 +0000)
committerSeth Nickell <seth@gnome.org>
Sun, 30 May 2004 08:20:58 +0000 (08:20 +0000)
* python/dbus.py:

Add a nicer-but-less-flexible alternate API for handling
calls to virtual objects in dbus.ObjectTree.

Screw up the argument order to the dbus.Object constructor
for consistency with dbus.ObjectTree (and to make dbus_methods
optional for future extension)

* python/examples/Makefile.am:
* python/examples/gconf-proxy-service.py:
* python/examples/gconf-proxy-service2.py:

Alternate implementation of gconf-proxy-service using the
nicer dbus.ObjectTree API.

* python/examples/example-service.py:
* python/tests/test-server.py

Reverse the argument order to deal with dbus.Object constructor
changes.

ChangeLog
python/dbus.py
python/examples/Makefile.am
python/examples/example-service.py
python/examples/gconf-proxy-service.py
python/examples/gconf-proxy-service2.py [new file with mode: 0644]
python/tests/test-server.py

index e6bf2c3..801e449 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,29 @@
 2004-05-30  Seth Nickell  <seth@gnome.org>
 
+       * python/dbus.py:
+
+       Add a nicer-but-less-flexible alternate API for handling 
+       calls to virtual objects in dbus.ObjectTree.
+
+       Screw up the argument order to the dbus.Object constructor
+       for consistency with dbus.ObjectTree (and to make dbus_methods
+       optional for future extension)
+       
+       * python/examples/Makefile.am:
+       * python/examples/gconf-proxy-service.py:
+       * python/examples/gconf-proxy-service2.py:
+
+       Alternate implementation of gconf-proxy-service using the
+       nicer dbus.ObjectTree API.
+       
+       * python/examples/example-service.py:
+       * python/tests/test-server.py
+
+       Reverse the argument order to deal with dbus.Object constructor
+       changes.
+       
+2004-05-30  Seth Nickell  <seth@gnome.org>
+
        * python/examples/example-client.py:
        * python/examples/example-service.py:
 
index 797b196..13ea1ad 100644 (file)
@@ -237,6 +237,14 @@ def _dispatch_dbus_method_call(target_method, argument_list, message):
 
     return reply
 
+def _build_method_dictionary(methods):
+    method_dict = {}
+    for method in methods:
+        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 Object:
     """A base class for exporting your own Objects across the Bus.
 
@@ -244,13 +252,18 @@ class Object:
     across the Bus. These will appear as member functions of your
     ServiceObject.
     """
-    def __init__(self, object_path, methods_to_share, service):
+    def __init__(self, object_path, service, dbus_methods=[]):
+        # Reversed constructor argument order. Add a temporary
+        # check to help people get things straightened out with minimal pain.
+        if type(service) == list:
+            raise TypeError, "dbus.Object.__init__(): the order of the 'service' and 'dbus_methods' arguments has been reversed (for consistency with dbus.ObjectTree)."
+        
         self._object_path = object_path
         self._service = service
         self._bus = service.get_bus()
         self._connection = self._bus.get_connection()
 
-        self._method_name_to_method = self._build_method_dictionary(methods_to_share)
+        self._method_name_to_method = _build_method_dictionary(dbus_methods)
         
         self._connection.register_object_path(object_path, self._unregister_cb, self._message_cb)
 
@@ -267,31 +280,36 @@ class Object:
         target_method_name = message.get_member()
         target_method = self._method_name_to_method[target_method_name]
         args = message.get_args_list()
-        
+
         reply = _dispatch_dbus_method_call(target_method, args, message)
         
         self._connection.send(reply)
 
-    def _build_method_dictionary(self, methods):
-        method_dict = {}
-        for method in methods:
-            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 ObjectTree:
     """An object tree allows you to register a handler for a tree of object paths.
     This means that literal Python objects do not need to be created for each object
     over the bus, but you can have a virtual tree of objects handled by a single
-    Python object.
+    Python object. There are two ways to handle method calls on virtual objects:
+
+    1) Pass a list of dbus_methods in to __init__. This works just like dbus.Object,
+    except an object_path is passed as the first argument to each method, denoting which
+    virtual object the call was made on. If all the objects in the tree support the same
+    methods, this is the best approach.
+
+    2) Override object_method_called. This allows you to define the valid methods dynamically
+    on an object by object basis. For example, if providing an object tree that represented
+    a filesystem heirarchy, you'd only want an ls method on directory objects, not file objects.
     """
 
-    def __init__(self, base_path, service):
+    def __init__(self, base_path, service, dbus_methods=[]):
         self._base_path = base_path
         self._service = service
         self._bus = service.get_bus()
         self._connection = self._bus.get_connection()
+
+        self._method_name_to_method = _build_method_dictionary(dbus_methods)
         
         self._connection.register_fallback(base_path, self._unregister_cb, self._message_cb)
         
@@ -308,9 +326,15 @@ class ObjectTree:
         target_object_full_path = message.get_path()
         assert(self._base_path == target_object_full_path[:len(self._base_path)])
         target_object_path = target_object_full_path[len(self._base_path):]
-        
         target_method_name = message.get_member()        
-        args = message.get_args_list()
+        message_args = message.get_args_list()
+
+        try:
+            target_method = self._method_name_to_method[target_method_name]
+            args = [target_object_path] + message_args
+        except KeyError:
+            target_method = self.object_method_called
+            args = [target_object_path, target_method_name, message_args]
 
         reply = _dispatch_dbus_method_call(target_method, args, message)
 
index 8eab412..4a9b8ab 100644 (file)
@@ -6,6 +6,7 @@ EXTRA_DIST =                    \
        example-signal.py       \
        gconf-proxy-client.py   \
        gconf-proxy-service.py  \
+       gconf-proxy-service2.py \
        list-system-services.py \
        $(NULL)
 
index ceb7f11..974f8b1 100644 (file)
@@ -5,7 +5,7 @@ import gtk
 
 class SomeObject(dbus.Object):
     def __init__(self, service):
-        dbus.Object.__init__(self, "/SomeObject", [self.HelloWorld], service)
+        dbus.Object.__init__(self, "/SomeObject", service, [self.HelloWorld])
 
     def HelloWorld(self, hello_message):
         print (hello_message)
index b5842e8..76e43ce 100644 (file)
@@ -11,35 +11,27 @@ class GConfService(dbus.Service):
         gconf_object_tree = self.GConfObjectTree(self)
         
     class GConfObjectTree(dbus.ObjectTree):
-
         def __init__(self, service):
-            dbus.ObjectTree.__init__(self, "/org/gnome/GConf", service)
+            dbus.ObjectTree.__init__(self, "/org/gnome/GConf", service, dbus_methods=[ self.getString, self.setString, self.getInt, self.setInt ])
 
             self.client = gconf.client_get_default()
-            
-        def object_method_called(self, object_path, method_name, argument_list):
-            print ("Method %s called on GConf key %s" % (method_name, object_path))
-
-            return_value = None
-
-            if "getString" == method_name:
-                assert(len(argument_list) == 0)
-                return_value = self.client.get_string (object_path)
-                
-            elif "setString" == method_name:
-                assert(len(argument_list) == 1)
-                self.client.set_string(object_path, argument_list[0])
-                
-            elif "getInt" == method_name:
-                assert(len(argument_list) == 0)
-                return_value = self.client.get_int(object_path)
-                
-            elif "setInt" == method_name:
-                assert(len(argument_list) == 1)
-                self.client.set_int(object_path, argument_list[0])
-
-            return return_value
 
+        def getString(self, object_path):
+            print ("getString called on GConf key %s" % (object_path))
+            return self.client.get_string(object_path)
+
+        def setString(self, object_path, new_value):
+            print ("setString called on GConf key %s" % (object_path))            
+            self.client.set_string(object_path, new_value)
+
+        def getInt(self, object_path):
+            print ("getInt called on GConf key %s" % (object_path))
+            return self.client.get_int(object_path)
+
+        def setInt(self, object_path, new_value):
+            print ("setInt called on GConf key %s" % (object_path))
+            self.client.set_int(object_path, new_value)
+            
 gconf_service = GConfService()
 
 print ("GConf Proxy service started.")
diff --git a/python/examples/gconf-proxy-service2.py b/python/examples/gconf-proxy-service2.py
new file mode 100644 (file)
index 0000000..4cec860
--- /dev/null
@@ -0,0 +1,36 @@
+import dbus
+
+import gtk
+import gconf
+
+class GConfService(dbus.Service):
+
+    def __init__(self):
+        dbus.Service.__init__(self, "org.gnome.GConf", dbus.SessionBus())
+
+        gconf_object_tree = self.GConfObjectTree(self)
+        
+    class GConfObjectTree(dbus.ObjectTree):
+        def __init__(self, service):
+            dbus.ObjectTree.__init__(self, "/org/gnome/GConf", service)
+
+            self.client = gconf.client_get_default()
+
+        def object_method_called(self, object_path, method_name, argument_list):
+            print ("Method %s called on GConf key %s" % (method_name, object_path))
+
+            if "getString" == method_name:
+                return self.client.get_string(object_path)
+            elif "setString" == method_name:
+                self.client.set_int(object_path, argument_list[0])
+            elif "getInt" == method_name:
+                return self.client.get_int(object_path)                
+            elif "setInt" == method_name:
+                self.client.set_int(object_path, argument_list[0])
+
+gconf_service = GConfService()
+
+print ("GConf Proxy service started.")
+print ("Run 'gconf-proxy-client.py' to fetch a GConf key through the proxy...")
+
+gtk.main()
index 2af685b..235da70 100644 (file)
@@ -4,7 +4,7 @@ import gtk
 class TestObject(dbus.Object):
     def __init__(self, service):
         method_list = [ self.Echo ]
-        dbus.Object.__init__(self, "/TestObject", method_list, service)
+        dbus.Object.__init__(self, "/TestObject", service, method_list)
 
     def Echo(self, variable):
         return variable