Skip to content

Commit

Permalink
Enable all ruff rules + disable specific ones
Browse files Browse the repository at this point in the history
Also adjust code accordingly.
  • Loading branch information
jherbel committed Jan 14, 2025
1 parent 6c2e0df commit a49fa88
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 43 deletions.
14 changes: 14 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,19 @@ dev = [
"ruff>=0.9.1",
]

[tool.ruff.lint]
select = ["ALL"]
ignore = [
"COM812",
"D100",
"D101",
"D103",
"D104",
"D203",
"D213",
"E501",
"S101",
]

[tool.mypy]
strict = true
12 changes: 6 additions & 6 deletions src/checkmk_weblate_syncer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from .cli import Mode, parse_arguments
from .config import UpdateSourcesConfig, UpdateTranslationsConfig
from .logging import LOGGER, configure_logger
from .logger import LOGGER, configure_logger
from .update_sources import run as run_update_sources
from .update_translations import run as run_update_translations

Expand All @@ -16,13 +16,13 @@ def _main() -> None:
match args.mode:
case Mode.UPDATE_SOURCES:
sys.exit(
run_update_sources(_load_config(args.config_path, UpdateSourcesConfig))
run_update_sources(_load_config(args.config_path, UpdateSourcesConfig)),
)
case Mode.UPDATE_TRANSLATIONS:
sys.exit(
run_update_translations(
_load_config(args.config_path, UpdateTranslationsConfig)
)
_load_config(args.config_path, UpdateTranslationsConfig),
),
)
case _:
assert_never(args.mode)
Expand All @@ -34,9 +34,9 @@ def _main() -> None:
def _load_config(config_path: Path, config_type: type[_ConfigTypeT]) -> _ConfigTypeT:
try:
return config_type.model_validate_json(config_path.read_text())
except Exception as e:
except Exception:
LOGGER.error("Loading config failed")
raise e
raise


_main()
3 changes: 2 additions & 1 deletion src/checkmk_weblate_syncer/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class BaseConfig(BaseModel, frozen=True):

class UpdateSourcesConfig(BaseConfig, frozen=True):
checkmk_pot_generation_script: Annotated[
Path, AfterValidator(_validate_path_is_relative)
Path,
AfterValidator(_validate_path_is_relative),
]
locale_pot_path: Annotated[Path, AfterValidator(_validate_path_is_relative)]
commit_message: str
Expand Down
19 changes: 11 additions & 8 deletions src/checkmk_weblate_syncer/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from git import Repo

from .config import RepositoryConfig
from .logging import LOGGER
from .logger import LOGGER


