bindings/python/ns3modulegen-modular.py
changeset 6893 5dccd86f90cf
parent 6874 c7537e62f2fa
child 6896 fb47685b1dad
equal deleted inserted replaced
6889:3a137dbf9789 6893:5dccd86f90cf
     1 import warnings
     1 import warnings
     2 import sys
     2 import sys
     3 import os
     3 import os
     4 import pybindgen.settings
     4 import pybindgen.settings
     5 from pybindgen import Module, FileCodeSink, param, retval, cppclass, typehandlers
     5 from pybindgen import Module, FileCodeSink, param, retval, cppclass, typehandlers
       
     6 from pybindgen.module import MultiSectionFactory
     6 import ns3modulegen_core_customizations
     7 import ns3modulegen_core_customizations
       
     8 
       
     9 
       
    10 
       
    11 
       
    12 pybindgen.settings.wrapper_registry = pybindgen.settings.StdMapWrapperRegistry
     7 
    13 
     8 class ErrorHandler(pybindgen.settings.ErrorHandler):
    14 class ErrorHandler(pybindgen.settings.ErrorHandler):
     9     def handle_error(self, wrapper, exception, traceback_):
    15     def handle_error(self, wrapper, exception, traceback_):
    10         warnings.warn("exception %r in wrapper %s" % (exception, wrapper))
    16         warnings.warn("exception %r in wrapper %s" % (exception, wrapper))
    11         return True
    17         return True
    12 pybindgen.settings.error_handler = ErrorHandler()
    18 pybindgen.settings.error_handler = ErrorHandler()
    13 
    19 
    14 
    20 
    15 
    21 
    16 
    22 
       
    23 class MyMultiSectionFactory(MultiSectionFactory):
       
    24     def __init__(self, main_file_name):
       
    25         super(MyMultiSectionFactory, self).__init__()
       
    26         self.main_file_name = main_file_name
       
    27         self.main_sink = FileCodeSink(open(main_file_name, "wt"))
       
    28         self.header_name = "ns3module.h"
       
    29         header_file_name = os.path.join(os.path.dirname(self.main_file_name), self.header_name)
       
    30         #print >> sys.stderr, ">>>>>>>>>>>>>>>>>", header_file_name, main_file_name
       
    31         self.header_sink = FileCodeSink(open(header_file_name, "wt"))
       
    32     def get_section_code_sink(self, section_name):
       
    33         return self.main_sink
       
    34     def get_main_code_sink(self):
       
    35         return self.main_sink
       
    36     def get_common_header_code_sink(self):
       
    37         return self.header_sink
       
    38     def get_common_header_include(self):
       
    39         return '"%s"' % self.header_name
       
    40     def close(self):
       
    41         self.header_sink.file.close()
       
    42         self.main_sink.file.close()
       
    43 
       
    44 
       
    45 
    17 def main(argv):
    46 def main(argv):
    18     assert len(argv) == 3
    47     module_abs_src_path, target, output_cc_file_name = argv[1:]
    19     module_abs_src_path, target = argv[1], argv[2]
       
    20     module_name = os.path.basename(module_abs_src_path)
    48     module_name = os.path.basename(module_abs_src_path)
       
    49     out = MyMultiSectionFactory(output_cc_file_name)
    21 
    50 
    22     sys.path.insert(0, os.path.join(module_abs_src_path, "bindings"))
    51     sys.path.insert(0, os.path.join(module_abs_src_path, "bindings"))
    23     try:
    52     try:
    24         module_apidefs = __import__("modulegen__%s" % target)
    53         module_apidefs = __import__("modulegen__%s" % target)
    25         del sys.modules["modulegen__%s" % target]
    54         del sys.modules["modulegen__%s" % target]
    26         try:
    55         try:
    27             module_customization = __import__("modulegen_local")
    56             module_customization = __import__("modulegen_customizations")
    28             del sys.modules["modulegen_local"]
    57             del sys.modules["modulegen_customizations"]
    29         except ImportError:
    58         except ImportError:
    30             module_customization = None
    59             module_customization = object()
    31     finally:
    60     finally:
    32         sys.path.pop(0)
    61         sys.path.pop(0)
    33     
    62     
    34     out = FileCodeSink(sys.stdout)
       
    35     root_module = module_apidefs.module_init()
    63     root_module = module_apidefs.module_init()
    36     root_module.add_include('"ns3/%s-module.h"' % module_name)
    64     root_module.add_include('"ns3/%s-module.h"' % module_name)
       
    65 
       
    66     # -----------
    37     module_apidefs.register_types(root_module)
    67     module_apidefs.register_types(root_module)
    38 
    68 
       
    69     if hasattr(module_customization, 'post_register_types'):
       
    70         module_customization.post_register_types(root_module)
       
    71 
       
    72     # -----------
    39     module_apidefs.register_methods(root_module)
    73     module_apidefs.register_methods(root_module)
    40     module_apidefs.register_functions(root_module)
    74 
    41     
    75     if hasattr(module_customization, 'post_register_methods'):
       
    76         module_customization.post_register_methods(root_module)
       
    77 
    42     ns3modulegen_core_customizations.Object_customizations(root_module)
    78     ns3modulegen_core_customizations.Object_customizations(root_module)
    43     ns3modulegen_core_customizations.Attribute_customizations(root_module)
    79     ns3modulegen_core_customizations.Attribute_customizations(root_module)
    44 
    80 
    45     if module_customization is not None:
       
    46         module_customization.customize(root_module)
       
    47 
    81 
       
    82     # -----------
       
    83     module_apidefs.register_functions(root_module)
       
    84     
       
    85     if hasattr(module_customization, 'post_register_functions'):
       
    86         module_customization.post_register_functions(root_module)
       
    87 
       
    88 
       
    89     # -----------
    48     root_module.generate(out)
    90     root_module.generate(out)
    49 
    91 
    50 if __name__ == '__main__':
    92 if __name__ == '__main__':
    51     import sys
    93     import sys
    52     main(sys.argv)
    94     main(sys.argv)