-
Notifications
You must be signed in to change notification settings - Fork 0
/
pde2py.py
165 lines (149 loc) · 5.43 KB
/
pde2py.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/python
"""
Utility to do whatever mechanical work can be done in converting
PDE examples to Python ones.
"""
from __future__ import with_statement
import logging
from optparse import OptionParser
import os
import re
import shutil
import sys
def usage():
print >> sys.stderr, 'Usage: pde2py [-f|--force] srcdir destdir'
sys.exit(1)
parser = OptionParser()
parser.add_option("-f", "--force",
action="store_true", dest="force", default=False,
help="overwrite existing files")
(opts, args) = parser.parse_args()
if len(args) < 2:
usage()
src, dest = args
if not (os.path.exists(src) and os.path.isdir(src)):
usage()
if os.path.exists(dest):
shutil.rmtree(dest)
os.makedirs(dest)
def copy_dir(s, d):
if not os.path.exists(d):
os.mkdir(d)
for f in os.listdir(s):
if f[0] == '.':
continue
copy(os.path.join(s, f), os.path.join(d, f))
def copy_file(s, d, xform=None):
with open(s, 'rb') as f:
text = f.read()
if xform:
(d, text) = xform(d, text)
if os.path.exists(d):
if opts.force:
logging.info('Overwriting %s.' % d)
else:
logging.warning('Not overwriting %s.' % d)
else:
logging.info('Writing %s.' % d)
with open(d, 'wb') as f:
f.write(text)
def xform_py(d, text, ext="pyde"):
d = re.sub(r'^(.+?).pde$', r'\1.' + ext, d)
# Remove closing brace.
text = text.replace('}', '')
# Remove trailing spaces - first pass.
text = re.sub(r'(?m)[ ]{1,}$', '', text)
# Fix inline comments.
text = text.replace('//', '#')
# Tabs to spaces
text = re.sub(r'\t', r' ', text)
# Save "history" for indent. We may end up with extraneous double-spaces
# after everything else; we have no way of distinguishing between them and
# legitimate indent, so we'll convert to tabs temporarily.
text = re.sub(r' {2}', r'\t', text)
# Fix functions - remove return type identifiers and add 'def' keyword.
text = re.sub(
r'(?m)^(\s*)(?:public void|void|public int|int|public float|float|public String|String)\s+([a-zA-Z0-9]+)\s*\(([^\)]*)\)',
r'\1def \2(\3):',
text)
# Fix class definitions.
text = re.sub(
r'(?m)^\s*(?:abstract\s+)?class\s+(\S+)\s*$', r'class \1:', text)
text = re.sub(
r'(?m)^\s*(?:abstract\s+)?class\s+(\S+)\s*extends\s*(\S+)\s*$',
r'class \1(\2):',
text)
text = re.sub(r'(?m)^(\s*)(?:void|int|float|String|public)\s+', r'\1', text)
# Remove as many type identifiers as possible.
text = re.sub(
r'\b(int|byte|short|long|float|double|boolean|char|String|final)(\(|\s|\[)+?',
r'',
text)
text = re.sub(
r'\((int|byte|short|long|float|double|boolean|char|String|final|Integer|Object)\)',
r'',
text)
# Fix 'if'.
text = re.sub(r'(?m)^(\s*)if\s*(?:\()?(.+\)*?)(?:\))(.*)(?:\{)*$', r'\1if \2:\r\3', text)
# Fix 'else if'.
text = re.sub(r'(?m)^(\s*)else\s+if\s*(?:\()?(.+\)*?)(?:\))(.*)(?:\{)*$', r'\1elif \2:', text)
# Fix 'else'.
text = re.sub(r'(?m)^(\s*)else(.*)(?:\{)*$', r'\1else:\r\2', text)
# Fix block comments (convert to docstrings, really).
text = re.sub(r'/\*+|\*+/', '"""', text)
# Remove 'new'.
text = text.replace('new ', '')
# Fix booleans.
text = text.replace('true', 'True')
text = text.replace('false', 'False')
# Fix 'this'.
text = text.replace('this(\.?)', 'self\1')
# Fix boolean operators.
text = text.replace('||', ' or ')
text = text.replace('&&', ' and ')
# Remove 'f' (float coercion... I guess...
# How is '0.09f' different from '0.09'?! ::smh::).
text = re.sub(r'(\d)f', r'\1', text)
# Add spaces after commas.
text = text.replace(',', ', ')
# Remove spaces before colons.
text = text.replace(' :', ':')
# Add spaces around operators.
text = re.sub(
r'([a-zA-Z0-9_()]+?)(!=|\*=|\+=|-=|\/=|==|>=|<=|=|\*|\+|-|\/|>|<|%|&)([a-zA-Z0-9_()]+?)',
r'\1 \2 \3',
text)
# Remove spaces around parens/brackets.
text = re.sub(r'(?: {1,})(\)|\])', r'\1', text)
text = re.sub(r'(\(|\[)(?: {1,})', r'\1', text)
# Fix up 'for'. Hopefully this makes it a *bit* more readable. In any
# case, we'll try to at least get the colon in at the end.
text = re.sub(
r'(?m)^(\s*)for\s*(?:\()(?:int|float)*(.*?);(.*?);(.*?)(?:\)).*$',
r'\1for \2 in range(\3): # \4',
text)
# Fix up imports. Obviously, since we're going from 'import *' to
# specifics, rthe end user has to figure out what to import. That
# being the cawse, I'm commenting out the imports to (hopefuilly)
# call attention to them. In any case, they'll definitely be found
# by any good linter
text = re.sub(r'(?m)^import(.*)\.\*;', r'# from\1 import', text)
# Remove brackets/remaining braces/semicolons.
text = re.sub(r'[{;]', '', text)
# Remove multiple spaces.
text = re.sub(r'( ){2,}', ' ', text)
# Remove trailing spaces, second pass.
text = re.sub(r'(?m)[ ]{1,}$', '', text)
# Restore indent with 4 spaces.
text = re.sub(r'\t', ' ', text)
# Remove multiple blank lines.
text = re.sub(r'(?m)^\n{3}', '\n', text)
return (d, text)
def copy(s, d):
if os.path.isdir(s):
copy_dir(s, d)
elif s.endswith(".pde"):
copy_file(s, d, xform_py)
else:
copy_file(s, d)
copy(src, dest)