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