From 1770f5c45077f157e212ab86ef311b1ffea421d1 Mon Sep 17 00:00:00 2001 From: JGStew Date: Mon, 1 Jul 2024 17:49:32 -0400 Subject: [PATCH] add text to speech autopkg processor and test recipe --- SharedProcessors/TextToSpeech.py | 67 ++++++++++++++++++++++ Test-Recipes/TextToSpeech.test.recipe.yaml | 11 ++++ 2 files changed, 78 insertions(+) create mode 100644 SharedProcessors/TextToSpeech.py create mode 100644 Test-Recipes/TextToSpeech.test.recipe.yaml diff --git a/SharedProcessors/TextToSpeech.py b/SharedProcessors/TextToSpeech.py new file mode 100644 index 0000000..d53bc4c --- /dev/null +++ b/SharedProcessors/TextToSpeech.py @@ -0,0 +1,67 @@ +#!/usr/local/autopkg/python +# +# James Stewart @JGStew - 2024 +# +# Related: +# - https://github.com/jgstew/bigfix_prefetch/blob/master/prefetch_from_dictionary.py +# +"""See docstring for TextToSpeech class""" + +import os +import platform + +from autopkglib import ( # pylint: disable=import-error,wrong-import-position,unused-import + Processor, + ProcessorError, +) + +__all__ = ["TextToSpeech"] + + +class TextToSpeech(Processor): # pylint: disable=invalid-name + """checks that assert_string is within input_string""" + + description = __doc__ + input_variables = { + "input_string": {"required": True, "description": "string to say"}, + } + output_variables = {} + __doc__ = description + + def speak(self, input_string): + """speak text aloud""" + tx = input_string + + # from: https://stackoverflow.com/a/59118441/861745 + syst = platform.system() + if syst == "Linux" and platform.linux_distribution()[0] == "Ubuntu": + os.system("spd-say %s" % tx) + elif syst == "Windows": + os.system( + 'PowerShell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak(%s);"' + % tx + ) + elif syst == "Darwin": + # add & at end of command to NOT wait. + os.system("say %s" % tx) + else: + # use pyttsx3 to handle other OSes: + import pyttsx3 + + engine = pyttsx3.init() + engine.say(input_string) + engine.runAndWait() + + def main(self): + """Execution starts here""" + + input_string = str(self.env.get("input_string")) + + self.output(f"say `{input_string}` aloud", 1) + + self.speak(input_string) + + +if __name__ == "__main__": + PROCESSOR = TextToSpeech() + PROCESSOR.execute_shell() diff --git a/Test-Recipes/TextToSpeech.test.recipe.yaml b/Test-Recipes/TextToSpeech.test.recipe.yaml new file mode 100644 index 0000000..c77f3e1 --- /dev/null +++ b/Test-Recipes/TextToSpeech.test.recipe.yaml @@ -0,0 +1,11 @@ +--- +Description: Test TextToSpeech Processor +Identifier: com.github.jgstew.test.TextToSpeech +Input: + NAME: TextToSpeechTest +MinimumVersion: "2.3" +Process: + - Processor: com.github.jgstew.SharedProcessors/TextToSpeech + Arguments: + input_string: "Hello World from AutoPkg!" + # so far tested on macos only, but should support windows and ubuntu as well with no deps or any os with pyttsx3