This repository has been archived by the owner on Sep 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
gen_wrap.py
executable file
·417 lines (371 loc) · 14.8 KB
/
gen_wrap.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#!/usr/bin/env python3
import sys
import os
import re
import configparser
import codecs
from nivision_parse import *
class CtypesEmitter:
def __init__(self, srcdir, outdir, config):
self.srcdir = srcdir
self.outdir = outdir
self.config = config
self.out = open(os.path.join(self.outdir, "core.py"), "wt")
for line in open(os.path.join(self.srcdir, "ctypes_core_prefix.py")):
print(line, end='', file=self.out)
self.block_comment("Opaque Structures")
for name in sorted(opaque_structs):
self.opaque_struct(name)
def finish(self):
for line in open(os.path.join(self.srcdir, "ctypes_core_suffix.py")):
print(line, end='', file=self.out)
def block_comment(self, comment):
print("#"*78, file=self.out)
print("# %s" % comment, file=self.out)
print("#"*78, file=self.out)
def opaque_struct(self, name):
print("class %s(Disposed): pass" % name, file=self.out)
def define(self, name, value, comment):
if self.config.getboolean(name, "exclude", fallback=False):
return
if name in opaque_structs:
return
clean = None
after_struct = False
if value == "TRUE":
clean = "True"
elif value == "FALSE":
clean = "False"
elif name.startswith("IMAQ_INIT_RGB") and value[0] == '{' and value[-1] == '}':
clean = "RGBValue(%s)" % ' '.join(value[1:-1].strip().split())
after_struct = True
elif value.startswith("imaqMake"):
clean = value[8:]
after_struct = True
elif value[0] == '"':
clean = 'b'+value
elif number_re.match(value):
clean = value
elif constant_re.match(value):
clean = value
after_struct = value not in defined
if clean is None:
print("Invalid #define: %s" % name)
return
if after_struct:
define_after_struct.append((name, "%s = %s" % (name, clean)))
return
print("%s = %s" % (name, clean), file=self.out)
defined.add(name)
def text(self, text):
print(text, file=self.out)
def static_const(self, name, ctype, value):
if hasattr(value, "__iter__"):
print("%s = %s(%s)" % (name, ctype, ", ".join(value)), file=self.out)
else:
print("%s = %s" % (name, value), file=self.out)
defined.add(name)
def enum(self, name, values):
if self.config.getboolean(name, "exclude", fallback=False):
return
if name in opaque_structs:
return
print("class %s(Enumeration): pass" % name, file=self.out)
prev_value = -1
for vname, value, comment in values:
if vname.endswith("SIZE_GUARD") or vname.endswith("Guard"):
continue
if value is None:
# auto-increment
value = "%d" % (prev_value + 1)
print("%s = %s(%s)" % (vname, name, value), file=self.out)
defined.add(vname)
prev_value = int(value, 0)
print("", file=self.out)
defined.add(name)
enums.add(name)
ctypes_map = {
"int": "c_int",
"char": "c_char",
"wchar_t": "c_wchar",
"unsigned char": "c_ubyte",
"short": "c_short",
"short int": "c_short",
"unsigned short": "c_ushort",
"unsigned short int": "c_ushort",
"int": "c_int",
"unsigned": "c_uint",
"unsigned int": "c_uint",
"long": "c_long",
"unsigned long": "c_ulong",
"__int64": "c_longlong",
"long long": "c_longlong",
"long long int": "c_longlong",
"unsigned __int64": "c_ulonglong",
"__uint64": "c_ulonglong",
"unsigned long long": "c_ulonglong",
"float": "c_float",
"double": "c_double",
"long double": "c_longdouble",
"char *": "c_char_p",
"char*": "c_char_p",
"wchar_t *": "c_wchar_p",
"wchar_t*": "c_wchar_p",
"void *": "c_void_p",
"void*": "c_void_p",
"size_t": "c_size_t",
"uInt8": "c_uint8",
"uInt16": "c_uint16",
"uInt32": "c_uint32",
"uInt64": "c_uint64",
"Int8": "c_int8",
"Int16": "c_int16",
"Int32": "c_int32",
"Int64": "c_int64",
"float32": "c_float",
"float64": "c_double",
}
def c_to_ctype(self, name, arr):
#print(name, arr)
if arr:
arr = '*'+arr
if name.startswith("const"):
name = name[5:].strip()
ctype = self.ctypes_map.get(name, None)
if ctype is not None:
return "ctypes." + ctype + (arr or "")
if name == "void":
return "None"
# Opaque structures are treated specially
if name[:-1] in opaque_structs:
name = name[:-1]
# handle pointers recursively
if name[-1] == '*':
name = "ctypes.POINTER(%s)" % self.c_to_ctype(name[:-1], None)
if arr == "":
return "ctypes.POINTER(%s)" % self.c_to_ctype(name, None)
return name + (arr or "")
def typedef(self, name, typedef, arr):
if self.config.getboolean(name, "exclude", fallback=False):
return
if name in opaque_structs:
return
if typedef.startswith("struct"):
print("class %s(ctypes.Structure): pass" % name, file=self.out)
elif typedef.startswith("union"):
print("class %s(ctypes.Union): pass" % name, file=self.out)
elif name in self.ctypes_map:
return
else:
print("%s = %s" % (name, self.c_to_ctype(typedef, arr)),
file=self.out)
defined.add(name)
def typedef_function(self, name, restype, params):
if self.config.getboolean(name, "exclude", fallback=False):
return
if name in opaque_structs:
return
paramstr = ""
if params:
paramstr = ", ".join(self.c_to_ctype(ctype, arr) for name, ctype, arr in params if name != 'void')
if paramstr:
paramstr = ", " + paramstr
print("%s = ctypes.CFUNCTYPE(%s%s)" %
(name, self.c_to_ctype(restype, None), paramstr), file=self.out)
defined.add(name)
def function(self, name, restype, params):
if self.config.getboolean(name, "exclude", fallback=False):
return
if name in opaque_structs:
return
try:
config = self.config[name]
except KeyError:
config = self.config['DEFAULT']
underscored = config.getboolean("underscored", fallback=False)
# common return cases
retpointer = config.getboolean("rvdisposed", fallback=False)
funcargs = ['"%s"' % name]
if restype == "int":
functype = "STDFUNC"
elif restype == "IMAQdxError":
functype = "DXFUNC"
else:
if restype[-1] == "*":
functype = "STDPTRFUNC"
else:
functype = "RETFUNC"
ctype = self.c_to_ctype(restype, None)
if "POINTER" in ctype:
retpointer = True
funcargs.append(ctype)
custom = False # generate a custom wrapper function?
sized_params = dict(tuple(y.strip() for y in x.split(':')) for x in
config.get("arraysize", "").split(',') if x)
size_params = set(sized_params.values())
retarraysize = config.get("retarraysize", "").strip()
if retarraysize:
size_params.add(retarraysize)
if size_params:
custom = True
if retpointer:
custom = True
config_outparams = \
set(x.strip() for x in config.get("outparams", "").split(','))
outparams = []
paramtypes = {}
if params:
defaults = dict((y.strip() for y in x.split(':')) for x in
config.get("defaults", "").split(',') if x)
inparams = set(x.strip() for x in
config.get("inparams", "").split(','))
for pname, ptype, arr in params:
if pname == "void":
continue
ctype = self.c_to_ctype(ptype, arr)
#if name == "IMAQdxEnumerateCameras":
# print(pname,ptype,arr,ctype)
paramtypes[pname] = (ptype, arr)
if pname in defaults:
paramstr = '("%s", %s, %s)' % (pname, ctype, defaults[pname])
else:
paramstr = '("%s", %s)' % (pname, ctype)
funcargs.append(paramstr)
# try to guess output parameters
if pname in config_outparams or (pname not in inparams
and not underscored
and not ptype.startswith("const")
and ptype[:-1] not in forward_structs
and ptype[-1] == "*"):
if functype not in ("STDFUNC", "DXFUNC") or ptype[:-1] in enums:
custom = True
outparams.append(pname)
if not custom and outparams:
funcargs.append("out=[" + ", ".join('"%s"' % x for x in outparams) + "]")
try:
library = self.config["_platform_"]["library"]
except KeyError:
library = "_dll"
if library != "_dll":
funcargs.append("library=%s" % library)
print('%s%s = %s(%s)' %
("_" if (underscored or custom) else "", name,
functype, ", ".join(funcargs)), file=self.out)
if custom and not underscored:
# generate list of input parameters
inparams = []
for pname, ptype, arr in params:
if pname in outparams:
continue
if pname in size_params:
continue
inparams.append(pname)
# function definition
print("def %s(%s):" % (name, ", ".join(inparams)), file=self.out)
retparams = []
# array input parameters
for pname, sizepname in sized_params.items():
# get ctype of parameter
ptype = paramtypes[pname][0][:-1]
arr = paramtypes[pname][1]
ctype = self.c_to_ctype(ptype, arr)
print(" %s, %s = iterableToArray(%s, %s)" % (pname, sizepname, pname, ctype), file=self.out)
# placeholders for output parameters
for pname in outparams:
if pname in sized_params:
continue # array input parameter
# get ctype of parameter
ptype = paramtypes[pname][0][:-1]
arr = paramtypes[pname][1]
ctype = self.c_to_ctype(ptype, arr)
if ptype in enums:
init = "0"
else:
init = ""
print(" %s = %s(%s)" % (pname, ctype, init), file=self.out)
if ptype in enums:
retparams.append(pname)
else:
retparams.append("%s.value" % pname)
# arguments to ctypes function
callargs = []
for pname, ptype, arr in params:
if pname in outparams and pname not in sized_params:
callargs.append("ctypes.byref(%s)" % pname)
else:
callargs.append(pname)
# call ctypes function
if functype in ("STDFUNC", "DXFUNC"):
print(" _%s(%s)" % (name, ", ".join(callargs)), file=self.out)
if retparams:
print(" return %s" % ", ".join(retparams), file=self.out)
else:
print(" rv = _%s(%s)" % (name, ", ".join(callargs)), file=self.out)
# Map return value
retval = None
if retarraysize:
retval = "DisposedArray(rv, %s.value)" % retarraysize
try:
retparams.remove("%s.value" % retarraysize)
except ValueError:
print("%s: could not find %s size return value"
% retarraysize)
pass
if retval is None:
if retpointer:
if config.get("rvdisposed", None) == False:
retval = "rv.contents"
else:
retval = "DisposedPointer(rv)"
else:
retval = "rv"
print(" return %s" % ", ".join([retval]+retparams),
file=self.out)
defined.add(name)
def structunion(self, ctype, name, fields):
if self.config.getboolean(name, "exclude", fallback=False):
return
if name in opaque_structs:
return
if name not in defined:
print("class %s(ctypes.%s): pass" % (name, ctype), file=self.out)
defined.add(name)
print("%s._fields_ = [" % name, file=self.out)
for fname, ftype, arr, comment in fields:
print(' ("%s", %s),' % (fname, self.c_to_ctype(ftype, arr)),
file=self.out)
print(" ]", file=self.out)
def struct(self, name, fields):
self.structunion("Structure", name, fields)
def union(self, name, fields):
self.structunion("Union", name, fields)
def generate(srcdir, outdir, inputs):
emit = None
for fname, configpath in inputs:
# read config file
config = configparser.ConfigParser()
config.read(configpath)
block_comment_exclude = set(x.strip() for x in
config["Block Comment"]["exclude"].splitlines())
# open input file
with codecs.open(fname, "r", "iso-8859-1") as inf:
# prescan for undefined structurs
prescan_file(inf)
inf.seek(0)
if emit is None:
emit = CtypesEmitter(srcdir, outdir, config)
else:
emit.config = config
# generate
parse_file(emit, inf, block_comment_exclude)
emit.finish()
if __name__ == "__main__":
if len(sys.argv) < 3 or ((len(sys.argv)-1) % 2) != 0:
print("Usage: gen_wrap.py <header.h config.ini>...")
exit(0)
inputs = []
for i in range(1, len(sys.argv), 2):
fname = sys.argv[i]
configname = sys.argv[i+1]
inputs.append((fname, configname))
generate("", "nivision", inputs)