diff --git a/plover_platform_specific_translation/config/actions.py b/plover_platform_specific_translation/config/actions.py index f68b881..20910b2 100644 --- a/plover_platform_specific_translation/config/actions.py +++ b/plover_platform_specific_translation/config/actions.py @@ -2,7 +2,10 @@ Module to handle reading in the application JSON config file. """ from pathlib import Path -from typing import Tuple +from typing import ( + Any, + Tuple +) from . import file @@ -14,8 +17,9 @@ def load(config_filepath: Path) -> dict[str, Tuple[str, str]]: Raises an error if the specified config file is not JSON format. """ - data = file.load(config_filepath) + data: dict[str, Any] = file.load(config_filepath) + config_platform_translations: Any try: config_platform_translations = data["platform_specific_translations"] except KeyError: @@ -24,7 +28,7 @@ def load(config_filepath: Path) -> dict[str, Tuple[str, str]]: if not isinstance(config_platform_translations, dict): raise ValueError("'platform_specific_translations' must be a dict") - platform_translations = { + platform_translations: dict[str, Tuple[str, str]] = { outline_translation: tuple(resolved_translation) for outline_translation, resolved_translation in config_platform_translations.items() @@ -39,5 +43,7 @@ def save( """ Saves the set of platform-specific translations to the config JSON file. """ - data = {"platform_specific_translations": platform_translations} + data: dict[str, dict[str, Tuple[str, str]]] = { + "platform_specific_translations": platform_translations + } file.save(config_filepath, data) diff --git a/plover_platform_specific_translation/config/file.py b/plover_platform_specific_translation/config/file.py index 1f0b78d..d1a24c7 100644 --- a/plover_platform_specific_translation/config/file.py +++ b/plover_platform_specific_translation/config/file.py @@ -15,6 +15,7 @@ def load(filepath: Path) -> dict[str, Any]: Raises an error if the specified config file is not JSON format. """ + data: dict[str, Any] try: with filepath.open(encoding="utf-8") as file: data = json.load(file) @@ -24,7 +25,7 @@ def load(filepath: Path) -> dict[str, Any]: except json.JSONDecodeError as exc: raise ValueError("Unable to decode file contents as JSON") from exc - return data # type: ignore[no-any-return] + return data def save(filepath: Path, data: dict[str, dict[str, Tuple[str, str]]]) -> None: """ diff --git a/plover_platform_specific_translation/platform/parser.py b/plover_platform_specific_translation/platform/parser.py index d468fce..8d13e09 100644 --- a/plover_platform_specific_translation/platform/parser.py +++ b/plover_platform_specific_translation/platform/parser.py @@ -6,7 +6,7 @@ import platform -_PLATFORM_MAPPINGS = { +_PLATFORM_MAPPINGS: dict[str, str] = { "Windows": "WINDOWS", "Darwin": "MAC", "Linux": "LINUX" diff --git a/plover_platform_specific_translation/translation/resolver.py b/plover_platform_specific_translation/translation/resolver.py index 09f8a39..2b20675 100644 --- a/plover_platform_specific_translation/translation/resolver.py +++ b/plover_platform_specific_translation/translation/resolver.py @@ -4,14 +4,19 @@ """ import re from typing import ( + Any, + Iterator, + Optional, + Match, Pattern, - Tuple + Tuple, + Union ) -COMBO = "combo" -COMMAND = "command" -_TEXT = "text" +COMBO: str = "combo" +COMMAND: str = "command" +_TEXT: str = "text" # Ignore any colons in commands contained in outline translations. _ARGUMENT_DIVIDER: Pattern[str] = re.compile( @@ -31,8 +36,11 @@ def resolve(platform: str, outline_translation: str) -> Tuple[str, str]: """ Resolves a single translation from a set of platform-specific translations. """ - platform_translations = _parse_outline_translation(outline_translation) + platform_translations: dict[str, str] = ( + _parse_outline_translation(outline_translation) + ) + translation: str try: translation = platform_translations[platform] except KeyError: @@ -43,16 +51,21 @@ def resolve(platform: str, outline_translation: str) -> Tuple[str, str]: f"No translation provided for platform: {platform}" ) from exc + combo_translation: Optional[Match[str]] if combo_translation := re.match(_COMBO_TYPE, translation): return (COMBO, combo_translation.group(1)) + command_translation: Optional[Match[str]] if command_translation := re.match(_COMMAND_TYPE, translation): return (COMMAND, command_translation.group(1)) return (_TEXT, translation) def _parse_outline_translation(outline_translation: str) -> dict[str, str]: - it = iter(re.split(_ARGUMENT_DIVIDER, outline_translation)) + it: Iterator[Union[str, Any]] = iter( + re.split(_ARGUMENT_DIVIDER, outline_translation) + ) + return { platform.upper(): translation for platform, translation diff --git a/test/config/test_config.py b/test/config/test_config.py index 79b54ca..4da9460 100644 --- a/test/config/test_config.py +++ b/test/config/test_config.py @@ -61,6 +61,7 @@ def test_loading_valid_config(valid_platform_translations_config_path): config_platform_translations = ( config.load(valid_platform_translations_config_path) ) + assert ( config_platform_translations[ "WINDOWS:Hello:MAC:Hi:LINUX:Good day:OTHER:Whassup" @@ -74,8 +75,8 @@ def test_saving_config(valid_platform_translations_config_path): valid_platform_translations_config_path, platform_translations ) - config_platform_translations = ( config.load(valid_platform_translations_config_path) ) + assert config_platform_translations == platform_translations