Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 83 lines (74 sloc) 2.415 kB
bdf2920 add java2py for converting java to python duh
lkcl authored
1 #!/usr/bin/env python
2 """ Use this to help speed up manual conversion of e.g. GWT Java to e.g.
3 Pyjamas python
4 """
5
6 import sys
7
37c4846 more java2py
lkcl authored
8 def countspaces(txt):
9 count = 0
10 while count < len(txt) and txt[count] == ' ':
11 count += 1
12 return count
bdf2920 add java2py for converting java to python duh
lkcl authored
13 def java2pythonlinebyline(txt):
14 if txt.find('if (') >= 0:
15 txt = txt.replace('if (', 'if ')
16 txt = txt.replace(') {', ':')
37c4846 more java2py
lkcl authored
17 elif txt.find('class ') >= 0 and txt.endswith("{"):
18 txt = txt.replace('{', ':')
19 elif txt.find('while (') >= 0:
20 txt = txt.replace('while (', 'while ')
21 txt = txt.replace(') {', ':')
22 elif txt.find('for (') >= 0:
23 txt = txt.replace('for (', 'for ')
24 txt = txt.replace(') {', ':')
25 count = countspaces(txt)
26 if txt[count:].startswith("}"):
27 txt = count * ' ' + txt[count+1:]
28
29 if txt[count:].startswith("protected ") >= 0:
30 txt = txt.replace("protected ", "")
31 if txt[count:].startswith("public ") >= 0:
32 txt = txt.replace("public ", "")
33 if txt[count:].startswith("private ") >= 0:
34 txt = txt.replace("private ", "")
35 if txt[count:].startswith("static ") >= 0:
36 txt = txt.replace("static ", "")
37 if txt[count:].startswith("final ") >= 0:
38 txt = txt.replace("final ", "")
bdf2920 add java2py for converting java to python duh
lkcl authored
39 return txt
40
41 def reindent(txt):
37c4846 more java2py
lkcl authored
42 """ reindents according to { and } braces. strips all whitespace,
43 possibly not smartest thing to do. oh well.
44 """
bdf2920 add java2py for converting java to python duh
lkcl authored
45 res = ''
46 indent = 0
47 for l in txt.split("\n"):
48 l = l.strip()
49 if l.startswith("}"):
50 indent -= 1
51 res += ' ' * indent + l + "\n"
52 if l.endswith("{"):
53 indent += 1
54 return res
55
56 def java2python(txt):
57 txt = reindent(txt)
58 txt = txt.replace("/*", '"""')
59 txt = txt.replace("*/", '"""')
37c4846 more java2py
lkcl authored
60 txt = txt.replace("<>", '!=')
61 txt = txt.replace("null", 'None')
62 txt = txt.replace("true", 'True')
63 txt = txt.replace("false", 'False')
bdf2920 add java2py for converting java to python duh
lkcl authored
64 txt = txt.replace("//", '#')
65 txt = txt.replace("this.", 'self.')
37c4846 more java2py
lkcl authored
66 txt = txt.replace("else if", 'elif')
67 txt = txt.replace("new ", '')
bdf2920 add java2py for converting java to python duh
lkcl authored
68 l = txt.split("\n")
69 l = map(java2pythonlinebyline, l)
37c4846 more java2py
lkcl authored
70 txt = txt.replace("}", '')
bdf2920 add java2py for converting java to python duh
lkcl authored
71 return '\n'.join(l)
72
73 if __name__ == "__main__":
74 fname = sys.argv[1]
75 f = open(fname + ".java", "r")
76 txt = java2python(f.read())
77 f.close()
78
79 f = open(fname + ".py", "w")
80 f.write(txt)
81 f.close()
82
Something went wrong with that request. Please try again.