diff --git a/checkmk_weblate_syncer/pot.py b/checkmk_weblate_syncer/pot.py index bf7da02..860a684 100644 --- a/checkmk_weblate_syncer/pot.py +++ b/checkmk_weblate_syncer/pot.py @@ -1,3 +1,5 @@ +import re +from pathlib import Path from subprocess import CalledProcessError from subprocess import run as run_subprocess @@ -29,10 +31,16 @@ def run(config: PotModeConfig) -> int: LOGGER.error("Generating pot file failed") 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, + relative_to=config.checkmk_repository.path, + ) + LOGGER.info("Writing pot file to locale repository") path_pot_file = config.locale_repository.path / config.locale_pot_path try: - path_pot_file.write_text(completed_pot_generation_process.stdout) + path_pot_file.write_text(pot_file_content) except IOError as e: LOGGER.error("Writing pot file failed") raise e @@ -53,3 +61,15 @@ def run(config: PotModeConfig) -> 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/tests/test_pot.py b/tests/test_pot.py new file mode 100644 index 0000000..c30264c --- /dev/null +++ b/tests/test_pot.py @@ -0,0 +1,81 @@ +from pathlib import Path + +from checkmk_weblate_syncer.pot 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 "" +""" + )