]> git.lyx.org Git - lyx.git/blob - development/cmake/doc/ReplaceValues.py
ReplaceValues.py: make the file compilable with Python 3
[lyx.git] / development / cmake / doc / ReplaceValues.py
1 #! /usr/bin/env python
2
3 from __future__ import print_function
4
5 # file ReplaceValues.py
6 #
7 # This file is part of LyX, the document processor.
8 # Licence details can be found in the file COPYING.
9 #
10 # author: Kornel Benko, kornel@lyx.org
11 #
12 # Syntax: ReplaceValues.py [<var1>=<Subst1> [<var2>=<Subst> ...]] <Inputfile> [<Inputfile> ...]
13
14 import sys
15 import re
16 import codecs
17
18 Subst = {}  # map of desired substitutions
19 prog = re.compile("")
20
21 def createProg():
22     matchingS = "\\b|\\b".join(Subst.keys())
23     pattern = "".join(["(.*)(\\b", matchingS, "\\b)(.*)"])
24     return re.compile(pattern)
25
26 def SubstituteDataInLine(line):
27     result = line
28     m = prog.match(result)
29     if m:
30         return "".join([SubstituteDataInLine(m.group(1)),
31                         Subst[m.group(2)],
32                         SubstituteDataInLine(m.group(3))])
33     return line
34
35
36 def SubstituteDataInFile(InFile):
37     for line in codecs.open(InFile, 'r', 'utf-8'):
38         print(SubstituteDataInLine(line[:-1]).encode("utf-8"))
39
40 ##########################################
41
42
43 args = sys.argv
44
45 del args[0] # we don't need the name ot this script
46 while len(args) > 0:
47     arg = args[0]
48     entry = args[0].split("=",1)
49     if len(entry) == 2:
50         key=entry[0]
51         val=entry[1]
52         if len(key) > 0:
53             Subst[key] = val
54     else:
55         prog = createProg()
56         SubstituteDataInFile(args[0])
57     del args[0]
58