Skip to content

Commit 3bd335b

Browse files
committed
Enable all ruff rules + disable specific ones
Also adjust code accordingly.
1 parent 6c2e0df commit 3bd335b

10 files changed

Lines changed: 71 additions & 45 deletions

File tree

pyproject.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,19 @@ dev = [
2626
"ruff>=0.9.1",
2727
]
2828

29+
[tool.ruff.lint]
30+
select = ["ALL"]
31+
ignore = [
32+
"COM812",
33+
"D100",
34+
"D101",
35+
"D103",
36+
"D104",
37+
"D203",
38+
"D213",
39+
"E501",
40+
"S101",
41+
]
42+
2943
[tool.mypy]
3044
strict = true

src/checkmk_weblate_syncer/__main__.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from .cli import Mode, parse_arguments
66
from .config import UpdateSourcesConfig, UpdateTranslationsConfig
7-
from .logging import LOGGER, configure_logger
7+
from .logger import LOGGER, configure_logger
88
from .update_sources import run as run_update_sources
99
from .update_translations import run as run_update_translations
1010

@@ -16,13 +16,13 @@ def _main() -> None:
1616
match args.mode:
1717
case Mode.UPDATE_SOURCES:
1818
sys.exit(
19-
run_update_sources(_load_config(args.config_path, UpdateSourcesConfig))
19+
run_update_sources(_load_config(args.config_path, UpdateSourcesConfig)),
2020
)
2121
case Mode.UPDATE_TRANSLATIONS:
2222
sys.exit(
2323
run_update_translations(
24-
_load_config(args.config_path, UpdateTranslationsConfig)
25-
)
24+
_load_config(args.config_path, UpdateTranslationsConfig),
25+
),
2626
)
2727
case _:
2828
assert_never(args.mode)
@@ -34,9 +34,9 @@ def _main() -> None:
3434
def _load_config(config_path: Path, config_type: type[_ConfigTypeT]) -> _ConfigTypeT:
3535
try:
3636
return config_type.model_validate_json(config_path.read_text())
37-
except Exception as e:
37+
except Exception:
3838
LOGGER.error("Loading config failed")
39-
raise e
39+
raise
4040

4141

4242
_main()

src/checkmk_weblate_syncer/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ class BaseConfig(BaseModel, frozen=True):
2727

2828
class UpdateSourcesConfig(BaseConfig, frozen=True):
2929
checkmk_pot_generation_script: Annotated[
30-
Path, AfterValidator(_validate_path_is_relative)
30+
Path,
31+
AfterValidator(_validate_path_is_relative),
3132
]
3233
locale_pot_path: Annotated[Path, AfterValidator(_validate_path_is_relative)]
3334
commit_message: str

src/checkmk_weblate_syncer/git.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from git import Repo
66

77
from .config import RepositoryConfig
8-
from .logging import LOGGER
8+
from .logger import LOGGER
99

1010

