Debian Codegraph @

ansible_collections/community/general/plugins/callback/say.py

raw
1 # Copyright (c) 2012, Michael DeHaan, <[email protected]>
2 # Copyright (c) 2017 Ansible Project
3 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 # Make coding more python3-ish
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 author: Unknown (!UNKNOWN)
11 name: say
12 type: notification
13 requirements:
14 - whitelisting in configuration
15 - the C(/usr/bin/say) command line program (standard on macOS) or C(espeak) command line program
16 short_description: Notify using software speech synthesizer
17 description:
18 - This plugin uses C(say) or C(espeak) to "speak" about play events.
19 """
20
21 import os
22 import platform
23 import subprocess
24
25 from ansible.module_utils.common.process import get_bin_path
26 from ansible.plugins.callback import CallbackBase
27
28
29 class CallbackModule(CallbackBase):
30 """
31 makes Ansible much more exciting.
32 """
33
34 CALLBACK_VERSION = 2.0
35 CALLBACK_TYPE = "notification"
36 CALLBACK_NAME = "community.general.say"
37 CALLBACK_NEEDS_WHITELIST = True
38
39 def __init__(self):
40 super().__init__()
41
42 self.FAILED_VOICE = None
43 self.REGULAR_VOICE = None
44 self.HAPPY_VOICE = None
45 self.LASER_VOICE = None
46
47 try:
48 self.synthesizer = get_bin_path("say")
49 if platform.system() != "Darwin":
50 # 'say' binary available, it might be GNUstep tool which doesn't support 'voice' parameter
51 self._display.warning(
52 f"'say' executable found but system is '{platform.system()}': ignoring voice parameter"
53 )
54 else:
55 self.FAILED_VOICE = "Zarvox"
56 self.REGULAR_VOICE = "Trinoids"
57 self.HAPPY_VOICE = "Cellos"
58 self.LASER_VOICE = "Princess"
59 except ValueError:
60 try:
61 self.synthesizer = get_bin_path("espeak")
62 self.FAILED_VOICE = "klatt"
63 self.HAPPY_VOICE = "f5"
64 self.LASER_VOICE = "whisper"
65 except ValueError:
66 self.synthesizer = None
67
68 # plugin disable itself if say is not present
69 # ansible will not call any callback if disabled is set to True
70 if not self.synthesizer:
71 self.disabled = True
72 self._display.warning(
73 f"Unable to find either 'say' or 'espeak' executable, plugin {os.path.basename(__file__)} disabled"
74 )
75
76 def say(self, msg, voice):
77 cmd = [self.synthesizer, msg]
78 if voice:
79 cmd.extend(("-v", voice))
80 subprocess.call(cmd)
81
82 def runner_on_failed(self, host, res, ignore_errors=False):
83 self.say(f"Failure on host {host}", self.FAILED_VOICE)
84
85 def runner_on_ok(self, host, res):
86 self.say("pew", self.LASER_VOICE)
87
88 def runner_on_skipped(self, host, item=None):
89 self.say("pew", self.LASER_VOICE)
90
91 def runner_on_unreachable(self, host, res):
92 self.say(f"Failure on host {host}", self.FAILED_VOICE)
93
94 def runner_on_async_ok(self, host, res, jid):
95 self.say("pew", self.LASER_VOICE)
96
97 def runner_on_async_failed(self, host, res, jid):
98 self.say(f"Failure on host {host}", self.FAILED_VOICE)
99
100 def playbook_on_start(self):
101 self.say("Running Playbook", self.REGULAR_VOICE)
102
103 def playbook_on_notify(self, host, handler):
104 self.say("pew", self.LASER_VOICE)
105
106 def playbook_on_task_start(self, name, is_conditional):
107 if not is_conditional:
108 self.say(f"Starting task: {name}", self.REGULAR_VOICE)
109 else:
110 self.say(f"Notifying task: {name}", self.REGULAR_VOICE)
111
112 def playbook_on_setup(self):
113 self.say("Gathering facts", self.REGULAR_VOICE)
114
115 def playbook_on_play_start(self, name):
116 self.say(f"Starting play: {name}", self.HAPPY_VOICE)
117
118 def playbook_on_stats(self, stats):
119 self.say("Play complete", self.HAPPY_VOICE)