File: hellomodulegen.py

package info (click to toggle)
pybindgen 0.20.0%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,932 kB
  • sloc: python: 15,981; cpp: 1,889; ansic: 617; makefile: 86; sh: 4
file content (64 lines) | stat: -rwxr-xr-x 1,987 bytes parent folder | download | duplicates (4)
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
#! /usr/bin/env python

import sys
import re

import pybindgen
from pybindgen import FileCodeSink


constructor_rx = re.compile("hello_foo_new(_.*)?")
method_rx = re.compile("hello_foo(_.*)?")

def pre_scan_hook(dummy_module_parser,
                  pygccxml_definition,
                  global_annotations,
                  parameter_annotations):
    if pygccxml_definition.name == "_HelloFoo":
        global_annotations['free_function'] = 'hello_foo_unref'
        global_annotations['incref_function'] = 'hello_foo_ref'
        global_annotations['decref_function'] = 'hello_foo_unref'
        global_annotations['custom_name'] = 'Foo'

    ## constructor?
    m = constructor_rx.match(pygccxml_definition.name)
    if m:
        global_annotations['is_constructor_of'] = 'HelloFoo'
        return

    ## method?
    m = method_rx.match(pygccxml_definition.name)
    if m:
        method_name = m.group(1)[1:]
        if method_name in ['ref', 'unref']:
            global_annotations['ignore'] = 'true'
            return
        global_annotations['as_method'] = m.group(1)[1:]
        global_annotations['of_class'] = 'HelloFoo'
        parameter_annotations['foo'] = {'transfer_ownership': 'false'}



def my_module_gen(out_file, pygccxml_mode):
    if pygccxml_mode == 'castxml':
        from pybindgen.castxmlparser import ModuleParser
    else:
        from pybindgen.gccxmlparser import ModuleParser

    out = FileCodeSink(out_file)
    #pybindgen.write_preamble(out)
    out.writeln("#include \"hello.h\"")
    module_parser = ModuleParser('hello')
    module_parser.add_pre_scan_hook(pre_scan_hook)
    module = module_parser.parse(sys.argv[2:])
    module.generate(out)


if __name__ == '__main__':
    try:
        import cProfile as profile
    except ImportError:
        my_module_gen(sys.stdout, sys.argv[1])
    else:
        sys.stderr.write("** running under profiler\n")
        profile.run('my_module_gen(sys.stdout, sys.argv[1])', 'hellomodulegen.pstat')