| Line | |
|---|
| 1 | |
|---|
| 2 | |
|---|
| 3 | import sys |
|---|
| 4 | |
|---|
| 5 | def main(progname, args): |
|---|
| 6 | if len(args) != 2: |
|---|
| 7 | sys.stderr.write("Usage: %s CONSTANT-LIST PXI-OUTPUT\n") |
|---|
| 8 | sys.exit(2) |
|---|
| 9 | (constants_path, pxi_path) = args |
|---|
| 10 | make_constants_pxi(constants_path, pxi_path) |
|---|
| 11 | |
|---|
| 12 | def make_constants_pxi(constants_path, pxi_path): |
|---|
| 13 | constants = [] |
|---|
| 14 | for line in open(constants_path): |
|---|
| 15 | data = line.split("#", 1)[0].strip() |
|---|
| 16 | |
|---|
| 17 | if not data: |
|---|
| 18 | continue |
|---|
| 19 | |
|---|
| 20 | elif len(data.split()) == 2: |
|---|
| 21 | (pyname, cname) = data.split() |
|---|
| 22 | constants.append((pyname, cname)) |
|---|
| 23 | |
|---|
| 24 | else: |
|---|
| 25 | constants.append(data) |
|---|
| 26 | out = open(pxi_path, "w") |
|---|
| 27 | out.write("cdef extern from *:\n") |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | for const in constants: |
|---|
| 36 | if isinstance(const, tuple): |
|---|
| 37 | out.write(' unsigned int %s %s\n' % const) |
|---|
| 38 | else: |
|---|
| 39 | out.write(' unsigned int %s\n' % (const,)) |
|---|
| 40 | |
|---|
| 41 | out.write("const = {\n") |
|---|
| 42 | for const in constants: |
|---|
| 43 | if isinstance(const, tuple): |
|---|
| 44 | pyname = const[0] |
|---|
| 45 | else: |
|---|
| 46 | pyname = const |
|---|
| 47 | out.write(' "%s": %s,\n' % (pyname, pyname)) |
|---|
| 48 | out.write("}\n") |
|---|
| 49 | |
|---|
| 50 | if __name__ == "__main__": |
|---|
| 51 | main(sys.argv[0], sys.argv[1:]) |
|---|