-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmakedfromi.py
175 lines (161 loc) · 5.77 KB
/
makedfromi.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import sys
import string
import os
import getopt
# Run this passing a ".i" file as param. Will generate ".d"
g_com_parent = ""
def GetComments(line, lineNo, lines):
# Get the comment from this and continuous lines, if they exist.
data = line.split("//", 2)
doc = ""
if len(data) == 2:
doc = data[1].strip()
lineNo = lineNo + 1
while lineNo < len(lines):
line = lines[lineNo]
data = line.split("//", 2)
if len(data) != 2:
break
if data[0].strip():
break # Not a continutation!
if data[1].strip().startswith("@"):
# new command
break
doc = doc + "\n// " + data[1].strip()
lineNo = lineNo + 1
# This line doesnt match - step back
lineNo = lineNo - 1
return doc, lineNo
def make_doc_summary(inFile, outFile):
methods = []
modDoc = ""
modName = ""
lines = inFile.readlines()
curMethod = None
constants = []
extra_tags = []
lineNo = 0
bInRawBlock = 0
while lineNo < len(lines):
line = lines[lineNo]
if bInRawBlock and len(line) > 2 and line[:2] == "%}":
bInRawBlock = 0
if not bInRawBlock and len(line) > 2 and line[:2] == "%{":
bInRawBlock = 1
try:
if line[:7] == "%module":
extra = line.split("//")
if len(extra) > 1:
modName = extra[0][7:].strip()
modDoc, lineNo = GetComments(line, lineNo, lines)
lineNo += 1
elif line[:7] == "#define" and not bInRawBlock:
cname = line.split()[1]
doc, lineNo = GetComments(line, lineNo, lines)
constants.append((cname, doc))
else:
try:
pos = line.index("//")
except ValueError:
pass
else:
rest = line[pos + 2:].strip()
if rest.startswith("@pymeth"):
# manual markup - reset the current method.
curMethod = None
if rest.startswith("@doc"):
pass
elif rest.startswith("@pyswig"):
doc, lineNo = GetComments(line, lineNo, lines)
curMethod = doc[8:], []
methods.append(curMethod)
elif rest.startswith("@const"):
doc, lineNo = GetComments(line, lineNo, lines)
else:
if rest.startswith("@"):
doc, lineNo = GetComments(line, lineNo, lines)
if curMethod:
curMethod[1].append("// " + doc + '\n')
else:
extra_tags.append("// " + doc + '\n')
except:
_, msg, _ = sys.exc_info()
print(("Line %d is badly formed - %s" % (lineNo, msg)))
lineNo = lineNo + 1
# autoduck seems to crash when > ~97 methods. Loop multiple times,
# creating a synthetic module name when this happens.
# Hrmph - maybe this was related to the way we generate -
# see rev 1.80 of win32gui.i for a change that prevents this!
max_methods = 999
method_num = 0
chunk_number = 0
while True:
these_methods = methods[method_num:method_num + max_methods]
if not these_methods:
break
thisModName = modName
if g_com_parent:
thisModName = "Py" + modName
if chunk_number == 0:
pass
elif chunk_number == 1:
thisModName = thisModName + " (more)"
else:
thisModName = thisModName + " (more %d)" % (chunk_number + 1,)
outFile.write("\n")
for (meth, extras) in these_methods:
fields = meth.split('|')
if len(fields) != 3:
print(("**Error - %s does not have enough fields" % meth))
else:
outFile.write(
"// @pymethod %s|%s|%s|%s\n" %
(fields[0], thisModName, fields[1], fields[2]))
for extra in extras:
outFile.write(extra)
if g_com_parent:
outFile.write("\n// @object %s|%s" % (thisModName, modDoc))
outFile.write("\n// <nl>Derived from <o %s>\n" % (g_com_parent))
else:
outFile.write("\n// @module %s|%s\n" % (thisModName, modDoc))
for (meth, extras) in these_methods:
fields = meth.split('|')
outFile.write("// @pymeth %s|%s\n" % (fields[1], fields[2]))
chunk_number += 1
method_num += max_methods
outFile.write("\n")
for extra in extra_tags:
outFile.write("%s\n" % (extra))
for (cname, doc) in constants:
outFile.write("// @const %s|%s|%s\n" % (modName, cname, doc))
def doit():
global g_com_parent
outName = ""
try:
opts, args = getopt.getopt(sys.argv[1:], 'p:o:')
for o, a in opts:
if o == '-p':
g_com_parent = a
elif o == '-o':
outName = a
msg = ' '.join(args)
except getopt.error:
_, msg, _ = sys.exc_info()
print(msg)
print((
"Usage: %s [-o output_name] [-p com_parent] filename" %
sys.argv[0]))
return
inName = args[0]
if not outName:
outName = os.path.splitext(os.path.split(inName)[1])[0] + ".d"
inFile = open(inName)
outFile = open(outName, "w")
outFile.write(
"// @doc\n// Generated file - built from %s\n// DO NOT CHANGE - CHANGES WILL BE LOST!\n\n" %
inName)
make_doc_summary(inFile, outFile)
inFile.close()
outFile.close()
if __name__ == '__main__':
doit()