python3: fix the preview framework to work with both python 2 and 3

This commit is contained in:
José Matos 2017-05-09 11:20:34 +01:00
parent 902b5f368b
commit 11f2a59ce9
3 changed files with 29 additions and 27 deletions

View File

@ -79,7 +79,7 @@
# If possible, the script will use pdftocairo instead of gs,
# as it's much faster and gives better results.
import glob, os, pipes, re, string, sys
import glob, os, pipes, re, sys
from lyxpreview_tools import check_latex_log, copyfileobj, error, filter_pages,\
find_exe, find_exe_or_terminate, join_metrics_and_rename, latex_commands, \
@ -118,8 +118,8 @@ def legacy_extract_metrics_info(log_file):
error("Unexpected data in %s\n%s" % (log_file, line))
if snippet:
ascent = string.atof(match.group(2))
descent = string.atof(match.group(3))
ascent = float(match.group(2))
descent = float(match.group(3))
frac = 0.5
if ascent == 0 and descent == 0:
@ -139,8 +139,8 @@ def legacy_extract_metrics_info(log_file):
results.append((int(match.group(1)), frac))
else:
tp_descent = string.atof(match.group(2))
tp_ascent = string.atof(match.group(4))
tp_descent = float(match.group(2))
tp_ascent = float(match.group(4))
except:
# Unable to open the file, but do nothing here because
@ -177,7 +177,7 @@ def extract_resolution(log_file, dpi):
match = extract_decimal_re.search(line)
if match == None:
error("Unable to parse: %s" % line)
fontsize = string.atof(match.group(1))
fontsize = float(match.group(1))
found_fontsize = 1
continue
@ -187,7 +187,7 @@ def extract_resolution(log_file, dpi):
match = extract_integer_re.search(line)
if match == None:
error("Unable to parse: %s" % line)
magnification = string.atof(match.group(1))
magnification = float(match.group(1))
found_magnification = 1
continue
@ -275,7 +275,7 @@ def legacy_conversion(argv, skipMetrics = False):
if len(dir) != 0:
os.chdir(dir)
dpi = string.atoi(argv[2])
dpi = int(argv[2])
output_format = argv[3]

View File

@ -75,7 +75,9 @@
# Moreover dvipng can't work with PDF files, so, if the CONVERTER
# paramter is pdflatex we have to fallback to legacy route (step 2).
import getopt, glob, os, re, shutil, string, sys
from __future__ import print_function
import getopt, glob, os, re, shutil, sys
from legacy_lyxpreview2ppm import extract_resolution, legacy_conversion_step1
@ -134,8 +136,8 @@ def extract_metrics_info(dvipng_stdout):
success = 1
# Calculate the 'ascent fraction'.
descent = string.atof(match.group(1))
ascent = string.atof(match.group(2))
descent = float(match.group(1))
ascent = float(match.group(2))
frac = 0.5
if ascent < 0:
@ -159,25 +161,25 @@ def extract_metrics_info(dvipng_stdout):
def fix_latex_file(latex_file, pdf_output):
def_re = re.compile(r"(\\newcommandx|\\global\\long\\def)(\\[a-zA-Z]+)")
def_re = re.compile(rb"(\\newcommandx|\\global\\long\\def)(\\[a-zA-Z]+)")
tmp = mkstemp()
changed = False
macros = []
for line in open(latex_file, 'r').readlines():
if not pdf_output and line.startswith("\\documentclass"):
for line in open(latex_file, 'rb').readlines():
if not pdf_output and line.startswith(b"\\documentclass"):
changed = True
line += "\\PassOptionsToPackage{draft}{microtype}\n"
line += b"\\PassOptionsToPackage{draft}{microtype}\n"
else:
match = def_re.match(line)
if match != None:
macroname = match.group(2)
if macroname in macros:
definecmd = match.group(1)
if definecmd == "\\newcommandx":
if definecmd == b"\\newcommandx":
changed = True
line = line.replace(definecmd, "\\renewcommandx")
line = line.replace(definecmd, b"\\renewcommandx")
else:
macros.append(macroname)
tmp.write(line)
@ -215,7 +217,7 @@ def find_ps_pages(dvi_file):
error("No DVI output.")
# Check for PostScript specials in the dvi, badly supported by dvipng,
# and inclusion of PDF/PNG/JPG files.
# and inclusion of PDF/PNG/JPG files.
# This is required for correct rendering of PSTricks and TikZ
dv2dt = find_exe_or_terminate(["dv2dt"])
dv2dt_call = '%s "%s"' % (dv2dt, dvi_file)
@ -324,13 +326,13 @@ def main(argv):
(opts, args) = getopt.gnu_getopt(argv[1:], "dhv", ["bibtex=", "bg=",
"debug", "dpi=", "fg=", "help", "latex=", "lilypond",
"lilypond-book=", "png", "ppm", "verbose"])
except getopt.GetoptError, err:
except getopt.GetoptError as err:
error("%s\n%s" % (err, usage(script_name)))
opts.reverse()
for opt, val in opts:
if opt in ("-h", "--help"):
print usage(script_name)
print(usage(script_name))
sys.exit(0)
elif opt == "--bibtex":
bibtex = [val]
@ -341,7 +343,7 @@ def main(argv):
lyxpreview_tools.debug = True
elif opt == "--dpi":
try:
dpi = string.atoi(val)
dpi = int(val)
except:
error("Cannot convert %s to an integer value" % val)
elif opt == "--fg":

View File

@ -15,7 +15,7 @@
# Requires python 2.4 or later (subprocess module).
import os, re, string, subprocess, sys, tempfile
import os, re, subprocess, sys, tempfile
# Control the output to stdout
@ -76,9 +76,9 @@ def make_texcolor(hexcolor, graphics):
if not hexcolor_re.match(hexcolor):
error("Cannot convert color '%s'" % hexcolor)
red = float(string.atoi(hexcolor[0:2], 16)) / 255.0
green = float(string.atoi(hexcolor[2:4], 16)) / 255.0
blue = float(string.atoi(hexcolor[4:6], 16)) / 255.0
red = float(int(hexcolor[0:2], 16)) / 255.0
green = float(int(hexcolor[2:4], 16)) / 255.0
blue = float(int(hexcolor[4:6], 16)) / 255.0
if graphics:
return "%f,%f,%f" % (red, green, blue)
@ -110,7 +110,7 @@ def find_exe(candidates):
def find_exe_or_terminate(candidates):
exe = find_exe(candidates)
if exe == None:
error("Unable to find executable from '%s'" % string.join(candidates))
error("Unable to find executable from '%s'" % " ".join(candidates))
return exe
@ -203,7 +203,7 @@ def get_version_info():
if match == None:
error("Unable to extract version info from 'sys.version'")
return string.atoi(match.group(1)), string.atoi(match.group(2))
return int(match.group(1)), int(match.group(2))
def copyfileobj(fsrc, fdst, rewind=0, length=16*1024):