From 005a3d60066808dc8e292d62dae56fa1c5530d3e Mon Sep 17 00:00:00 2001 From: Torben <59419684+entorb@users.noreply.github.com> Date: Wed, 3 Apr 2024 07:49:49 +0200 Subject: [PATCH] Add .ruff.toml and fix some findings --- .ruff.toml | 32 ++++++++++++++++++++++ chapters/translations/1_download.py | 4 +++ chapters/translations/2_extract.py | 4 +++ chapters/translations/3_clean.py | 4 +++ chapters/translations/4_html2latex.py | 9 ++++-- chapters/translations/5_latex_cleanup.py | 4 +++ scripts/check_chapters.py | 35 ++++++++++++++++-------- scripts/check_chapters_settings.py | 32 ++++++++++++---------- scripts/compare-translations.py | 4 +++ scripts/ebook/3.py | 4 +++ scripts/ebook/4.py | 4 +++ scripts/ebook/6.py | 4 +++ 12 files changed, 111 insertions(+), 29 deletions(-) create mode 100644 .ruff.toml diff --git a/.ruff.toml b/.ruff.toml new file mode 100644 index 000000000..074665cf1 --- /dev/null +++ b/.ruff.toml @@ -0,0 +1,32 @@ +fix = true # auto-fix findings +line-length = 88 # same as Black +target-version = "py310" # Python 3.10 + +[lint] +# comment this out to use only default rules (["E4", "E7", "E9", "F"]) +select = ["ALL"] # activate all rules + +# add some more rules to include +# extend-select = ["B", "Q", "E", "W"] + +# rule not to apply +extend-ignore = [ + "COM812", # missing-trailing-comma, + "D200", # fits-on-one-line" + "D211", # blank-line-before-class + "D212", # multi-line-summary-second-line + "ERA", # commented-out code + "FIX002", # line-contains-todo + "ISC001", # implicit-str-concat + "PD901", # df name + "PGH003", # blanket-type-ignore + "RET504", # unnecessary-assign + "T201", # print + "TD002", # missing-todo-author + "TD003", # missing-todo-link +] + + +[format] +line-ending = "lf" # force lf +preview = false # enable unstable preview style formatting diff --git a/chapters/translations/1_download.py b/chapters/translations/1_download.py index 41572e18f..1ee758a84 100755 --- a/chapters/translations/1_download.py +++ b/chapters/translations/1_download.py @@ -1,4 +1,8 @@ #!/usr/bin/env python3 + +# TODO: fix ruff findings +# ruff: noqa + """ Download chapter files from the web. """ diff --git a/chapters/translations/2_extract.py b/chapters/translations/2_extract.py index 267037e15..1c5afa535 100755 --- a/chapters/translations/2_extract.py +++ b/chapters/translations/2_extract.py @@ -1,4 +1,8 @@ #!/usr/bin/env python3 + +# TODO: fix ruff findings +# ruff: noqa + """ Extract text from downloaded HTML files. """ diff --git a/chapters/translations/3_clean.py b/chapters/translations/3_clean.py index 4ca3a3330..e51c9bde7 100755 --- a/chapters/translations/3_clean.py +++ b/chapters/translations/3_clean.py @@ -1,4 +1,8 @@ #!/usr/bin/env python3 + +# TODO: fix ruff findings +# ruff: noqa + """ Clean text. """ diff --git a/chapters/translations/4_html2latex.py b/chapters/translations/4_html2latex.py index ac08dbd48..a5486de19 100755 --- a/chapters/translations/4_html2latex.py +++ b/chapters/translations/4_html2latex.py @@ -1,11 +1,15 @@ #!/usr/bin/env python3 + +# TODO: fix ruff findings +# ruff: noqa + """ Convert HTML to LaTeX. """ import glob import os -import subprocess # noqa: S404 +import subprocess import sys import helper @@ -32,10 +36,11 @@ def html2latex(): print(fileIn) fileOut = fileIn.replace("3-clean/", "4-latex/").replace(".html", ".tex") # pandoc -s fileIn -o fileOut - process = subprocess.run( # noqa: S607,S603 + process = subprocess.run( ["pandoc", "-s", fileIn, "-o", fileOut], capture_output=True, text=True, + check=False, ) print(process.stdout) diff --git a/chapters/translations/5_latex_cleanup.py b/chapters/translations/5_latex_cleanup.py index eaa43d0a1..29df36015 100755 --- a/chapters/translations/5_latex_cleanup.py +++ b/chapters/translations/5_latex_cleanup.py @@ -1,4 +1,8 @@ #!/usr/bin/env python3 + +# TODO: fix ruff findings +# ruff: noqa + """ Cleanup LaTeX Code. """ diff --git a/scripts/check_chapters.py b/scripts/check_chapters.py index 27d360a38..41d9c7d98 100755 --- a/scripts/check_chapters.py +++ b/scripts/check_chapters.py @@ -1,4 +1,9 @@ #!/usr/bin/env python3 + +# ruff: noqa: S101 RUF001 RUF003 +# TODO: fix these +# ruff: noqa: PTH123 PT006 C901 D103 PLR0912 PLR0915 TRY002 + # by Torben Menke https://entorb.net """ Check chapter .tex files for known issues and propose fixes. @@ -38,8 +43,7 @@ # Apostroph: ’ -# TODO: -# \latersection must be at newline +# TODO: \latersection must be at newline # chars manually find and replace # *, ", ', », «, ”, @@ -108,7 +112,7 @@ def process_file(file_in: Path) -> bool: cont_lines_new.append(line) else: # check not commented-out lines - line = fix_line(s=line) + line = fix_line(s=line) # noqa: PLW2901 cont_lines_new.append(line) if issues_found is False and line_orig != line: issues_found = True @@ -126,12 +130,15 @@ def process_file(file_in: Path) -> bool: fh.write("\n".join(cont_lines_new)) if settings["print_diff"]: - with open(file_in, encoding="utf-8") as file1, open( - file_out, - encoding="utf-8", - ) as file2: + with ( + open(file_in, encoding="utf-8") as file1, + open( + file_out, + encoding="utf-8", + ) as file2, + ): diff = difflib.ndiff(file1.readlines(), file2.readlines()) - delta = "".join(x for x in diff if x.startswith("+ ") or x.startswith("- ")) + delta = "".join(x for x in diff if x.startswith(("+ ", "- "))) print(file_in.name + "\n" + delta) return issues_found @@ -167,6 +174,9 @@ def fix_line(s: str) -> str: def fix_spaces(s: str) -> str: + """ + Fix spaces. + """ # invisible strange spaces s = re.sub(r" +", " ", s) # tabs to space @@ -191,12 +201,12 @@ def fix_spaces(s: str) -> str: @pytest.mark.parametrize( "text, expected_output", - ( + [ ("tabs\tto\t\tspace", "tabs to space"), ("trailing spaces ", "trailing spaces"), (" ", ""), ("multiple spaces", "multiple spaces"), - ), + ], ) def test_fix_spaces(text: str, expected_output: str) -> None: assert fix_spaces(s=text) == expected_output, fix_spaces(s=text) @@ -281,7 +291,7 @@ def fix_dots(s: str) -> str: assert fix_dots("bad… „dots") == "bad… „dots" -def fix_MrMrs(s: str) -> str: +def fix_MrMrs(s: str) -> str: # noqa: N802 # Mr / Mrs s = s.replace("Mr. H. Potter", "Mr~H.~Potter") # s = s.replace("Mr. Potter", "Mr~Potter") @@ -745,4 +755,5 @@ def fix_linebreaks_speach(s: str) -> str: # any_issue_found = True if settings["raise_error"] and any_issue_found: - raise Exception("Issues found, please fix!") + msg = "Issues found, please fix!" + raise Exception(msg) diff --git a/scripts/check_chapters_settings.py b/scripts/check_chapters_settings.py index b78633f9e..05d18e7c8 100644 --- a/scripts/check_chapters_settings.py +++ b/scripts/check_chapters_settings.py @@ -1,15 +1,17 @@ -""" -Settings. - -lang: EN, DE, FR, ... -raise_error: true -> script exits with error, used for autobuild of releases -print_diff: true : print line of issues -inline_fixing: modify the source file directly, USE WITH CAUTION -""" - -settings = { - "lang": "DE", - "print_diff": True, - "raise_error": True, - "inline_fixing": False, -} +# ruff: # noqa: INP001 + +""" +Settings. + +lang: EN, DE, FR, ... +raise_error: true -> script exits with error, used for autobuild of releases +print_diff: true : print line of issues +inline_fixing: modify the source file directly, USE WITH CAUTION +""" + +settings = { + "lang": "DE", + "print_diff": True, + "raise_error": True, + "inline_fixing": False, +} diff --git a/scripts/compare-translations.py b/scripts/compare-translations.py index a29fa7f77..b96d77e50 100755 --- a/scripts/compare-translations.py +++ b/scripts/compare-translations.py @@ -1,5 +1,9 @@ #!/usr/bin/env python3 # by Torben Menke https://entorb.net + +# TODO: fix ruff findings +# ruff: noqa + """ Compare chapters between translations. diff --git a/scripts/ebook/3.py b/scripts/ebook/3.py index b18af3a7e..1afbcebc5 100755 --- a/scripts/ebook/3.py +++ b/scripts/ebook/3.py @@ -1,5 +1,9 @@ #!/usr/bin/env python3 # by Torben Menke https://entorb.net + +# TODO: fix ruff findings +# ruff: noqa + """ Modify flattened .tex file. """ diff --git a/scripts/ebook/4.py b/scripts/ebook/4.py index fb89c23c6..aee2b2cd0 100755 --- a/scripts/ebook/4.py +++ b/scripts/ebook/4.py @@ -1,5 +1,9 @@ #!/usr/bin/env python3 # by Torben Menke https://entorb.net + +# TODO: fix ruff findings +# ruff: noqa + """ Parselify flattened .tex file. """ diff --git a/scripts/ebook/6.py b/scripts/ebook/6.py index 2ee6fea9d..468bfef1f 100755 --- a/scripts/ebook/6.py +++ b/scripts/ebook/6.py @@ -1,5 +1,9 @@ #!/usr/bin/env python3 # by Torben Menke https://entorb.net + +# TODO: fix ruff findings +# ruff: noqa + """ HTML modifications. """