6 if SYS.version_info >= (3,):
7 from io import StringIO
10 from cStringIO import StringIO
12 PROG = OS.path.basename(SYS.argv[0])
13 USAGE = "usage: %s TEMPLATE INPUT TPLOUT OUTPUT" % PROG
15 ## The TEMPLATE contains text containing placeholders of the form
19 ## TAG is some string, and PAT is a Python regular expression. The TEMPLATE
20 ## matches the INPUT if there is some way to replace each placeholder by a
21 ## string matching its PAT such that the two are equal.
23 ## The TPLOUT file is a copy of the TEMPLATE file, with each placeholder
24 ## replaced by ={TAG}. If the TEMPLATE matches the INPUT, then the OUTPUT
25 ## equals TPLOUT; otherwise, the program tries to replace as many portions of
26 ## the INPUT which match placeholder PATs as it can, but it doesn't currently
27 ## do an especially good job.
29 if len(SYS.argv) != 5:
30 SYS.stderr.write("%s\n" % USAGE)
32 _, tplfn, infn, toutfn, outfn = SYS.argv
34 tfin = open(tplfn, "r"); tfout = open(toutfn, "w")
35 fin = open(infn, "r"); fout = open(outfn, "w")
37 R_PLCH = RX.compile(r"""
38 = \{ ([^}:]+) : ((?: [^\\}]+ | \\.)*) \}
42 t = tfin.readline(); l = fin.readline()
43 if not (t or l): break
45 lit = []; tag = []; pat = []; pos = 0
46 for m in R_PLCH.finditer(t):
47 lit.append(t[pos:m.start()])
48 tag.append(m.group(1))
49 pat.append(m.group(2))
54 skelsio = StringIO(); gensio = StringIO(); xctsio = StringIO()
55 gensio.write("^"); xctsio.write("^")
58 skelsio.write("%s={%s}" % (lit[i], tag[i]))
59 xctsio.write(q + pat[i])
60 gensio.write(q + "(.*)")
62 skelsio.write(lit[n]); skel = skelsio.getvalue()
63 xctsio.write(q); xctsio.write("$"); xct = xctsio.getvalue()
64 gensio.write(q); gensio.write("$"); gen = gensio.getvalue()
69 if RX.match(xct, l): fout.write(skel); continue
72 if not m: fout.write(l); continue
76 if RX.match("^%s$" % pat[i], m.group(i + 1)): sio.write("={%s}" % tag[i])
77 else: sio.write("!{%s:%s}" % (tag[i], m.group(i + 1)))
79 fout.write(sio.getvalue())
81 tfin.close(); tfout.close()
82 fin.close(); fout.close()