Remove traces when decoding JSON arguments for dbus patterns
[contrib/cloudeebus.git] / cloudeebus / cloudeebus.py
index 1ab1346..7f1517e 100755 (executable)
@@ -51,7 +51,7 @@ from xml.etree.ElementTree import XMLParser
 
 ###############################################################################
 
 
 ###############################################################################
 
-VERSION = "0.5.100"
+VERSION = "0.6.0"
 OPENDOOR = False
 CREDENTIALS = {}
 WHITELIST = []
 OPENDOOR = False
 CREDENTIALS = {}
 WHITELIST = []
@@ -447,6 +447,16 @@ class CloudeebusService:
         self.localCtx = locals()
         self.globalCtx = globals()
 
         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):
         '''
 
     def proxyObject(self, busName, serviceName, objectName):
         '''
@@ -472,6 +482,45 @@ class CloudeebusService:
             self.proxyMethods[id] = obj.get_dbus_method(methodName, interfaceName)
         return self.proxyMethods[id]
 
             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 ).lower() in ("yes", "true", "t", "1")),
+           "Byte"    : lambda x : dbus.Byte( int( 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.patternDbusInt64.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.patternDbusUInt64.match(   x ).group( 1 )),
+           "Double"  : lambda x : dbus.Double(    self.patternDbusDouble.match(  x ).group( 1 ))
+        }[matchDbus.group(1)](dbusString)
+
+        return result
 
     @exportRpc
     def dbusRegister(self, list):
 
     @exportRpc
     def dbusRegister(self, list):
@@ -513,7 +562,9 @@ class CloudeebusService:
         # parse JSON arg list
         args = []
         if len(list) == 6:
         # parse JSON arg list
         args = []
         if len(list) == 6:
-            args = json.loads(list[5])
+            jsonArgs = json.loads(list[5])
+            if jsonArgs:
+                args = self.decodeArgs( jsonArgs )
         
         # get dbus proxy method
         method = self.proxyMethod(*list[0:5])
         
         # get dbus proxy method
         method = self.proxyMethod(*list[0:5])
@@ -527,17 +578,25 @@ class CloudeebusService:
     @exportRpc
     def emitSignal(self, list):
         '''
     @exportRpc
     def emitSignal(self, list):
         '''
-        arguments: agentObjectPath, signalName, result (to emit)
+        arguments: agentObjectPath, signalName, args (to emit)
         '''
         objectPath = list[0]
         className = re.sub('/', '_', objectPath[1:])
         signalName = list[1]
         '''
         objectPath = list[0]
         className = re.sub('/', '_', objectPath[1:])
         signalName = list[1]
-        result = list[2]
-        if (self.serviceAgents.has_key(className) == True):
-            if (result != None):
-                exe_str = "self.serviceAgents['"+ className +"']."+ signalName + "(" + str(result) + ")"
-            else:
-                exe_str = "self.serviceAgents['"+ className +"']."+ signalName + "()"
+        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:
+                exe_str += json.dumps(args[0])
+                for idx in args[1:]:
+                    exe_str += "," + json.dumps(idx)
+            exe_str += ")"               
             eval(exe_str, self.globalCtx, self.localCtx)
         else:
             raise Exception("No object path " + objectPath)
             eval(exe_str, self.globalCtx, self.localCtx)
         else:
             raise Exception("No object path " + objectPath)