forked from shedsaw/exciting-plus-rgvw-mod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ftemplate.py
57 lines (53 loc) · 1.56 KB
/
ftemplate.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
# ftemplate - Fortran Templates
import sys
tidx=0
for line in sys.stdin.readlines():
sline=line.strip()
# default: write the line
wline=1
# scan for python code outside of a template
if sline.find("@python")==0 and tidx==0:
wline=0
py_code=sline.replace("@python","").strip()
# execute the code
exec(py_code)
# we are at the beginning of a template
if sline.find("@template")==0 and sline.find("begin")!=-1:
wline=0
tidx=1
template=[]
# we are in the template
if tidx==1:
wline=0
template.append(line)
# now we have the full template; let's unroll it
if sline.find("@template")==0 and sline.find("end")!=-1:
wline=0
tidx=0
# remove first and last lines which are '@template begin' and '@template end'
template.pop()
template.pop(0)
# scan for variables, body and initial python code
variables=[]
body=[]
for l in template:
sl=l.strip()
if sl.find("@template")==0 and sl.find("variable")!=1:
varname=sl.replace("@template","").replace("variable","").strip()
variables.append(varname)
if sl.find("@")!=0:
body.append(l)
if sl.find("@python")==0:
py_code_initial=sl.replace("@python","").strip()
# main trick: compose python code from initial code and templated text
tstr=""
for l in body:
tstr=tstr+l
# replace templates with variables
s="tstr"
for v in variables:
s=s+".replace(\"#"+v+"\","+v+")"
exec(py_code_initial+" sys.stdout.write("+s+")");
# write normal line
if wline==1:
sys.stdout.write(line)