[Mod]reformat ctp wrap generator script

This commit is contained in:
vn.py 2019-02-23 16:26:14 +08:00
parent ee548db7d8
commit 61cbe9dc4a
3 changed files with 119 additions and 111 deletions

View File

@ -1,6 +1,7 @@
"""""" """"""
import importlib import importlib
class ApiGenerator: class ApiGenerator:
"""API生成器""""" """API生成器"""""
@ -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()

View File

@ -7,6 +7,7 @@ TYPE_CPP2PY = {
"short": "int", "short": "int",
} }
class DataTypeGenerator: class DataTypeGenerator:
"""DataType生成器""" """DataType生成器"""

View File

@ -1,6 +1,7 @@
"""""" """"""
import importlib import importlib
class StructGenerator: class StructGenerator:
"""Struct生成器""" """Struct生成器"""