3 from __future__ import print_function
5 # file ReplaceValues.py
7 # This file is part of LyX, the document processor.
8 # Licence details can be found in the file COPYING.
10 # author: Kornel Benko, kornel@lyx.org
12 # Syntax: ReplaceValues.py [<var1>=<Subst1> [<var2>=<Subst> ...]] <Inputfile> [<Inputfile> ...]
18 Subst = {} # map of desired substitutions
22 matchingS = u"\\b|\\b".join(Subst.keys())
23 pattern = u"".join(["(.*)(\\b", matchingS, "\\b)(.*)"])
24 return re.compile(pattern)
26 def SubstituteDataInLine(line):
29 return "".join([SubstituteDataInLine(m.group(1)),
31 SubstituteDataInLine(m.group(3))])
35 def SubstituteDataInFile(InFile):
36 for line in codecs.open(InFile, 'r', 'UTF-8'):
37 print(SubstituteDataInLine(line[:-1]))
39 ##########################################
41 # ensure standard output with UTF8 encoding:
42 if sys.version_info < (3,0):
43 sys.stdout = codecs.getwriter('UTF-8')(sys.stdout)
45 sys.stdout = codecs.getwriter('UTF-8')(sys.stdout.buffer)
47 for arg in sys.argv[1:]: # skip first arg (name of this script)
48 if sys.version_info < (3,0):
49 # support non-ASCII characters in arguments
50 arg = arg.decode(sys.stdin.encoding or 'UTF-8')
51 entry = arg.split("=", 1)
58 SubstituteDataInFile(arg)