Repository navigation
Validate the Piper voice catalog #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
steinbro
merged 3 commits into
project-spiel:main
from
paul-geoghegan:fix/piper-voice-catalog
Nov 30, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| #!/usr/bin/env python3 | ||
| """Reject duplicate mapping keys in the Piper voice catalog.""" | ||
|
|
||
| import sys | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| import yaml | ||
| from yaml.nodes import MappingNode | ||
|
|
||
|
|
||
| class DuplicateKeyError(ValueError): | ||
| """Raised when a YAML mapping defines the same key more than once.""" | ||
|
|
||
|
|
||
| class UniqueKeyLoader(yaml.SafeLoader): | ||
| """Safe YAML loader that rejects duplicate mapping keys.""" | ||
|
|
||
|
|
||
| def construct_unique_mapping( | ||
| loader: UniqueKeyLoader, node: MappingNode, deep: bool = False | ||
| ) -> dict[Any, Any]: | ||
| mapping: dict[Any, Any] = {} | ||
| first_key_lines: dict[Any, int] = {} | ||
|
|
||
| for key_node, value_node in node.value: | ||
| key = loader.construct_object(key_node, deep=deep) | ||
| try: | ||
| first_line = first_key_lines.get(key) | ||
| except TypeError as exc: | ||
| raise yaml.constructor.ConstructorError( | ||
| "while constructing a mapping", | ||
| node.start_mark, | ||
| "found an unhashable key", | ||
| key_node.start_mark, | ||
| ) from exc | ||
|
|
||
| if first_line is not None: | ||
| raise DuplicateKeyError( | ||
| f"{key_node.start_mark.name}:{key_node.start_mark.line + 1}: " | ||
| f"duplicate YAML key {key!r} (first defined at line {first_line})" | ||
| ) | ||
|
|
||
| first_key_lines[key] = key_node.start_mark.line + 1 | ||
| mapping[key] = loader.construct_object(value_node, deep=deep) | ||
|
|
||
| return mapping | ||
|
|
||
|
|
||
| UniqueKeyLoader.add_constructor( | ||
| yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, construct_unique_mapping | ||
| ) | ||
|
|
||
|
|
||
| def main() -> int: | ||
| if len(sys.argv) != 2: | ||
| print(f"Usage: {Path(sys.argv[0]).name} <yaml-file>", file=sys.stderr) | ||
| return 2 | ||
|
|
||
| path = Path(sys.argv[1]) | ||
| try: | ||
| with path.open(encoding="utf-8") as yaml_file: | ||
| yaml.load(yaml_file, Loader=UniqueKeyLoader) | ||
| except (OSError, yaml.YAMLError, DuplicateKeyError) as error: | ||
| print(f"Error: {error}", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| #!/usr/bin/env python3 | ||
| """Failure and atomicity tests for the Piper manifest generator.""" | ||
|
|
||
| import shutil | ||
| import subprocess | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| ROOT = Path(__file__).resolve().parents[1] | ||
| PIPER_DIR = ROOT / "speech-provider-piper" | ||
|
|
||
|
|
||
| class GenerateSnapcraftTests(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| self.temp_dir = tempfile.TemporaryDirectory() | ||
| self.fixture = Path(self.temp_dir.name) | ||
| for filename in ( | ||
| "generate_snapcraft.sh", | ||
| "snapcraft.yaml.in", | ||
| "validate_yaml.py", | ||
| ): | ||
| shutil.copy2(PIPER_DIR / filename, self.fixture / filename) | ||
|
|
||
| self.output = self.fixture / "snapcraft.yaml" | ||
| self.output.write_text("existing manifest\n", encoding="utf-8") | ||
| self.expected_output = self.output.read_bytes() | ||
| self.voices = self.fixture / "voices.yaml" | ||
|
|
||
| def tearDown(self) -> None: | ||
| self.temp_dir.cleanup() | ||
|
|
||
| def assert_generation_fails(self, diagnostic: str) -> None: | ||
| result = subprocess.run( | ||
| ["bash", str(self.fixture / "generate_snapcraft.sh")], | ||
| capture_output=True, | ||
| check=False, | ||
| text=True, | ||
| ) | ||
|
|
||
| self.assertNotEqual(result.returncode, 0) | ||
| self.assertIn(diagnostic, result.stdout + result.stderr) | ||
| self.assertEqual(self.expected_output, self.output.read_bytes()) | ||
|
|
||
| def test_missing_input_leaves_existing_manifest_unchanged(self) -> None: | ||
| self.assert_generation_fails("required input is missing or unreadable") | ||
|
|
||
| def test_invalid_yaml_leaves_existing_manifest_unchanged(self) -> None: | ||
| self.voices.write_text("en-US: [\n", encoding="utf-8") | ||
| self.assert_generation_fails("duplicate or invalid YAML in") | ||
|
|
||
| def test_duplicate_locale_leaves_existing_manifest_unchanged(self) -> None: | ||
| self.voices.write_text( | ||
| "en-US:\n test_phrase: Hello\n voices: [amy-medium]\n" | ||
| "en-US:\n test_phrase: Hello\n voices: [ryan-medium]\n", | ||
| encoding="utf-8", | ||
| ) | ||
| self.assert_generation_fails( | ||
| "duplicate YAML key 'en-US' (first defined at line 1)" | ||
| ) | ||
|
|
||
| def test_duplicate_mapping_key_leaves_existing_manifest_unchanged(self) -> None: | ||
| self.voices.write_text( | ||
| "en-US:\n test_phrase: Hello\n voices: [amy-medium]\n" | ||
| " voices: [ryan-medium]\n", | ||
| encoding="utf-8", | ||
| ) | ||
| self.assert_generation_fails( | ||
| "duplicate YAML key 'voices' (first defined at line 3)" | ||
| ) | ||
|
|
||
| def test_empty_voice_list_leaves_existing_manifest_unchanged(self) -> None: | ||
| self.voices.write_text( | ||
| "en-US: {test_phrase: Hello, voices: []}\n", encoding="utf-8" | ||
| ) | ||
| self.assert_generation_fails( | ||
| "must have a non-empty test_phrase and voices list" | ||
| ) | ||
|
|
||
| def test_invalid_voice_id_leaves_existing_manifest_unchanged(self) -> None: | ||
| self.voices.write_text( | ||
| 'en-US: {test_phrase: Hello, voices: ["amy-medium\\nryan-medium"]}\n', | ||
| encoding="utf-8", | ||
| ) | ||
| self.assert_generation_fails("invalid voice ID for locale en-US") | ||
|
|
||
| def test_invalid_component_name_leaves_existing_manifest_unchanged(self) -> None: | ||
| self.voices.write_text( | ||
| "en-US: {test_phrase: Hello, voices: [test_voice-x_low]}\n", | ||
| encoding="utf-8", | ||
| ) | ||
| self.assert_generation_fails( | ||
| "invalid snap component name for en-US test_voice-x_low" | ||
| ) | ||
|
|
||
| def test_duplicate_component_leaves_existing_manifest_unchanged(self) -> None: | ||
| self.voices.write_text( | ||
| "en-US: {test_phrase: Hello, voices: [amy-medium, amy-medium]}\n", | ||
| encoding="utf-8", | ||
| ) | ||
| self.assert_generation_fails("duplicate voice component") | ||
|
|
||
| def test_invalid_template_leaves_existing_manifest_unchanged(self) -> None: | ||
| self.voices.write_text( | ||
| "en-US: {test_phrase: Hello, voices: [amy-medium]}\n", | ||
| encoding="utf-8", | ||
| ) | ||
| (self.fixture / "snapcraft.yaml.in").write_text( | ||
| "not: valid: yaml: [\n", encoding="utf-8" | ||
| ) | ||
| self.assert_generation_fails( | ||
| "generated manifest must be a single YAML mapping with parts and components" | ||
| ) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can add a workshop action for this. Ideally we'd capture all the build dependencies in there so the user doesn't have to install any other dependencies outside of their environment.