javaclass

Change of tools/wrap.py

98:61c61585ef67
tools/wrap.py
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/wrap.py	Sun Jan 09 22:53:27 2005 +0100
     1.3 @@ -0,0 +1,58 @@
     1.4 +#!/usr/bin/env python
     1.5 +
     1.6 +"""
     1.7 +Wrap Java packages, converting the skeleton Java classes to Python modules which
     1.8 +connect to concrete Python implementation classes.
     1.9 +"""
    1.10 +
    1.11 +import classfile
    1.12 +import glob
    1.13 +import sys
    1.14 +import os
    1.15 +
    1.16 +if __name__ == "__main__":
    1.17 +    if len(sys.argv) < 2:
    1.18 +        print "wrap.py <package directory> <wrapped package>"
    1.19 +        print "For example:"
    1.20 +        print "wrap.py qtjava qt"
    1.21 +        sys.exit(1)
    1.22 +
    1.23 +    # Process all directories in the list, producing for each a Python source
    1.24 +    # file containing the classes found in the given directory.
    1.25 +
    1.26 +    directory, package = sys.argv[1:3]
    1.27 +    f = open(os.path.join(directory, "__init__.py"), "w")
    1.28 +    f.write("import %s\n" % package)
    1.29 +
    1.30 +    # Process each class file.
    1.31 +
    1.32 +    for filename in glob.glob(os.path.join(directory, "*.class")):
    1.33 +        print "Processing", filename
    1.34 +        cf = open(filename, "rb")
    1.35 +        c = classfile.ClassFile(cf.read())
    1.36 +        cf.close()
    1.37 +
    1.38 +        # Write the class into the source file.
    1.39 +
    1.40 +        full_name = c.this_class.get_python_name()
    1.41 +        class_name = full_name.split(".")[-1]
    1.42 +        f.write("class %s(%s.%s):\n" % (class_name, package, class_name))
    1.43 +
    1.44 +        # Process methods in the class, writing wrapper code.
    1.45 +
    1.46 +        method_names = []
    1.47 +        for method in c.methods:
    1.48 +            wrapped_method_name = method.get_unqualified_python_name()
    1.49 +            f.write("    def %s(*args):\n" % wrapped_method_name)
    1.50 +            f.write("        return %s.%s.%s(*args)\n" % (package, class_name, wrapped_method_name))
    1.51 +            method_name = method.get_python_name()
    1.52 +            method_names.append((method_name, wrapped_method_name))
    1.53 +
    1.54 +        # Produce method entries for the specially named methods.
    1.55 +
    1.56 +        for method_name, wrapped_method_name in method_names:
    1.57 +            f.write("setattr(%s, '%s', %s.%s)\n" % (class_name, method_name, class_name, wrapped_method_name))
    1.58 +
    1.59 +    f.close()
    1.60 +
    1.61 +# vim: tabstop=4 expandtab shiftwidth=4