| 1 |
#!/usr/bin/env python |
| 2 |
# Copyright (c) Michael Sharpe, 2014 |
| 3 |
# This is free software, subject to the LATEX Project Public License. |
| 4 |
|
| 5 |
import sys |
| 6 |
|
| 7 |
filename=sys.argv[1] |
| 8 |
with open(filename,'r') as f: |
| 9 |
tmp=f.read().splitlines() |
| 10 |
s='\n'.join(tmp) |
| 11 |
tmp=s.split("%Repetitions=") |
| 12 |
if len(tmp) != 2: |
| 13 |
print "Bailing! The file does not contain exactly one '%Repetitions='" |
| 14 |
sys.exit() |
| 15 |
ss=tmp[1] |
| 16 |
tmp2=ss.split('\n') |
| 17 |
numrep=int(tmp2[0]) |
| 18 |
#get variable names |
| 19 |
tmp=s.split("%Variables=") |
| 20 |
if len(tmp) != 2: |
| 21 |
print "Bailing! The file does not contain exactly one '%Variables='" |
| 22 |
sys.exit() |
| 23 |
ss=tmp[1] |
| 24 |
tmp2=ss.split('\n') |
| 25 |
vbllst=tmp2[0].strip() #like {nnn,0:1+1,1:3+-2} |
| 26 |
vbllst3=vbllst[1:-1] |
| 27 |
vbles=vbllst3.split(',') #line {nnn,0:1+1,1:3+-2} |
| 28 |
n=len(vbles) |
| 29 |
root=vbles[0].strip() |
| 30 |
vlst=[] |
| 31 |
vinit=[] |
| 32 |
vinc=[] |
| 33 |
valt=[] |
| 34 |
for j in range(1,n): |
| 35 |
vbles[j]=vbles[j].strip().replace("+",":") |
| 36 |
x=vbles[j].split(":") |
| 37 |
if len(x) != 3: |
| 38 |
print "Bad variable descriptor-- " + vbles[j] |
| 39 |
sys.exit |
| 40 |
vlst.append(x[0]) |
| 41 |
nnn=0 |
| 42 |
hasp=0 |
| 43 |
if x[2][-1:] == ")": |
| 44 |
hasp=1 |
| 45 |
nnn=int(x[2][-2]) |
| 46 |
x[2]=x[2][:-3] |
| 47 |
else: |
| 48 |
if x[1][0] == "[": |
| 49 |
nnn=-100 |
| 50 |
if x[2][-2:]=="/2": |
| 51 |
x[2]=x[2][:-2] |
| 52 |
if hasp == 1: |
| 53 |
nnn=-nnn |
| 54 |
else: |
| 55 |
nnn=-10 |
| 56 |
valt.append(nnn) |
| 57 |
if nnn == -100: |
| 58 |
vinit.append(x[1][1:]) |
| 59 |
vinc.append(x[2][:-1]) |
| 60 |
else: |
| 61 |
vinit.append(x[1]) |
| 62 |
vinc.append(x[2]) |
| 63 |
|
| 64 |
viniti=[] |
| 65 |
vinci=[] |
| 66 |
kk=len(vinit) |
| 67 |
for j in range(kk): |
| 68 |
try: |
| 69 |
viniti.append(int(vinit[j])) |
| 70 |
except: |
| 71 |
viniti.append(0) |
| 72 |
try: |
| 73 |
vinci.append(int(vinc[j])) |
| 74 |
except: |
| 75 |
vinci.append(0) |
| 76 |
tmp=s.split('\n') |
| 77 |
m=len(tmp) |
| 78 |
tmp2=[] |
| 79 |
for j in range(2,m): |
| 80 |
t=tmp[j] |
| 81 |
if len(t) > 1: |
| 82 |
t=t[1:] |
| 83 |
else: |
| 84 |
t="" |
| 85 |
tmp2.append(t) |
| 86 |
body='\n'.join(tmp2) |
| 87 |
repl=[s,] |
| 88 |
for p in range(numrep): |
| 89 |
newbody=body |
| 90 |
for k in range(kk): |
| 91 |
v="" |
| 92 |
alt=valt[k] |
| 93 |
if alt == -100: |
| 94 |
if (p % 2) == 1: |
| 95 |
v=vinc[k] |
| 96 |
else: |
| 97 |
v=vinit[k] |
| 98 |
else: |
| 99 |
v=viniti[k] |
| 100 |
if (alt < 0): |
| 101 |
v=v//2 |
| 102 |
alt=-alt |
| 103 |
if (alt!=10) and (alt!= 0): |
| 104 |
v =str(v).zfill(alt) |
| 105 |
else: |
| 106 |
v=str(v) |
| 107 |
newbody=newbody.replace(root+vlst[k],v) |
| 108 |
viniti[k] =int(viniti[k]) + vinci[k] |
| 109 |
repl.append(newbody) |
| 110 |
|
| 111 |
with open(filename,'w') as f: |
| 112 |
for j in range(len(repl)): |
| 113 |
f.write(repl[j]+'\n\n') |
| 114 |
|