-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapiwriter.py
More file actions
138 lines (118 loc) · 4.84 KB
/
apiwriter.py
File metadata and controls
138 lines (118 loc) · 4.84 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def writeInline(writer, json_val):
if json_val is None:
writer("null")
elif isinstance(json_val, dict):
writer("{")
writeInlineFields(writer, json_val.keys(), "", "", json_val)
writer("}")
elif isinstance(json_val, list):
writer("[")
prefix = ""
for val in json_val:
writer(prefix)
writeInline(writer, val)
prefix = ","
writer("]")
elif isinstance(json_val, bool):
writer("true" if json_val else "false")
elif isinstance(json_val, str):
writer("\"{}\"".format(json_val))
elif isinstance(json_val, int):
writer("{}".format(json_val))
#if (json_val is None) or isinstance(json_val, bool) or isinstance(json_val, int):
# pass
else:
import sys
sys.exit("{}".format(type(json_val).__name__))
def writeFieldName(writer, field_prefix, opt_comma, name):
writer("{}{}\"{}\":".format(field_prefix, opt_comma, name))
def writeInlineFields(writer, fields, field_prefix, field_suffix, obj, separator=""):
for field in fields:
writeFieldName(writer, field_prefix, separator, field)
writeInline(writer, obj[field])
writer(field_suffix)
separator = ","
def writeInlineElements(writer, line_prefix, arr, separator=""):
for element in arr:
writer("\r\n{}{}".format(line_prefix, separator))
writeInline(writer, element)
separator = ","
def writeConstant(writer, prefix, constant):
writer("\t{}{{\r\n".format(prefix))
writeInlineFields(writer, ["Name","Type","ValueType","Value","Attrs"], "\t\t", "\r\n", constant)
writer("\t}\r\n")
def writeComMethod(writer, method_separator, method):
line_prefix = "\t\t\t"
writer("{}{}{{\r\n".format(line_prefix, method_separator))
child_prefix = line_prefix + "\t"
writeInlineFields(writer, ["Name", "SetLastError", "ReturnType", "Architectures", "Platform", "Attrs"], child_prefix, "\r\n", method)
writeFieldName(writer, child_prefix, ",", "Params")
writer("[")
writeInlineElements(writer, "\t\t\t\t\t", method["Params"])
writer("\r\n{}]\r\n".format(child_prefix))
writer("{}}}\r\n".format(line_prefix))
def writeType(writer, line_prefix, type_separator, type_obj):
writer("{}{}{{\r\n".format(line_prefix, type_separator))
writeInlineFields(writer, ["Name", "Architectures", "Platform", "Kind"], line_prefix + "\t", "\r\n", type_obj)
kind = type_obj["Kind"]
child_prefix = line_prefix + "\t"
if kind == "Enum":
writeInlineFields(writer, ["Flags", "Scoped"], child_prefix, "\r\n", type_obj, separator=",")
writeFieldName(writer, child_prefix, ",", "Values")
writer("[")
writeInlineElements(writer, child_prefix + "\t", type_obj["Values"])
writer("\r\n{}]\r\n".format(child_prefix))
writeInlineFields(writer, ["IntegerBase"], child_prefix, "\r\n", type_obj, separator=",")
elif kind == "Struct" or kind == "Union":
writeInlineFields(writer, ["Size", "PackingSize"], child_prefix, "\r\n", type_obj, separator=",")
writeFieldName(writer, child_prefix, ",", "Fields")
writer("[")
writeInlineElements(writer, child_prefix + "\t", type_obj["Fields"])
writer("\r\n{}]\r\n".format(child_prefix))
writeFieldName(writer, child_prefix, ",", "NestedTypes")
writer("[\r\n")
nested_separator = ""
for nested_type in type_obj["NestedTypes"]:
writeType(writer, child_prefix, nested_separator, nested_type)
nested_separator=","
writer("{}]\r\n".format(child_prefix))
elif kind == "Com":
writeInlineFields(writer, ["Guid", "Interface"], child_prefix, "\r\n", type_obj, separator=",")
writeFieldName(writer, child_prefix, ",", "Methods")
writer("[\r\n")
method_separator = ""
for method in type_obj["Methods"]:
writeComMethod(writer, method_separator, method)
method_separator=","
writer("{}]\r\n".format(child_prefix))
else:
import sys
sys.exit("Unhandled Kind '{}'".format(kind))
#writeInlineFields(writer, ["Name","Architectures", "Type","ValueType","Value","Attrs"], "\t\t", "\r\n", constant)
writer("{}}}\r\n".format(line_prefix))
def write(writer, api):
writer("{\r\n")
writer("\r\n")
writer("\"Constants\":[\r\n")
prefix = ""
for constant in api["Constants"]:
writeConstant(writer, prefix, constant)
prefix = ","
writer("]\r\n")
writer("\r\n")
writer(",\"Types\":[\r\n")
prefix = ""
for t in api["Types"]:
writeType(writer, "\t", prefix, t)
prefix = ","
writer("]\r\n")
writer("\r\n")
writer(",\"Functions\":[\r\n")
##
writer("]\r\n")
writer("\r\n")
writer(",\"UnicodeAliases\":[\r\n")
##
writer("]\r\n")
writer("\r\n")
writer("}\r\n")