Skip to content

Commit

Permalink
Add .ruff.toml and fix some findings
Browse files Browse the repository at this point in the history
  • Loading branch information
entorb committed Apr 3, 2024
1 parent 1c17817 commit 005a3d6
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 29 deletions.
32 changes: 32 additions & 0 deletions .ruff.toml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions chapters/translations/1_download.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/usr/bin/env python3

# TODO: fix ruff findings
# ruff: noqa

"""
Download chapter files from the web.
"""
Expand Down
4 changes: 4 additions & 0 deletions chapters/translations/2_extract.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/usr/bin/env python3

# TODO: fix ruff findings
# ruff: noqa

"""
Extract text from downloaded HTML files.
"""
Expand Down
4 changes: 4 additions & 0 deletions chapters/translations/3_clean.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/usr/bin/env python3

# TODO: fix ruff findings
# ruff: noqa

"""
Clean text.
"""
Expand Down
9 changes: 7 additions & 2 deletions chapters/translations/4_html2latex.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)

Expand Down
4 changes: 4 additions & 0 deletions chapters/translations/5_latex_cleanup.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#!/usr/bin/env python3

# TODO: fix ruff findings
# ruff: noqa

"""
Cleanup LaTeX Code.
"""
Expand Down
35 changes: 23 additions & 12 deletions scripts/check_chapters.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -38,8 +43,7 @@
# Apostroph: ’


# TODO:
# \latersection must be at newline
# TODO: \latersection must be at newline

# chars manually find and replace
# *, ", ', », «, ”,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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)
32 changes: 17 additions & 15 deletions scripts/check_chapters_settings.py
Original file line number Diff line number Diff line change
@@ -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,
}
4 changes: 4 additions & 0 deletions scripts/compare-translations.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env python3
# by Torben Menke https://entorb.net

# TODO: fix ruff findings
# ruff: noqa

"""
Compare chapters between translations.
Expand Down
4 changes: 4 additions & 0 deletions scripts/ebook/3.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env python3
# by Torben Menke https://entorb.net

# TODO: fix ruff findings
# ruff: noqa

"""
Modify flattened .tex file.
"""
Expand Down
4 changes: 4 additions & 0 deletions scripts/ebook/4.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env python3
# by Torben Menke https://entorb.net

# TODO: fix ruff findings
# ruff: noqa

"""
Parselify flattened .tex file.
"""
Expand Down
4 changes: 4 additions & 0 deletions scripts/ebook/6.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env python3
# by Torben Menke https://entorb.net

# TODO: fix ruff findings
# ruff: noqa

"""
HTML modifications.
"""
Expand Down

0 comments on commit 005a3d6

Please sign in to comment.