| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
#parses the output of /usr/sbin/kamctl rpc app_python.api_list |
| 4 |
# |
| 5 |
#usage ./kemi_mock.py api.json > KSR.py |
| 6 |
#or for python 3.2 |
| 7 |
#./kemi_mock.py api.json --no-union > KSR.py |
| 8 |
|
| 9 |
import json |
| 10 |
import sys |
| 11 |
|
| 12 |
from collections import defaultdict |
| 13 |
|
| 14 |
#python 3.2 doesnt support types.Union |
| 15 |
noUnion = False |
| 16 |
|
| 17 |
reserved_keywords = {"async"} |
| 18 |
|
| 19 |
|
| 20 |
def printMocReturn(module_name, func, indent): |
| 21 |
param_names = [] |
| 22 |
param_list = [] |
| 23 |
param_signature = "" |
| 24 |
if func['params'] is not None and func['params'] != 'none': |
| 25 |
param_list = func['params'].split(", ") |
| 26 |
i = 0 |
| 27 |
|
| 28 |
for _ in param_list: |
| 29 |
param_names.append("param"+str(i)) |
| 30 |
i = i + 1 |
| 31 |
|
| 32 |
param_signature = ", ".join(param_names) |
| 33 |
|
| 34 |
prefix = "" |
| 35 |
for i in range(indent): |
| 36 |
prefix = prefix+"\t" |
| 37 |
|
| 38 |
print(prefix + "if \""+func['name']+"\" not in _mock_data['"+module_name+"']:") |
| 39 |
printDefaultReturn(func, indent+1) |
| 40 |
|
| 41 |
print(prefix + "node = _mock_data['"+module_name+"']['"+func['name']+"']") |
| 42 |
|
| 43 |
print(prefix + "if isinstance(node, types.FunctionType):") |
| 44 |
print(prefix + "\treturn node("+param_signature+")") |
| 45 |
|
| 46 |
for param in param_names: |
| 47 |
print(prefix + "if not isinstance(node, dict):") |
| 48 |
print(prefix + "\treturn node") |
| 49 |
print(prefix + "if str(" + param + ") in node:") |
| 50 |
print(prefix + "\tnode = node[str("+param+")]") |
| 51 |
print(prefix + "else:") |
| 52 |
printDefaultReturn(func, indent+1) |
| 53 |
|
| 54 |
print(prefix + "return node") |
| 55 |
|
| 56 |
|
| 57 |
def printDefaultReturn(func, indent): |
| 58 |
prefix = "" |
| 59 |
for i in range(indent): |
| 60 |
prefix = prefix+"\t" |
| 61 |
|
| 62 |
if func['ret'] == "bool": |
| 63 |
print(prefix + "return True") |
| 64 |
elif func['ret'] == "int": |
| 65 |
print(prefix + "return 1") |
| 66 |
elif func['ret'] == "str": |
| 67 |
print(prefix + "return \"\"") |
| 68 |
elif func['ret'] == "xval": |
| 69 |
print(prefix + "return None") |
| 70 |
else: |
| 71 |
print(prefix + "return") |
| 72 |
|
| 73 |
|
| 74 |
def printFunction(module_name, func, indent): |
| 75 |
params = "" |
| 76 |
log_params = "" |
| 77 |
if module_name == "": |
| 78 |
log_params = "\"" + func['name'] + "\"" |
| 79 |
else: |
| 80 |
log_params = "\"" + module_name + "." + func['name'] + "\"" |
| 81 |
|
| 82 |
log_format_params = "%s" |
| 83 |
|
| 84 |
param_list = [] |
| 85 |
if func['params'] is not None and func['params'] != "none": |
| 86 |
param_list = func['params'].split(", ") |
| 87 |
i = 0 |
| 88 |
for _ in param_list: |
| 89 |
if params != "": |
| 90 |
params = params + ", " |
| 91 |
params = params + "param" + str(i) + ": " + param_list[i] |
| 92 |
log_params = log_params + ", param" + str(i) |
| 93 |
log_format_params = log_format_params + ", %s" |
| 94 |
i = i+1 |
| 95 |
if len(param_list) > 0: |
| 96 |
log_params = "(" + log_params + ")" |
| 97 |
prefix = "" |
| 98 |
for i in range(indent): |
| 99 |
prefix = prefix+"\t" |
| 100 |
if indent > 0: |
| 101 |
print(prefix + "@staticmethod") |
| 102 |
if func['ret'] == "bool": |
| 103 |
print(prefix + "def " + func['name'] +"("+params+") -> bool:") |
| 104 |
elif func['ret'] == "int": |
| 105 |
print(prefix + "def " + func['name'] +"("+params+") -> int:") |
| 106 |
elif func['ret'] == "str": |
| 107 |
print(prefix + "def " + func['name'] + "(" + params + ") -> int:") |
| 108 |
elif func['ret'] == "xval": |
| 109 |
if noUnion: |
| 110 |
print(prefix + "def " + func['name'] + "(" + params + "):") |
| 111 |
else: |
| 112 |
print(prefix + "def " + func['name'] +"("+params+") -> Union[int, str, None]:") |
| 113 |
else: |
| 114 |
print(prefix + "def " + func['name'] +"("+params+"):") |
| 115 |
|
| 116 |
generate_function_doc(module_name, func, prefix) |
| 117 |
|
| 118 |
print(prefix + "\tprint(\"Calling " + log_format_params + "\" % "+log_params+")") |
| 119 |
printMocReturn(module_name, func, indent+1) |
| 120 |
print("") |
| 121 |
|
| 122 |
|
| 123 |
def generate_function_doc(module_name, func, prefix): |
| 124 |
if documentation is not None and module_name in documentation: |
| 125 |
function_parts = func['name'].split("_") |
| 126 |
for i in range(len(function_parts), 0, -1): |
| 127 |
function_prefix = "_".join(function_parts[:i]) |
| 128 |
if function_prefix in documentation[module_name]["functions"]: |
| 129 |
print(prefix + "\t\"\"\"") |
| 130 |
documentation_lines = documentation[module_name]["functions"][function_prefix].split("\n") |
| 131 |
for line in documentation_lines: |
| 132 |
print(prefix + "\t" + line.replace("\\", "\\\\\\\\")) |
| 133 |
print(prefix + "\t\"\"\"") |
| 134 |
break |
| 135 |
|
| 136 |
|
| 137 |
classes = defaultdict(list) |
| 138 |
|
| 139 |
if len(sys.argv) < 2: |
| 140 |
print("Please specify the json file to parse") |
| 141 |
sys.exit(-1) |
| 142 |
|
| 143 |
documentation = None |
| 144 |
if len(sys.argv) > 2: |
| 145 |
for i in range(2,len(sys.argv)): |
| 146 |
if sys.argv[i] == "--no-union": |
| 147 |
noUnion = True |
| 148 |
else: |
| 149 |
with open(sys.argv[i]) as f: |
| 150 |
documentation = json.load(f) |
| 151 |
|
| 152 |
if not noUnion: |
| 153 |
print("from typing import Union") |
| 154 |
|
| 155 |
print("import sys") |
| 156 |
print("import types") |
| 157 |
print("_mock_data = {}") |
| 158 |
print("") |
| 159 |
|
| 160 |
with open(sys.argv[1]) as f: |
| 161 |
data = json.load(f) |
| 162 |
|
| 163 |
for method in data['result']['methods']: |
| 164 |
classes[method['func']['module']].append(method['func']) |
| 165 |
|
| 166 |
if "pv" not in classes: |
| 167 |
classes['pv'].append({'params': 'str', |
| 168 |
'ret': 'xval', |
| 169 |
'name': 'get'} |
| 170 |
) |
| 171 |
classes['pv'].append({'params': 'str', |
| 172 |
'ret': 'xval', |
| 173 |
'name': 'gete'} |
| 174 |
) |
| 175 |
classes['pv'].append({'params': 'str, int', |
| 176 |
'ret': 'xval', |
| 177 |
'name': 'getvn'} |
| 178 |
) |
| 179 |
classes['pv'].append({'params': 'str, str', |
| 180 |
'ret': 'xval', |
| 181 |
'name': 'getvs'} |
| 182 |
) |
| 183 |
classes['pv'].append({'params': 'str', |
| 184 |
'ret': 'xval', |
| 185 |
'name': 'getw'} |
| 186 |
) |
| 187 |
classes['pv'].append({'params': 'str, int', |
| 188 |
'ret': 'none', |
| 189 |
'name': 'seti'} |
| 190 |
) |
| 191 |
classes['pv'].append({'params': 'str, str', |
| 192 |
'ret': 'none', |
| 193 |
'name': 'sets'} |
| 194 |
) |
| 195 |
classes['pv'].append({'params': 'str', |
| 196 |
'ret': 'none', |
| 197 |
'name': 'unset'} |
| 198 |
) |
| 199 |
classes['pv'].append({'params': 'str', |
| 200 |
'ret': 'none', |
| 201 |
'name': 'is_null'} |
| 202 |
) |
| 203 |
|
| 204 |
for module_name, module in classes.items(): |
| 205 |
if module_name != "": |
| 206 |
print("") |
| 207 |
print("class " + module_name.capitalize() + ":") |
| 208 |
|
| 209 |
for func in module: |
| 210 |
printFunction(module_name, func, 1) |
| 211 |
|
| 212 |
for func in classes['']: |
| 213 |
print("") |
| 214 |
printFunction('', func, 0) |
| 215 |
|
| 216 |
|
| 217 |
def document_module(module_name): |
| 218 |
if documentation is not None and module_name in documentation: |
| 219 |
print("\"\"\"") |
| 220 |
documentation_lines = documentation[module_name]["overview"].split("\n") |
| 221 |
for line in documentation_lines: |
| 222 |
print("" + line) |
| 223 |
print("\"\"\"") |
| 224 |
|
| 225 |
|
| 226 |
for module_name in classes.keys(): |
| 227 |
if module_name != "": |
| 228 |
if module_name in reserved_keywords: |
| 229 |
print("setattr(sys.modules[__name__], '" + module_name + "', " + module_name.capitalize() + "())") |
| 230 |
else: |
| 231 |
print(module_name + " = "+module_name.capitalize()+"()") |
| 232 |
document_module(module_name) |
| 233 |
|
| 234 |
print("") |
| 235 |
|
| 236 |
for module_name in classes.keys(): |
| 237 |
print("_mock_data['" + module_name + "'] = {}") |
| 238 |
|