bindings/python/ns3modulegen.py
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Wed, 18 Feb 2009 12:23:08 +0000
changeset 4196 ed59d07c5373
parent 4105 ce8be572c4d4
child 4208 ef9ae8962bbb
permissions -rwxr-xr-x
Python: wrap std::ostream/ofstream, for ascii tracing.
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@3546
    39
pybindgen.settings.wrapper_registry = pybindgen.settings.StdMapWrapperRegistry
gjc@3546
    40
gjc@3408
    41
gjc@3408
    42
class MyMultiSectionFactory(MultiSectionFactory):
gjc@3408
    43
gjc@3408
    44
    def __init__(self, main_file_name, modules):
gjc@3408
    45
        super(MyMultiSectionFactory, self).__init__()
gjc@3408
    46
        self.main_file_name = main_file_name
gjc@3408
    47
        self.main_sink = FileCodeSink(open(main_file_name, "wt"))
gjc@3408
    48
        self.header_name = "ns3module.h"
gjc@3408
    49
        header_file_name = os.path.join(os.path.dirname(self.main_file_name), self.header_name)
gjc@3408
    50
        self.header_sink = FileCodeSink(open(header_file_name, "wt"))
gjc@3538
    51
        self.section_sinks = {'__main__': self.main_sink}
gjc@3408
    52
gjc@3408
    53
        for module in modules:
gjc@3408
    54
            section_name = 'ns3_module_%s' % module.replace('-', '_')
gjc@3408
    55
            file_name = os.path.join(os.path.dirname(self.main_file_name), "%s.cc" % section_name)
gjc@3408
    56
            sink = FileCodeSink(open(file_name, "wt"))
gjc@3408
    57
            self.section_sinks[section_name] = sink            
gjc@3408
    58
gjc@3408
    59
    def get_section_code_sink(self, section_name):
gjc@3408
    60
        return self.section_sinks[section_name]
gjc@3408
    61
gjc@3408
    62
    def get_main_code_sink(self):
gjc@3408
    63
        return self.main_sink
gjc@3408
    64
gjc@3408
    65
    def get_common_header_code_sink(self):
gjc@3408
    66
        return self.header_sink
gjc@3408
    67
gjc@3408
    68
    def get_common_header_include(self):
gjc@3408
    69
        return '"%s"' % self.header_name
gjc@3408
    70
gjc@3408
    71
    def close(self):
gjc@3408
    72
        self.header_sink.file.close()
gjc@3408
    73
        self.main_sink.file.close()
gjc@3408
    74
        for sink in self.section_sinks.itervalues():
gjc@3408
    75
            sink.file.close()
gjc@3408
    76
gjc@3408
    77
gjc@3408
    78
gjc@3408
    79
def main():
gjc@3408
    80
    out = MyMultiSectionFactory(sys.argv[1], sys.argv[2:])
gjc@3408
    81
    root_module = module_init()
gjc@3409
    82
    root_module.add_include('"everything.h"')
gjc@3408
    83
gjc@3408
    84
    register_types(root_module)
gjc@3408
    85
gjc@3408
    86
    ns3modulegen_core_customizations.Simulator_customizations(root_module)
gjc@3408
    87
    ns3modulegen_core_customizations.CommandLine_customizations(root_module)
gjc@3753
    88
    ns3modulegen_core_customizations.TypeId_customizations(root_module)
gjc@4196
    89
    ns3modulegen_core_customizations.add_std_ofstream(root_module)
gjc@3408
    90
gjc@3408
    91
gjc@3408
    92
    for local_module in LOCAL_MODULES:
gjc@3408
    93
        mod = __import__(local_module)
gjc@3408
    94
        mod.register_types(root_module)
gjc@3408
    95
gjc@3408
    96
    ns3modulegen_core_customizations.generate_callback_classes(root_module.after_forward_declarations,
gjc@3408
    97
                                                               callbacks_list.callback_classes)
gjc@3408
    98
gjc@3408
    99
gjc@3408
   100
    register_methods(root_module)
gjc@3408
   101
gjc@3408
   102
    for local_module in LOCAL_MODULES:
gjc@3408
   103
        mod = __import__(local_module)
gjc@3408
   104
        mod.register_methods(root_module)
gjc@3408
   105
gjc@3408
   106
    ns3modulegen_core_customizations.Object_customizations(root_module)
gjc@3421
   107
    ns3modulegen_core_customizations.Attribute_customizations(root_module)
gjc@3408
   108
gjc@3408
   109
    register_functions(root_module)
gjc@3408
   110
gjc@3408
   111
    for local_module in LOCAL_MODULES:
gjc@3408
   112
        mod = __import__(local_module)
gjc@3408
   113
        mod.register_functions(root_module)
gjc@3408
   114
gjc@3639
   115
    enabled_features = os.environ['NS3_ENABLED_FEATURES'].split(',')
gjc@3420
   116
gjc@3420
   117
    # if GtkConfigStore support is disabled, disable the class wrapper
gjc@3639
   118
    if 'GtkConfigStore' not in enabled_features:
mathieu@3582
   119
        try:
mathieu@3582
   120
            root_module.classes.remove(root_module['ns3::GtkConfigStore'])
mathieu@3582
   121
        except KeyError:
mathieu@3582
   122
            pass
gjc@3420
   123
gjc@3575
   124
    # if no sqlite, the class SqliteDataOutput is disabled
gjc@3639
   125
    if 'SqliteDataOutput' not in enabled_features:
mathieu@3582
   126
        try:
mathieu@3582
   127
            root_module.classes.remove(root_module['ns3::SqliteDataOutput'])
mathieu@3582
   128
        except KeyError:
mathieu@3582
   129
            pass
gjc@3575
   130
gjc@3648
   131
    if 'Threading' not in enabled_features:
gjc@3648
   132
        for clsname in ['SystemThread', 'SystemMutex', 'SystemCondition', 'CriticalSection']:
gjc@3648
   133
            root_module.classes.remove(root_module['ns3::%s' % clsname])
gjc@3648
   134
gjc@3916
   135
gjc@3916
   136
    if 'EmuNetDevice' not in enabled_features:
gjc@3916
   137
        for clsname in ['EmuNetDevice', 'EmuHelper']:
gjc@3916
   138
            root_module.classes.remove(root_module['ns3::%s' % clsname])
gjc@3916
   139
gjc@3648
   140
    if 'RealTime' not in enabled_features:
gjc@4090
   141
        for clsname in ['WallClockSynchronizer', 'RealtimeSimulatorImpl']:
gjc@3648
   142
            root_module.classes.remove(root_module['ns3::%s' % clsname])
gjc@3648
   143
        root_module.enums.remove(root_module['ns3::RealtimeSimulatorImpl::SynchronizationMode'])
gjc@3648
   144
gjc@3408
   145
    root_module.generate(out, '_ns3')
gjc@3408
   146
gjc@3408
   147
    out.close()
gjc@3408
   148
gjc@3408
   149
if __name__ == '__main__':
gjc@4105
   150
    if 0:
gjc@4105
   151
        try:
gjc@4105
   152
            import cProfile as profile
gjc@4105
   153
        except ImportError:
gjc@4105
   154
            main()
gjc@4105
   155
        else:
gjc@4105
   156
            print >> sys.stderr, "** running under profiler"
gjc@4105
   157
            profile.run('main()', 'ns3modulegen.pstat')
gjc@4105
   158
    else:
gjc@4105
   159
        main()
gjc@3408
   160