javaclass

tools/wrap.py

100:feda623ce900
2005-01-10 Paul Boddie Introduced removal of skeleton classes.
     1 #!/usr/bin/env python     2      3 """     4 Wrap Java packages, converting the skeleton Java classes to Python modules which     5 connect to concrete Python implementation classes.     6 """     7      8 import classfile     9 import glob    10 import sys    11 import os    12     13 if __name__ == "__main__":    14     if len(sys.argv) < 2:    15         print "wrap.py <package directory> <wrapped package>"    16         print "For example:"    17         print "wrap.py qtjava qt"    18         sys.exit(1)    19     20     # Process all directories in the list, producing for each a Python source    21     # file containing the classes found in the given directory.    22     23     directory, package = sys.argv[1:3]    24     f = open(os.path.join(directory, "__init__.py"), "w")    25     f.write("import %s\n" % package)    26     27     # Process each class file.    28     29     for filename in glob.glob(os.path.join(directory, "*.class")):    30         print "Processing", filename    31         cf = open(filename, "rb")    32         c = classfile.ClassFile(cf.read())    33         cf.close()    34     35         # Write the class into the source file.    36     37         full_name = c.this_class.get_python_name()    38         class_name = full_name.split(".")[-1]    39         f.write("class %s(%s.%s):\n" % (class_name, package, class_name))    40     41         # Process methods in the class, writing wrapper code.    42     43         method_names = []    44         for method in c.methods:    45             wrapped_method_name = method.get_unqualified_python_name()    46             f.write("    def %s(*args):\n" % wrapped_method_name)    47             f.write("        return %s.%s.%s(*args)\n" % (package, class_name, wrapped_method_name))    48             method_name = method.get_python_name()    49             method_names.append((method_name, wrapped_method_name))    50     51         # Produce method entries for the specially named methods.    52     53         for method_name, wrapped_method_name in method_names:    54             f.write("setattr(%s, '%s', %s.%s)\n" % (class_name, method_name, class_name, wrapped_method_name))    55     56         # Remove the original class.    57     58         print "Removing", filename    59         os.remove(filename)    60     61     f.close()    62     63 # vim: tabstop=4 expandtab shiftwidth=4