[LyX/master] python3: fix the preview framework to work with both python 2 and 3 (part 4)

2017-05-12 Thread José Matos
commit 376cb6763fe1ea047fc3cfaad80969c3a5ebdd7e
Author: José Matos 
Date:   Fri May 12 10:01:48 2017 +0100

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.
---
 lib/scripts/legacy_lyxpreview2ppm.py |8 ++--
 lib/scripts/lyxpreview2bitmap.py |6 ++--
 lib/scripts/lyxpreview_tools.py  |   57 +
 3 files changed, 9 insertions(+), 62 deletions(-)

diff --git a/lib/scripts/legacy_lyxpreview2ppm.py 
b/lib/scripts/legacy_lyxpreview2ppm.py
index 36d57ca..99f4e56 100644
--- a/lib/scripts/legacy_lyxpreview2ppm.py
+++ b/lib/scripts/legacy_lyxpreview2ppm.py
@@ -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():
diff --git a/lib/scripts/lyxpreview2bitmap.py b/lib/scripts/lyxpreview2bitmap.py
index 6a49f16..e4bf131 100755
--- a/lib/scripts/lyxpreview2bitmap.py
+++ b/lib/scripts/lyxpreview2bitmap.py
@@ -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|globallongdef)"
 b"([a-zA-Z]+)")
 
-tmp = mkstemp()
+tmp = tempfile.TemporaryFile()
 
 changed = False
 macros = []
diff --git a/lib/scripts/lyxpreview_tools.py b/lib/scripts/lyxpreview_tools.py
index 8e6dee4..91cc4d6 100644
--- a/lib/scripts/lyxpreview_tools.py
+++ b/lib/scripts/lyxpreview_tools.py
@@ -10,7 +10,7 @@
 #   Paul A. Rubin, ru...@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 = 

[LyX/master] python3: fix the preview framework to work with both python 2 and 3 (part 3)

2017-05-11 Thread José Matos
commit 1f5241f7474b99699a8d1d6a9d60bcdc599790d7
Author: José Matos 
Date:   Thu May 11 20:56:56 2017 +0100

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

trivial fix: xrange -> range
---
 lib/scripts/lyxpreview2bitmap.py |2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/lib/scripts/lyxpreview2bitmap.py b/lib/scripts/lyxpreview2bitmap.py
index 11f2ba9..6a49f16 100755
--- a/lib/scripts/lyxpreview2bitmap.py
+++ b/lib/scripts/lyxpreview2bitmap.py
@@ -283,7 +283,7 @@ def find_ps_pages(dvi_file):
 
 # Use page ranges, as a list of pages could exceed command line
 # maximum length (especially under Win32)
-for index in xrange(1, page_index + 1):
+for index in range(1, page_index + 1):
 if (not index in ps_or_pdf_pages) and skip:
 # We were skipping pages but current page shouldn't be skipped.
 # Add this page to -pp, it could stay alone or become the


[LyX/master] python3: fix the preview framework to work with both python 2 and 3 (part 2)

2017-05-11 Thread José Matos
commit c546977c6eb7d780a2995b57c468f1e935fc9fe3
Author: José Matos 
Date:   Thu May 11 15:50:08 2017 +0100

python3: fix the preview framework to work with both python 2 and 3 (part 2)
---
 lib/scripts/legacy_lyxpreview2ppm.py |   32 
 lib/scripts/lyxpreview2bitmap.py |6 ++
 lib/scripts/lyxpreview_tools.py  |6 +++---
 3 files changed, 25 insertions(+), 19 deletions(-)

diff --git a/lib/scripts/legacy_lyxpreview2ppm.py 
b/lib/scripts/legacy_lyxpreview2ppm.py
index 6a91402..36d57ca 100644
--- a/lib/scripts/legacy_lyxpreview2ppm.py
+++ b/lib/scripts/legacy_lyxpreview2ppm.py
@@ -201,7 +201,7 @@ def extract_resolution(log_file, dpi):
 
 
 def legacy_latex_file(latex_file, fg_color, bg_color):
-use_preview_re = re.compile(r"\s*\\usepackage\[([^]]+)\]{preview}")
+use_preview_re = re.compile(b"\\s*usepackage\\[([^]]+)\\]{preview}")
 fg_color_gr = make_texcolor(fg_color, True)
 bg_color_gr = make_texcolor(bg_color, True)
 
@@ -209,7 +209,7 @@ def legacy_latex_file(latex_file, fg_color, bg_color):
 
 success = 0
 try:
-f = open(latex_file, 'r')
+f = open(latex_file, 'rb')
 except:
 # Unable to open the file, but do nothing here because
 # the calling function will act on the value of 'success'.
