paul@98 | 1 | #!/usr/bin/env python |
paul@98 | 2 | |
paul@98 | 3 | """ |
paul@98 | 4 | Wrap Java packages, converting the skeleton Java classes to Python modules which |
paul@98 | 5 | connect to concrete Python implementation classes. |
paul@98 | 6 | """ |
paul@98 | 7 | |
paul@98 | 8 | import classfile |
paul@98 | 9 | import glob |
paul@98 | 10 | import sys |
paul@98 | 11 | import os |
paul@98 | 12 | |
paul@98 | 13 | if __name__ == "__main__": |
paul@98 | 14 | if len(sys.argv) < 2: |
paul@98 | 15 | print "wrap.py <package directory> <wrapped package>" |
paul@98 | 16 | print "For example:" |
paul@98 | 17 | print "wrap.py qtjava qt" |
paul@98 | 18 | sys.exit(1) |
paul@98 | 19 | |
paul@98 | 20 | # Process all directories in the list, producing for each a Python source |
paul@98 | 21 | # file containing the classes found in the given directory. |
paul@98 | 22 | |
paul@98 | 23 | directory, package = sys.argv[1:3] |
paul@98 | 24 | f = open(os.path.join(directory, "__init__.py"), "w") |
paul@98 | 25 | f.write("import %s\n" % package) |
paul@106 | 26 | f.write("import java.lang\n") |
paul@98 | 27 | |
paul@98 | 28 | # Process each class file. |
paul@98 | 29 | |
paul@98 | 30 | for filename in glob.glob(os.path.join(directory, "*.class")): |
paul@98 | 31 | print "Processing", filename |
paul@98 | 32 | cf = open(filename, "rb") |
paul@98 | 33 | c = classfile.ClassFile(cf.read()) |
paul@98 | 34 | cf.close() |
paul@98 | 35 | |
paul@98 | 36 | # Write the class into the source file. |
paul@98 | 37 | |
paul@98 | 38 | full_name = c.this_class.get_python_name() |
paul@98 | 39 | class_name = full_name.split(".")[-1] |
paul@106 | 40 | f.write("class %s(%s.%s, java.lang.Object):\n" % (class_name, package, class_name)) |
paul@98 | 41 | |
paul@98 | 42 | # Process methods in the class, writing wrapper code. |
paul@98 | 43 | |
paul@98 | 44 | method_names = [] |
paul@98 | 45 | for method in c.methods: |
paul@98 | 46 | wrapped_method_name = method.get_unqualified_python_name() |
paul@106 | 47 | |
paul@106 | 48 | # Find out more about the parameters, introducing special |
paul@106 | 49 | # conversions where appropriate. |
paul@106 | 50 | |
paul@106 | 51 | parameters = [("self", None)] |
paul@106 | 52 | parameter_index = 1 |
paul@106 | 53 | for parameter in method.get_descriptor()[0]: |
paul@106 | 54 | base_type, object_type, array_type = parameter |
paul@106 | 55 | if object_type == "java/lang/String": |
paul@106 | 56 | parameters.append(("p" + str(parameter_index), "unicode")) |
paul@106 | 57 | else: |
paul@106 | 58 | parameters.append(("p" + str(parameter_index), None)) |
paul@106 | 59 | parameter_index += 1 |
paul@106 | 60 | |
paul@106 | 61 | # Write the signature. |
paul@106 | 62 | |
paul@106 | 63 | parameter_names = ", ".join([t[0] for t in parameters]) |
paul@106 | 64 | f.write(" def %s(%s):\n" % (wrapped_method_name, parameter_names)) |
paul@106 | 65 | |
paul@106 | 66 | # Write any conversions. |
paul@106 | 67 | |
paul@106 | 68 | for parameter_name, conversion in parameters: |
paul@106 | 69 | if conversion: |
paul@106 | 70 | f.write(" %s = %s(%s)\n" % (parameter_name, conversion, parameter_name)) |
paul@106 | 71 | |
paul@106 | 72 | # Write the call to the wrapped method. |
paul@106 | 73 | |
paul@106 | 74 | f.write(" return %s.%s.%s(%s)\n" % (package, class_name, wrapped_method_name, parameter_names)) |
paul@106 | 75 | |
paul@106 | 76 | # Record the correspondence between the Java-accessible and wrapped |
paul@106 | 77 | # method names. |
paul@106 | 78 | |
paul@98 | 79 | method_name = method.get_python_name() |
paul@98 | 80 | method_names.append((method_name, wrapped_method_name)) |
paul@98 | 81 | |
paul@98 | 82 | # Produce method entries for the specially named methods. |
paul@98 | 83 | |
paul@98 | 84 | for method_name, wrapped_method_name in method_names: |
paul@98 | 85 | f.write("setattr(%s, '%s', %s.%s)\n" % (class_name, method_name, class_name, wrapped_method_name)) |
paul@98 | 86 | |
paul@100 | 87 | # Remove the original class. |
paul@100 | 88 | |
paul@100 | 89 | print "Removing", filename |
paul@100 | 90 | os.remove(filename) |
paul@100 | 91 | |
paul@98 | 92 | f.close() |
paul@98 | 93 | |
paul@98 | 94 | # vim: tabstop=4 expandtab shiftwidth=4 |