/
pde2py.py
executable file
·72 lines (62 loc) · 2.14 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
#!/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 os
import re
import shutil
import sys
(_, sketch) = sys.argv
tld = 'C:/helios/workspace/zamples'
src = '%s/examples/%s' % (tld, sketch)
if not (os.path.exists(src) and os.path.isdir(src)):
raise Exception("I expect the first argument to be the source directory.")
dest = '%s/py/%s' % (tld, sketch)
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 file in os.listdir(s):
if file[0] == '.':
continue
copy(os.path.join(s, file), os.path.join(d, file))
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):
raise Exception("I refuse to overwrite %s." % d)
with open(d, 'wb') as f:
f.write(text)
def xform_py(d, text):
d = re.sub(r'^(.+?).pde$', r'\1.py', d)
text = text.replace('//', '#')
text = text.replace(' ', ' ')
text = re.sub(r'(?m)^(\s*)(?:void|int|float|String)\s+([a-zA-Z0-9]+)\s*\(([^\)]*)\)',
r'\1def \2(\3):',
text)
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)\s+', r'\1', text)
text = re.sub(r'[{};]', '', text)
text = re.sub(r'\n\n+', '\n', text)
text = re.sub(r'(?m)^(\s*)if\s*\((.+?)\)\s*$', r'\1if \2:', text)
text = re.sub(r'(?m)^(\s*)else\s+if\s*\((.+?)\)\s*$', r'\1elif \2:', text)
text = re.sub(r'(?m)^(\s*)else\s*$', r'\1else:', text)
text = re.sub(r'/\*+|\*+/', '"""', text)
text = text.replace('new ', '')
text=text.replace('this.', 'self.')
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)