@@ -227,20 +227,20 @@ def legacy_latex_file(latex_file, fg_color, bg_color):
 success = 1
 # Package order: color should be loaded before preview
 # Preview options: add the options lyx and tightpage
-tmp.write(r"""
-\usepackage{color}
-\definecolor{fg}{rgb}{%s}
-\definecolor{bg}{rgb}{%s}
-\pagecolor{bg}
-\usepackage[%s,tightpage]{preview}
-\makeatletter
-\def\t@a{cmr}
-\if\f@family\t@a
-\IfFileExists{lmodern.sty}{\usepackage{lmodern}}{\usepackage{ae,aecompl}}
-\fi
-\g@addto@macro\preview{\begingroup\color{bg}\special{ps::clippath 
fill}\color{fg}}
-\g@addto@macro\endpreview{\endgroup}
-\makeatother
+tmp.write(b"""
+\\usepackage{color}
+\\definecolor{fg}{rgb}{%s}
+\\definecolor{bg}{rgb}{%s}
+\\pagecolor{bg}
+\\usepackage[%s,tightpage]{preview}
+\\makeatletter
+\\def\\t@a{cmr}
+\\if\\f@family\\t@a
+\\IfFileExists{lmodern.sty}{\\usepackage{lmodern}}{\\usepackage{ae,aecompl}}
+\\fi
+\\g@addto@macro\\preview{\\begingroup\\color{bg}\\special{ps::clippath 
fill}\\color{fg}}
+\\g@addto@macro\\endpreview{\\endgroup}
+\\makeatother
 """ % (fg_color_gr, bg_color_gr, match.group(1)))
 
 if success:
diff --git a/lib/scripts/lyxpreview2bitmap.py b/lib/scripts/lyxpreview2bitmap.py
index 1848366..11f2ba9 100755
--- a/lib/scripts/lyxpreview2bitmap.py
+++ b/lib/scripts/lyxpreview2bitmap.py
@@ -87,6 +87,7 @@ from lyxpreview_tools import bibtex_commands, 
check_latex_log, copyfileobj, \
  mkstemp, pdflatex_commands, progress, run_command, run_latex, run_tex, \
  warning, write_metrics_info
 
+PY2 = sys.version_info[0] == 2
 
 def usage(prog_name):
 msg = """
@@ -385,6 +386,11 @@ def main(argv):
 if len(dir) != 0:
 os.chdir(dir)
 
+# For python > 2 convert strings to bytes
+if not PY2:
+fg_color = bytes(fg_color, 'ascii')
+bg_color = bytes(bg_color, 'ascii')
+
 fg_color_dvipng = make_texcolor(fg_color, False)
 bg_color_dvipng = make_texcolor(bg_color, False)
 
diff --git a/lib/scripts/lyxpreview_tools.py b/lib/scripts/lyxpreview_tools.py
index b7a89dc..8e6dee4 100644
--- a/lib/scripts/lyxpreview_tools.py
+++ b/lib/scripts/lyxpreview_tools.py
@@ -72,7 +72,7 @@ def error(message):
 
 def make_texcolor(hexcolor, graphics):
 # Test that the input string contains 6 hexadecimal chars.
-hexcolor_re = re.compile("^[0-9a-fA-F]{6}$")
+hexcolor_re = re.compile(b"^[0-9a-fA-F]{6}$")
 if not hexcolor_re.match(hexcolor):
 error("Cannot convert color '%s'" % hexcolor)
 
@@ -81,9 +81,9 @@ def make_texcolor(hexcolor, graphics):
 blue  = float(int(hexcolor[4:6], 16)) / 255.0
 
 if graphics:
-return "%f,%f,%f" % (red, green, blue)
+return b"%f,%f,%f" % (red, green, blue)
 else:
-return "rgb %f %f %f" % (red, green, blue)
+return b"rgb %f %f %f" % (red, green, blue)
 
 
 def find_exe(candidates):


Re: [LyX/master] python3: fix the preview framework to work with both python 2 and 3

2017-05-09 Thread Jean-Marc Lasgouttes

Le 09/05/2017 à 12:21, José Matos a écrit :

commit 11f2a59ce90b406537ff3379276d5e0bb1bf6c37
Author: José Matos 
Date:   Tue May 9 11:20:34 2017 +0100

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


I just got the following. Is it related?

File "/home/lasgoutt/src/lyx/master/lib/scripts/lyxpreview2bitmap.py", 
line 164
def_re = 
re.compile(rb"(\\newcommandx|\\global\\long\\def)(\\[a-zA-Z]+)")


   ^
SyntaxError: invalid syntax


JMarc



[LyX/master] python3: fix the preview framework to work with both python 2 and 3

2017-05-09 Thread José Matos
commit 11f2a59ce90b406537ff3379276d5e0bb1bf6c37
Author: José Matos 
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 =