1111
def repository_in_clean_state(
@@ -17,11 +17,12 @@ def repository_in_clean_state(
1717
repo_config.path,
1818
repo_config.branch,
1919
)
20-
except Exception as e:
20+
except Exception:
2121
LOGGER.error(
22-
"Error while cleaning up and updating %s repository", repo_config.path
22+
"Error while cleaning up and updating %s repository",
23+
repo_config.path,
2324
)
24-
raise e
25+
raise
2526

2627

2728
def _repository_in_clean_state(path: Path, branch: str) -> Repo:
@@ -42,11 +43,13 @@ def commit_and_push_files(
4243
repo.index.add(files)
4344
repo.index.commit(commit_message)
4445
repo.remotes.origin.push()
45-
except CalledProcessError as e:
46+
except CalledProcessError:
4647
LOGGER.error(
47-
"Committing and pushing files for repository %s failed", repo.working_dir
48+
"Committing and pushing files for repository %s failed",
49+
repo.working_dir,
4850
)
49-
raise e
51+
raise
5052
LOGGER.info(
51-
"Committing and pushing files for repository %s succeeded", repo.working_dir
53+
"Committing and pushing files for repository %s succeeded",
54+
repo.working_dir,
5255
)

src/checkmk_weblate_syncer/html_tags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# keep in sync with tests/pylint/checker_localization.py:HTMLTagsChecker
44
_TAG_PATTERN = re.compile("<.*?>")
55
_ALLOWED_TAGS_PATTERN = re.compile(
6-
r"</?(h1|h2|b|tt|i|u|hr|br(?: /)?|nobr(?: /)?|pre|sup|p|li|ul|ol|a|(a.*? href=.*?))>"
6+
r"</?(h1|h2|b|tt|i|u|hr|br(?: /)?|nobr(?: /)?|pre|sup|p|li|ul|ol|a|(a.*? href=.*?))>",
77
)
88

99

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
def configure_logger(level: int) -> None:
77
handler = logging.StreamHandler()
88
formatter = logging.Formatter(
9-
"%(asctime)s %(filename)s:%(lineno)d [%(levelname)s] %(message)s"
9+
"%(asctime)s %(filename)s:%(lineno)d [%(levelname)s] %(message)s",
1010
)
1111
handler.setFormatter(formatter)
1212
LOGGER.addHandler(handler)

src/checkmk_weblate_syncer/update_sources.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .config import UpdateSourcesConfig
55
from .git import commit_and_push_files, repository_in_clean_state
66
from .html_tags import forbidden_tags
7-
from .logging import LOGGER
7+
from .logger import LOGGER
88
from .portable_object import make_soure_string_locations_relative, remove_header
99

1010

@@ -14,7 +14,7 @@ def run(config: UpdateSourcesConfig) -> int:
1414

1515
LOGGER.info("Calling pot generation script")
1616
try:
17-
pot_file_content = run_subprocess(
17+
pot_file_content = run_subprocess( # noqa: S603
1818
config.checkmk_repository.path / config.checkmk_pot_generation_script,
1919
check=True,
2020
capture_output=True,
@@ -26,16 +26,17 @@ def run(config: UpdateSourcesConfig) -> int:
2626
e.stdout,
2727
e.stderr,
2828
)
29-
raise e
30-
except IOError as e:
29+
raise
30+
except OSError:
3131
LOGGER.error("Generating pot file failed")
32-
raise e
32+
raise
3333

3434
LOGGER.info("Checking HTML tags")
3535
if forbidden_html_tags := forbidden_tags(remove_header(pot_file_content)):
36-
raise ValueError(
36+
error_msg = (
3737
f"Found forbidden HTML tags: {', '.join(sorted(forbidden_html_tags))}"
3838
)
39+
raise ValueError(error_msg)
3940

4041
LOGGER.info("Making source string locations relative")
4142
pot_file_content = make_soure_string_locations_relative(
@@ -47,18 +48,18 @@ def run(config: UpdateSourcesConfig) -> int:
4748
path_pot_file = config.locale_repository.path / config.locale_pot_path
4849
try:
4950
path_pot_file.write_text(pot_file_content)
50-
except IOError as e:
51+
except OSError:
5152
LOGGER.error("Writing pot file failed")
52-
raise e
53+
raise
5354

5455
LOGGER.info("Checking if pot file has changed in locale repository")
5556
try:
5657
if not locale_repo.is_dirty(untracked_files=True):
5758
LOGGER.info("No changes, exiting")
5859
return 0
59-
except Exception as e:
60+
except Exception:
6061
LOGGER.error("Checking if pot file has changed failed")
61-
raise e
62+
raise
6263

6364
LOGGER.info("Committing and pushing pot file to locale repository")
6465
commit_and_push_files(

src/checkmk_weblate_syncer/update_translations.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .config import PoFilePair, RepositoryConfig, UpdateTranslationsConfig
1010
from .git import commit_and_push_files, repository_in_clean_state
1111
from .html_tags import forbidden_tags
12-
from .logging import LOGGER
12+
from .logger import LOGGER
1313
from .portable_object import (
1414
remove_header,
1515
remove_last_translator,
@@ -47,7 +47,7 @@ def run(config: UpdateTranslationsConfig) -> int:
4747
case _Failure():
4848
LOGGER.error(
4949
"Encountered an error while processing the .po file pair. "
50-
"See the logging output at the end for more information."
50+
"See the logging output at the end for more information.",
5151
)
5252
failures.append(result)
5353
case _:
@@ -85,26 +85,33 @@ def _process_po_file_pair(
8585
locale_po_file = locale_repo.path / file_pair.locale
8686
LOGGER.info("Checking for formatting errors in %s", locale_po_file)
8787
try:
88-
run_subprocess(
89-
["msgfmt", "--check-format", "-o", "-", locale_po_file],
88+
run_subprocess( # noqa: S603
89+
[
90+
"/usr/bin/msgfmt",
91+
"--check-format",
92+
"-o",
93+
"-",
94+
locale_po_file,
95+
],
9096
check=True,
9197
stdout=DEVNULL,
9298
stderr=PIPE,
9399
encoding="UTF-8",
94100
)
95101
except CalledProcessError as e:
96102
return _Failure(
97-
error_message=f"Found formatting errors: {e.stderr}", path=locale_po_file
103+
error_message=f"Found formatting errors: {e.stderr}",
104+
path=locale_po_file,
98105
)
99-
except IOError as e:
106+
except OSError as e:
100107
return _Failure(error_message=str(e), path=locale_po_file)
101108

102109
LOGGER.info("Reading %s", locale_po_file)
103110
try:
104111
po_file_content = locale_po_file.read_text()
105-
except IOError as e:
112+
except OSError as e:
106113
return _Failure(
107-
error_message=f"Encountered error while reading file: {str(e)}",
114+
error_message=f"Encountered error while reading file: {e}",
108115
path=locale_po_file,
109116
)
110117

@@ -122,19 +129,19 @@ def _process_po_file_pair(
122129
LOGGER.info("Writing stripped .po file to checkmk repository: %s", checkmk_po_file)
123130
try:
124131
checkmk_po_file.write_text(po_file_content)
125-
except IOError as e:
132+
except OSError as e:
126133
return _Failure(
127-
f"Encountered error while writing po file to checkmk repository: {e}",
128-
checkmk_po_file,
134+
error_message=f"Encountered error while writing po file to checkmk repository: {e}",
135+
path=checkmk_po_file,
129136
)
130137
return _Success(checkmk_po_file)
131138

132139

133140
def _is_repo_dirty(repo: Repo) -> bool:
134141
try:
135142
return repo.is_dirty(untracked_files=True)
136-
except Exception as e:
143+
except Exception:
137144
LOGGER.error(
138-
"Checking if any .po files changed in the checkmk repository failed"
145+
"Checking if any .po files changed in the checkmk repository failed",
139146
)
140-
raise e
147+
raise

tests/test_html_tags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
@pytest.mark.parametrize(
7-
["text", "expected_result"],
7+
("text", "expected_result"),
88
[
99
pytest.param(
1010
"abc123",

tests/test_portable_object.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def test_remove_header() -> None:
4545
#, python-format
4646
msgid " (Duration: %s)"
4747
msgstr ""
48-
"""
48+
""",
4949
)
5050
# pylint: disable=line-too-long
5151
== """#: /home/weblate/checkmk_weblate_sync/git/checkmk/cmk/gui/wato/pages/host_rename.py:640
@@ -183,7 +183,7 @@ def test_remove_source_string_locations() -> None:
183183
#: cmk/gui/visuals/_page_edit_visual.py:137
184184
msgid " (Copy)"
185185
msgstr " (Kopie)"
186-
"""
186+
""",
187187
)
188188
== """# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
189189
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
@@ -255,7 +255,7 @@ def test_remove_last_translator() -> None:
255255
#: cmk/gui/visuals/_page_edit_visual.py:137
256256
msgid " (Copy)"
257257
msgstr " (Kopie)"
258-
"""
258+
""",
259259
)
260260
== """# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
261261
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and

0 commit comments

Comments
 (0)