commit 11f2a59ce90b406537ff3379276d5e0bb1bf6c37
Author: José Matos <[email protected]>
Date: Tue May 9 11:20:34 2017 +0100
python3: fix the preview framework to work with both python 2 and 3
---
lib/scripts/legacy_lyxpreview2ppm.py | 16 ++++++++--------
lib/scripts/lyxpreview2bitmap.py | 28 +++++++++++++++-------------
lib/scripts/lyxpreview_tools.py | 12 ++++++------
3 files changed, 29 insertions(+), 27 deletions(-)
diff --git a/lib/scripts/legacy_lyxpreview2ppm.py
b/lib/scripts/legacy_lyxpreview2ppm.py
index 3049b44..6a91402 100644
--- a/lib/scripts/legacy_lyxpreview2ppm.py
+++ b/lib/scripts/legacy_lyxpreview2ppm.py
@@ -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]
diff --git a/lib/scripts/lyxpreview2bitmap.py b/lib/scripts/lyxpreview2bitmap.py
index 95bb895..68d0a4d 100755
--- a/lib/scripts/lyxpreview2bitmap.py
+++ b/lib/scripts/lyxpreview2bitmap.py
@@ -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":
diff --git a/lib/scripts/lyxpreview_tools.py b/lib/scripts/lyxpreview_tools.py
index 2012927..b7a89dc 100644
--- a/lib/scripts/lyxpreview_tools.py
+++ b/lib/scripts/lyxpreview_tools.py
@@ -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):