예제 #1
0
    def assertMultifileGeneratedOutput(self, cpp, py, errors=None, flags=None):
        self.maxDiff = None
        converter = CodeConverter('test')

        # Parse the content of each file
        with capture_output(redirect_stdout=False) as console:
            converter.parse_text(
                [
                    (filename, adjust(content))
                    for filename, content in cpp
                ],
                flags=flags if flags else ['-std=c++0x']
            )

        if errors is not None:
            self.assertEqual(adjust(errors), console.getvalue())
        else:
            self.assertEqual('', console.getvalue())

        # Output each generated code file
        for module, content in py:
            buf = StringIO()
            converter.output(module, buf)

            # Compare the generated code to expectation.
            self.assertEqual(adjust(content), buf.getvalue(), "Discrepancy in %s" % module)
예제 #2
0
    def assertGeneratedOutput(self, cpp, py, errors=None, flags=None):
        self.maxDiff = None
        converter = CodeConverter('test')

        # Parse the content
        with capture_output(redirect_stdout=False) as console:
            converter.parse_text(
                [
                    ('test.cpp', adjust(cpp))
                ],
                flags=flags if flags else ['-std=c++0x']
            )

        if errors:
            self.assertEqual(adjust(errors), console.getvalue())
        else:
            self.assertEqual('', console.getvalue())

        # Output the generated code
        buf = StringIO()
        converter.output('test', buf)

        # Compare the generated code to expectation.
        self.assertEqual(adjust(py), buf.getvalue())
예제 #3
0
def main():
    opts = argparse.ArgumentParser(description='Convert C++ code to Python.', )

    opts.add_argument(
        '-o',
        '--output',
        metavar='module',
        help='The name of the output module to write.',
    )

    opts.add_argument('-s',
                      '--stdout',
                      help='The name of the output module to write.',
                      action='store_true')

    opts.add_argument('-v', '--verbosity', action='count', default=0)

    opts.add_argument('-I',
                      '--include',
                      dest='includes',
                      metavar='/path/to/includes',
                      help='A directory of includes',
                      action='append',
                      default=[])

    opts.add_argument('-D',
                      dest='defines',
                      metavar='SYMBOL',
                      help='Preprocessor symbols to use',
                      action='append',
                      default=[])

    opts.add_argument('-std',
                      help='The C/C++ standard to use (default: c++0x)',
                      default='c++0x')

    opts.add_argument('-stdlib',
                      help='The standard library to use (default: libstdc++)',
                      default='libstdc++')

    opts.add_argument('filename',
                      metavar='file.cpp',
                      help='The file(s) to compile.',
                      nargs="+")

    args = opts.parse_args()

    converter = CodeConverter('output', verbosity=args.verbosity)
    converter.parse(args.filename,
                    flags=['-I%s' % inc for inc in args.includes] +
                    ['-D%s' % define for define in args.defines] +
                    ['-std=%s' % args.std] + ['-stdlib=%s' % args.stdlib])

    converter.diagnostics(sys.stderr)

    if args.output:
        with open('%s.py' % args.output, 'w') as out:
            converter.output('%s.py' % args.output, out)
    else:
        if args.stdout:
            converter.output_all(sys.stdout)
        else:
            print("Can't output multiple files (yet!)")