Clean python code (lyx2lyx)

Please linter where it makes sense:

* Avoid bare exceptions;
* Use formatted strings instead of string interpolation
This commit is contained in:
José Matos 2024-07-28 09:56:29 +01:00
parent 181c8ce7c1
commit 52295693d6
4 changed files with 14 additions and 12 deletions

View File

@ -41,7 +41,8 @@ try:
version__ = lyx2lyx_version.version version__ = lyx2lyx_version.version
stable_version = True 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" version__ = "2.5"
stable_version = False stable_version = False
@ -488,7 +489,7 @@ class LyX_base:
gzip.open(input).readline() gzip.open(input).readline()
self.input = gzip.open(input) self.input = gzip.open(input)
self.compressed = True self.compressed = True
except: except OSError:
self.input = open(input, "rb") self.input = open(input, "rb")
self.compressed = False self.compressed = False
else: else:
@ -695,17 +696,17 @@ class LyX_base:
init_t = time.time() init_t = time.time()
try: try:
conv(self) conv(self)
except: except Exception as exception:
self.warning( self.warning(
"An error occurred in %s, %s" % (version, str(conv)), f"An error occurred in {version}, {conv}",
default_debug__, default_debug__,
) )
if not self.try_hard: if not self.try_hard:
raise raise exception
self.status = 2 self.status = 2
else: else:
self.warning( 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, default_debug__ + 1,
) )
self.format = version self.format = version
@ -776,7 +777,7 @@ class LyX_base:
if last_step[1][-1] == self.end_format: if last_step[1][-1] == self.end_format:
steps.pop() steps.pop()
self.warning("Convertion mode: %s\tsteps%s" % (mode, steps), 10) self.warning(f"Convertion mode: {mode}\tsteps{steps}", 10)
return mode, steps return mode, steps
def append_local_layout(self, new_layout): def append_local_layout(self, new_layout):

View File

@ -20,6 +20,7 @@
" Program used to convert between different versions of the lyx file format." " Program used to convert between different versions of the lyx file format."
import argparse import argparse
import sys import sys
import LyX import LyX

View File

@ -1527,7 +1527,7 @@ def revert_colorbox(document):
"Malformed LyX document: 'has_inner_box' or " "Malformed LyX document: 'has_inner_box' or "
"'use_makebox' option not found in box inset!" "'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: else:
ertinset = put_cmd_in_ert("\\colorbox{%s}{" % backcolor) ertinset = put_cmd_in_ert("\\colorbox{%s}{" % backcolor)
document.body[i:i] = ertinset + [""] document.body[i:i] = ertinset + [""]
@ -2704,12 +2704,12 @@ def revert_solution(document):
add_to_preamble(document, "\\theoremstyle{definition}") add_to_preamble(document, "\\theoremstyle{definition}")
if is_starred or mod == "theorems-bytype" or mod == "theorems-ams-bytype": if is_starred or mod == "theorems-bytype" or mod == "theorems-ams-bytype":
add_to_preamble( 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 else: # mod == "theorems-std" or mod == "theorems-ams" and not is_starred
add_to_preamble( add_to_preamble(
document, document,
"\\%s{%s}[thm]{\\protect\\solutionname}" % (theoremName, LaTeXName), f"\\{theoremName}{{{LaTeXName}}}[thm]{{\\protect\\solutionname}}",
) )
add_to_preamble(document, "\\providecommand{\\solutionname}{Solution}") add_to_preamble(document, "\\providecommand{\\solutionname}{Solution}")

View File

@ -2027,7 +2027,7 @@ def revert_baselineskip(document):
# check if it is the starred version # check if it is the starred version
star = "*" if "*" in document.body[i] else "" star = "*" if "*" in document.body[i] else ""
# now output TeX code # 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) document.body[i : end + 1] = put_cmd_in_ert(cmd)
i += 8 i += 8
continue continue
@ -2036,7 +2036,7 @@ def revert_baselineskip(document):
# output space inset as TeX code # output space inset as TeX code
baselineskip = document.body[i].split()[-1] baselineskip = document.body[i].split()[-1]
star = "*" if "*" in document.body[i - 1] else "" 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) document.body[begin : end + 1] = put_cmd_in_ert(cmd)