2018-12-29 19:14:41 +00:00
|
|
|
#! /usr/bin/python3
|
2011-05-21 13:48:45 +00:00
|
|
|
|
2015-03-11 19:54:28 +00:00
|
|
|
from __future__ import print_function
|
|
|
|
|
2011-05-21 13:48:45 +00:00
|
|
|
# file ReplaceValues.py
|
|
|
|
#
|
|
|
|
# This file is part of LyX, the document processor.
|
|
|
|
# Licence details can be found in the file COPYING.
|
|
|
|
#
|
|
|
|
# author: Kornel Benko, kornel@lyx.org
|
|
|
|
#
|
|
|
|
# Syntax: ReplaceValues.py [<var1>=<Subst1> [<var2>=<Subst> ...]] <Inputfile> [<Inputfile> ...]
|
|
|
|
|
2011-05-20 06:40:04 +00:00
|
|
|
import sys
|
|
|
|
import re
|
2017-10-16 21:29:57 +00:00
|
|
|
import codecs
|
2011-05-20 06:40:04 +00:00
|
|
|
|
|
|
|
Subst = {} # map of desired substitutions
|
|
|
|
prog = re.compile("")
|
|
|
|
|
|
|
|
def createProg():
|
2017-10-29 08:49:39 +00:00
|
|
|
matchingS = u"\\b|\\b".join(Subst.keys())
|
|
|
|
pattern = u"".join(["(.*)(\\b", matchingS, "\\b)(.*)"])
|
2011-05-20 06:40:04 +00:00
|
|
|
return re.compile(pattern)
|
|
|
|
|
|
|
|
def SubstituteDataInLine(line):
|
2017-10-29 08:49:39 +00:00
|
|
|
m = prog.match(line)
|
2011-05-20 06:40:04 +00:00
|
|
|
if m:
|
|
|
|
return "".join([SubstituteDataInLine(m.group(1)),
|
|
|
|
Subst[m.group(2)],
|
|
|
|
SubstituteDataInLine(m.group(3))])
|
|
|
|
return line
|
|
|
|
|
|
|
|
|
|
|
|
def SubstituteDataInFile(InFile):
|
2017-10-29 08:49:39 +00:00
|
|
|
for line in codecs.open(InFile, 'r', 'UTF-8'):
|
2017-10-16 22:23:19 +00:00
|
|
|
print(SubstituteDataInLine(line[:-1]))
|
2011-05-20 06:40:04 +00:00
|
|
|
|
|
|
|
##########################################
|
|
|
|
|
2017-10-29 08:49:39 +00:00
|
|
|
# ensure standard output with UTF8 encoding:
|
|
|
|
if sys.version_info < (3,0):
|
|
|
|
sys.stdout = codecs.getwriter('UTF-8')(sys.stdout)
|
|
|
|
else:
|
|
|
|
sys.stdout = codecs.getwriter('UTF-8')(sys.stdout.buffer)
|
|
|
|
|
|
|
|
for arg in sys.argv[1:]: # skip first arg (name of this script)
|
|
|
|
if sys.version_info < (3,0):
|
|
|
|
# support non-ASCII characters in arguments
|
|
|
|
arg = arg.decode(sys.stdin.encoding or 'UTF-8')
|
|
|
|
entry = arg.split("=", 1)
|
2011-05-20 06:40:04 +00:00
|
|
|
if len(entry) == 2:
|
2017-10-29 08:49:39 +00:00
|
|
|
key, val = entry
|
2011-05-20 06:40:04 +00:00
|
|
|
if len(key) > 0:
|
|
|
|
Subst[key] = val
|
|
|
|
else:
|
|
|
|
prog = createProg()
|
2017-10-29 08:49:39 +00:00
|
|
|
SubstituteDataInFile(arg)
|