Prepare generate_symbols* for python3

This is the usual encoding and print stuff, but the scripts don't run yet
under python 3 because of the missing Image module.
This commit is contained in:
Georg Baum 2016-06-05 12:51:07 +02:00
parent e9fa76054e
commit 96d63d28a7
2 changed files with 26 additions and 22 deletions

View File

@ -17,8 +17,10 @@
# created images should never be replaced by automatically created ones. # created images should never be replaced by automatically created ones.
from __future__ import print_function
import os, re, string, sys, subprocess, tempfile, shutil import os, re, string, sys, subprocess, tempfile, shutil
import Image import Image
import io
def usage(prog_name): def usage(prog_name):
return ("Usage: %s lyxexe outputpath\n" % prog_name) return ("Usage: %s lyxexe outputpath\n" % prog_name)
@ -65,7 +67,7 @@ def getreplacements(filename):
replacements['*'] = 'ast' replacements['*'] = 'ast'
replacements['AA'] = 'textrm_AA' replacements['AA'] = 'textrm_AA'
replacements['O'] = 'textrm_O' replacements['O'] = 'textrm_O'
cppfile = open(filename, 'rt') cppfile = io.open(filename, 'r', encoding='utf_8')
regexp = re.compile(r'.*"([^"]+)",\s*"([^"]+)"') regexp = re.compile(r'.*"([^"]+)",\s*"([^"]+)"')
found = False found = False
for line in cppfile.readlines(): for line in cppfile.readlines():
@ -81,7 +83,7 @@ def getreplacements(filename):
def gettoolbaritems(filename): def gettoolbaritems(filename):
items = [] items = []
uifile = open(filename, 'rt') uifile = io.open(filename, 'r', encoding='utf_8')
regexp = re.compile(r'.*Item "([^"\[]+)(\[\[[^\]]+\]\])?"\s*"math-insert\s+([^"]+)"') regexp = re.compile(r'.*Item "([^"\[]+)(\[\[[^\]]+\]\])?"\s*"math-insert\s+([^"]+)"')
for line in uifile.readlines(): for line in uifile.readlines():
m = regexp.match(line) m = regexp.match(line)
@ -93,7 +95,7 @@ def gettoolbaritems(filename):
def getmakefileentries(filename): def getmakefileentries(filename):
items = [] items = []
makefile = open(filename, 'rt') makefile = io.open(filename, 'r', encoding='utf_8')
regexp = re.compile(r'.*images/math/(.+)\.(png|svgz)') regexp = re.compile(r'.*images/math/(.+)\.(png|svgz)')
for line in makefile.readlines(): for line in makefile.readlines():
m = regexp.match(line) m = regexp.match(line)
@ -108,13 +110,13 @@ def createimage(name, path, template, lyxexe, tempdir, math, replacements, toolb
if name in replacements.keys(): if name in replacements.keys():
filename = replacements[name] filename = replacements[name]
elif name.startswith('lyx'): elif name.startswith('lyx'):
print 'Skipping ' + name print('Skipping ' + name)
return return
else: else:
skipchars = ['|', '/', '\\', '*', '!', '?', ':', ';', '^', '<', '>'] skipchars = ['|', '/', '\\', '*', '!', '?', ':', ';', '^', '<', '>']
for i in skipchars: for i in skipchars:
if name.find(i) >= 0: if name.find(i) >= 0:
print 'Skipping ' + name print( 'Skipping ' + name)
return return
filename = name filename = name
pngname = os.path.join(path, filename + '.png') pngname = os.path.join(path, filename + '.png')
@ -129,11 +131,11 @@ def createimage(name, path, template, lyxexe, tempdir, math, replacements, toolb
else: else:
suffix = ' (not found)' suffix = ' (not found)'
if os.path.exists(pngname): if os.path.exists(pngname):
print 'Skipping ' + name + suffix print('Skipping ' + name + suffix)
return return
print 'Generating ' + name + suffix print('Generating ' + name + suffix)
lyxname = os.path.join(tempdir, filename) lyxname = os.path.join(tempdir, filename)
lyxfile = open(lyxname + '.lyx', 'wt') lyxfile = io.open(lyxname + '.lyx', 'w', encoding='utf_8')
if math: if math:
lyxfile.write(template.replace('$a$', '$\\' + name + '$')) lyxfile.write(template.replace('$a$', '$\\' + name + '$'))
else: else:
@ -143,7 +145,7 @@ def createimage(name, path, template, lyxexe, tempdir, math, replacements, toolb
proc = subprocess.Popen(cmd, shell=True) proc = subprocess.Popen(cmd, shell=True)
proc.wait() proc.wait()
if proc.returncode != 0: if proc.returncode != 0:
print 'Error in DVI creation for ' + name print('Error in DVI creation for ' + name)
return return
# The magnifaction factor is calculated such that we get an image of # The magnifaction factor is calculated such that we get an image of
# height 18 px for most symbols and document font size 11. Then we can # height 18 px for most symbols and document font size 11. Then we can
@ -152,7 +154,7 @@ def createimage(name, path, template, lyxexe, tempdir, math, replacements, toolb
proc = subprocess.Popen(cmd, shell=True) proc = subprocess.Popen(cmd, shell=True)
proc.wait() proc.wait()
if proc.returncode != 0: if proc.returncode != 0:
print 'Error in PNG creation for ' + name print('Error in PNG creation for ' + name)
return return
image = Image.open(pngname) image = Image.open(pngname)
(width, height) = image.size (width, height) = image.size
@ -185,7 +187,7 @@ def main(argv):
makefile = os.path.join(os.path.dirname(base), '../../lib/Makefile.am') makefile = os.path.join(os.path.dirname(base), '../../lib/Makefile.am')
makefileentries = getmakefileentries(makefile) makefileentries = getmakefileentries(makefile)
lyxtemplate = base + '.lyx' lyxtemplate = base + '.lyx'
templatefile = open(base + '.lyx', 'rt') templatefile = io.open(base + '.lyx', 'r', encoding='utf_8')
template = templatefile.read() template = templatefile.read()
templatefile.close() templatefile.close()
tempdir = tempfile.mkdtemp() tempdir = tempfile.mkdtemp()

View File

@ -18,8 +18,10 @@
# created images should never be replaced by automatically created ones. # created images should never be replaced by automatically created ones.
from __future__ import print_function
import os, re, string, sys, subprocess, tempfile, shutil import os, re, string, sys, subprocess, tempfile, shutil
import Image import Image
import io
def usage(prog_name): def usage(prog_name):
return ("Usage: %s lyxexe outputpath\n" % prog_name) return ("Usage: %s lyxexe outputpath\n" % prog_name)
@ -66,7 +68,7 @@ def getreplacements(filename):
replacements['*'] = 'ast' replacements['*'] = 'ast'
replacements['AA'] = 'textrm_AA' replacements['AA'] = 'textrm_AA'
replacements['O'] = 'textrm_O' replacements['O'] = 'textrm_O'
cppfile = open(filename, 'rt') cppfile = io.open(filename, 'r', encoding='utf_8')
regexp = re.compile(r'.*"([^"]+)",\s*"([^"]+)"') regexp = re.compile(r'.*"([^"]+)",\s*"([^"]+)"')
found = False found = False
for line in cppfile.readlines(): for line in cppfile.readlines():
@ -82,7 +84,7 @@ def getreplacements(filename):
def gettoolbaritems(filename): def gettoolbaritems(filename):
items = [] items = []
uifile = open(filename, 'rt') uifile = io.open(filename, 'r', encoding='utf_8')
regexp = re.compile(r'.*Item "([^"\[]+)(\[\[[^\]]+\]\])?"\s*"math-insert\s+([^"]+)"') regexp = re.compile(r'.*Item "([^"\[]+)(\[\[[^\]]+\]\])?"\s*"math-insert\s+([^"]+)"')
for line in uifile.readlines(): for line in uifile.readlines():
m = regexp.match(line) m = regexp.match(line)
@ -94,7 +96,7 @@ def gettoolbaritems(filename):
def getmakefileentries(filename): def getmakefileentries(filename):
items = [] items = []
makefile = open(filename, 'rt') makefile = io.open(filename, 'r', encoding='utf_8')
regexp = re.compile(r'.*images/math/(.+)\.(png|svgz)') regexp = re.compile(r'.*images/math/(.+)\.(png|svgz)')
for line in makefile.readlines(): for line in makefile.readlines():
m = regexp.match(line) m = regexp.match(line)
@ -109,13 +111,13 @@ def createimage(name, path, template, lyxexe, tempdir, math, replacements, toolb
if name in replacements.keys(): if name in replacements.keys():
filename = replacements[name] filename = replacements[name]
elif name.startswith('lyx'): elif name.startswith('lyx'):
print 'Skipping ' + name print('Skipping ' + name)
return return
else: else:
skipchars = ['|', '/', '\\', '*', '!', '?', ':', ';', '^', '<', '>'] skipchars = ['|', '/', '\\', '*', '!', '?', ':', ';', '^', '<', '>']
for i in skipchars: for i in skipchars:
if name.find(i) >= 0: if name.find(i) >= 0:
print 'Skipping ' + name print('Skipping ' + name)
return return
filename = name filename = name
svgname = os.path.join(path, filename + '.svgz') svgname = os.path.join(path, filename + '.svgz')
@ -130,11 +132,11 @@ def createimage(name, path, template, lyxexe, tempdir, math, replacements, toolb
else: else:
suffix = ' (not found)' suffix = ' (not found)'
if os.path.exists(svgname): if os.path.exists(svgname):
print 'Skipping ' + name + suffix print('Skipping ' + name + suffix)
return return
print 'Generating ' + name + suffix print('Generating ' + name + suffix)
lyxname = os.path.join(tempdir, filename) lyxname = os.path.join(tempdir, filename)
lyxfile = open(lyxname + '.lyx', 'wt') lyxfile = io.open(lyxname + '.lyx', 'w', encoding='utf_8')
if math: if math:
lyxfile.write(template.replace('$a$', '$\\' + name + '$')) lyxfile.write(template.replace('$a$', '$\\' + name + '$'))
else: else:
@ -144,7 +146,7 @@ def createimage(name, path, template, lyxexe, tempdir, math, replacements, toolb
proc = subprocess.Popen(cmd, shell=True) proc = subprocess.Popen(cmd, shell=True)
proc.wait() proc.wait()
if proc.returncode != 0: if proc.returncode != 0:
print 'Error in DVI creation for ' + name print('Error in DVI creation for ' + name)
return return
# The magnifaction factor is calculated such that we get an image of # The magnifaction factor is calculated such that we get an image of
# height 18 px for most symbols and document font size 11. Then we can # height 18 px for most symbols and document font size 11. Then we can
@ -153,7 +155,7 @@ def createimage(name, path, template, lyxexe, tempdir, math, replacements, toolb
proc = subprocess.Popen(cmd, shell=True) proc = subprocess.Popen(cmd, shell=True)
proc.wait() proc.wait()
if proc.returncode != 0: if proc.returncode != 0:
print 'Error in SVG creation for ' + name print('Error in SVG creation for ' + name)
return return
@ -169,7 +171,7 @@ def main(argv):
makefile = os.path.join(os.path.dirname(base), '../../lib/Makefile.am') makefile = os.path.join(os.path.dirname(base), '../../lib/Makefile.am')
makefileentries = getmakefileentries(makefile) makefileentries = getmakefileentries(makefile)
lyxtemplate = base + '.lyx' lyxtemplate = base + '.lyx'
templatefile = open(base + '.lyx', 'rt') templatefile = io.open(base + '.lyx', 'r', encoding='utf_8')
template = templatefile.read() template = templatefile.read()
templatefile.close() templatefile.close()
tempdir = tempfile.mkdtemp() tempdir = tempfile.mkdtemp()