Skip to content

Commit

Permalink
Fix po file stripping and add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jherbel committed May 15, 2024
1 parent c847653 commit 4d303fc
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 24 deletions.
52 changes: 30 additions & 22 deletions checkmk_weblate_syncer/po.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,18 @@ def _process_po_file_pair(
except IOError as e:
return _Failure(error_message=str(e), path=locale_po_file)

LOGGER.info("Removing unwanted lines from %s", locale_po_file)
if isinstance(po_file_content := _remove_unwanted_lines(locale_po_file), _Failure):
return po_file_content
LOGGER.info("Reading %s", locale_po_file)
try:
po_file_content = locale_po_file.read_text()
except IOError as e:
return _Failure(
error_message=f"Encountered error while reading file: {str(e)}",
path=locale_po_file,
)

LOGGER.info("Stripping source string locations and Last-Translator")
po_file_content = _remove_source_string_locations(po_file_content)
po_file_content = _remove_last_translator(po_file_content)

LOGGER.info("Writing stripped .po file to checkmk repository: %s", checkmk_po_file)
try:
Expand All @@ -110,6 +119,24 @@ def _process_po_file_pair(
return _Success(checkmk_po_file)


def _remove_source_string_locations(po_file_content: str) -> str:
return re.sub(
r"^#: .*?:\d+\n",
"",
po_file_content,
flags=re.MULTILINE | re.DOTALL,
)


def _remove_last_translator(po_file_content: str) -> str:
return re.sub(
r"^\"Last-Translator:.*?\"\n",
"",
po_file_content,
flags=re.MULTILINE | re.DOTALL,
)


def _is_repo_dirty(repo: Repo) -> bool:
try:
return repo.is_dirty(untracked_files=True)
Expand All @@ -118,22 +145,3 @@ def _is_repo_dirty(repo: Repo) -> bool:
"Checking if any .po files changed in the checkmk repository failed"
)
raise e


def _remove_unwanted_lines(file_path: Path) -> str | _Failure:
LOGGER.info("Reading %s", file_path)
try:
po_file_content = file_path.read_text()
except IOError as e:
return _Failure(
error_message=f"Encountered error while reading file: {str(e)}",
path=file_path,
)
LOGGER.info("Removing code comments from %s", file_path)
po_file_content = re.sub(r"^#.+\d\n", "", po_file_content, flags=re.DOTALL)

LOGGER.info("Removing last translator information from %s", file_path)
po_file_content = re.sub(
r"\"Last-Translator:.+?\n", "", po_file_content, flags=re.DOTALL
)
return po_file_content
2 changes: 0 additions & 2 deletions tests/test_fake.py

This file was deleted.

115 changes: 115 additions & 0 deletions tests/test_po.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
from checkmk_weblate_syncer.po import (
_remove_last_translator,
_remove_source_string_locations,
)

_PO_FILE_CONTENT = """# 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
# conditions defined in the file COPYING, which is part of this source code package.
msgid ""
msgstr ""
"Project-Id-Version: Checkmk user interface translation 0.1\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2011-05-13 09:42+0200\n"
"PO-Revision-Date: 2024-05-15 06:43+0000\n"
"Last-Translator: Employee <[email protected]>\n"
"Language-Team: German <https://translate.checkmk.com/projects/checkmk/"
"software/de/>\n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: Weblate 5.0.2\n"
#: /home/weblate/git/check_mk/cmk/gui/cee/sla/_sla_painter/_service_outage_count.py:31
#, python-format
msgid " %s, outage duration %s"
msgstr " %s, Dauer des Ausfalls %s"
#: /home/weblate/git/check_mk/cmk/gui/wato/pages/host_rename.py:628
#, python-format
msgid " (%d times)"
msgstr " (%d mal)"
#: /home/weblate/git/check_mk/cmk/gui/visuals/_page_edit_visual.py:137
msgid " (Copy)"
msgstr " (Kopie)"
"""


def test_remove_source_string_locations() -> None:
assert (
_remove_source_string_locations(_PO_FILE_CONTENT)
== """# 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
# conditions defined in the file COPYING, which is part of this source code package.
msgid ""
msgstr ""
"Project-Id-Version: Checkmk user interface translation 0.1\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2011-05-13 09:42+0200\n"
"PO-Revision-Date: 2024-05-15 06:43+0000\n"
"Last-Translator: Employee <[email protected]>\n"
"Language-Team: German <https://translate.checkmk.com/projects/checkmk/"
"software/de/>\n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: Weblate 5.0.2\n"
#, python-format
msgid " %s, outage duration %s"
msgstr " %s, Dauer des Ausfalls %s"
#, python-format
msgid " (%d times)"
msgstr " (%d mal)"
msgid " (Copy)"
msgstr " (Kopie)"
"""
)


def test_remove_last_translator() -> None:
assert (
_remove_last_translator(_PO_FILE_CONTENT)
== """# 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
# conditions defined in the file COPYING, which is part of this source code package.
msgid ""
msgstr ""
"Project-Id-Version: Checkmk user interface translation 0.1\n"
"Report-Msgid-Bugs-To: [email protected]\n"
"POT-Creation-Date: 2011-05-13 09:42+0200\n"
"PO-Revision-Date: 2024-05-15 06:43+0000\n"
"Language-Team: German <https://translate.checkmk.com/projects/checkmk/"
"software/de/>\n"
"Language: de\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=n != 1;\n"
"X-Generator: Weblate 5.0.2\n"
#: /home/weblate/git/check_mk/cmk/gui/cee/sla/_sla_painter/_service_outage_count.py:31
#, python-format
msgid " %s, outage duration %s"
msgstr " %s, Dauer des Ausfalls %s"
#: /home/weblate/git/check_mk/cmk/gui/wato/pages/host_rename.py:628
#, python-format
msgid " (%d times)"
msgstr " (%d mal)"
#: /home/weblate/git/check_mk/cmk/gui/visuals/_page_edit_visual.py:137
msgid " (Copy)"
msgstr " (Kopie)"
"""
)

0 comments on commit 4d303fc

Please sign in to comment.