]> git.lyx.org Git - lyx.git/commitdiff
Clean python code (lyx2lyx)
authorJosé Matos <jamatos@lyx.org>
Sun, 28 Jul 2024 08:56:29 +0000 (09:56 +0100)
committerJosé Matos <jamatos@lyx.org>
Sun, 28 Jul 2024 08:56:29 +0000 (09:56 +0100)
Please linter where it makes sense:

* Avoid bare exceptions;
* Use formatted strings instead of string interpolation

lib/lyx2lyx/LyX.py
lib/lyx2lyx/lyx2lyx
lib/lyx2lyx/lyx_2_2.py
lib/lyx2lyx/lyx_2_3.py

index ff0e18813ca64524271373e3bf54d66bfb7e287b..4056bbf145b2d1419ec2c04bc46d49e2301fc447 100644 (file)
@@ -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):
index 389decaf13276e47cd5185c5bfeedb8c359131e3..4c2c2eaacbd6cac53256bd2f033329ead858368f 100755 (executable)
@@ -20,6 +20,7 @@
 " Program used to convert between different versions of the lyx file format."
 import argparse
 import sys
+
 import LyX
 
 
index aa3ceb1a75b84f86b3a4b44eb58ceaf52e9c7b3f..5ac259af804eedc7f0885b369c32a62ec4075041 100644 (file)
@@ -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}")
index c0cfcaac207030f92b9590d297d2ceec6709ade4..f08b581b7da9598c88bcf0988cb4b3db222bb2c9 100644 (file)
@@ -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)