File: generate_python_ctypes.py

package info (click to toggle)
allegro5 2%3A5.0.10-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 14,856 kB
  • ctags: 15,948
  • sloc: ansic: 87,540; cpp: 9,693; objc: 3,491; python: 2,057; sh: 829; makefile: 93; perl: 37; pascal: 24
file content (503 lines) | stat: -rwxr-xr-x 16,365 bytes parent folder | download
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/usr/bin/env python
import sys
import re
import optparse
from ctypes import *

"""
This script will use the prototypes from "checkdocs.py -s" to concoct
a 1:1 Python wrapper for Allegro.
"""


class _AL_UTF8String:
    pass


class Allegro:
    def __init__(self):
        self.types = {}
        self.functions = {}
        self.constants = {}

    def add_struct(self, name):
        x = type(name, (Structure, ), {})
        self.types[name] = x

    def add_union(self, name):
        x = type(name, (Union, ), {})
        self.types[name] = x

    def get_type(self, ptype):
        conversion = {
            "bool": c_bool,
            "_Bool": c_bool,
            "char": c_byte,
            "unsignedchar": c_ubyte,
            "int": c_int,
            "unsigned": c_uint,
            "unsignedint": c_uint,
            "int16_t": c_int16,
            "int32_t": c_int32,
            "uint32_t": c_uint32,
            "int64_t": c_int64,
            "uint64_t": c_uint64,
            "uintptr_t": c_void_p,
            "intptr_t": c_void_p,
            "GLuint": c_uint,
            "unsignedlong": c_ulong,
            "long": c_long,
            "size_t": c_size_t,
            "off_t": c_int64,
            "time_t": c_int64,
            "va_list": c_void_p,
            "float": c_float,
            "double": c_double,
            "al_fixed": c_int,
            "HWND": c_void_p,
            "char*": _AL_UTF8String,

            # hack: this probably shouldn't be in the public docs
            "postprocess_callback_t": c_void_p,
            }

        ptype = re.sub(r"\bconst\b", "", ptype)
        ptype = re.sub(r"\extern\b", "", ptype)
        ptype = re.sub(r"\__inline__\b", "", ptype)
        ptype = re.sub(r"\s+", "", ptype)

        if ptype.endswith("*"):
            if ptype in conversion:
                return conversion[ptype]
            t = ptype[:-1]
            if t in self.types:
                return POINTER(self.types[t])
            return c_void_p
        elif ptype in self.types:
            return self.types[ptype]
        else:
            try:
                return conversion[ptype]
            except KeyError:
                print("Error:" + str(ptype))
        return None

    def parse_funcs(self, funcs):
        """
        Go through all documented functions and add their prototypes
        as Python functions.

        The file should have been generated by Allegro's documentation
        generation scripts.
        """

        for func in funcs:
            name, proto = func.split(":", 1)
            if not name.startswith("al_"):
                continue
            proto = proto.strip()
            name = name[:-2]
            if proto.startswith("enum"):
                continue
            if proto.startswith("typedef"):
                continue
            if "=" in proto:
                continue
            if proto.startswith("#"):
                continue
            funcstart = proto.find(name)
            funcend = funcstart + len(name)
            ret = proto[:funcstart].rstrip()
            params = proto[funcend:].strip(" ;")
            if params[0] != "(" or params[-1] != ")":
                print("Error:")
                print(params)
                continue
            params2 = params[1:-1]
            # remove callback argument lists
            balance = 0
            params = ""
            for c in params2:
                if c == ")":
                    balance -= 1
                if balance == 0:
                    params += c
                if c == "(":
                    balance += 1
            params = params.split(",")
            plist = []
            for param in params:
                param = re.sub(r"\bconst\b", "", param)
                param = param.strip()
                if param == "void":
                    continue
                if param == "":
                    continue
                if param == "...":
                    continue

                # treat arrays as a void pointer, for now
                if param.endswith("]") or param.endswith("*"):
                    plist.append(c_void_p)
                    continue

                # treat callbacks as a void pointer, for now
                if param.endswith(")"):
                    plist.append(c_void_p)
                    continue

                mob = re.match("^.*?(\w+)$", param)
                if mob:
                    pnamepos = mob.start(1)
                    if pnamepos == 0:
                        # Seems the parameter is not named
                        pnamepos = len(param)
                else:
                    print(params)
                    print(proto)
                    print("")
                    continue
                ptype = param[:pnamepos]
                ptype = self.get_type(ptype)
                plist.append(ptype)

            f = type("", (object, ), {"restype": c_int})
            if not ret.endswith("void"):
                f.restype = self.get_type(ret)
            try:
                f.argtypes = plist
            except TypeError, e:
                print(e)
                print(name)
                print(plist)
            self.functions[name] = f

    def parse_protos(self, filename):
        protos = []
        unions = []
        funcs = []

        # first pass: create all structs, but without fields
        for line in open(filename):
            name, proto = line.split(":", 1)
            proto = proto.lstrip()
            if name.endswith("()"):
                funcs.append(line)
                continue
            # anonymous structs have no name at all
            if name and not name.startswith("ALLEGRO_"):
                continue
            if name == "ALLEGRO_OGL_EXT_API":
                continue
            if proto.startswith("union") or\
                proto.startswith("typedef union"):
                self.add_union(name)
                unions.append((name, proto))
            elif proto.startswith("struct") or\
                proto.startswith("typedef struct"):
                self.add_struct(name)
                protos.append((name, proto))
            elif proto.startswith("enum") or\
                proto.startswith("typedef enum"):
                if name:
                    self.types[name] = c_int
                protos.append(("", proto))
            elif proto.startswith("#define"):
                if not name.startswith("_") and not name.startswith("GL_"):
                    i = eval(proto.split(None, 2)[2])
                    self.constants[name] = i
            else:
                # actual typedef
                mob = re.match("typedef (.*) " + name, proto)
                if mob:
                    t = mob.group(1)
                    self.types[name] = self.get_type(t.strip())
                else:
                    # Probably a function pointer
                    self.types[name] = c_void_p

        # Unions must come last because they finalize the fields.
        protos += unions

        # second pass: fill in fields
        for name, proto in protos:
            bo = proto.find("{")
            if bo == -1:
                continue
            bc = proto.rfind("}")
            braces = proto[bo + 1:bc]

            if proto.startswith("enum") or \
                proto.startswith("typedef enum"):

                fields = braces.split(",")
                i = 0
                for field in fields:
                    if "=" in field:
                        fname, val = field.split("=", 1)
                        fname = fname.strip()
                        try:
                            i = int(eval(val, globals(), self.constants))
                        except NameError:
                            i = val
                    else:
                        fname = field.strip()
                    if not fname:
                        continue
                    self.constants[fname] = i
                    try:
                        i += 1
                    except TypeError:
                        pass
                continue

            balance = 0
            fields = [""]
            for c in braces:
                if c == "{":
                    balance += 1
                if c == "}":
                    balance -= 1
                if c == ";" and balance == 0:
                    fields.append("")
                else:
                    fields[-1] += c

            flist = []
            for field in fields:
                if not field:
                    continue

                # add function pointer as void pointer
                mob = re.match(".*?\(\*(\w+)\)", field)
                if mob:
                    flist.append((mob.group(1), "c_void_p"))
                    continue

                # add any pointer as void pointer
                mob = re.match(".*?\*(\w+)$", field)
                if mob:
                    flist.append((mob.group(1), "c_void_p"))
                    continue

                # add an array
                mob = re.match("(.*)( \w+)\[(.*?)\]$", field)
                if mob:
                    # this is all a hack
                    n = 0
                    ftype = mob.group(1)
                    if ftype.startswith("struct"):
                        if ftype == "struct {float axis[3];}":
                            t = "c_float * 3"
                        else:
                            print("Error: Can't parse " + ftype + " yet.")
                            t = None
                    else:
                        n = mob.group(3)
                        # something in A5 uses a 2d array
                        if "][" in n:
                            n = n.replace("][", " * ")
                        # something uses a division expression
                        if "/" in n:
                            n = "(" + n.replace("/", "//") + ")"
                        t = self.get_type(ftype).__name__ + " * " + n
                    fname = mob.group(2)
                    flist.append((fname, t))
                    continue

                vars = field.split(",")
                mob = re.match("\s*(.*?)\s+(\w+)\s*$", vars[0])
                t = self.get_type(mob.group(1))
                flist.append((mob.group(2), t.__name__))
                for v in vars[1:]:
                    flist.append((v.strip(), t.__name__))

            try:
                self.types[name].my_fields = flist
            except AttributeError:
                print(name, flist)

        self.parse_funcs(funcs)


