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@186 | 6 | |
paul@186 | 7 | Copyright (C) 2004, 2005, 2006, 2011 Paul Boddie <paul@boddie.org.uk> |
paul@186 | 8 | |
paul@186 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@186 | 10 | the terms of the GNU Lesser General Public License as published by the Free |
paul@186 | 11 | Software Foundation; either version 3 of the License, or (at your option) any |
paul@186 | 12 | later version. |
paul@186 | 13 | |
paul@186 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@186 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@186 | 16 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
paul@186 | 17 | details. |
paul@186 | 18 | |
paul@186 | 19 | You should have received a copy of the GNU Lesser General Public License along |
paul@186 | 20 | with this program. If not, see <http://www.gnu.org/licenses/>. |
paul@98 | 21 | """ |
paul@98 | 22 | |
paul@150 | 23 | import javaclass.classfile |
paul@98 | 24 | import glob |
paul@98 | 25 | import sys |
paul@98 | 26 | import os |
paul@98 | 27 | |
paul@98 | 28 | if __name__ == "__main__": |
paul@98 | 29 | if len(sys.argv) < 2: |
paul@98 | 30 | print "wrap.py <package directory> <wrapped package>" |
paul@98 | 31 | print "For example:" |
paul@98 | 32 | print "wrap.py qtjava qt" |
paul@98 | 33 | sys.exit(1) |
paul@98 | 34 | |
paul@98 | 35 | # Process all directories in the list, producing for each a Python source |
paul@98 | 36 | # file containing the classes found in the given directory. |
paul@98 | 37 | |
paul@98 | 38 | directory, package = sys.argv[1:3] |
paul@98 | 39 | f = open(os.path.join(directory, "__init__.py"), "w") |
paul@98 | 40 | f.write("import %s\n" % package) |
paul@106 | 41 | f.write("import java.lang\n") |
paul@98 | 42 | |
paul@98 | 43 | # Process each class file. |
paul@98 | 44 | |
paul@98 | 45 | for filename in glob.glob(os.path.join(directory, "*.class")): |
paul@98 | 46 | print "Processing", filename |
paul@98 | 47 | cf = open(filename, "rb") |
paul@150 | 48 | c = javaclass.classfile.ClassFile(cf.read()) |
paul@98 | 49 | cf.close() |
paul@98 | 50 | |
paul@98 | 51 | # Write the class into the source file. |
paul@98 | 52 | |
paul@98 | 53 | full_name = c.this_class.get_python_name() |
paul@98 | 54 | class_name = full_name.split(".")[-1] |
paul@106 | 55 | f.write("class %s(%s.%s, java.lang.Object):\n" % (class_name, package, class_name)) |
paul@98 | 56 | |
paul@98 | 57 | # Process methods in the class, writing wrapper code. |
paul@98 | 58 | |
paul@98 | 59 | method_names = [] |
paul@98 | 60 | for method in c.methods: |
paul@98 | 61 | wrapped_method_name = method.get_unqualified_python_name() |
paul@106 | 62 | |
paul@106 | 63 | # Find out more about the parameters, introducing special |
paul@106 | 64 | # conversions where appropriate. |
paul@106 | 65 | |
paul@117 | 66 | parameter_names = ["self"] |
paul@106 | 67 | parameter_index = 1 |
paul@117 | 68 | conversions = [] |
paul@117 | 69 | |
paul@106 | 70 | for parameter in method.get_descriptor()[0]: |
paul@117 | 71 | parameter_name = "p" + str(parameter_index) |
paul@106 | 72 | base_type, object_type, array_type = parameter |
paul@117 | 73 | |
paul@117 | 74 | # Special cases. |
paul@117 | 75 | |
paul@106 | 76 | if object_type == "java/lang/String": |
paul@117 | 77 | conversions.append("%s = unicode(%s)" % (parameter_name, parameter_name)) |
paul@117 | 78 | # elif object_type == "java/util/Map": |
paul@117 | 79 | # NOTE: Using special private interface. |
paul@117 | 80 | # conversions.append("%s = %s.as_dict()" % (parameter_name, parameter_name)) |
paul@117 | 81 | |
paul@117 | 82 | parameter_names.append(parameter_name) |
paul@106 | 83 | parameter_index += 1 |
paul@106 | 84 | |
paul@106 | 85 | # Write the signature. |
paul@106 | 86 | |
paul@117 | 87 | f.write(" def %s(%s):\n" % (wrapped_method_name, ", ".join(parameter_names))) |
paul@106 | 88 | |
paul@106 | 89 | # Write any conversions. |
paul@106 | 90 | |
paul@117 | 91 | for conversion in conversions: |
paul@117 | 92 | f.write(" %s\n" % conversion) |
paul@106 | 93 | |
paul@106 | 94 | # Write the call to the wrapped method. |
paul@106 | 95 | |
paul@117 | 96 | f.write(" return %s.%s.%s(%s)\n" % (package, class_name, wrapped_method_name, ", ".join(parameter_names))) |
paul@106 | 97 | |
paul@106 | 98 | # Record the correspondence between the Java-accessible and wrapped |
paul@106 | 99 | # method names. |
paul@106 | 100 | |
paul@98 | 101 | method_name = method.get_python_name() |
paul@98 | 102 | method_names.append((method_name, wrapped_method_name)) |
paul@98 | 103 | |
paul@98 | 104 | # Produce method entries for the specially named methods. |
paul@98 | 105 | |
paul@98 | 106 | for method_name, wrapped_method_name in method_names: |
paul@98 | 107 | f.write("setattr(%s, '%s', %s.%s)\n" % (class_name, method_name, class_name, wrapped_method_name)) |
paul@98 | 108 | |
paul@100 | 109 | # Remove the original class. |
paul@100 | 110 | |
paul@100 | 111 | print "Removing", filename |
paul@100 | 112 | os.remove(filename) |
paul@100 | 113 | |
paul@98 | 114 | f.close() |
paul@98 | 115 | |
paul@98 | 116 | # vim: tabstop=4 expandtab shiftwidth=4 |