bindings/python/ns3modulegen-modular.py
author Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
Sat, 12 Mar 2011 18:34:30 +0000
changeset 6893 5dccd86f90cf
parent 6874 c7537e62f2fa
child 6896 fb47685b1dad
permissions -rw-r--r--
Modular Python bindings work (many bug fixes, more modules tested)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6874
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     1
import warnings
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     2
import sys
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     3
import os
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     4
import pybindgen.settings
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     5
from pybindgen import Module, FileCodeSink, param, retval, cppclass, typehandlers
6893
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
     6
from pybindgen.module import MultiSectionFactory
6874
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     7
import ns3modulegen_core_customizations
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
     8
6893
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
     9
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    10
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    11
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    12
pybindgen.settings.wrapper_registry = pybindgen.settings.StdMapWrapperRegistry
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    13
6874
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    14
class ErrorHandler(pybindgen.settings.ErrorHandler):
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    15
    def handle_error(self, wrapper, exception, traceback_):
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    16
        warnings.warn("exception %r in wrapper %s" % (exception, wrapper))
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    17
        return True
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    18
pybindgen.settings.error_handler = ErrorHandler()
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    19
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    20
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    21
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    22
6893
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    23
class MyMultiSectionFactory(MultiSectionFactory):
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    24
    def __init__(self, main_file_name):
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    25
        super(MyMultiSectionFactory, self).__init__()
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    26
        self.main_file_name = main_file_name
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    27
        self.main_sink = FileCodeSink(open(main_file_name, "wt"))
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    28
        self.header_name = "ns3module.h"
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    29
        header_file_name = os.path.join(os.path.dirname(self.main_file_name), self.header_name)
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    30
        #print >> sys.stderr, ">>>>>>>>>>>>>>>>>", header_file_name, main_file_name
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    31
        self.header_sink = FileCodeSink(open(header_file_name, "wt"))
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    32
    def get_section_code_sink(self, section_name):
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    33
        return self.main_sink
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    34
    def get_main_code_sink(self):
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    35
        return self.main_sink
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    36
    def get_common_header_code_sink(self):
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    37
        return self.header_sink
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    38
    def get_common_header_include(self):
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    39
        return '"%s"' % self.header_name
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    40
    def close(self):
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    41
        self.header_sink.file.close()
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    42
        self.main_sink.file.close()
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    43
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    44
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    45
6874
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    46
def main(argv):
6893
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    47
    module_abs_src_path, target, output_cc_file_name = argv[1:]
6874
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    48
    module_name = os.path.basename(module_abs_src_path)
6893
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    49
    out = MyMultiSectionFactory(output_cc_file_name)
6874
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    50
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    51
    sys.path.insert(0, os.path.join(module_abs_src_path, "bindings"))
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    52
    try:
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    53
        module_apidefs = __import__("modulegen__%s" % target)
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    54
        del sys.modules["modulegen__%s" % target]
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    55
        try:
6893
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    56
            module_customization = __import__("modulegen_customizations")
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    57
            del sys.modules["modulegen_customizations"]
6874
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    58
        except ImportError:
6893
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    59
            module_customization = object()
6874
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    60
    finally:
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    61
        sys.path.pop(0)
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    62
    
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    63
    root_module = module_apidefs.module_init()
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    64
    root_module.add_include('"ns3/%s-module.h"' % module_name)
6893
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    65
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    66
    # -----------
6874
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    67
    module_apidefs.register_types(root_module)
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    68
6893
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    69
    if hasattr(module_customization, 'post_register_types'):
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    70
        module_customization.post_register_types(root_module)
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    71
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    72
    # -----------
6874
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    73
    module_apidefs.register_methods(root_module)
6893
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    74
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    75
    if hasattr(module_customization, 'post_register_methods'):
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    76
        module_customization.post_register_methods(root_module)
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    77
6874
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    78
    ns3modulegen_core_customizations.Object_customizations(root_module)
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    79
    ns3modulegen_core_customizations.Attribute_customizations(root_module)
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    80
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    81
6893
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    82
    # -----------
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    83
    module_apidefs.register_functions(root_module)
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    84
    
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    85
    if hasattr(module_customization, 'post_register_functions'):
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    86
        module_customization.post_register_functions(root_module)
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    87
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    88
5dccd86f90cf Modular Python bindings work (many bug fixes, more modules tested)
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents: 6874
diff changeset
    89
    # -----------
6874
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    90
    root_module.generate(out)
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    91
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    92
if __name__ == '__main__':
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    93
    import sys
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    94
    main(sys.argv)
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    95
c7537e62f2fa Start of work on modular Python bindings, unstable
Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
parents:
diff changeset
    96