cmake documentation handling: sort out encoding problems.

Let srcipt work under Python2 and Python3,
also if the locale is not set to utf8.
This commit is contained in:
Günter Milde 2017-10-21 22:25:59 +02:00
parent 72122beee8
commit 9086db662d

View File

@ -13,18 +13,18 @@ from __future__ import print_function
import sys import sys
import re import re
import codecs
Subst = {} # map of desired substitutions Subst = {} # map of desired substitutions
prog = re.compile("") prog = re.compile("")
def createProg(): def createProg():
matchingS = "\\b|\\b".join(Subst.keys()) matchingS = u"\\b|\\b".join(Subst.keys())
pattern = "".join(["(.*)(\\b", matchingS, "\\b)(.*)"]) pattern = u"".join(["(.*)(\\b", matchingS, "\\b)(.*)"])
return re.compile(pattern) return re.compile(pattern)
def SubstituteDataInLine(line): def SubstituteDataInLine(line):
result = line m = prog.match(line)
m = prog.match(result)
if m: if m:
return "".join([SubstituteDataInLine(m.group(1)), return "".join([SubstituteDataInLine(m.group(1)),
Subst[m.group(2)], Subst[m.group(2)],
@ -33,25 +33,26 @@ def SubstituteDataInLine(line):
def SubstituteDataInFile(InFile): def SubstituteDataInFile(InFile):
for line in open(InFile): for line in codecs.open(InFile, 'r', 'UTF-8'):
print(SubstituteDataInLine(line[:-1])) print(SubstituteDataInLine(line[:-1]))
########################################## ##########################################
# 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)
args = sys.argv for arg in sys.argv[1:]: # skip first arg (name of this script)
if sys.version_info < (3,0):
del args[0] # we don't need the name ot this script # support non-ASCII characters in arguments
while len(args) > 0: arg = arg.decode(sys.stdin.encoding or 'UTF-8')
arg = args[0] entry = arg.split("=", 1)
entry = args[0].split("=",1)
if len(entry) == 2: if len(entry) == 2:
key=entry[0] key, val = entry
val=entry[1]
if len(key) > 0: if len(key) > 0:
Subst[key] = val Subst[key] = val
else: else:
prog = createProg() prog = createProg()
SubstituteDataInFile(args[0]) SubstituteDataInFile(arg)
del args[0]