diff --git a/checkmk_weblate_syncer/portable_object.py b/checkmk_weblate_syncer/portable_object.py new file mode 100644 index 0000000..de047ce --- /dev/null +++ b/checkmk_weblate_syncer/portable_object.py @@ -0,0 +1,32 @@ +import re +from pathlib import Path + + +def make_soure_string_locations_relative( + portable_object_content: str, + relative_to: Path, +) -> str: + return re.sub( + rf"^#: ({relative_to}\/)(.*?:\d+)\n", + r"#: \g<2>\n", + portable_object_content, + flags=re.MULTILINE | re.DOTALL, + ) + + +def remove_source_string_locations(portable_object_content: str) -> str: + return re.sub( + r"^#: .*?:\d+\n", + "", + portable_object_content, + flags=re.MULTILINE | re.DOTALL, + ) + + +def remove_last_translator(portable_object_content: str) -> str: + return re.sub( + r"^\"Last-Translator:.*?\"\n", + "", + portable_object_content, + flags=re.MULTILINE | re.DOTALL, + ) diff --git a/checkmk_weblate_syncer/update_sources.py b/checkmk_weblate_syncer/update_sources.py index 7f90eb6..d92af2e 100644 --- a/checkmk_weblate_syncer/update_sources.py +++ b/checkmk_weblate_syncer/update_sources.py @@ -1,11 +1,10 @@ -import re -from pathlib import Path from subprocess import CalledProcessError from subprocess import run as run_subprocess from .config import UpdateSourcesConfig from .git import commit_and_push_files, repository_in_clean_state from .logging import LOGGER +from .portable_object import make_soure_string_locations_relative def run(config: UpdateSourcesConfig) -> int: @@ -14,12 +13,12 @@ def run(config: UpdateSourcesConfig) -> int: LOGGER.info("Calling pot generation script") try: - completed_pot_generation_process = run_subprocess( + pot_file_content = run_subprocess( config.checkmk_repository.path / config.checkmk_pot_generation_script, check=True, capture_output=True, encoding="UTF-8", - ) + ).stdout except CalledProcessError as e: LOGGER.error( "Generating pot file failed.\n\nStdout:\n%s\n\nStderr:\n%s", @@ -32,8 +31,8 @@ def run(config: UpdateSourcesConfig) -> int: raise e LOGGER.info("Making source string locations relative") - pot_file_content = _make_soure_string_locations_relative( - pot_file_content=completed_pot_generation_process.stdout, + pot_file_content = make_soure_string_locations_relative( + portable_object_content=pot_file_content, relative_to=config.checkmk_repository.path, ) @@ -61,15 +60,3 @@ def run(config: UpdateSourcesConfig) -> int: commit_message=config.commit_message, ) return 0 - - -def _make_soure_string_locations_relative( - pot_file_content: str, - relative_to: Path, -) -> str: - return re.sub( - rf"^#: ({relative_to}\/)(.*?:\d+)\n", - r"#: \g<2>\n", - pot_file_content, - flags=re.MULTILINE | re.DOTALL, - ) diff --git a/checkmk_weblate_syncer/update_translations.py b/checkmk_weblate_syncer/update_translations.py index 7a056e0..0a9efe4 100644 --- a/checkmk_weblate_syncer/update_translations.py +++ b/checkmk_weblate_syncer/update_translations.py @@ -1,4 +1,3 @@ -import re from dataclasses import dataclass from pathlib import Path from subprocess import DEVNULL, PIPE, CalledProcessError @@ -10,6 +9,7 @@ from .config import PoFilePair, RepositoryConfig, UpdateTranslationsConfig from .git import commit_and_push_files, repository_in_clean_state from .logging import LOGGER +from .portable_object import remove_last_translator, remove_source_string_locations @dataclass(frozen=True) @@ -106,8 +106,8 @@ def _process_po_file_pair( ) 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) + 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: @@ -120,24 +120,6 @@ 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) diff --git a/tests/test_portable_object.py b/tests/test_portable_object.py new file mode 100644 index 0000000..3e2863b --- /dev/null +++ b/tests/test_portable_object.py @@ -0,0 +1,231 @@ +from pathlib import Path + +from checkmk_weblate_syncer.portable_object import ( + make_soure_string_locations_relative, + remove_last_translator, + remove_source_string_locations, +) + + +def test_make_soure_string_locations_relative() -> None: + assert ( + make_soure_string_locations_relative( + """# 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: feedback@checkmk.com\n" +"POT-Creation-Date: 2011-05-13 09:42+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: /home/weblate/checkmk_weblate_sync/git/checkmk/cmk/gui/wato/pages/host_rename.py:640 +#, python-format +msgid " (%d times)" +msgstr "" + +#: /home/weblate/checkmk_weblate_sync/git/checkmk/cmk/gui/visuals/_page_edit_visual.py:137 +msgid " (Copy)" +msgstr "" + +#: /home/weblate/checkmk_weblate_sync/git/checkmk/cmk/gui/nodevis/topology.py:1814 +msgid " (Data incomplete, maximum number of nodes reached)" +msgstr "" + +#: /home/weblate/checkmk_weblate_sync/git/checkmk/cmk/gui/backup/handler.py:969 +#, python-format +msgid " (Duration: %s)" +msgstr "" +""", + Path("/home/weblate/checkmk_weblate_sync/git/checkmk"), + ) + == """# 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: feedback@checkmk.com\n" +"POT-Creation-Date: 2011-05-13 09:42+0200\n" +"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: LANGUAGE \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=utf-8\n" +"Content-Transfer-Encoding: 8bit\n" + +#: cmk/gui/wato/pages/host_rename.py:640 +#, python-format +msgid " (%d times)" +msgstr "" + +#: cmk/gui/visuals/_page_edit_visual.py:137 +msgid " (Copy)" +msgstr "" + +#: cmk/gui/nodevis/topology.py:1814 +msgid " (Data incomplete, maximum number of nodes reached)" +msgstr "" + +#: cmk/gui/backup/handler.py:969 +#, python-format +msgid " (Duration: %s)" +msgstr "" +""" + ) + + +def test_remove_source_string_locations() -> None: + assert ( + remove_source_string_locations( + """# 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: someone@checkmk.com\n" +"POT-Creation-Date: 2011-05-13 09:42+0200\n" +"PO-Revision-Date: 2024-05-15 06:43+0000\n" +"Last-Translator: Employee \n" +"Language-Team: German \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" + +#: cmk/gui/cee/sla/_sla_painter/_service_outage_count.py:31 +#, python-format +msgid " %s, outage duration %s" +msgstr " %s, Dauer des Ausfalls %s" + +#: cmk/gui/wato/pages/host_rename.py:628 +#, python-format +msgid " (%d times)" +msgstr " (%d mal)" + +#: 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 +# 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: someone@checkmk.com\n" +"POT-Creation-Date: 2011-05-13 09:42+0200\n" +"PO-Revision-Date: 2024-05-15 06:43+0000\n" +"Last-Translator: Employee \n" +"Language-Team: German \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( + """# 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: someone@checkmk.com\n" +"POT-Creation-Date: 2011-05-13 09:42+0200\n" +"PO-Revision-Date: 2024-05-15 06:43+0000\n" +"Last-Translator: Employee \n" +"Language-Team: German \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" + +#: cmk/gui/cee/sla/_sla_painter/_service_outage_count.py:31 +#, python-format +msgid " %s, outage duration %s" +msgstr " %s, Dauer des Ausfalls %s" + +#: cmk/gui/wato/pages/host_rename.py:628 +#, python-format +msgid " (%d times)" +msgstr " (%d mal)" + +#: 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 +# 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: someone@checkmk.com\n" +"POT-Creation-Date: 2011-05-13 09:42+0200\n" +"PO-Revision-Date: 2024-05-15 06:43+0000\n" +"Language-Team: German \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" + +#: cmk/gui/cee/sla/_sla_painter/_service_outage_count.py:31 +#, python-format +msgid " %s, outage duration %s" +msgstr " %s, Dauer des Ausfalls %s" + +#: cmk/gui/wato/pages/host_rename.py:628 +#, python-format +msgid " (%d times)" +msgstr " (%d mal)" + +#: cmk/gui/visuals/_page_edit_visual.py:137 +msgid " (Copy)" +msgstr " (Kopie)" +""" + ) diff --git a/tests/test_update_sources.py b/tests/test_update_sources.py deleted file mode 100644 index 9244a84..0000000 --- a/tests/test_update_sources.py +++ /dev/null @@ -1,81 +0,0 @@ -from pathlib import Path - -from checkmk_weblate_syncer.update_sources import _make_soure_string_locations_relative - - -def test_make_soure_string_locations_relative() -> None: - assert ( - _make_soure_string_locations_relative( - """# 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: feedback@checkmk.com\n" -"POT-Creation-Date: 2011-05-13 09:42+0200\n" -"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" -"Last-Translator: FULL NAME \n" -"Language-Team: LANGUAGE \n" -"Language: LANGUAGE \n" -"MIME-Version: 1.0\n" -"Content-Type: text/plain; charset=utf-8\n" -"Content-Transfer-Encoding: 8bit\n" - -#: /home/weblate/checkmk_weblate_sync/git/checkmk/cmk/gui/wato/pages/host_rename.py:640 -#, python-format -msgid " (%d times)" -msgstr "" - -#: /home/weblate/checkmk_weblate_sync/git/checkmk/cmk/gui/visuals/_page_edit_visual.py:137 -msgid " (Copy)" -msgstr "" - -#: /home/weblate/checkmk_weblate_sync/git/checkmk/cmk/gui/nodevis/topology.py:1814 -msgid " (Data incomplete, maximum number of nodes reached)" -msgstr "" - -#: /home/weblate/checkmk_weblate_sync/git/checkmk/cmk/gui/backup/handler.py:969 -#, python-format -msgid " (Duration: %s)" -msgstr "" -""", - Path("/home/weblate/checkmk_weblate_sync/git/checkmk"), - ) - == """# 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: feedback@checkmk.com\n" -"POT-Creation-Date: 2011-05-13 09:42+0200\n" -"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" -"Last-Translator: FULL NAME \n" -"Language-Team: LANGUAGE \n" -"Language: LANGUAGE \n" -"MIME-Version: 1.0\n" -"Content-Type: text/plain; charset=utf-8\n" -"Content-Transfer-Encoding: 8bit\n" - -#: cmk/gui/wato/pages/host_rename.py:640 -#, python-format -msgid " (%d times)" -msgstr "" - -#: cmk/gui/visuals/_page_edit_visual.py:137 -msgid " (Copy)" -msgstr "" - -#: cmk/gui/nodevis/topology.py:1814 -msgid " (Data incomplete, maximum number of nodes reached)" -msgstr "" - -#: cmk/gui/backup/handler.py:969 -#, python-format -msgid " (Duration: %s)" -msgstr "" -""" - ) diff --git a/tests/test_update_translations.py b/tests/test_update_translations.py deleted file mode 100644 index bf09621..0000000 --- a/tests/test_update_translations.py +++ /dev/null @@ -1,115 +0,0 @@ -from checkmk_weblate_syncer.update_translations 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: someone@checkmk.com\n" -"POT-Creation-Date: 2011-05-13 09:42+0200\n" -"PO-Revision-Date: 2024-05-15 06:43+0000\n" -"Last-Translator: Employee \n" -"Language-Team: German \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: someone@checkmk.com\n" -"POT-Creation-Date: 2011-05-13 09:42+0200\n" -"PO-Revision-Date: 2024-05-15 06:43+0000\n" -"Last-Translator: Employee \n" -"Language-Team: German \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: someone@checkmk.com\n" -"POT-Creation-Date: 2011-05-13 09:42+0200\n" -"PO-Revision-Date: 2024-05-15 06:43+0000\n" -"Language-Team: German \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)" -""" - )