From 5cc02f2a77283791a6ed6e2d05d578c38157764b Mon Sep 17 00:00:00 2001 From: Bart Feenstra Date: Fri, 20 Dec 2024 14:37:59 +0000 Subject: [PATCH] Fix a fatal error in the commands to create and update an extension's translations --- .../cli/commands/extension_new_translation.py | 4 ++-- .../commands/extension_update_translations.py | 4 ++-- betty/locale/translation.py | 17 +++++++++++++-- betty/tests/locale/test_translation.py | 21 +++++++++++++++++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/betty/cli/commands/extension_new_translation.py b/betty/cli/commands/extension_new_translation.py index b68969186..0fbd46be9 100644 --- a/betty/cli/commands/extension_new_translation.py +++ b/betty/cli/commands/extension_new_translation.py @@ -10,7 +10,7 @@ from betty.cli.commands import command, parameter_callback, Command from betty.locale import translation from betty.locale.localizable import _ -from betty.locale.translation import assert_extension_assets_directory_path +from betty.locale.translation import assert_extension_has_assets_directory_path from betty.plugin import ShorthandPluginBase from betty.project import extension @@ -53,7 +53,7 @@ async def click_command(self) -> click.Command: "extension", required=True, callback=parameter_callback( - lambda extension_id: assert_extension_assets_directory_path( + lambda extension_id: assert_extension_has_assets_directory_path( extension_id_to_type_map.get(extension_id) ) ), diff --git a/betty/cli/commands/extension_update_translations.py b/betty/cli/commands/extension_update_translations.py index 42c8b696e..cab388646 100644 --- a/betty/cli/commands/extension_update_translations.py +++ b/betty/cli/commands/extension_update_translations.py @@ -15,7 +15,7 @@ from betty.cli.commands import command, Command, parameter_callback from betty.locale import translation from betty.locale.localizable import _ -from betty.locale.translation import assert_extension_assets_directory_path +from betty.locale.translation import assert_extension_has_assets_directory_path from betty.plugin import ShorthandPluginBase from betty.project import extension @@ -59,7 +59,7 @@ async def click_command(self) -> click.Command: "extension", required=True, callback=parameter_callback( - lambda extension_id: assert_extension_assets_directory_path( + lambda extension_id: assert_extension_has_assets_directory_path( extension_id_to_type_mapping.get(extension_id) ) ), diff --git a/betty/locale/translation.py b/betty/locale/translation.py index 10fa131be..6adb139d6 100644 --- a/betty/locale/translation.py +++ b/betty/locale/translation.py @@ -8,7 +8,7 @@ from contextlib import redirect_stdout from io import StringIO from pathlib import Path -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, TypeVar from aiofiles.os import makedirs from aiofiles.ospath import exists @@ -18,11 +18,14 @@ from betty.locale import get_data from betty.locale.babel import run_babel from betty.locale.localizable import _ +from betty.project.extension import Extension if TYPE_CHECKING: from collections.abc import Iterable from betty.project import Project - from betty.project.extension import Extension + + +ExtensionT = TypeVar("ExtensionT", bound=Extension) def assert_extension_assets_directory_path(extension: type[Extension]) -> Path: @@ -39,6 +42,16 @@ def assert_extension_assets_directory_path(extension: type[Extension]) -> Path: return assets_directory_path +def assert_extension_has_assets_directory_path( + extension: type[ExtensionT], +) -> type[ExtensionT]: + """ + Check that the given extension has an assets directory, and return it. + """ + assert_extension_assets_directory_path(extension) + return extension + + async def new_extension_translation(locale: str, extension: type[Extension]) -> None: """ Create a new translation for the given extension. diff --git a/betty/tests/locale/test_translation.py b/betty/tests/locale/test_translation.py index 0240b2738..decca1a7b 100644 --- a/betty/tests/locale/test_translation.py +++ b/betty/tests/locale/test_translation.py @@ -10,6 +10,7 @@ from betty.locale.translation import ( update_dev_translations, assert_extension_assets_directory_path, + assert_extension_has_assets_directory_path, ) from betty.test_utils.locale import PotFileTestBase from betty.test_utils.project.extension import DummyExtension @@ -35,6 +36,26 @@ def test_with_assets_directory(self) -> None: ) +class TestAssertExtensionHasAssetsDirectoryPath: + class _DummyExtensionWithAssetsDirectory(DummyExtension): + @override + @classmethod + def assets_directory_path(cls) -> Path | None: + return Path(__file__) + + def test_without_assets_directory(self) -> None: + with pytest.raises(UserFacingError): + assert_extension_has_assets_directory_path(DummyExtension) + + def test_with_assets_directory(self) -> None: + assert ( + assert_extension_has_assets_directory_path( + self._DummyExtensionWithAssetsDirectory + ) + == self._DummyExtensionWithAssetsDirectory + ) + + class TestPotFile(PotFileTestBase): @override def assets_directory_path(self) -> Path: