From 04ecabef6005640450cb20e813eeb2bb284b9487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Matos?= Date: Mon, 10 Jun 2024 11:29:56 +0100 Subject: [PATCH] Update Python scripts to Python 3+ Remove support for Python 2 Use formatted strings where appropriated --- lib/scripts/TeXFiles.py | 3 --- lib/scripts/clean_dvi.py | 2 +- lib/scripts/convertDefault.py | 7 ++---- lib/scripts/convert_pdf.py | 2 -- lib/scripts/csv2lyx.py | 2 -- lib/scripts/docbook2epub.py | 10 +++----- lib/scripts/docbook_copy.py | 8 +++---- lib/scripts/ext_copy.py | 6 ++--- lib/scripts/fen2ascii.py | 3 +-- lib/scripts/fig2pdftex.py | 11 ++++----- lib/scripts/fig2pstex.py | 7 ++---- lib/scripts/fig_copy.py | 3 --- lib/scripts/html2latexwrapper.py | 8 +++---- lib/scripts/include_bib.py | 7 ++---- lib/scripts/layout2layout.py | 35 +++++++--------------------- lib/scripts/legacy_lyxpreview2ppm.py | 24 ++++++++----------- lib/scripts/lyxpak.py | 28 +++++++--------------- lib/scripts/lyxpaperview.py | 1 - lib/scripts/lyxpreview2bitmap.py | 28 +++++++--------------- lib/scripts/lyxpreview_tools.py | 4 ++-- lib/scripts/lyxwin_getopt.py | 4 +--- lib/scripts/prefs2prefs.py | 9 +++---- lib/scripts/prefs2prefs_lfuns.py | 2 -- lib/scripts/prefs2prefs_prefs.py | 8 +++---- lib/scripts/svg2pdftex.py | 2 -- lib/scripts/svg2pstex.py | 2 -- lib/scripts/tex_copy.py | 2 -- 27 files changed, 67 insertions(+), 161 deletions(-) diff --git a/lib/scripts/TeXFiles.py b/lib/scripts/TeXFiles.py index a5787eaacb..c41d83f9a7 100755 --- a/lib/scripts/TeXFiles.py +++ b/lib/scripts/TeXFiles.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # file TeXFiles.py # This file is part of LyX, the document processor. # Licence details can be found in the file COPYING. @@ -35,7 +33,6 @@ # relies on python and kpsewhich (no shell command is used). # -from __future__ import print_function import os, sys, re cls_stylefile = 'clsFiles.lst' diff --git a/lib/scripts/clean_dvi.py b/lib/scripts/clean_dvi.py index 317ea92222..4f7d73dc67 100644 --- a/lib/scripts/clean_dvi.py +++ b/lib/scripts/clean_dvi.py @@ -94,7 +94,7 @@ def main(argv): if dv2dt_status != None or len(dv2dt_data) == 0: dv2dt_err = dv2dt_stderr.read() - error("Failed: %s\n%s\n" % ( dv2dt_call, dv2dt_err) ) + error(f"Failed: {dv2dt_call}\n{dv2dt_err}\n" ) # Manipulate the .dtl file. dtl_data = manipulated_dtl(dv2dt_data) diff --git a/lib/scripts/convertDefault.py b/lib/scripts/convertDefault.py index d04fa78799..460f712fe3 100644 --- a/lib/scripts/convertDefault.py +++ b/lib/scripts/convertDefault.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # file convertDefault.py # This file is part of LyX, the document processor. # Licence details can be found in the file COPYING. @@ -16,7 +14,6 @@ # replacement in ~/.lyx/scripts # converts an image $2 (format $1) to $4 (format $3) -from __future__ import print_function import os, re, sys # We may need some extra options only supported by recent convert versions @@ -73,11 +70,11 @@ if sys.argv[3] == 'ppm' and (im and version >= (6,3,5) or gm): topts = '-flatten' # print (command, sys.argv[2], sys.argv[4], file= sys.stdout) -if (im or gm) and os.system(r'%s %s "%s" %s "%s"' % (command, sopts, sys.argv[2], topts, sys.argv[3] + ':' + sys.argv[4])) != 0: +if (im or gm) and os.system(r'{} {} "{}" {} "{}"'.format(command, sopts, sys.argv[2], topts, sys.argv[3] + ':' + sys.argv[4])) != 0: print(sys.argv[0], 'ERROR', file= sys.stderr) print('Execution of "%s" failed.' % command, file= sys.stderr) sys.exit(1) -elif not im and not gm and sys.platform == 'darwin' and os.system(r'%s "%s" "%s"' % (command, sys.argv[2], sys.argv[4])) != 0: +elif not im and not gm and sys.platform == 'darwin' and os.system(fr'{command} "{sys.argv[2]}" "{sys.argv[4]}"') != 0: print(sys.argv[0], 'ERROR', file= sys.stderr) print('Execution of "%s" failed.' % command, file=sys.stderr) sys.exit(1) diff --git a/lib/scripts/convert_pdf.py b/lib/scripts/convert_pdf.py index e1996259f1..881be6a25c 100644 --- a/lib/scripts/convert_pdf.py +++ b/lib/scripts/convert_pdf.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # file convert_pdf.py # This file is part of LyX, the document processor. # Licence details can be found in the file COPYING. diff --git a/lib/scripts/csv2lyx.py b/lib/scripts/csv2lyx.py index 4547c7d1c4..eba6533e8f 100644 --- a/lib/scripts/csv2lyx.py +++ b/lib/scripts/csv2lyx.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # file csv2lyx.py # This file is part of LyX, the document processor. # Licence details can be found in the file COPYING. diff --git a/lib/scripts/docbook2epub.py b/lib/scripts/docbook2epub.py index 0e9d268a47..5cd8593dbb 100644 --- a/lib/scripts/docbook2epub.py +++ b/lib/scripts/docbook2epub.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # file docbook2epub.py # This file is part of LyX, the document processor. # Licence details can be found in the file COPYING. @@ -11,7 +9,6 @@ # Usage: # python docbook2epub.py java_binary saxon_path xsltproc_path xslt_path in.docbook in.orig.path out.epub -from __future__ import print_function import glob import os @@ -19,7 +16,6 @@ import shutil import sys import tempfile import zipfile -from io import open # Required for Python 2. def _parse_nullable_argument(arg): @@ -39,7 +35,7 @@ class DocBookToEpub: args = sys.argv if len(args) != 8: - print('Exactly eight arguments are expected, only %s found: %s.' % (len(args), args)) + print(f'Exactly eight arguments are expected, only {len(args)} found: {args}.') sys.exit(1) self.own_path = sys.argv[0] @@ -127,7 +123,7 @@ class DocBookToEpub: # The XHTML files are also tags: # try: - with open(self.package_opf, 'r') as f: + with open(self.package_opf) as f: for line in f.readlines(): if ' @@ -202,7 +197,7 @@ def convert_to_ppm_format(pngtopnm, basename): 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) @@ -226,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) @@ -333,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: @@ -367,7 +362,7 @@ def main(argv): # 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) @@ -394,18 +389,11 @@ def main(argv): progress("Resolution (dpi): %s" % dpi) progress("File to process: %s" % input_path) - # 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) - - # For python > 2 convert bytes to string - if not PY2: - fg_color_dvipng = fg_color_dvipng.decode('ascii') - bg_color_dvipng = bg_color_dvipng.decode('ascii') + 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) diff --git a/lib/scripts/lyxpreview_tools.py b/lib/scripts/lyxpreview_tools.py index c75563d464..56ff33e38b 100644 --- a/lib/scripts/lyxpreview_tools.py +++ b/lib/scripts/lyxpreview_tools.py @@ -300,7 +300,7 @@ def run_latex(latex, latex_file, bibtex = None): def run_tex(tex, tex_file): - tex_call = '%s "%s"' % (tex, tex_file) + tex_call = f'{tex} "{tex_file}"' tex_status, tex_stdout = run_command(tex_call) if tex_status: @@ -347,7 +347,7 @@ def check_latex_log(log_file): found_error = False match = data_re.search(line) if match == None: - error("Unexpected data in %s\n%s" % (log_file, line)) + error(f"Unexpected data in {log_file}\n{line}") error_pages.append(int(match.group(1))) diff --git a/lib/scripts/lyxwin_getopt.py b/lib/scripts/lyxwin_getopt.py index a6f356a084..90571896ee 100755 --- a/lib/scripts/lyxwin_getopt.py +++ b/lib/scripts/lyxwin_getopt.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - """Parser for command line options. This module helps scripts to parse the command line arguments in @@ -83,7 +81,7 @@ def getopt(args, shortopts, longopts = []): """ opts = [] - if type(longopts) == type(""): + if type(longopts) == str: longopts = [longopts] else: longopts = list(longopts) diff --git a/lib/scripts/prefs2prefs.py b/lib/scripts/prefs2prefs.py index 00d619de21..1194abdf7a 100644 --- a/lib/scripts/prefs2prefs.py +++ b/lib/scripts/prefs2prefs.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # file prefs2prefs.py # This file is part of LyX, the document processor. # Licence details can be found in the file COPYING. @@ -18,7 +16,6 @@ # # The format of the existing files was format 0, as of 2.0.alpha6. -from __future__ import print_function import os, re, string, sys from getopt import getopt import io @@ -28,7 +25,7 @@ import io def trim_bom(line): " Remove byte order mark." - if line[0:3] == u"\357\273\277": + if line[0:3] == "\357\273\277": return line[3:] else: return line @@ -128,8 +125,8 @@ def main(argv): source = sys.stdin output = sys.stdout elif len(args) == 2: - source = io.open(args[0], 'r', encoding='utf_8', errors='surrogateescape') - output = io.open(args[1], 'w', encoding='utf_8', newline='\n') + source = open(args[0], encoding='utf_8', errors='surrogateescape') + output = open(args[1], 'w', encoding='utf_8', newline='\n') opened_files = True else: usage() diff --git a/lib/scripts/prefs2prefs_lfuns.py b/lib/scripts/prefs2prefs_lfuns.py index a3416a0e52..5c5501ebbd 100644 --- a/lib/scripts/prefs2prefs_lfuns.py +++ b/lib/scripts/prefs2prefs_lfuns.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # file prefs2prefs-lfuns.py # This file is part of LyX, the document processor. # Licence details can be found in the file COPYING. diff --git a/lib/scripts/prefs2prefs_prefs.py b/lib/scripts/prefs2prefs_prefs.py index 69b97fd30e..fe7e0a1ff4 100644 --- a/lib/scripts/prefs2prefs_prefs.py +++ b/lib/scripts/prefs2prefs_prefs.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # file prefs2prefs-prefs.py # This file is part of LyX, the document processor. # Licence details can be found in the file COPYING. @@ -271,7 +269,7 @@ def latex_flavor(line): if flavor == "latex": return no_match return (True, - "\\converter \"%s\" \"%s\" \"%s\" \"latex=%s\"" % (conv, fmat, args, flavor)) + f"\\converter \"{conv}\" \"{fmat}\" \"{args}\" \"latex={flavor}\"") emre = re.compile(r'^\\format\s+(.*)\s+"(document[^"]*?)"', re.IGNORECASE) @@ -284,7 +282,7 @@ def export_menu(line): fmat = m.group(1) opts = m.group(2) return (True, - "\\Format %s \"%s,menu=export\"" % (fmat, opts)) + f"\\Format {fmat} \"{opts},menu=export\"") # End format 1 conversions (for LyX 2.0) ######################################## @@ -301,7 +299,7 @@ def zipped_native(line): fmat = m.group(1) opts = m.group(2) return (True, - "\\Format %s \"%s,zipped=native\"" % (fmat, opts)) + f"\\Format {fmat} \"{opts},zipped=native\"") def remove_default_papersize(line): if not line.lower().startswith("\\default_papersize"): diff --git a/lib/scripts/svg2pdftex.py b/lib/scripts/svg2pdftex.py index 74266e55cb..aad6d0b321 100644 --- a/lib/scripts/svg2pdftex.py +++ b/lib/scripts/svg2pdftex.py @@ -1,5 +1,4 @@ #!/usr/bin/python3 -# -*- coding: utf-8 -*- # file svg2pdftex.py # This file is part of LyX, the document processor. @@ -28,7 +27,6 @@ # the real PDF file would be overwritten by a TeX file named outputfile.pdf. # -from __future__ import print_function import os, sys, re, subprocess diff --git a/lib/scripts/svg2pstex.py b/lib/scripts/svg2pstex.py index 09d8ac7183..33aedc226a 100644 --- a/lib/scripts/svg2pstex.py +++ b/lib/scripts/svg2pstex.py @@ -1,5 +1,4 @@ #!/usr/bin/python3 -# -*- coding: utf-8 -*- # file svg2pstex.py # This file is part of LyX, the document processor. @@ -31,7 +30,6 @@ # This script converts an SVG image to something that latex can process # into high quality PostScript. -from __future__ import print_function import os, sys, re, subprocess diff --git a/lib/scripts/tex_copy.py b/lib/scripts/tex_copy.py index 529b13e7d0..dac153a906 100644 --- a/lib/scripts/tex_copy.py +++ b/lib/scripts/tex_copy.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - # file tex_copy.py # This file is part of LyX, the document processor. # Licence details can be found in the file COPYING.