Fixes re: zognia's testing #2

Merged
skeh merged 11 commits from feat/zogpog into main 2025-01-24 08:28:14 +00:00
9 changed files with 61 additions and 36 deletions
Showing only changes of commit 82691789f7 - Show all commits

View file

@ -219,9 +219,9 @@ class MainProcess:
plugin_module = import_or_reload_mod(module_name,
default_package='ovtk_audiencekit.plugins',
external=False)
plugin = plugin_module.Plugin(self.chat_processes, self.event_queue, plugin_name, global_ctx,
**node.props, **secrets_for_mod, _children=node.nodes)
plugin = plugin_module.Plugin(self.chat_processes, self.event_queue, plugin_name, global_ctx)
self.plugins[plugin_name] = plugin
await plugin._setup(*node.args[1:], **node.props, **secrets_for_mod)
# Register UI with webserver
self.webserver.register_blueprint(plugin.blueprint)
except Exception as e:

View file

@ -86,6 +86,18 @@ class PluginBase(ABC):
raise e
raise PluginError(self._name, str(e)) from e
async def _setup(self, *args, **kwargs):
try:
res = self.setup(*args, **kwargs)
if asyncio.iscoroutinefunction(self.setup):
return await res
else:
return res
except Exception as e:
if isinstance(e, KeyboardInterrupt):
raise e
raise PluginError(self._name, str(e)) from e
# Base class helpers
def broadcast(self, event):
"""Send event to every active chat"""
@ -123,6 +135,10 @@ class PluginBase(ABC):
self._event_queue.put_nowait(event)
# User-defined
async def setup(self, *args, **kwargs):
"""Called when plugin is being loaded."""
pass
def close(self):
"""Called when plugin is about to be unloaded. Use this to safely close any resouces if needed"""
pass
@ -143,7 +159,7 @@ class PluginBase(ABC):
pass
@abstractmethod
async def run(self, _children=None, _ctx={}, **kwargs):
async def run(self, *args, _children=None, _ctx={}, **kwargs):
"""
Run plugin action, either due to a definition in the config, or due to another plugin
"""

View file

@ -1,11 +1,11 @@
import asyncio
from collections import deque
from ovtk_audiencekit.plugins import PluginBase
from ovtk_audiencekit.core import Clip
class AudioAlert(PluginBase):
def __init__(self, *args, output=None, buffer_length=2048, cutoff_prevention_buffers=None, **kwargs):
super().__init__(*args, **kwargs)
def setup(self, output=None, buffer_length=2048, cutoff_prevention_buffers=None):
if cutoff_prevention_buffers:
self.logger.info('`cutoff_prevention_buffers` are depricated')
@ -13,13 +13,33 @@ class AudioAlert(PluginBase):
self._buffer_length = int(buffer_length)
self._output_index = Clip.find_output_index(output)
def run(self, path, speed=1, immediate=True, **kwargs):
if self.sounds.get(path) is None:
self.sounds[path] = Clip(path,
self._output_index,
buffer_length=self._buffer_length,
speed=speed)
sound = self.sounds.get(path)
def run(self, path, speed=1, immediate=True, poly=1, **kwargs):
sound = None
if poly != 1:
poly = int(poly)
sound_dq = self.sounds.get(path)
if sound_dq is None or type(sound_dq) != deque or sound_dq.maxlen != poly:
sound_dq = deque(maxlen=poly)
self.sounds[path] = sound_dq
if len(sound_dq) != poly:
self.logger.debug("filling", len(sound_dq), poly, sound_dq)
sound = Clip(path,
self._output_index,
buffer_length=self._buffer_length,
speed=speed)
sound_dq.append(sound)
else:
self.logger.debug("rotate", len(sound_dq), poly, sound_dq)
sound_dq.rotate(1)
sound = sound_dq[0]
else:
if self.sounds.get(path) is None:
self.sounds[path] = Clip(path,
self._output_index,
buffer_length=self._buffer_length,
speed=speed)
sound = self.sounds.get(path)
if immediate:
asyncio.create_task(sound.aplay())

View file

@ -35,8 +35,7 @@ owomap = {
}
class JailPlugin(PluginBase):
def __init__(self, *args, min_level='vip', persist=True, **kwargs):
super().__init__(*args, **kwargs)
def setup(self, min_level='vip', persist=True):
self.persist = persist
self._cache = os.path.join(CACHE_DIR, 'Jail', 'sentences')
os.makedirs(os.path.dirname(self._cache), exist_ok=True)

View file

@ -6,19 +6,15 @@ from ovtk_audiencekit.plugins import PluginBase
class OBSWSPlugin(PluginBase):
def __init__(self, *args, password=None, uri='ws://localhost:4455', **kwargs):
super().__init__(*args, **kwargs)
async def setup(self, password=None, uri='ws://localhost:4455'):
self.uri = uri
self.obsws = simpleobsws.WebSocketClient(url=uri, password=password)
asyncio.get_event_loop().run_until_complete(self.setup())
async def setup(self):
await self.obsws.connect()
success = await self.obsws.wait_until_identified()
if not success:
await self.obsws.disconnect()
raise RuntimeError(f'Could not connect to OBS websocket at {self.uri}')
await self.obsws.connect()
success = await self.obsws.wait_until_identified()
if not success:
await self.obsws.disconnect()
raise RuntimeError(f'Could not connect to OBS websocket at {self.uri}')
async def run(self, type, _children=None, _ctx={}, **kwargs):
req = simpleobsws.Request(type, requestData=kwargs)

View file

@ -4,8 +4,7 @@ from ovtk_audiencekit.plugins import PluginBase
class OSCPlugin(PluginBase):
def __init__(self, *args, ip='localhost', port=None, **kwargs):
super().__init__(*args, **kwargs)
def setup(self, ip='localhost', port=None):
if port is None:
raise RuntimeError('A unique port must be specified')
self.client = SimpleUDPClient(ip, int(port))

View file

@ -64,8 +64,7 @@ class PhraseCounter:
class PhraseCounterPlugin(PluginBase):
def __init__(self, *args, debounce_time=1, persist=False, **kwargs):
super().__init__(*args, **kwargs)
def setup(self, debounce_time=1, persist=False):
self.debounce_time = debounce_time
self.persist = persist

View file

@ -9,10 +9,8 @@ from ovtk_audiencekit.chats.Twitch import Process as Twitch
class ShoutoutPlugin(PluginBase):
def __init__(self, *args, command='so', min_level='vip',
text='Check out {link}!~ They were last streaming {last_game}',
**kwargs):
super().__init__(*args, **kwargs)
def setup(self, command='so', min_level='vip',
text='Check out {link}!~ They were last streaming {last_game}'):
self.text = text
if command:
self.command = Command(name=command, help='Shoutout another user', required_level=min_level)

View file

@ -13,10 +13,8 @@ from ovtk_audiencekit.core.Data import CACHE_DIR
class TextToSpeechPlugin(PluginBase):
def __init__(self, *args, output=None, cuda=None,
engine="tts_models/en/ljspeech/tacotron2-DDC", speaker_wav=None,
_children=None, **kwargs):
super().__init__(*args, _children=_children)
def setup(self, output=None, cuda=None,
engine="tts_models/en/ljspeech/tacotron2-DDC", speaker_wav=None, **kwargs):
self.speaker_wav = speaker_wav