dbus/python _dbus.py,1.10,1.11 matchrules.py,1.4,1.5

John Palmieri johnp at freedesktop.org
Wed Aug 17 21:04:59 PDT 2005


Update of /cvs/dbus/dbus/python
In directory gabe:/tmp/cvs-serv4207/python

Modified Files:
	_dbus.py matchrules.py 
Log Message:
	* ChangeLog: clean up my last entry a bit

	* doc/introspect.xsl: New stylesheet for converting introspection data
	into browser renderable xhtml. Contributed by Lennart Poettering.

	* doc/introspect.dtd: Fixups in the introspect format from Lennart
	Poettering.

	* doc/dbus-tutorial.xml: 
	- Add Colin Walter to the Authors section for authoring the GLib
	section
	- Add descriptions of the new signature and type functionality
	in the Python complex type mapping section
	- Add a sidenote on the new args matching functionality in 
	the Python bindings
	- Fixed up some of the examples to use the gobject.MainLoop
	instead of gtk.main
	
	* python/_dbus.py:
	(Bus::_create_args_dict): New. Converts a hash of arg matches
	to a more useable format
	(Bus::add_signal_receiver): add a **keywords parameter for catching
	arg match parameters
	(Bus::remove_signal_receiver): add a **keywords parameter for catching
	arg match parameters
	
	* python/matchrules.py:
	(MatchTree::exec_matches): Check for arg matches
	(SignalMatchRule::add_args_match): New method
	(SignalMatchRule::execute): Added args_list parameter as an optimization
	so we don't have to marshal the args more than once
	(SignalMatchRule::match_args_from_list): New method that checks to see
	if the rule's arg matches match an argument list.  Only arguments
	set in the rule are checked.
	(SignalMatchRule::match_args_from_rule): New method that checks to see
	if the rule's arg matches match another rule's.  All args have to match
	in order for this method to return true.  If either rule has more args
	then it is not a match.
	(SignalMatchRule::is_match): Add args match
	(SignalMatchRule::repr): Add args to the final output if they exist


