-
Notifications
You must be signed in to change notification settings - Fork 86
Add initial Python module helpers #1104
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
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # | ||
| # __init__.py - Python output module helpers package | ||
| # | ||
| # Copyright (C) 2026 Jean-François David <jeanfrancoismanutea@gmail.com> | ||
| # All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions | ||
| # are met: | ||
| # 1. Redistributions of source code must retain the above copyright | ||
| # notice, this list of conditions and the following disclaimer. | ||
| # 2. Redistributions in binary form must reproduce the above copyright | ||
| # notice, this list of conditions and the following disclaimer in the | ||
| # documentation and/or other materials provided with the distribution. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND | ||
| # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| # SUCH DAMAGE. | ||
| # | ||
|
|
||
|
|
||
| """Helpers for Python Speech Dispatcher output modules.""" |
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,77 @@ | ||
| # | ||
| # module_utils.py - Utilities for Python output modules | ||
| # | ||
| # Copyright (C) 2026 Jean-François David <jeanfrancoismanutea@gmail.com> | ||
| # All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions | ||
| # are met: | ||
| # 1. Redistributions of source code must retain the above copyright | ||
| # notice, this list of conditions and the following disclaimer. | ||
| # 2. Redistributions in binary form must reproduce the above copyright | ||
| # notice, this list of conditions and the following disclaimer in the | ||
| # documentation and/or other materials provided with the distribution. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND | ||
| # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| # SUCH DAMAGE. | ||
| # | ||
|
|
||
|
|
||
| def module_strip_ssml(message: str) -> str: | ||
|
sthibaul marked this conversation as resolved.
|
||
| out = [] | ||
| append = out.append | ||
| omit = False | ||
| i = 0 | ||
| length = len(message) | ||
|
|
||
| while i < length: | ||
| char = message[i] | ||
|
|
||
| if char == "<": | ||
| omit = True | ||
| i += 1 | ||
| continue | ||
| if char == ">": | ||
| omit = False | ||
| i += 1 | ||
| continue | ||
| if omit: | ||
| i += 1 | ||
| continue | ||
|
|
||
| if char == "&": | ||
| if message.startswith("<", i): | ||
| append("<") | ||
| i += 4 | ||
| continue | ||
| if message.startswith(">", i): | ||
| append(">") | ||
| i += 4 | ||
| continue | ||
| if message.startswith("&", i): | ||
| append("&") | ||
| i += 5 | ||
| continue | ||
| if message.startswith(""", i): | ||
| append('"') | ||
| i += 6 | ||
| continue | ||
| if message.startswith("'", i): | ||
| append("'") | ||
| i += 6 | ||
| continue | ||
|
|
||
| append(char) | ||
| i += 1 | ||
|
|
||
| return "".join(out) | ||
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,53 @@ | ||
| # | ||
| # speechd_types.py - Types for Python output modules | ||
| # | ||
| # Copyright (C) 2026 Jean-François David <jeanfrancoismanutea@gmail.com> | ||
| # All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without | ||
| # modification, are permitted provided that the following conditions | ||
| # are met: | ||
| # 1. Redistributions of source code must retain the above copyright | ||
| # notice, this list of conditions and the following disclaimer. | ||
| # 2. Redistributions in binary form must reproduce the above copyright | ||
| # notice, this list of conditions and the following disclaimer in the | ||
| # documentation and/or other materials provided with the distribution. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND | ||
| # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
| # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| # SUCH DAMAGE. | ||
| # | ||
|
|
||
|
|
||
| SPD_MSGTYPE_TEXT = 0 | ||
| SPD_MSGTYPE_SOUND_ICON = 1 | ||
| SPD_MSGTYPE_CHAR = 2 | ||
| SPD_MSGTYPE_KEY = 3 | ||
| SPD_MSGTYPE_SPELL = 99 | ||
|
|
||
| SPD_AUDIO_LE = 0 | ||
| SPD_AUDIO_BE = 1 | ||
|
|
||
|
|
||
| class SPDVoice: | ||
| def __init__(self, name=None, language=None, variant=None): | ||
| self.name = name | ||
| self.language = language | ||
| self.variant = variant | ||
|
|
||
|
|
||
| class AudioTrack: | ||
| def __init__(self, bits, num_channels, sample_rate, num_samples, samples): | ||
| self.bits = bits | ||
| self.num_channels = num_channels | ||
| self.sample_rate = sample_rate | ||
| self.num_samples = num_samples | ||
| self.samples = samples |
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 |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| /Makefile.in | ||
| /TAGS | ||
| /atconfig | ||
| /atlocal | ||
| /clibrary | ||
| /clibrary2 | ||
| /clibrary3 | ||
|
|
||
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,2 @@ | ||
| TEST_PYTHON_ENABLED='@TEST_PYTHON_ENABLED@' | ||
| PYTHON='@PYTHON@' |
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,138 @@ | ||
| # python_module.at - python module tests | ||
| # | ||
| # Copyright (C) 2026 Jean-François David | ||
| # | ||
| # This is free software; you can redistribute it and/or modify it | ||
| # under the terms of the GNU General Public License as published by | ||
| # the Free Software Foundation; either version 2, or (at your option) | ||
| # any later version. | ||
| # | ||
| # This software is distributed in the hope that it will be useful, | ||
| # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| # General Public License for more details. | ||
| # | ||
| # You should have received a copy of the GNU General Public License | ||
| # along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
|
||
| AT_BANNER([Python module]) | ||
|
|
||
| AT_SETUP([Python module imports]) | ||
| AT_KEYWORDS([python import]) | ||
|
|
||
| AT_SKIP_IF([test "x$TEST_PYTHON_ENABLED" != xyes]) | ||
|
|
||
| AT_CHECK([[test_pythonpath=${TEST_PYTHONPATH-"$abs_top_srcdir/src/modules"} | ||
| PYTHONDONTWRITEBYTECODE=1 \ | ||
| PYTHONNOUSERSITE=1 \ | ||
| PYTHONPATH="$test_pythonpath" \ | ||
| "$PYTHON" - "$test_pythonpath" <<'PY' | ||
| import os | ||
| import sys | ||
|
|
||
| from speechd_python_modules import module_utils, speechd_types | ||
|
|
||
| pythonpath_dir = os.path.realpath(sys.argv[1]) | ||
| module_dir = os.path.join(pythonpath_dir, "speechd_python_modules") | ||
| modules = [module_utils, speechd_types] | ||
|
|
||
|
|
||
| def check_module_path(module): | ||
| module_file = os.path.realpath(module.__file__) | ||
| if os.path.dirname(module_file) != module_dir: | ||
| raise SystemExit( | ||
| "%s imported from %r, expected %r" | ||
| % (module.__name__, module_file, module_dir) | ||
| ) | ||
|
|
||
|
|
||
| for module in modules: | ||
| check_module_path(module) | ||
| PY | ||
| ]], [0], [], []) | ||
|
|
||
| AT_CLEANUP | ||
|
|
||
| AT_SETUP([module_strip_ssml]) | ||
| AT_KEYWORDS([python module_utils ssml]) | ||
|
|
||
| AT_SKIP_IF([test "x$TEST_PYTHON_ENABLED" != xyes]) | ||
|
|
||
| AT_CHECK([[test_pythonpath=${TEST_PYTHONPATH-"$abs_top_srcdir/src/modules"} | ||
| PYTHONDONTWRITEBYTECODE=1 \ | ||
| PYTHONNOUSERSITE=1 \ | ||
| PYTHONPATH="$test_pythonpath" \ | ||
| "$PYTHON" - <<'PY' | ||
| from speechd_python_modules import module_utils | ||
|
|
||
| strip_ssml = module_utils.module_strip_ssml | ||
|
|
||
|
|
||
| def check(message, expected): | ||
| actual = strip_ssml(message) | ||
| if actual != expected: | ||
| raise SystemExit( | ||
| "strip_ssml(%r) returned %r, expected %r" | ||
| % (message, actual, expected) | ||
| ) | ||
|
|
||
|
|
||
| check("Plain text.", "Plain text.") | ||
| check("Hello <emphasis>world</emphasis>.", "Hello world.") | ||
| check('<speak>Hello <break time="500ms"/>world</speak>', "Hello world") | ||
| check('<tag attr="&">text</tag>', "text") | ||
| check( | ||
| "Use <tag>, &, "quotes" and 'apostrophes'.", | ||
| "Use <tag>, &, \"quotes\" and 'apostrophes'.", | ||
| ) | ||
| check("Keep <unfinished", "Keep ") | ||
| check("Unknown © entity stays.", "Unknown © entity stays.") | ||
| PY | ||
| ]], [0], [], []) | ||
|
|
||
| AT_CLEANUP | ||
|
|
||
| AT_SETUP([Python module types]) | ||
| AT_KEYWORDS([python speechd_types]) | ||
|
|
||
| AT_SKIP_IF([test "x$TEST_PYTHON_ENABLED" != xyes]) | ||
|
|
||
| AT_CHECK([[test_pythonpath=${TEST_PYTHONPATH-"$abs_top_srcdir/src/modules"} | ||
| PYTHONDONTWRITEBYTECODE=1 \ | ||
| PYTHONNOUSERSITE=1 \ | ||
| PYTHONPATH="$test_pythonpath" \ | ||
| "$PYTHON" - <<'PY' | ||
| from speechd_python_modules import speechd_types | ||
|
|
||
| if speechd_types.SPD_MSGTYPE_TEXT != 0: | ||
| raise SystemExit("unexpected SPD_MSGTYPE_TEXT value") | ||
| if speechd_types.SPD_MSGTYPE_SPELL != 99: | ||
| raise SystemExit("unexpected SPD_MSGTYPE_SPELL value") | ||
| if speechd_types.SPD_AUDIO_LE != 0: | ||
| raise SystemExit("unexpected SPD_AUDIO_LE value") | ||
| if speechd_types.SPD_AUDIO_BE != 1: | ||
| raise SystemExit("unexpected SPD_AUDIO_BE value") | ||
|
|
||
| voice = speechd_types.SPDVoice(name="name", language="en", variant="male") | ||
| if (voice.name, voice.language, voice.variant) != ("name", "en", "male"): | ||
| raise SystemExit("unexpected SPDVoice fields") | ||
|
|
||
| track = speechd_types.AudioTrack( | ||
| bits=16, | ||
| num_channels=1, | ||
| sample_rate=22050, | ||
| num_samples=2, | ||
| samples=b"\0\0\1\0", | ||
| ) | ||
| if ( | ||
| track.bits, | ||
| track.num_channels, | ||
| track.sample_rate, | ||
| track.num_samples, | ||
| track.samples, | ||
| ) != (16, 1, 22050, 2, b"\0\0\1\0"): | ||
| raise SystemExit("unexpected AudioTrack fields") | ||
| PY | ||
| ]], [0], [], []) | ||
|
|
||
| AT_CLEANUP |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.