[LyX/master] Clean python code (lyx2lyx)
José Matos
jamatos at lyx.org
Sun Jul 28 08:56:40 UTC 2024
commit 52295693d6bee47f0d8615d3981e5bcb11e7d4c8
Author: José Matos <jamatos at lyx.org>
Date: Sun Jul 28 09:56:29 2024 +0100
Clean python code (lyx2lyx)
Please linter where it makes sense:
* Avoid bare exceptions;
* Use formatted strings instead of string interpolation
---
lib/lyx2lyx/LyX.py | 15 ++++++++-------
lib/lyx2lyx/lyx2lyx | 1 +
lib/lyx2lyx/lyx_2_2.py | 6 +++---
lib/lyx2lyx/lyx_2_3.py | 4 ++--
4 files changed, 14 insertions(+), 12 deletions(-)
diff --git a/lib/lyx2lyx/LyX.py b/lib/lyx2lyx/LyX.py
index ff0e18813c..4056bbf145 100644
--- a/lib/lyx2lyx/LyX.py
+++ b/lib/lyx2lyx/LyX.py
@@ -41,7 +41,8 @@ try:
version__ = lyx2lyx_version.version
stable_version = True
-except: # we are running from build directory so assume the last version
+except ModuleNotFoundError:
+ # we are running from the build directory so assume the last version
version__ = "2.5"
stable_version = False
@@ -488,7 +489,7 @@ class LyX_base:
gzip.open(input).readline()
self.input = gzip.open(input)
self.compressed = True
- except:
+ except OSError:
self.input = open(input, "rb")
self.compressed = False
else:
@@ -695,17 +696,17 @@ class LyX_base:
init_t = time.time()
try:
conv(self)
- except:
+ except Exception as exception:
self.warning(
- "An error occurred in %s, %s" % (version, str(conv)),
+ f"An error occurred in {version}, {conv}",
default_debug__,
)
if not self.try_hard:
- raise
+ raise exception
self.status = 2
else:
self.warning(
- "%lf: Elapsed time on %s" % (time.time() - init_t, str(conv)),
+ f"{time.time() - init_t:f}: Elapsed time on {conv}",
default_debug__ + 1,
)
self.format = version
@@ -776,7 +777,7 @@ class LyX_base:
if last_step[1][-1] == self.end_format:
steps.pop()
- self.warning("Convertion mode: %s\tsteps%s" % (mode, steps), 10)
+ self.warning(f"Convertion mode: {mode}\tsteps{steps}", 10)
return mode, steps
def append_local_layout(self, new_layout):
diff --git a/lib/lyx2lyx/lyx2lyx b/lib/lyx2lyx/lyx2lyx
index 389decaf13..4c2c2eaacb 100755
--- a/lib/lyx2lyx/lyx2lyx
+++ b/lib/lyx2lyx/lyx2lyx
@@ -20,6 +20,7 @@
" Program used to convert between different versions of the lyx file format."
import argparse
import sys
+
import LyX
diff --git a/lib/lyx2lyx/lyx_2_2.py b/lib/lyx2lyx/lyx_2_2.py
index aa3ceb1a75..5ac259af80 100644
--- a/lib/lyx2lyx/lyx_2_2.py
+++ b/lib/lyx2lyx/lyx_2_2.py
@@ -1527,7 +1527,7 @@ def revert_colorbox(document):
"Malformed LyX document: 'has_inner_box' or "
"'use_makebox' option not found in box inset!"
)
- ertinset = put_cmd_in_ert("\\fcolorbox{%s}{%s}{" % (framecolor, backcolor))
+ ertinset = put_cmd_in_ert(f"\\fcolorbox{{{framecolor}}}{{{backcolor}}}{{")
else:
ertinset = put_cmd_in_ert("\\colorbox{%s}{" % backcolor)
document.body[i:i] = ertinset + [""]
@@ -2704,12 +2704,12 @@ def revert_solution(document):
add_to_preamble(document, "\\theoremstyle{definition}")
if is_starred or mod == "theorems-bytype" or mod == "theorems-ams-bytype":
add_to_preamble(
- document, "\\%s{%s}{\\protect\\solutionname}" % (theoremName, LaTeXName)
+ document, f"\\{theoremName}{{{LaTeXName}}}{{\\protect\\solutionname}}"
)
else: # mod == "theorems-std" or mod == "theorems-ams" and not is_starred
add_to_preamble(
document,
- "\\%s{%s}[thm]{\\protect\\solutionname}" % (theoremName, LaTeXName),
+ f"\\{theoremName}{{{LaTeXName}}}[thm]{{\\protect\\solutionname}}",
)
add_to_preamble(document, "\\providecommand{\\solutionname}{Solution}")
diff --git a/lib/lyx2lyx/lyx_2_3.py b/lib/lyx2lyx/lyx_2_3.py
index c0cfcaac20..f08b581b7d 100644
--- a/lib/lyx2lyx/lyx_2_3.py
+++ b/lib/lyx2lyx/lyx_2_3.py
@@ -2027,7 +2027,7 @@ def revert_baselineskip(document):
# check if it is the starred version
star = "*" if "*" in document.body[i] else ""
# now output TeX code
- cmd = "\\vspace%s{%s}" % (star, latex_length(baselineskip)[1])
+ cmd = f"\\vspace{star}{{{latex_length(baselineskip)[1]}}}"
document.body[i : end + 1] = put_cmd_in_ert(cmd)
i += 8
continue
@@ -2036,7 +2036,7 @@ def revert_baselineskip(document):
# output space inset as TeX code
baselineskip = document.body[i].split()[-1]
star = "*" if "*" in document.body[i - 1] else ""
- cmd = "\\hspace%s{%s}" % (star, latex_length(baselineskip)[1])
+ cmd = f"\\hspace{star}{{{latex_length(baselineskip)[1]}}}"
document.body[begin : end + 1] = put_cmd_in_ert(cmd)
More information about the lyx-cvs
mailing list