Bläddra i källkod

tools/kemi: update kemi_mock for python (#3751)

fixes code generation when python keywords are uses
fixes formating issues of generated code
fixes crash from pv.setl reporting parameters as null instead of 'none'
tsearle 1 år sedan
förälder
incheckning
3e1a7bb5f6
2 ändrade filer med 43 tillägg och 20 borttagningar
  1. 31 20
      misc/tools/kemi/python_mock/kemi_mock.py
  2. 12 0
      misc/tools/kemi/python_mock/test.py

+ 31 - 20
misc/tools/kemi/python_mock/kemi_mock.py

@@ -11,18 +11,21 @@ import sys
 
 from collections import defaultdict
 
-#python 3.2 doesn't support types.Union
+#python 3.2 doesnt support types.Union
 noUnion = False
 
+reserved_keywords = {"async"}
+
+
 def printMocReturn(module_name, func, indent):
     param_names = []
     param_list = []
     param_signature = ""
-    if (func['params'] != 'none'):
+    if func['params'] is not None and func['params'] != 'none':
         param_list = func['params'].split(", ")
     i = 0
 
-    for param in param_list:
+    for _ in param_list:
         param_names.append("param"+str(i))
         i = i + 1
 
@@ -56,13 +59,13 @@ def printDefaultReturn(func, indent):
     for i in range(indent):
         prefix = prefix+"\t"
 
-    if(func['ret'] == "bool"):
+    if func['ret'] == "bool":
         print(prefix + "return True")
-    elif(func['ret'] == "int"):
+    elif func['ret'] == "int":
         print(prefix + "return 1")
-    elif (func['ret'] == "str"):
+    elif func['ret'] == "str":
         print(prefix + "return \"\"")
-    elif (func['ret'] == "xval"):
+    elif func['ret'] == "xval":
         print(prefix + "return None")
     else:
         print(prefix + "return")
@@ -78,38 +81,40 @@ def printFunction(module_name, func, indent):
 
     log_format_params = "%s"
 
-    if indent > 0:
-        params = "self"
-
     param_list = []
-    if(func['params']!="none"):
+    print("func['params']=%s" % func['params'])
+    if func['params'] is not None and func['params'] != "none":
         param_list = func['params'].split(", ")
         i = 0
-        for param in param_list:
+        for _ in param_list:
             if params != "":
                  params = params + ", "
             params = params + "param" + str(i) + ": " + param_list[i]
             log_params = log_params + ", param" + str(i)
             log_format_params = log_format_params + ", %s"
             i = i+1
+    if len(param_list) > 0:
+        log_params = "(" + log_params + ")"
     prefix = ""
     for i in range(indent):
         prefix = prefix+"\t"
-    if(func['ret'] == "bool"):
+    if indent > 0:
+        print(prefix + "@staticmethod")
+    if func['ret'] == "bool":
         print(prefix + "def " + func['name'] +"("+params+") -> bool:")
-    elif(func['ret'] == "int"):
+    elif func['ret'] == "int":
         print(prefix + "def " + func['name'] +"("+params+") -> int:")
-    elif (func['ret'] == "str"):
+    elif func['ret'] == "str":
         print(prefix + "def " + func['name'] + "(" + params + ") -> int:")
-    elif(func['ret'] == "xval"):
+    elif func['ret'] == "xval":
         if noUnion:
             print(prefix + "def " + func['name'] + "(" + params + "):")
         else:
-            print(prefix + "def " + func['name'] +"("+params+") -> Union[int,str]:")
+            print(prefix + "def " + func['name'] +"("+params+") -> Union[int, str, None]:")
     else:
         print(prefix + "def " + func['name'] +"("+params+"):")
 
-    print(prefix + "\tprint(\"Calling " + log_format_params + "\" % ("+log_params+"))")
+    print(prefix + "\tprint(\"Calling " + log_format_params + "\" % "+log_params+")")
     printMocReturn(module_name, func, indent+1)
     print("")
 
@@ -128,8 +133,10 @@ if len(sys.argv) > 2:
 if not noUnion:
     print("from typing import Union")
 
+print("import sys")
 print("import types")
-print("_mock_data={}")
+print("_mock_data = {}")
+print("")
 
 with open(sys.argv[1]) as f:
     data = json.load(f)
@@ -177,6 +184,7 @@ if "pv" not in classes:
 
 for module_name, module in classes.items():
     if module_name != "":
+        print("")
         print("class " + module_name.capitalize() + ":")
 
         for func in module:
@@ -188,7 +196,10 @@ for func in classes['']:
 
 for module_name in classes.keys():
     if module_name != "":
-        print(module_name + " = "+module_name.capitalize()+"()")
+        if module_name in reserved_keywords:
+            print("setattr(sys.modules[__name__], '" + module_name + "', " + module_name.capitalize() + "())")
+        else:
+            print(module_name + " = "+module_name.capitalize()+"()")
 
 print("")
 

+ 12 - 0
misc/tools/kemi/python_mock/test.py

@@ -2,6 +2,13 @@
 
 import KSR
 
+# this assumes you have your mock KSR.py in the local directory e.g. /test/
+# and your kemi code is in ../conf/kamailio.py
+
+sys.path.insert(0, "../conf/")
+import kamailio as kamailio
+
+
 #return sip:hello@world only if $ru is passed to pv.get
 KSR._mock_data['pv']['get'] = {}
 KSR._mock_data['pv']['get']['$ru'] = "sip:hello@world"
@@ -22,6 +29,11 @@ def appendHeader(param0: str):
 
 KSR._mock_data['hdr']['append'] = appendHeader
 KSR.hdr.append("X-HDR: my-header")
+
+k = kamailio.kamailio()
+k.ksr_request_route(None) # Call the kemi script, the mock implementations will be called
+
+# Validate the results
 if appendCalled:
     print("hdr.append successfully called!")
 else: