[Mod]reformat ctp wrap generator script
This commit is contained in:
parent
ee548db7d8
commit
61cbe9dc4a
@ -1,6 +1,7 @@
|
|||||||
""""""
|
""""""
|
||||||
import importlib
|
import importlib
|
||||||
|
|
||||||
|
|
||||||
class ApiGenerator:
|
class ApiGenerator:
|
||||||
"""API生成器"""""
|
"""API生成器"""""
|
||||||
|
|
||||||
@ -62,7 +63,7 @@ class ApiGenerator:
|
|||||||
elif "virtual int Req" in line:
|
elif "virtual int Req" in line:
|
||||||
self.process_function(line)
|
self.process_function(line)
|
||||||
|
|
||||||
def process_callback(self, line:str):
|
def process_callback(self, line: str):
|
||||||
"""处理回掉函数"""
|
"""处理回掉函数"""
|
||||||
name = line[line.index("On"):line.index("(")]
|
name = line[line.index("On"):line.index("(")]
|
||||||
self.lines[name] = line
|
self.lines[name] = line
|
||||||
@ -70,16 +71,16 @@ class ApiGenerator:
|
|||||||
d = self.generate_arg_dict(line)
|
d = self.generate_arg_dict(line)
|
||||||
self.callbacks[name] = d
|
self.callbacks[name] = d
|
||||||
|
|
||||||
def process_function(self, line:str):
|
def process_function(self, line: str):
|
||||||
"""处理主动函数"""
|
"""处理主动函数"""
|
||||||
name = line[line.index("Req"):line.index("(")]
|
name = line[line.index("Req"):line.index("(")]
|
||||||
|
|
||||||
d = self.generate_arg_dict(line)
|
d = self.generate_arg_dict(line)
|
||||||
self.functions[name] = d
|
self.functions[name] = d
|
||||||
|
|
||||||
def generate_arg_dict(self, line:str):
|
def generate_arg_dict(self, line: str):
|
||||||
"""生成参数字典"""
|
"""生成参数字典"""
|
||||||
args_str = line[line.index("(")+1:line.index(")")]
|
args_str = line[line.index("(") + 1:line.index(")")]
|
||||||
if not args_str:
|
if not args_str:
|
||||||
return {}
|
return {}
|
||||||
args = args_str.split(",")
|
args = args_str.split(",")
|
||||||
@ -115,18 +116,18 @@ class ApiGenerator:
|
|||||||
for name, d in self.callbacks.items():
|
for name, d in self.callbacks.items():
|
||||||
name = name.replace("On", "on")
|
name = name.replace("On", "on")
|
||||||
|
|
||||||
l = []
|
args_list = []
|
||||||
for type_ in d.values():
|
for type_ in d.values():
|
||||||
if type_ == "int":
|
if type_ == "int":
|
||||||
l.append("int reqid")
|
args_list.append("int reqid")
|
||||||
elif type_ == "bool":
|
elif type_ == "bool":
|
||||||
l.append("bool last")
|
args_list.append("bool last")
|
||||||
elif type_ == "CThostFtdcRspInfoField":
|
elif type_ == "CThostFtdcRspInfoField":
|
||||||
l.append("const dict &error")
|
args_list.append("const dict &error")
|
||||||
else:
|
else:
|
||||||
l.append("const dict &data")
|
args_list.append("const dict &data")
|
||||||
|
|
||||||
args_str = ", ".join(l)
|
args_str = ", ".join(args_list)
|
||||||
line = f"virtual void {name}({args_str}) {{}};\n\n"
|
line = f"virtual void {name}({args_str}) {{}};\n\n"
|
||||||
|
|
||||||
f.write(line)
|
f.write(line)
|
||||||
@ -147,7 +148,8 @@ class ApiGenerator:
|
|||||||
for name, d in self.callbacks.items():
|
for name, d in self.callbacks.items():
|
||||||
line = self.lines[name]
|
line = self.lines[name]
|
||||||
|
|
||||||
f.write(line.replace("virtual void ", f"void {self.class_name}::") + "\n")
|
f.write(line.replace("virtual void ",
|
||||||
|
f"void {self.class_name}::") + "\n")
|
||||||
f.write("{\n")
|
f.write("{\n")
|
||||||
f.write("\tTask task = Task();\n")
|
f.write("\tTask task = Task();\n")
|
||||||
f.write(f"\ttask.task_name = {name.upper()};\n")
|
f.write(f"\ttask.task_name = {name.upper()};\n")
|
||||||
@ -195,7 +197,8 @@ class ApiGenerator:
|
|||||||
process_name = name.replace("On", "process")
|
process_name = name.replace("On", "process")
|
||||||
on_name = name.replace("On", "on")
|
on_name = name.replace("On", "on")
|
||||||
|
|
||||||
f.write(f"void {self.class_name}::{process_name}(Task *task)\n")
|
f.write(
|
||||||
|
f"void {self.class_name}::{process_name}(Task *task)\n")
|
||||||
f.write("{\n")
|
f.write("{\n")
|
||||||
f.write("\tgil_scoped_acquire acquire;\n")
|
f.write("\tgil_scoped_acquire acquire;\n")
|
||||||
|
|
||||||
@ -212,14 +215,17 @@ class ApiGenerator:
|
|||||||
f.write("\tdict error;\n")
|
f.write("\tdict error;\n")
|
||||||
f.write("\tif (task->task_error)\n")
|
f.write("\tif (task->task_error)\n")
|
||||||
f.write("\t{\n")
|
f.write("\t{\n")
|
||||||
f.write(f"\t\t{type_} *task_error = ({type_}*)task->task_error;\n")
|
f.write(
|
||||||
|
f"\t\t{type_} *task_error = ({type_}*)task->task_error;\n")
|
||||||
|
|
||||||
struct_fields = self.structs[type_]
|
struct_fields = self.structs[type_]
|
||||||
for struct_field, struct_type in struct_fields.items():
|
for struct_field, struct_type in struct_fields.items():
|
||||||
if struct_type == "string":
|
if struct_type == "string":
|
||||||
f.write(f"\t\terror[\"{struct_field}\"] = toUtf(task_error->{struct_field});\n")
|
f.write(
|
||||||
|
f"\t\terror[\"{struct_field}\"] = toUtf(task_error->{struct_field});\n")
|
||||||
else:
|
else:
|
||||||
f.write(f"\t\terror[\"{struct_field}\"] = task_error->{struct_field};\n")
|
f.write(
|
||||||
|
f"\t\terror[\"{struct_field}\"] = task_error->{struct_field};\n")
|
||||||
|
|
||||||
f.write("\t\tdelete task->task_error;\n")
|
f.write("\t\tdelete task->task_error;\n")
|
||||||
f.write("\t}\n")
|
f.write("\t}\n")
|
||||||
@ -229,19 +235,21 @@ class ApiGenerator:
|
|||||||
f.write("\tdict data;\n")
|
f.write("\tdict data;\n")
|
||||||
f.write("\tif (task->task_data)\n")
|
f.write("\tif (task->task_data)\n")
|
||||||
f.write("\t{\n")
|
f.write("\t{\n")
|
||||||
f.write(f"\t\t{type_} *task_data = ({type_}*)task->task_data;\n")
|
f.write(
|
||||||
|
f"\t\t{type_} *task_data = ({type_}*)task->task_data;\n")
|
||||||
|
|
||||||
struct_fields = self.structs[type_]
|
struct_fields = self.structs[type_]
|
||||||
for struct_field, struct_type in struct_fields.items():
|
for struct_field, struct_type in struct_fields.items():
|
||||||
if struct_type == "string":
|
if struct_type == "string":
|
||||||
f.write(f"\t\tdata[\"{struct_field}\"] = toUtf(task_data->{struct_field});\n")
|
f.write(
|
||||||
|
f"\t\tdata[\"{struct_field}\"] = toUtf(task_data->{struct_field});\n")
|
||||||
else:
|
else:
|
||||||
f.write(f"\t\tdata[\"{struct_field}\"] = task_data->{struct_field};\n")
|
f.write(
|
||||||
|
f"\t\tdata[\"{struct_field}\"] = task_data->{struct_field};\n")
|
||||||
|
|
||||||
f.write("\t\tdelete task->task_data;\n")
|
f.write("\t\tdelete task->task_data;\n")
|
||||||
f.write("\t}\n")
|
f.write("\t}\n")
|
||||||
|
|
||||||
|
|
||||||
args_str = ", ".join(args)
|
args_str = ", ".join(args)
|
||||||
f.write(f"\tthis->{on_name}({args_str});\n")
|
f.write(f"\tthis->{on_name}({args_str});\n")
|
||||||
f.write("};\n\n")
|
f.write("};\n\n")
|
||||||
@ -254,7 +262,8 @@ class ApiGenerator:
|
|||||||
req_name = name.replace("Req", "req")
|
req_name = name.replace("Req", "req")
|
||||||
type_ = list(d.values())[0]
|
type_ = list(d.values())[0]
|
||||||
|
|
||||||
f.write(f"int {self.class_name}::{req_name}(const dict &req, int reqid)\n")
|
f.write(
|
||||||
|
f"int {self.class_name}::{req_name}(const dict &req, int reqid)\n")
|
||||||
f.write("{\n")
|
f.write("{\n")
|
||||||
f.write(f"\t{type_} myreq = {type_}();\n")
|
f.write(f"\t{type_} myreq = {type_}();\n")
|
||||||
f.write("\tmemset(&myreq, 0, sizeof(myreq));\n")
|
f.write("\tmemset(&myreq, 0, sizeof(myreq));\n")
|
||||||
@ -271,7 +280,6 @@ class ApiGenerator:
|
|||||||
f.write("\treturn i;\n")
|
f.write("\treturn i;\n")
|
||||||
f.write("};\n\n")
|
f.write("};\n\n")
|
||||||
|
|
||||||
|
|
||||||
def generate_source_on(self):
|
def generate_source_on(self):
|
||||||
""""""
|
""""""
|
||||||
filename = f"{self.prefix}_{self.name}_source_on.cpp"
|
filename = f"{self.prefix}_{self.name}_source_on.cpp"
|
||||||
@ -310,7 +318,6 @@ class ApiGenerator:
|
|||||||
f.write("\t}\n")
|
f.write("\t}\n")
|
||||||
f.write("};\n\n")
|
f.write("};\n\n")
|
||||||
|
|
||||||
|
|
||||||
def generate_source_module(self):
|
def generate_source_module(self):
|
||||||
""""""
|
""""""
|
||||||
filename = f"{self.prefix}_{self.name}_source_module.cpp"
|
filename = f"{self.prefix}_{self.name}_source_module.cpp"
|
||||||
@ -328,7 +335,6 @@ class ApiGenerator:
|
|||||||
f.write(";\n")
|
f.write(";\n")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
md_generator = ApiGenerator("ThostFtdcMdApi.h", "ctp", "md", "MdApi")
|
md_generator = ApiGenerator("ThostFtdcMdApi.h", "ctp", "md", "MdApi")
|
||||||
md_generator.run()
|
md_generator.run()
|
||||||
|
@ -7,6 +7,7 @@ TYPE_CPP2PY = {
|
|||||||
"short": "int",
|
"short": "int",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
class DataTypeGenerator:
|
class DataTypeGenerator:
|
||||||
"""DataType生成器"""
|
"""DataType生成器"""
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
""""""
|
""""""
|
||||||
import importlib
|
import importlib
|
||||||
|
|
||||||
|
|
||||||
class StructGenerator:
|
class StructGenerator:
|
||||||
"""Struct生成器"""
|
"""Struct生成器"""
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user