4
function_pattern = r'(?P<code>(unsigned[ ]+)?[a-zA-Z_]+[a-zA-Z0-9_]*\**)[ ]+(?P<fname>[a-zA-Z_]+[a-zA-Z0-9_]*)[ ]*\((?P<sig>[a-zA-Z0-9_,* ]*)\)'
5
sig_pattern = r'(?:[a-zA-Z_]+[a-zA-Z0-9_]*\**)[ ]+[ *]*(?P<param>[a-zA-Z_]+[a-zA-Z0-9_]*)'
7
fp = re.compile(function_pattern)
8
sp = re.compile(sig_pattern)
10
class BadLineError(Exception):
13
class UnknownCodeError(Exception):
16
class FunctionCruncher(object):
18
def load_line(self, line):
21
raise BadLineError("Line <<%s>> did not match regexp" % line)
22
self.code = m.group('code')
23
self.fname = m.group('fname')
24
self.sig = m.group('sig')
26
m = sp.findall(self.sig)
28
raise BadLineError("Signature for line <<%s>> did not match regexp" % line)
32
self.condition = "==NULL"
34
elif self.code in ('int', 'herr_t', 'htri_t', 'hid_t','hssize_t','ssize_t') \
35
or re.match(r'H5[A-Z]+_[a-zA-Z_]+_t',self.code):
38
elif self.code in ('unsigned int','haddr_t','hsize_t','size_t'):
39
self.condition = "==0"
42
raise UnknownCodeError("Return code <<%s>> unknown" % self.code)
44
def put_cython_signature(self):
46
return "cdef %s %s_p(%s) except? %s" % (self.code, self.fname,
47
self.sig, self.retval)
49
def put_cython_wrapper(self):
51
code_dict = {'code': self.code, 'fname': self.fname,
52
'sig': self.sig, 'args': ", ".join(self.sig_parts),
53
'condition': self.condition, 'retval': self.retval}
56
cdef %(code)s %(fname)s_p(%(sig)s) except? %(retval)s:
58
r = %(fname)s(%(args)s)
64
return code % code_dict
66
def put_cython_import(self):
68
return "%s %s(%s)" % (self.code, self.fname, self.sig)
74
if __name__ == '__main__':
76
fc = FunctionCruncher()
77
f = open('auto_functions.txt','r')
78
f_pxd = open('auto_defs.pxd','w')
79
f_pyx = open('auto_defs.pyx','w')
80
f_names = open('auto_names.txt','w')
82
f_pxd.write("# This file is auto-generated. Do not edit.\n\n")
83
f_pyx.write("# This file is auto-generated. Do not edit.\n\n")
85
defs = 'cdef extern from "hdf5.h":\n'
92
if not line or line.startswith('#'):
97
print "skipped <<%s>>" % line
99
defs += " "+fc.put_cython_import()+"\n"
100
sigs += fc.put_cython_signature()+"\n"
101
wrappers += fc.put_cython_wrapper()+"\n"
102
names += fc.put_name()+"\n"
107
f_pyx.write(wrappers)