bindings/python/ns3modulegen.py
changeset 3408 2cc40b3e4fa5
child 3409 94ac3e381075
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/bindings/python/ns3modulegen.py	Tue Jul 08 10:43:58 2008 -0700
     1.3 @@ -0,0 +1,116 @@
     1.4 +
     1.5 +LOCAL_MODULES = [
     1.6 +    #'my_extra_api_definitions',
     1.7 +    ]
     1.8 +
     1.9 +
    1.10 +
    1.11 +import sys
    1.12 +import os
    1.13 +
    1.14 +from pybindgen import FileCodeSink, write_preamble
    1.15 +from pybindgen.module import MultiSectionFactory
    1.16 +import pybindgen.settings
    1.17 +
    1.18 +from ns3modulegen_generated import (module_init, register_types, register_methods, register_functions)
    1.19 +import ns3modulegen_core_customizations
    1.20 +import callbacks_list
    1.21 +
    1.22 +this_script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
    1.23 +
    1.24 +class ErrorHandler(pybindgen.settings.ErrorHandler):
    1.25 +    def handle_error(self, wrapper, exception, traceback_):
    1.26 +        try:
    1.27 +            stack = wrapper.stack_where_defined
    1.28 +        except AttributeError:
    1.29 +            print >> sys.stderr, "??:??: %s / %r" % (wrapper, exception)
    1.30 +        else:
    1.31 +            stack = list(stack)
    1.32 +            stack.reverse()
    1.33 +            for (filename, line_number, function_name, text) in stack:
    1.34 +                file_dir = os.path.dirname(os.path.abspath(filename))
    1.35 +                if file_dir == this_script_dir:
    1.36 +                    print >> sys.stderr, "%s:%i: %r" % (os.path.join("..", "bindings", "python", os.path.basename(filename)),
    1.37 +                                                        line_number, exception)
    1.38 +                    break
    1.39 +        return True
    1.40 +pybindgen.settings.error_handler = ErrorHandler()
    1.41 +
    1.42 +
    1.43 +class MyMultiSectionFactory(MultiSectionFactory):
    1.44 +
    1.45 +    def __init__(self, main_file_name, modules):
    1.46 +        super(MyMultiSectionFactory, self).__init__()
    1.47 +        self.main_file_name = main_file_name
    1.48 +        self.main_sink = FileCodeSink(open(main_file_name, "wt"))
    1.49 +        self.header_name = "ns3module.h"
    1.50 +        header_file_name = os.path.join(os.path.dirname(self.main_file_name), self.header_name)
    1.51 +        self.header_sink = FileCodeSink(open(header_file_name, "wt"))
    1.52 +        self.section_sinks = {}
    1.53 +
    1.54 +        for module in modules:
    1.55 +            section_name = 'ns3_module_%s' % module.replace('-', '_')
    1.56 +            file_name = os.path.join(os.path.dirname(self.main_file_name), "%s.cc" % section_name)
    1.57 +            sink = FileCodeSink(open(file_name, "wt"))
    1.58 +            self.section_sinks[section_name] = sink            
    1.59 +
    1.60 +    def get_section_code_sink(self, section_name):
    1.61 +        return self.section_sinks[section_name]
    1.62 +
    1.63 +    def get_main_code_sink(self):
    1.64 +        return self.main_sink
    1.65 +
    1.66 +    def get_common_header_code_sink(self):
    1.67 +        return self.header_sink
    1.68 +
    1.69 +    def get_common_header_include(self):
    1.70 +        return '"%s"' % self.header_name
    1.71 +
    1.72 +    def close(self):
    1.73 +        self.header_sink.file.close()
    1.74 +        self.main_sink.file.close()
    1.75 +        for sink in self.section_sinks.itervalues():
    1.76 +            sink.file.close()
    1.77 +
    1.78 +
    1.79 +
    1.80 +def main():
    1.81 +    out = MyMultiSectionFactory(sys.argv[1], sys.argv[2:])
    1.82 +    root_module = module_init()
    1.83 +    root_module.add_include('"ns3/everything.h"')
    1.84 +
    1.85 +    register_types(root_module)
    1.86 +
    1.87 +    ns3modulegen_core_customizations.Simulator_customizations(root_module)
    1.88 +    ns3modulegen_core_customizations.CommandLine_customizations(root_module)
    1.89 +
    1.90 +
    1.91 +    for local_module in LOCAL_MODULES:
    1.92 +        mod = __import__(local_module)
    1.93 +        mod.register_types(root_module)
    1.94 +
    1.95 +    ns3modulegen_core_customizations.generate_callback_classes(root_module.after_forward_declarations,
    1.96 +                                                               callbacks_list.callback_classes)
    1.97 +
    1.98 +
    1.99 +    register_methods(root_module)
   1.100 +
   1.101 +    for local_module in LOCAL_MODULES:
   1.102 +        mod = __import__(local_module)
   1.103 +        mod.register_methods(root_module)
   1.104 +
   1.105 +    ns3modulegen_core_customizations.Object_customizations(root_module)
   1.106 +
   1.107 +    register_functions(root_module)
   1.108 +
   1.109 +    for local_module in LOCAL_MODULES:
   1.110 +        mod = __import__(local_module)
   1.111 +        mod.register_functions(root_module)
   1.112 +
   1.113 +    root_module.generate(out, '_ns3')
   1.114 +
   1.115 +    out.close()
   1.116 +
   1.117 +if __name__ == '__main__':
   1.118 +    main()
   1.119 +