def repository_in_clean_state(
Expand All @@ -17,11 +17,12 @@ def repository_in_clean_state(
repo_config.path,
repo_config.branch,
)
except Exception as e:
except Exception:
LOGGER.error(
"Error while cleaning up and updating %s repository", repo_config.path
"Error while cleaning up and updating %s repository",
repo_config.path,
)
raise e
raise


def _repository_in_clean_state(path: Path, branch: str) -> Repo:
Expand All @@ -42,11 +43,13 @@ def commit_and_push_files(
repo.index.add(files)
repo.index.commit(commit_message)
repo.remotes.origin.push()
except CalledProcessError as e:
except CalledProcessError:
LOGGER.error(
"Committing and pushing files for repository %s failed", repo.working_dir
"Committing and pushing files for repository %s failed",
repo.working_dir,
)
raise e
raise
LOGGER.info(
"Committing and pushing files for repository %s succeeded", repo.working_dir
"Committing and pushing files for repository %s succeeded",
repo.working_dir,
)
2 changes: 1 addition & 1 deletion src/checkmk_weblate_syncer/html_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# keep in sync with tests/pylint/checker_localization.py:HTMLTagsChecker
_TAG_PATTERN = re.compile("<.*?>")
_ALLOWED_TAGS_PATTERN = re.compile(
r"</?(h1|h2|b|tt|i|u|hr|br(?: /)?|nobr(?: /)?|pre|sup|p|li|ul|ol|a|(a.*? href=.*?))>"
r"</?(h1|h2|b|tt|i|u|hr|br(?: /)?|nobr(?: /)?|pre|sup|p|li|ul|ol|a|(a.*? href=.*?))>",
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def configure_logger(level: int) -> None:
handler = logging.StreamHandler()
formatter = logging.Formatter(
"%(asctime)s %(filename)s:%(lineno)d [%(levelname)s] %(message)s"
"%(asctime)s %(filename)s:%(lineno)d [%(levelname)s] %(message)s",
)
handler.setFormatter(formatter)
LOGGER.addHandler(handler)
Expand Down
21 changes: 11 additions & 10 deletions src/checkmk_weblate_syncer/update_sources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .config import UpdateSourcesConfig
from .git import commit_and_push_files, repository_in_clean_state
from .html_tags import forbidden_tags
from .logging import LOGGER
from .logger import LOGGER
from .portable_object import make_soure_string_locations_relative, remove_header


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

LOGGER.info("Calling pot generation script")
try:
pot_file_content = run_subprocess(
pot_file_content = run_subprocess( # noqa: S603
config.checkmk_repository.path / config.checkmk_pot_generation_script,
check=True,
capture_output=True,
Expand All @@ -26,16 +26,17 @@ def run(config: UpdateSourcesConfig) -> int:
e.stdout,
e.stderr,
)
raise e
except IOError as e:
raise
except OSError:
LOGGER.error("Generating pot file failed")
raise e
raise

LOGGER.info("Checking HTML tags")
if forbidden_html_tags := forbidden_tags(remove_header(pot_file_content)):
raise ValueError(
error_msg = (
f"Found forbidden HTML tags: {', '.join(sorted(forbidden_html_tags))}"
)
raise ValueError(error_msg)

LOGGER.info("Making source string locations relative")
pot_file_content = make_soure_string_locations_relative(
Expand All @@ -47,18 +48,18 @@ def run(config: UpdateSourcesConfig) -> int:
path_pot_file = config.locale_repository.path / config.locale_pot_path
try:
path_pot_file.write_text(pot_file_content)
except IOError as e:
except OSError:
LOGGER.error("Writing pot file failed")
raise e
raise

LOGGER.info("Checking if pot file has changed in locale repository")
try:
if not locale_repo.is_dirty(untracked_files=True):
LOGGER.info("No changes, exiting")
return 0
except Exception as e:
except Exception:
LOGGER.error("Checking if pot file has changed failed")
raise e
raise

LOGGER.info("Committing and pushing pot file to locale repository")
commit_and_push_files(
Expand Down
31 changes: 19 additions & 12 deletions src/checkmk_weblate_syncer/update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .config import PoFilePair, RepositoryConfig, UpdateTranslationsConfig
from .git import commit_and_push_files, repository_in_clean_state
from .html_tags import forbidden_tags
from .logging import LOGGER
from .logger import LOGGER
from .portable_object import (
remove_header,
remove_last_translator,
Expand Down Expand Up @@ -47,7 +47,7 @@ def run(config: UpdateTranslationsConfig) -> int:
case _Failure():
LOGGER.error(
"Encountered an error while processing the .po file pair. "
"See the logging output at the end for more information."
"See the logging output at the end for more information.",
)
failures.append(result)
case _:
Expand Down Expand Up @@ -85,26 +85,33 @@ def _process_po_file_pair(
locale_po_file = locale_repo.path / file_pair.locale
LOGGER.info("Checking for formatting errors in %s", locale_po_file)
try:
run_subprocess(
["msgfmt", "--check-format", "-o", "-", locale_po_file],
run_subprocess( # noqa: S603
[
"/usr/bin/msgfmt",
"--check-format",
"-o",
"-",
locale_po_file,
],
check=True,
stdout=DEVNULL,
stderr=PIPE,
encoding="UTF-8",
)
except CalledProcessError as e:
return _Failure(
error_message=f"Found formatting errors: {e.stderr}", path=locale_po_file
error_message=f"Found formatting errors: {e.stderr}",
path=locale_po_file,
)
except IOError as e:
except OSError as e:
return _Failure(error_message=str(e), path=locale_po_file)

LOGGER.info("Reading %s", locale_po_file)
try:
po_file_content = locale_po_file.read_text()
except IOError as e:
except OSError as e:
return _Failure(
error_message=f"Encountered error while reading file: {str(e)}",
error_message=f"Encountered error while reading file: {e!s}",
path=locale_po_file,
)

Expand All @@ -122,7 +129,7 @@ def _process_po_file_pair(
LOGGER.info("Writing stripped .po file to checkmk repository: %s", checkmk_po_file)
try:
checkmk_po_file.write_text(po_file_content)
except IOError as e:
except OSError as e:
return _Failure(
f"Encountered error while writing po file to checkmk repository: {e}",
checkmk_po_file,
Expand All @@ -133,8 +140,8 @@ def _process_po_file_pair(
def _is_repo_dirty(repo: Repo) -> bool:
try:
return repo.is_dirty(untracked_files=True)
except Exception as e:
except Exception:
LOGGER.error(
"Checking if any .po files changed in the checkmk repository failed"
"Checking if any .po files changed in the checkmk repository failed",
)
raise e
raise
2 changes: 1 addition & 1 deletion tests/test_html_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


@pytest.mark.parametrize(
["text", "expected_result"],
("text", "expected_result"),
[
pytest.param(
"abc123",
Expand Down
6 changes: 3 additions & 3 deletions tests/test_portable_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_remove_header() -> None:
#, python-format
msgid " (Duration: %s)"
msgstr ""
"""
""",
)
# pylint: disable=line-too-long
== """#: /home/weblate/checkmk_weblate_sync/git/checkmk/cmk/gui/wato/pages/host_rename.py:640
Expand Down Expand Up @@ -183,7 +183,7 @@ def test_remove_source_string_locations() -> None:
#: cmk/gui/visuals/_page_edit_visual.py:137
msgid " (Copy)"
msgstr " (Kopie)"
"""
""",
)
== """# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
Expand Down Expand Up @@ -255,7 +255,7 @@ def test_remove_last_translator() -> None:
#: cmk/gui/visuals/_page_edit_visual.py:137
msgid " (Copy)"
msgstr " (Kopie)"
"""
""",
)
== """# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
Expand Down

0 comments on commit a49fa88

Please sign in to comment.