]> git.lyx.org Git - lyx.git/blobdiff - lib/scripts/lyxpreview2bitmap.py
Remove profiling.py
[lyx.git] / lib / scripts / lyxpreview2bitmap.py
index e4bf1314a40c7e058e394e01e7ca8e11627db513..ab9afaa21cc432c67b878656b53f7471b598acce 100755 (executable)
@@ -1,5 +1,3 @@
-# -*- coding: utf-8 -*-
-
 # file lyxpreview2bitmap.py
 # This file is part of LyX, the document processor.
 # Licence details can be found in the file COPYING.
 # Moreover dvipng can't work with PDF files, so, if the CONVERTER
 # paramter is pdflatex we have to fallback to legacy route (step 2).
 
-from __future__ import print_function
 
 import getopt, glob, os, re, shutil, sys, tempfile
 
+import lyxpreview_tools
+
 from legacy_lyxpreview2ppm import extract_resolution, legacy_conversion_step1
 
 from lyxpreview_tools import bibtex_commands, check_latex_log, copyfileobj, \
@@ -87,8 +86,6 @@ from lyxpreview_tools import bibtex_commands, check_latex_log, copyfileobj, \
      pdflatex_commands, progress, run_command, run_latex, run_tex, \
      warning, write_metrics_info
 
-PY2 = sys.version_info[0] == 2
-
 def usage(prog_name):
     msg = """
 Usage: %s <options> <input file>
@@ -118,7 +115,7 @@ def extract_metrics_info(dvipng_stdout):
     # "\[[0-9]+" can match two kinds of numbers: page numbers from dvipng
     # and glyph numbers from mktexpk. The glyph numbers always match
     # "\[[0-9]+\]" while the page number never is followed by "\]". Thus:
-    page_re = re.compile("\[([0-9]+)[^]]");
+    page_re = re.compile(r"\[([0-9]+)[^]]");
     metrics_re = re.compile("depth=(-?[0-9]+) height=(-?[0-9]+)")
 
     success = 0
@@ -195,12 +192,12 @@ def fix_latex_file(latex_file, pdf_output):
 
 
 def convert_to_ppm_format(pngtopnm, basename):
-    png_file_re = re.compile("\.png$")
+    png_file_re = re.compile(r"\.png$")
 
     for png_file in glob.glob("%s*.png" % basename):
         ppm_file = png_file_re.sub(".ppm", png_file)
 
-        p2p_cmd = '%s "%s"' % (pngtopnm, png_file)
+        p2p_cmd = f'{pngtopnm} "{png_file}"'
         p2p_status, p2p_stdout = run_command(p2p_cmd)
         if p2p_status:
             error("Unable to convert %s to ppm format" % png_file)
@@ -224,7 +221,7 @@ def find_ps_pages(dvi_file):
     # 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)
+    dv2dt_call = f'{dv2dt} "{dvi_file}"'
 
     # The output from dv2dt goes to stdout
     dv2dt_status, dv2dt_output = run_command(dv2dt_call)
@@ -331,7 +328,7 @@ def main(argv):
             "debug", "dpi=", "fg=", "help", "latex=", "lilypond",
             "lilypond-book=", "png", "ppm", "verbose"])
     except getopt.GetoptError as err:
-        error("%s\n%s" % (err, usage(script_name)))
+        error(f"{err}\n{usage(script_name)}")
 
     opts.reverse()
     for opt, val in opts:
@@ -343,7 +340,6 @@ def main(argv):
         elif opt == "--bg":
             bg_color = val
         elif opt in ("-d", "--debug"):
-            import lyxpreview_tools
             lyxpreview_tools.debug = True
         elif opt == "--dpi":
             try:
@@ -361,38 +357,43 @@ def main(argv):
         elif opt in ("--png", "--ppm"):
             output_format = opt[2:]
         elif opt in ("-v", "--verbose"):
-            import lyxpreview_tools
             lyxpreview_tools.verbose = True
 
     # Determine input file
     if len(args) != 1:
         err = "A single input file is required, %s given" % (len(args) or "none")
-        error("%s\n%s" % (err, usage(script_name)))
+        error(f"{err}\n{usage(script_name)}")
 
     input_path = args[0]
     dir, latex_file = os.path.split(input_path)
 
+    # Check for the input file
+    if not os.path.exists(input_path):
+        error('File "%s" not found.' % input_path)
+    if len(dir) != 0:
+        os.chdir(dir)
+
+    if lyxpreview_tools.verbose:
+        f_out = open('verbose.txt', 'a')
+        sys.stdout = f_out
+        sys.stderr = f_out
+
     # Echo the settings
+    progress("Running Python %s" % str(sys.version_info[:3]))
     progress("Starting %s..." % script_name)
+    if os.name == "nt":
+        progress("Use win32_modules: %d" % lyxpreview_tools.use_win32_modules)
     progress("Output format: %s" % output_format)
     progress("Foreground color: %s" % fg_color)
     progress("Background color: %s" % bg_color)
     progress("Resolution (dpi): %s" % dpi)
     progress("File to process: %s" % input_path)
 
-    # Check for the input file
-    if not os.path.exists(input_path):
-        error('File "%s" not found.' % input_path)
-    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 = 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)
+    fg_color_dvipng = make_texcolor(fg_color, False).decode('ascii')
+    bg_color_dvipng = make_texcolor(bg_color, False).decode('ascii')
 
     # External programs used by the script.
     latex = find_exe_or_terminate(latex or latex_commands)