Skip to content

Commit

Permalink
Add more typing
Browse files Browse the repository at this point in the history
  • Loading branch information
paulfioravanti committed Aug 28, 2024
1 parent 72013d9 commit db18927
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 13 deletions.
14 changes: 10 additions & 4 deletions plover_platform_specific_translation/config/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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:
Expand All @@ -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()
Expand All @@ -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)
3 changes: 2 additions & 1 deletion plover_platform_specific_translation/config/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
"""
Expand Down
2 changes: 1 addition & 1 deletion plover_platform_specific_translation/platform/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import platform


_PLATFORM_MAPPINGS = {
_PLATFORM_MAPPINGS: dict[str, str] = {
"Windows": "WINDOWS",
"Darwin": "MAC",
"Linux": "LINUX"
Expand Down
25 changes: 19 additions & 6 deletions plover_platform_specific_translation/translation/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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:
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion test/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

0 comments on commit db18927

Please sign in to comment.