def main():
    p = optparse.OptionParser()
    p.add_option("-o", "--output", help="location of generated file")
    p.add_option("-p", "--protos", help="A file with all " +
        "prototypes to generate Python wrappers for, one per line. "
        "Generate it with docs/scripts/checkdocs.py -p")
    p.add_option("-t", "--type", help="the library type to " +
        "use, e.g. debug")
    p.add_option("-v", "--version", help="the library version to " +
        "use, e.g. 5.1")
    options, args = p.parse_args()

    if not options.protos:
        p.print_help()
        return

    al = Allegro()

    al.parse_protos(options.protos)

    f = open(options.output, "w") if options.output else sys.stdout

    release = options.type
    version = options.version
    f.write(r"""# Generated by generate_python_ctypes.py.
import os, platform, sys
from ctypes import *
from ctypes.util import *

# You must adjust this function to point ctypes to the A5 DLLs you are
# distributing.
_dlls = []
def _add_dll(name):
    release = "%(release)s"
    if os.name == "nt":
        release = "%(release)s-%(version)s"

    # Under Windows, DLLs are found in the current directory, so this
    # would be an easy way to keep all your DLLs in a sub-folder.

    # os.chdir("dlls")

    path = find_library(name + release)
    if not path:
        if os.name == "mac":
            path = name + release + ".dylib"
        elif os.name == "nt":
            path = name + release + ".dll"
        elif os.name == "posix":
            if platform.mac_ver()[0]:
                path = name + release + ".dylib"
            else:
                path = "lib" + name + release + ".so"
        else:
            sys.stderr.write("Cannot find library " + name + "\n")

        # In most cases, you actually don't want the above and instead
        # use the exact filename within your game distribution, possibly
        # even within a .zip file.
        # if not os.path.exists(path):
        #     path = "dlls/" + path

    try:
        # RTLD_GLOBAL is required under OSX for some reason (?)
        _dlls.append(CDLL(path, RTLD_GLOBAL))
    except OSError:
        # No need to fail here, might just be one of the addons.
        pass

    # os.chdir("..")

_add_dll("allegro")
_add_dll("allegro_acodec")
_add_dll("allegro_audio")
_add_dll("allegro_primitives")
_add_dll("allegro_color")
_add_dll("allegro_font")
_add_dll("allegro_ttf")
_add_dll("allegro_image")
_add_dll("allegro_dialog")
_add_dll("allegro_memfile")
_add_dll("allegro_physfs")
_add_dll("allegro_main")

# We don't have information ready which A5 function is in which DLL,
# so we just try them all.
def _dll(func, ret, params):
    for dll in _dlls:
        try:
            f = dll[func]
            f.restype = ret
            f.argtypes = params
            return f
        except AttributeError: pass
    sys.stderr.write("Cannot find function " + func + "\n")
    return lambda *args: None

# In Python3, all Python strings are unicode so we have to convert to
# UTF8 byte strings before passing to Allegro.
if sys.version_info[0] > 2:
    class _AL_UTF8String:
        def from_param(x):
            return x.encode("utf8")
else:
    _AL_UTF8String = c_char_p

""" % locals())

    for name, val in sorted(al.constants.items()):
        if isinstance(val, str):
            val = int(eval(val, globals(), al.constants))
        f.write(name + " = " + str(val) + "\n")

    for name, x in sorted(al.types.items()):
        if not name:
            continue
        base = x.__bases__[0]
        if base != Structure and base != Union:
            f.write(name + " = " + x.__name__ + "\n")

    for kind in Structure, Union:
        for name, x in sorted(al.types.items()):
            if not x:
                continue
            base = x.__bases__[0]
            if base != kind:
                continue
            f.write("class " + name + "(" + base.__name__ + "): pass\n")
            pt = POINTER(x)
            f.write("%s = POINTER(%s)\n" % (pt.__name__, name))

        for name, x in sorted(al.types.items()):
            base = x.__bases__[0]
            if base != kind:
                continue
            if hasattr(x, "my_fields"):
                f.write(name + "._fields_ = [\n")
                for fname, ftype in x.my_fields:
                    f.write("    (\"" + fname + "\", " + ftype + "),\n")
                f.write("    ]\n")

    for name, x in sorted(al.functions.items()):
        try:
            line = name + " = _dll(\"" + name + "\", "
            line += x.restype.__name__ + ", "
            line += "[" + (", ".join([a.__name__ for a in x.argtypes])) +\
                "])\n"
            f.write(line)
        except AttributeError as e:
            print("Ignoring " + name + " because of errors (" + str(e) + ").")

    # some stuff the automated parser doesn't pick up
    f.write(r"""
ALLEGRO_VERSION_INT = \
    ((ALLEGRO_VERSION << 24) | (ALLEGRO_SUB_VERSION << 16) | \
    (ALLEGRO_WIP_VERSION << 8) | ALLEGRO_RELEASE_NUMBER)
    """)

    f.write(r"""
# work around bug http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36834
if os.name == "nt":
    def al_map_rgba_f(r, g, b, a): return ALLEGRO_COLOR(r, g, b, a)
    def al_map_rgb_f(r, g, b): return ALLEGRO_COLOR(r, g, b, 1)
    def al_map_rgba(r, g, b, a):
        return ALLEGRO_COLOR(r / 255.0, g / 255.0, b / 255.0, a / 255.0)
    def al_map_rgb(r, g, b):
        return ALLEGRO_COLOR(r / 255.0, g / 255.0, b / 255.0, 1)
    """)

    f.write("""
def al_main(real_main, *args):
    def python_callback(argc, argv):
        real_main(*args)
        return 0
    cb = CFUNCTYPE(c_int, c_int, c_void_p)(python_callback)
    al_run_main(0, 0, cb);
""")

    f.close()

main()