changeset 374:193fd5995430

bridge-constructor: added dbus-xml constructor (used for Qt frontend)
author Goffi <goffi@goffi.org>
date Thu, 28 Jul 2011 03:02:31 +0200 (2011-07-28)
parents a3aa4d046914
children 502489e17685
files src/bridge/bridge_constructor/bridge_contructor.py
diffstat 1 files changed, 82 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/bridge/bridge_constructor/bridge_contructor.py	Thu Jul 28 01:03:22 2011 +0200
+++ b/src/bridge/bridge_constructor/bridge_contructor.py	Thu Jul 28 03:02:31 2011 +0200
@@ -35,7 +35,7 @@
 
 This script construct a SàT bridge using the given protocol
 """
-MANAGED_PROTOCOLES=['dbus','mediawiki']
+MANAGED_PROTOCOLES=['dbus','mediawiki', 'dbus-xml']
 DEFAULT_PROTOCOLE='dbus'
 FLAGS=['deprecated', 'async']
 
@@ -49,6 +49,7 @@
 from ConfigParser import NoOptionError
 import re
 from datetime import datetime
+from xml.dom import minidom
 
 
 class ParseError(Exception):
@@ -132,32 +133,23 @@
         if self.bridge_template.has_option(name, "doc"):
             return self.bridge_template.get(name, "doc")
         return None
-    
-    def getArguments(self, signature, name=None, default=None, unicode_protect=False):
-        """Return arguments to user given a signature
-        @param signature: signature in the short form (using s,a,i,b etc)
-        @param name: dictionary of arguments name like given by getArguments
-        @param default: dictionary of default values, like given by getDefault
-        @param unicode_protect: activate unicode protection on strings (return strings as unicode(str))
-        @return: list of arguments that correspond to a signature (e.g.: "sss" return "arg1, arg2, arg3")""" 
+
+
+    def argumentsParser(self, signature):
+        """Generator which return individual arguments signatures from a global signature"""
+        start=0
         i=0
-        idx=0
-        attr_string=[]
+
         while i<len(signature):
             if signature[i] not in ['b','y','n','i','x','q','u','t','d','s','a']:
                 raise ParseError("Unmanaged attribute type [%c]" % signature[i])
-            
-            attr_string.append(("unicode(%(name)s)%(default)s" if (unicode_protect and signature[i]=='s') else "%(name)s%(default)s") % {
-                'name':name[idx][0] if (name and name.has_key(idx)) else "arg_%i" % idx,
-                'default':"="+default[idx] if (default and default.has_key(idx)) else ''
-                }) #give arg_1, arg2, etc or name1, name2=default, etc. \
-                   #give unicode(arg_1), unicode(arg_2), etc. if unicode_protect is set and arg is a string
-            idx+=1
 
             if signature[i] == 'a':
                 i+=1
                 if signature[i]!='{' and signature[i]!='(': #FIXME: must manage tuples out of arrays
                     i+=1
+                    yield signature[start:i]
+                    start=i
                     continue #we have a simple type for the array
                 opening_car = signature[i]
                 assert(opening_car in ['{','('])
@@ -174,6 +166,27 @@
                         if opening_count == 0:
                             break
             i+=1
+            yield signature[start:i]
+            start=i
+
+    def getArguments(self, signature, name=None, default=None, unicode_protect=False):
+        """Return arguments to user given a signature
+        @param signature: signature in the short form (using s,a,i,b etc)
+        @param name: dictionary of arguments name like given by getArguments
+        @param default: dictionary of default values, like given by getDefault
+        @param unicode_protect: activate unicode protection on strings (return strings as unicode(str))
+        @return: list of arguments that correspond to a signature (e.g.: "sss" return "arg1, arg2, arg3")""" 
+        idx=0
+        attr_string=[]
+        
+        for arg in self.argumentsParser(signature):
+            attr_string.append(("unicode(%(name)s)%(default)s" if (unicode_protect and arg=='s') else "%(name)s%(default)s") % {
+                'name':name[idx][0] if (name and name.has_key(idx)) else "arg_%i" % idx,
+                'default':"="+default[idx] if (default and default.has_key(idx)) else ''
+                }) #give arg_1, arg2, etc or name1, name2=default, etc. \
+                   #give unicode(arg_1), unicode(arg_2), etc. if unicode_protect is set and arg is a string
+            idx+=1
+        
         return ", ".join(attr_string)
 
     def generateCoreSide(self):
@@ -462,6 +475,55 @@
         #now we write to final file
         self.finalWrite(self.frontend_dest, frontend_bridge)
 
+class DbusXmlConstructor(Constructor):
+    """Constructor for DBus XML syntaxt (used by Qt frontend)"""
+
+    def __init__(self, bridge_template, options):
+        Constructor.__init__(self, bridge_template, options)
+        
+        self.template="dbus_xml_template.xml" 
+        self.core_dest="org.goffi.sat.xml" 
+
+    def generateCoreSide(self):
+        try:
+            doc = minidom.parse(self.template)
+            interface_elt = doc.getElementsByTagName('interface')[0]
+        except IOError:
+            print ("Can't access template")
+            sys.exit(1)
+        except IndexError:
+            print ("Template error")
+            sys.exit(1)
+
+        sections = self.bridge_template.sections()
+        sections.sort()
+        for section in sections:
+            function = self.getValues(section)
+            print ("Adding %s %s" % (section, function["type"]))
+            new_elt = doc.createElement('method' if function["type"]=='method' else 'signal')
+            new_elt.setAttribute('name', section)
+            args_in_str = self.getArguments(function['sig_in'])
+
+            idx=0
+            args_doc = self.getArgumentsDoc(section)
+            for arg in self.argumentsParser(function['sig_in'] or ''):
+                arg_elt = doc.createElement('arg')
+                arg_elt.setAttribute('name', args_doc[idx][0] if args_doc.has_key(idx) else "arg_%i" % idx)
+                arg_elt.setAttribute('type', arg)
+                arg_elt.setAttribute('direction', 'in' if function["type"]=='method' else 'out')
+                new_elt.appendChild(arg_elt)
+                idx+=1
+            
+            if function['sig_out']:
+                arg_elt = doc.createElement('arg')
+                arg_elt.setAttribute('type', function['sig_out'])
+                arg_elt.setAttribute('direction', 'out')
+                new_elt.appendChild(arg_elt)
+
+            interface_elt.appendChild(new_elt)
+
+        #now we write to final file
+        self.finalWrite(self.core_dest, [doc.toprettyxml()])
 
 class ConstructorError(Exception):
     pass
@@ -472,6 +534,8 @@
            return DbusConstructor(bridge_template, options)
        elif options.protocole=='mediawiki':
            return MediawikiConstructor(bridge_template, options)
+       elif options.protocole=='dbus-xml':
+           return DbusXmlConstructor(bridge_template, options)
 
        raise ConstructorError('Unknown constructor type')