Send attributes of dbus commands with a certain dbus type. This makes it possible...
authorMario Lueder <monsieur.mona@gmail.com>
Wed, 2 Jul 2014 12:33:23 +0000 (14:33 +0200)
committerMario Lueder <mario.lueder@neusoft.com>
Wed, 2 Jul 2014 12:33:23 +0000 (14:33 +0200)
cloudeebus/cloudeebus.py

index c7109b9..159a6b5 100755 (executable)
@@ -447,6 +447,16 @@ class CloudeebusService:
         self.localCtx = locals()
         self.globalCtx = globals()
 
+        self.patternDbus        = re.compile('^dbus\.(\w+)')
+        self.patternDbusBoolean = re.compile('^dbus.Boolean\((\w+)\)$')
+        self.patternDbusByte    = re.compile('^dbus.Byte\((\d+)\)$')
+        self.patternDbusInt16   = re.compile('^dbus.Int16\((\d+)\)$')
+        self.patternDbusInt32   = re.compile('^dbus.Int32\((\d+)\)$')
+        self.patternDbusInt64   = re.compile('^dbus.Int64\((\d+)\)$')
+        self.patternDbusUInt16  = re.compile('^dbus.UInt16\((\d+)\)$')
+        self.patternDbusUInt32  = re.compile('^dbus.UInt32\((\d+)\)$')
+        self.patternDbusUInt64  = re.compile('^dbus.UInt64\((\d+)\)$')
+        self.patternDbusDouble  = re.compile('^dbus.Double\((\d+\.\d+)\)$')
 
     def proxyObject(self, busName, serviceName, objectName):
         '''
@@ -472,6 +482,47 @@ class CloudeebusService:
             self.proxyMethods[id] = obj.get_dbus_method(methodName, interfaceName)
         return self.proxyMethods[id]
 
+    def decodeArgs( self, args ):
+        if isinstance( args, list ):
+            newArgs = []
+            for arg in args:
+                newArgs.append( self.decodeArgs( arg ))
+            return newArgs
+        elif isinstance( args, dict ):
+            newDict = {}
+            for key, value in args.iteritems():
+                key = self.decodeArgs( key )
+                newValue = self.decodeArgs( value )
+                newDict[key] = newValue
+            return newDict
+        elif isinstance( args, basestring ):
+            newArg = self.decodeDbusString( args )
+            return newArg
+        else:
+            return args
+
+    def decodeDbusString( self, dbusString ):
+        matchDbus = self.patternDbus.match( dbusString )
+
+        if not matchDbus:
+            return dbusString
+
+        result = {
+           "Boolean" : lambda x : dbus.Boolean( self.patternDbusBoolean.match( x ).group( 1 )),
+           "Byte"    : lambda x : dbus.Byte(    self.patternDbusByte.match(    x ).group( 1 )),
+           "Int16"   : lambda x : dbus.Int16(   self.patternDbusInt16.match(   x ).group( 1 )),
+           "Int32"   : lambda x : dbus.Int32(   self.patternDbusInt32.match(   x ).group( 1 )),
+           "Int64"   : lambda x : dbus.Int64(   self.patternDbusInt6.match(    x ).group( 1 )),
+           "UInt16"  : lambda x : dbus.UInt16(  self.patternDbusUInt16.match(  x ).group( 1 )),
+           "UInt32"  : lambda x : dbus.UInt32(  self.patternDbusUInt32.match(  x ).group( 1 )),
+           "UInt64"  : lambda x : dbus.UInt64(  self.patternDbusUInt6.match(   x ).group( 1 )),
+           "Double"  : lambda x : dbus.Double(  self.patternDbusDouble.match(  x ).group( 1 ))
+        }[matchDbus.group(1)](dbusString)
+
+        if not result:
+           return dbusString
+
+        return result
 
     @exportRpc
     def dbusRegister(self, list):
@@ -513,7 +564,11 @@ class CloudeebusService:
         # parse JSON arg list
         args = []
         if len(list) == 6:
-            args = json.loads(list[5])
+            jsonArgs = json.loads(list[5])
+            print "JSON Arguments:", jsonArgs
+            if jsonArgs:
+                args = self.decodeArgs( jsonArgs )
+                print "Decoded Arguments: ", args
         
         # get dbus proxy method
         method = self.proxyMethod(*list[0:5])
@@ -532,7 +587,13 @@ class CloudeebusService:
         objectPath = list[0]
         className = re.sub('/', '_', objectPath[1:])
         signalName = list[1]
-        args = json.loads(list[2])
+        args = []
+        jsonArgs = json.loads(list[2])
+        print "JSON Arguments:", jsonArgs
+        if jsonArgs:
+            args = self.decodeArgs( jsonArgs )
+            print "Decoded Arguments: ", args
+
         if (self.serviceAgents.has_key(className) == True):            
             exe_str = "self.serviceAgents['"+ className +"']."+ signalName + "("
             if len(args) > 0: