bindings/python/ns3modulegen.py
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Mon, 18 Aug 2008 16:00:47 +0100
changeset 3538 99f49426595a
parent 3473 6bce86ea4778
child 3546 cecda7126440
permissions -rw-r--r--
Python: fix bad parameter passed into pybindgen.
gjc@3408
     1
gjc@3408
     2
LOCAL_MODULES = [
gjc@3408
     3
    #'my_extra_api_definitions',
gjc@3408
     4
    ]
gjc@3408
     5
gjc@3408
     6
gjc@3408
     7
gjc@3408
     8
import sys
gjc@3408
     9
import os
gjc@3408
    10
gjc@3408
    11
from pybindgen import FileCodeSink, write_preamble
gjc@3408
    12
from pybindgen.module import MultiSectionFactory
gjc@3408
    13
import pybindgen.settings
gjc@3408
    14
gjc@3473
    15
from ns3modulegen_generated import module_init, register_types, register_methods, register_functions
gjc@3408
    16
import ns3modulegen_core_customizations
gjc@3408
    17
import callbacks_list
gjc@3408
    18
gjc@3408
    19
this_script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
gjc@3408
    20
gjc@3408
    21
class ErrorHandler(pybindgen.settings.ErrorHandler):
gjc@3408
    22
    def handle_error(self, wrapper, exception, traceback_):
gjc@3408
    23
        try:
gjc@3408
    24
            stack = wrapper.stack_where_defined
gjc@3408
    25
        except AttributeError:
gjc@3408
    26
            print >> sys.stderr, "??:??: %s / %r" % (wrapper, exception)
gjc@3408
    27
        else:
gjc@3408
    28
            stack = list(stack)
gjc@3408
    29
            stack.reverse()
gjc@3408
    30
            for (filename, line_number, function_name, text) in stack:
gjc@3408
    31
                file_dir = os.path.dirname(os.path.abspath(filename))
gjc@3408
    32
                if file_dir == this_script_dir:
gjc@3408
    33
                    print >> sys.stderr, "%s:%i: %r" % (os.path.join("..", "bindings", "python", os.path.basename(filename)),
gjc@3408
    34
                                                        line_number, exception)
gjc@3408
    35
                    break
gjc@3408
    36
        return True
gjc@3408
    37
pybindgen.settings.error_handler = ErrorHandler()
gjc@3408
    38
gjc@3408
    39
gjc@3408
    40
class MyMultiSectionFactory(MultiSectionFactory):
gjc@3408
    41
gjc@3408
    42
    def __init__(self, main_file_name, modules):
gjc@3408
    43
        super(MyMultiSectionFactory, self).__init__()
gjc@3408
    44
        self.main_file_name = main_file_name
gjc@3408
    45
        self.main_sink = FileCodeSink(open(main_file_name, "wt"))
gjc@3408
    46
        self.header_name = "ns3module.h"
gjc@3408
    47
        header_file_name = os.path.join(os.path.dirname(self.main_file_name), self.header_name)
gjc@3408
    48
        self.header_sink = FileCodeSink(open(header_file_name, "wt"))
gjc@3538
    49
        self.section_sinks = {'__main__': self.main_sink}
gjc@3408
    50
gjc@3408
    51
        for module in modules:
gjc@3408
    52
            section_name = 'ns3_module_%s' % module.replace('-', '_')
gjc@3408
    53
            file_name = os.path.join(os.path.dirname(self.main_file_name), "%s.cc" % section_name)
gjc@3408
    54
            sink = FileCodeSink(open(file_name, "wt"))
gjc@3408
    55
            self.section_sinks[section_name] = sink            
gjc@3408
    56
gjc@3408
    57
    def get_section_code_sink(self, section_name):
gjc@3408
    58
        return self.section_sinks[section_name]
gjc@3408
    59
gjc@3408
    60
    def get_main_code_sink(self):
gjc@3408
    61
        return self.main_sink
gjc@3408
    62
gjc@3408
    63
    def get_common_header_code_sink(self):
gjc@3408
    64
        return self.header_sink
gjc@3408
    65
gjc@3408
    66
    def get_common_header_include(self):
gjc@3408
    67
        return '"%s"' % self.header_name
gjc@3408
    68
gjc@3408
    69
    def close(self):
gjc@3408
    70
        self.header_sink.file.close()
gjc@3408
    71
        self.main_sink.file.close()
gjc@3408
    72
        for sink in self.section_sinks.itervalues():
gjc@3408
    73
            sink.file.close()
gjc@3408
    74
gjc@3408
    75
gjc@3408
    76
gjc@3408
    77
def main():
gjc@3408
    78
    out = MyMultiSectionFactory(sys.argv[1], sys.argv[2:])
gjc@3408
    79
    root_module = module_init()
gjc@3409
    80
    root_module.add_include('"everything.h"')
gjc@3408
    81
gjc@3408
    82
    register_types(root_module)
gjc@3408
    83
gjc@3408
    84
    ns3modulegen_core_customizations.Simulator_customizations(root_module)
gjc@3408
    85
    ns3modulegen_core_customizations.CommandLine_customizations(root_module)
gjc@3408
    86
gjc@3408
    87
gjc@3408
    88
    for local_module in LOCAL_MODULES:
gjc@3408
    89
        mod = __import__(local_module)
gjc@3408
    90
        mod.register_types(root_module)
gjc@3408
    91
gjc@3408
    92
    ns3modulegen_core_customizations.generate_callback_classes(root_module.after_forward_declarations,
gjc@3408
    93
                                                               callbacks_list.callback_classes)
gjc@3408
    94
gjc@3408
    95
gjc@3408
    96
    register_methods(root_module)
gjc@3408
    97
gjc@3408
    98
    for local_module in LOCAL_MODULES:
gjc@3408
    99
        mod = __import__(local_module)
gjc@3408
   100
        mod.register_methods(root_module)
gjc@3408
   101
gjc@3408
   102
    ns3modulegen_core_customizations.Object_customizations(root_module)
gjc@3421
   103
    ns3modulegen_core_customizations.Attribute_customizations(root_module)
gjc@3408
   104
gjc@3408
   105
    register_functions(root_module)
gjc@3408
   106
gjc@3408
   107
    for local_module in LOCAL_MODULES:
gjc@3408
   108
        mod = __import__(local_module)
gjc@3408
   109
        mod.register_functions(root_module)
gjc@3408
   110
gjc@3420
   111
gjc@3420
   112
    # if GtkConfigStore support is disabled, disable the class wrapper
gjc@3420
   113
    if 'DISABLE_GTK_CONFIG_STORE' in os.environ:
gjc@3420
   114
        root_module.classes.remove(root_module['ns3::GtkConfigStore'])
gjc@3420
   115
gjc@3408
   116
    root_module.generate(out, '_ns3')
gjc@3408
   117
gjc@3408
   118
    out.close()
gjc@3408
   119
gjc@3408
   120
if __name__ == '__main__':
gjc@3408
   121
    main()
gjc@3408
   122