#!/usr/bin/env python # # Generate the PyConnection wrapper for a Python class. import sys import os INTERFACE_TEMPLATE = """ package %(package)s; public interface %(pyClassName)sType { // write the public interface for the Python // class '%(pyClassName)s' here. } """ FACTORY_TEMPLATE = """ package %(package)s; import pyconnection.PyFactory; public class %(pyClassName)sFactory extends PyFactory { public %(pyClassName)sFactory() { super("%(package)s.%(pyClassName)sType", "%(pyClassName)s"); } public %(pyClassName)sType create(Object... objects) { return (%(pyClassName)sType)super.create(objects); } } """ def check_location(pyClassName): """ Check whether the directory contains a Python file with the name of the class. """ try: file('%s.py' %pyClassName) except IOError: print "Couldn't find Python file: %s.py" %pyClassName sys.exit(1) def make_java_project_dir(javaProjectName, pyClassName): """ Create the java project directory structure, if it does not exist. """ try: cwd = os.getcwd() os.chdir(os.pardir) package_dir = os.path.join(javaProjectName, 'src', pyClassName.lower()) os.makedirs(package_dir) python_dir = os.path.join(javaProjectName, 'src/python') os.symlink(cwd, python_dir) package = pyClassName.lower() iface_src = INTERFACE_TEMPLATE %locals() factory_src = FACTORY_TEMPLATE %locals() iface_file = file(os.path.join(package_dir, pyClassName+'Type.java'), 'w') factory_file = file(os.path.join(package_dir, pyClassName+'Factory.java'), 'w') iface_file.write(iface_src) factory_file.write(factory_src) iface_file.close() factory_file.close() except OSError, e: if e.errno != 17: # ignore if some directory already exists raise finally: os.chdir(cwd) if __name__ == '__main__': if len(sys.argv) != 3: print 'Usage: %s ' %sys.argv[0] sys.exit(1) pyClassName = sys.argv[1] javaProjectName = sys.argv[2] check_location(pyClassName) make_java_project_dir(javaProjectName, pyClassName)