~ubuntu-branches/ubuntu/utopic/h5py/utopic

« back to all changes in this revision

Viewing changes to h5py/auto_api.py

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2011-01-12 14:48:19 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110112144819-9kbgh192yngf6o05
Tags: 1.3.1-1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
import re
 
3
 
 
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_]*)'
 
6
 
 
7
fp = re.compile(function_pattern)
 
8
sp = re.compile(sig_pattern)
 
9
 
 
10
class BadLineError(Exception):
 
11
    pass
 
12
 
 
13
class UnknownCodeError(Exception):
 
14
    pass
 
15
 
 
16
class FunctionCruncher(object):
 
17
 
 
18
    def load_line(self, line):
 
19
        m = fp.match(line)
 
20
        if m is None:
 
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')
 
25
        
 
26
        m = sp.findall(self.sig)
 
27
        if m is None:
 
28
            raise BadLineError("Signature for line <<%s>> did not match regexp" % line)
 
29
        self.sig_parts = m
 
30
 
 
31
        if '*' in self.code:
 
32
            self.condition = "==NULL"
 
33
            self.retval = "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):
 
36
            self.condition = "<0"
 
37
            self.retval = "-1"
 
38
        elif self.code in ('unsigned int','haddr_t','hsize_t','size_t'):
 
39
            self.condition = "==0"
 
40
            self.retval = 0
 
41
        else:
 
42
            raise UnknownCodeError("Return code <<%s>> unknown" % self.code)
 
43
 
 
44
    def put_cython_signature(self):
 
45
        
 
46
        return "cdef %s %s_p(%s) except? %s" % (self.code, self.fname,
 
47
                                              self.sig, self.retval)
 
48
 
 
49
    def put_cython_wrapper(self):
 
50
 
 
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}
 
54
 
 
55
        code = """\
 
56
cdef %(code)s %(fname)s_p(%(sig)s) except? %(retval)s:
 
57
    cdef %(code)s r;
 
58
    r = %(fname)s(%(args)s)
 
59
    if r%(condition)s:
 
60
        if set_exception():
 
61
            return %(retval)s;
 
62
    return r
 
63
"""
 
64
        return code % code_dict
 
65
 
 
66
    def put_cython_import(self):
 
67
 
 
68
        return "%s %s(%s)" % (self.code, self.fname, self.sig)
 
69
 
 
70
    def put_name(self):
 
71
        
 
72
        return self.fname
 
73
 
 
74
if __name__ == '__main__':
 
75
 
 
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')
 
81
 
 
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")
 
84
 
 
85
    defs = 'cdef extern from "hdf5.h":\n'
 
86
    sigs = ""
 
87
    wrappers = ""
 
88
    names = ""
 
89
 
 
90
    for line in f:
 
91
        line = line.strip()
 
92
        if not line or line.startswith('#'):
 
93
            continue
 
94
        try:
 
95
            fc.load_line(line)
 
96
        except BadLineError:
 
97
            print "skipped <<%s>>" % line
 
98
            continue
 
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"
 
103
 
 
104
    f_pxd.write(defs)
 
105
    f_pxd.write("\n\n")
 
106
    f_pxd.write(sigs)
 
107
    f_pyx.write(wrappers)
 
108
    f_names.write(names)
 
109
 
 
110
    f_pxd.close()
 
111
    f_pyx.close()
 
112
    f_names.close()
 
113
 
 
114
 
 
115