-
Notifications
You must be signed in to change notification settings - Fork 157
/
generate.py
102 lines (78 loc) · 3.23 KB
/
generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# encoding:utf-8
import codecs
import os
import re
from collections import OrderedDict
CTP_PATH = os.path.join(os.path.dirname(__file__), "ctp")
HEADER_PATH = os.path.join(CTP_PATH, "header")
USERAPI_DATA_FILE = os.path.join(HEADER_PATH, "ThostFtdcUserApiDataType.h")
USERAPI_STRUCT_FILE = os.path.join(HEADER_PATH, "ThostFtdcUserApiStruct.h")
GENERATE_PATH = os.path.join(os.path.dirname(__file__), "ctpwrapper/headers")
def generate_structure(datatype_dict):
"""
generate structure file
:return:
"""
generate_file = os.path.join(GENERATE_PATH, "ThostFtdcUserApiStruct.pxd")
data_struct_file = codecs.open(generate_file, "w", encoding="utf-8")
data_struct_file.write("# encoding:utf-8")
data_struct_file.write("\n" * 2)
data_struct_file.write("from .ThostFtdcUserApiDataType cimport *\n")
data_struct_file.write("\n" * 2)
data_struct_file.write("cdef extern from 'ThostFtdcUserApiStruct.h':\n")
for line in codecs.open(USERAPI_STRUCT_FILE, encoding="utf-8"):
if line.startswith("struct"):
result = re.findall("\w+", line)
name = result[1]
data_struct_file.write(" cdef struct {name}:\n".format(name=name))
else:
result = re.findall("\w+", line)
if result:
type_name = result[0]
if type_name in datatype_dict:
data_struct_file.write(" " + line.replace(";", ""))
data_struct_file.close()
def generate_datatype():
"""
generate structure data
:return:
"""
datatype_dict = OrderedDict()
generate_file = os.path.join(GENERATE_PATH, "ThostFtdcUserApiDataType.pxd")
data_type_file = codecs.open(generate_file, "w", encoding="utf-8")
data_type_file.write("# encoding:utf-8")
data_type_file.write("\n" * 2)
data_type_file.write("cdef extern from 'ThostFtdcUserApiDataType.h':\n")
for line in codecs.open(USERAPI_DATA_FILE, encoding="utf-8"):
if line.startswith("enum"):
# special output data
data_type_file.write("""
cdef enum THOST_TE_RESUME_TYPE:
THOST_TERT_RESTART = 0
THOST_TERT_RESUME
THOST_TERT_QUICK
THOST_TERT_NONE
""")
# special write data
if line.startswith("#define"):
pass
elif line.startswith("typedef"):
result = re.findall("\w+", line)
_type = result[1]
name = result[2]
datatype_dict.setdefault(name)
length = None
if len(result) == 4: # char xxxxx [10]
length = result[3]
if length:
data_type_file.write(" ctypedef {_type} {name}[{length}]\n".format(_type=_type,
name=name,
length=length))
else:
data_type_file.write(" ctypedef {_type} {name}\n".format(_type=_type,
name=name))
data_type_file.close()
return datatype_dict
if __name__ == "__main__":
datatype_dict = generate_datatype()
generate_structure(datatype_dict)