python3: fix the preview framework to work with both python 2 and 3 (part 4)

Remove support for python 1.x (really)

This code has not been used for a long time, probably never, since some code
above requires at least python 2.4 to work.

I got to this code by running futurize from python-future. There are no
significant warnings, mostly are related with the division but since
we are dividing floats there is no change between python 2 and 3.
This commit is contained in:
José Matos 2017-05-12 10:01:48 +01:00
parent 19cc4a1fcb
commit 376cb6763f
3 changed files with 9 additions and 62 deletions

View File

@ -79,11 +79,11 @@
# If possible, the script will use pdftocairo instead of gs,
# as it's much faster and gives better results.
import glob, os, pipes, re, sys
import glob, os, pipes, re, sys, tempfile
from lyxpreview_tools import check_latex_log, copyfileobj, error, filter_pages,\
find_exe, find_exe_or_terminate, join_metrics_and_rename, latex_commands, \
latex_file_re, make_texcolor, mkstemp, pdflatex_commands, progress, \
latex_file_re, make_texcolor, pdflatex_commands, progress, \
run_command, run_latex, warning, write_metrics_info
@ -205,7 +205,7 @@ def legacy_latex_file(latex_file, fg_color, bg_color):
fg_color_gr = make_texcolor(fg_color, True)
bg_color_gr = make_texcolor(bg_color, True)
tmp = mkstemp()
tmp = tempfile.TemporaryFile()
success = 0
try:
@ -255,7 +255,7 @@ def crop_files(pnmcrop, basename):
t.append('%s -right' % pnmcrop, '--')
for file in glob.glob("%s*.ppm" % basename):
tmp = mkstemp()
tmp = tempfile.TemporaryFile()
new = t.open(file, "r")
copyfileobj(new, tmp)
if not new.close():

View File

@ -77,14 +77,14 @@
from __future__ import print_function
import getopt, glob, os, re, shutil, sys
import getopt, glob, os, re, shutil, sys, tempfile
from legacy_lyxpreview2ppm import extract_resolution, legacy_conversion_step1
from lyxpreview_tools import bibtex_commands, check_latex_log, copyfileobj, \
error, filter_pages, find_exe, find_exe_or_terminate, \
join_metrics_and_rename, latex_commands, latex_file_re, make_texcolor, \
mkstemp, pdflatex_commands, progress, run_command, run_latex, run_tex, \
pdflatex_commands, progress, run_command, run_latex, run_tex, \
warning, write_metrics_info
PY2 = sys.version_info[0] == 2
@ -167,7 +167,7 @@ def fix_latex_file(latex_file, pdf_output):
def_re = re.compile(b"(\\\\newcommandx|\\\\global\\\\long\\\\def)"
b"(\\\\[a-zA-Z]+)")
tmp = mkstemp()
tmp = tempfile.TemporaryFile()
changed = False
macros = []

View File

@ -10,7 +10,7 @@
# Paul A. Rubin, rubin@msu.edu.
# A repository of the following functions, used by the lyxpreview2xyz scripts.
# copyfileobj, error, find_exe, find_exe_or_terminate, make_texcolor, mkstemp,
# copyfileobj, error, find_exe, find_exe_or_terminate, make_texcolor,
# progress, run_command, run_latex, warning
# Requires python 2.4 or later (subprocess module).
@ -196,16 +196,6 @@ def run_command(cmd, stderr2stdout = True):
return run_command_popen(cmd, stderr2stdout)
def get_version_info():
version_re = re.compile("([0-9])\.([0-9])")
match = version_re.match(sys.version)
if match == None:
error("Unable to extract version info from 'sys.version'")
return int(match.group(1)), int(match.group(2))
def copyfileobj(fsrc, fdst, rewind=0, length=16*1024):
"""copy data from file-like object fsrc to file-like object fdst"""
if rewind:
@ -219,56 +209,13 @@ def copyfileobj(fsrc, fdst, rewind=0, length=16*1024):
fdst.write(buf)
class TempFile:
"""clone of tempfile.TemporaryFile to use with python < 2.0."""
# Cache the unlinker so we don't get spurious errors at shutdown
# when the module-level "os" is None'd out. Note that this must
# be referenced as self.unlink, because the name TempFile
# may also get None'd out before __del__ is called.
unlink = os.unlink
def __init__(self):
self.filename = tempfile.mktemp()
self.file = open(self.filename,"w+b")
self.close_called = 0
def close(self):
if not self.close_called:
self.close_called = 1
self.file.close()
self.unlink(self.filename)
def __del__(self):
self.close()
def read(self, size = -1):
return self.file.read(size)
def write(self, line):
return self.file.write(line)
def seek(self, offset):
return self.file.seek(offset)
def flush(self):
return self.file.flush()
def mkstemp():
"""create a secure temporary file and return its object-like file"""
major, minor = get_version_info()
if major >= 2 and minor >= 0:
return tempfile.TemporaryFile()
else:
return TempFile()
def write_metrics_info(metrics_info, metrics_file):
metrics = open(metrics_file, 'w')
for metric in metrics_info:
metrics.write("Snippet %s %f\n" % metric)
metrics.close()
# Reads a .tex files and create an identical file but only with
# pages whose index is in pages_to_keep
def filter_pages(source_path, destination_path, pages_to_keep):