Permalink
Browse files

add java2py for converting java to python duh

git-svn-id: https://pyjamas.svn.sourceforge.net/svnroot/pyjamas/trunk@527 7a2bd370-bda8-463c-979e-2900ccfb811e
  • Loading branch information...
1 parent 411c30e commit bdf2920d498f5080c9dfd30831944cf9d46e2587 lkcl committed Apr 10, 2009
Showing with 45 additions and 0 deletions.
  1. +45 −0 contrib/java2py.py
View
@@ -0,0 +1,45 @@
+#!/usr/bin/env python
+""" Use this to help speed up manual conversion of e.g. GWT Java to e.g.
+ Pyjamas python
+"""
+
+import sys
+
+def java2pythonlinebyline(txt):
+ if txt.find('if (') >= 0:
+ txt = txt.replace('if (', 'if ')
+ txt = txt.replace(') {', ':')
+ return txt
+
+def reindent(txt):
+ res = ''
+ indent = 0
+ for l in txt.split("\n"):
+ l = l.strip()
+ if l.startswith("}"):
+ indent -= 1
+ res += ' ' * indent + l + "\n"
+ if l.endswith("{"):
+ indent += 1
+ return res
+
+def java2python(txt):
+ txt = reindent(txt)
+ txt = txt.replace("/*", '"""')
+ txt = txt.replace("*/", '"""')
+ txt = txt.replace("//", '#')
+ txt = txt.replace("this.", 'self.')
+ l = txt.split("\n")
+ l = map(java2pythonlinebyline, l)
+ return '\n'.join(l)
+
+if __name__ == "__main__":
+ fname = sys.argv[1]
+ f = open(fname + ".java", "r")
+ txt = java2python(f.read())
+ f.close()
+
+ f = open(fname + ".py", "w")
+ f.write(txt)
+ f.close()
+

0 comments on commit bdf2920

Please sign in to comment.