Index: _dbus.py
===================================================================
RCS file: /cvs/dbus/dbus/python/_dbus.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- _dbus.py	16 Aug 2005 22:54:04 -0000	1.10
+++ _dbus.py	18 Aug 2005 04:04:57 -0000	1.11
@@ -41,9 +41,10 @@
 print(dbus_object.ListServices())
 """
 
+import dbus
+
 import dbus_bindings
 
-import dbus
 from proxies import *
 from exceptions import *
 from matchrules import *
@@ -104,25 +105,67 @@
         """Get a proxy object to call over the bus"""
         return self.ProxyObjectClass(self, named_service, object_path)
 
-    def add_signal_receiver(self, handler_function, signal_name=None, dbus_interface=None, named_service=None, path=None):
+    def _create_args_dict(self, keywords):
+        args_dict = None 
+        for (key, value) in keywords.iteritems():
+            if key.startswith('arg'):
+                try:
+                    snum = key[3:]
+                    num = int(snum)
+
+                    if not args_dict:
+                        args_dict = {}
+
+                    args_dict[num] = value
+                except ValueError:
+                    raise TypeError("Invalid arg index %s"%snum)
+            else:
+                raise TypeError("Unknown keyword %s"%(key)) 
+
+        return args_dict    
+
+    def add_signal_receiver(self, handler_function, 
+                                  signal_name=None,
+                                  dbus_inteface=None,
+                                  named_service=None,
+                                  path=None,
+                                  **keywords):
+
+        args_dict = self._create_args_dict(keywords)
+
         if (named_service and named_service[0] != ':'):
             bus_object = self.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
             named_service = bus_object.GetNameOwner(named_service, dbus_interface='org.freedesktop.DBus')
         
         match_rule = SignalMatchRule(signal_name, dbus_interface, named_service, path)
+
+        if args_dict:
+            match_rule.add_args_match(args_dict)
+
         match_rule.add_handler(handler_function)
 
         self._match_rule_tree.add(match_rule)
 
-        dbus_bindings.bus_add_match(self._connection, str(match_rule))
+        dbus_bindings.bus_add_match(self._connection, repr(match_rule))
 
-    def remove_signal_receiver(self, handler_function, signal_name=None, dbus_interface=None, named_service=None, path=None):
+    def remove_signal_receiver(self, handler_function, 
+                      	       signal_name=None,
+                               dbus_interface=None,
+                               named_service=None,
+                               path=None,
+                               **keywords):
+
+        args_dict = self._create_args_dict(keywords)
+    
         if (named_service and named_service[0] != ':'):
             bus_object = self.get_object('org.freedesktop.DBus', '/org/freedesktop/DBus')
             named_service = bus_object.GetNameOwner(named_service, dbus_interface='org.freedesktop.DBus')
         
         match_rule = SignalMatchRule(signal_name, dbus_interface, named_service, path)
 
+        if (args_dict):
+            match_rule.add_args_match(args_dict)
+
 	if (handler_function):
 	    match_rule.add_handler(handler_function)
         
@@ -139,8 +182,8 @@
             return dbus_bindings.HANDLER_RESULT_NOT_YET_HANDLED
         
         dbus_interface      = message.get_interface()
-        named_service     = message.get_sender()
-        path                      = message.get_path()
+        named_service       = message.get_sender()
+        path                = message.get_path()
         signal_name         = message.get_member()
 
         match_rule = SignalMatchRule(signal_name, dbus_interface, named_service, path)

Index: matchrules.py
===================================================================
RCS file: /cvs/dbus/dbus/python/matchrules.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- matchrules.py	20 Jul 2005 14:15:08 -0000	1.4
+++ matchrules.py	18 Aug 2005 04:04:57 -0000	1.5
@@ -70,6 +70,8 @@
         path.add(rule.path, leaf=rule)
        
     def exec_matches(self, match_rule, message):
+        args = message.get_args_list()
+    
         sender_matches = self._tree.get_matches(match_rule.sender)
         for sender_node in sender_matches:
             interface_matches = sender_node.get_matches(match_rule.dbus_interface)
@@ -80,7 +82,8 @@
                     for path_node in path_matches:
                         if(path_node.rules):
                             for rule in path_node.rules:
-                                rule.execute(message)
+                                if (rule.match_args_from_list(args)):
+                                    rule.execute(message, args)
             
     def remove(self, rule):
         try:
@@ -121,9 +124,16 @@
         self.dbus_interface = dbus_interface
         self.sender = sender
         self.path = path
+        self.args = None
 
-    def execute(self, message):
-        args = message.get_args_list()
+    def add_args_match(self, args):
+        self.args = args
+
+    def execute(self, message, args=None):
+        #optimization just in case we already extarcted the args
+        if not args:
+           args = message.get_args_list()
+           
         for handler in self.handler_functions:
             if getattr(handler, "_dbus_pass_message", False):
                 keywords = {"dbus_message": message}
@@ -133,12 +143,48 @@
 
     def add_handler(self, handler):
         self.handler_functions.append(handler)
-        
+    
+    #matches only those arguments listed by self
+    def match_args_from_list(self, args_list):
+        if not self.args:
+            return True
+
+        last_index = len(args_list) - 1
+        for (index, value) in self.args.iteritems():
+            if index > last_index:
+                return False
+                
+            if not (args_list[index] == value):
+                return False
+
+        return True
+    
+    #does exact matching
+    def match_args_from_rule(self, rule):
+        if self.args == rule.args:
+            return True
+
+        if self.args == None or rule.args == None:
+            return False
+
+        my_args_list = self.args.items()
+        match_args_list = rule.args.iterms()
+
+        if len(my_args_list) != len(match_args_list):
+            return False
+
+        for (key, value) in my_args_list:
+            if rule.args.get(key) != value:
+                return False
+
+        return True
+
     def is_match(self, rule):
         if (self.signal_name == rule.signal_name and
             self.dbus_interface == rule.dbus_interface and
             self.sender == rule.sender and
-            self.path == rule.path):
+            self.path == rule.path and
+            self.match_args_from_rule(rule)):
                 if rule.handler_functions == []:
                     return True
             
@@ -167,5 +213,11 @@
             
         if (self.signal_name):
             repr = repr + ",member='%s'" % (self.signal_name)
-    
+   
+        if (self.args):
+            my_args_list = self.args.items()
+            my_args_list.sort()
+            for (index, value) in my_args_list:
+                repr = repr + ",arg%i='%s'" % (index, value)
+
         return repr



More information about the dbus-commit mailing list