Compare commits

..

No commits in common. "4c42320c7c48c4f7d574e1e6779b03c7a86ad1a0" and "9c830e4f6c85121a1cbc15138e354ad73ae4d8d8" have entirely different histories.

5 changed files with 24 additions and 43 deletions

View file

@ -131,6 +131,7 @@ class MainProcess:
if e.fatal:
self._unload_plugin(e.source)
except Exception as e:
self._plugin_error
logger.critical(f'Failure when processing {plugin_name} ({e}) - disabling...')
logger.debug(format_exception(e))
self._unload_plugin(plugin_name)
@ -238,7 +239,10 @@ class MainProcess:
if plugin_module is None:
logger.error(f'Unknown plugin: {node.name}')
else:
await plugin_module._kdl_call(node, global_ctx)
res = await plugin_module._call(node.sub, node.tag, *node.args, **node.props, _ctx=global_ctx, _children=node.nodes)
if node.alias:
global_ctx[node.alias] = res
async def user_shutdown(self):
for process_name, process in list(reversed(self.chat_processes.items())):

View file

@ -9,8 +9,6 @@ import kdl
import quart
from ovtk_audiencekit.core.Config import kdl_parse_config, compute_dynamic
from ovtk_audiencekit.utils import format_exception
class PluginError(Exception):
@ -32,7 +30,6 @@ class OvtkBlueprint(quart.Blueprint):
class PluginBase(ABC):
plugins = {}
hooks = {} # the hookerrrrrrrr
def __init__(self, chat_processes, event_queue, name, global_ctx, _children=None, **kwargs):
super().__init__(**kwargs)
@ -59,41 +56,24 @@ class PluginBase(ABC):
def __del__(self):
if self.plugins.get(self._name) == self:
del self.plugins[self._name]
if self._name in self.hooks:
del self.hooks[self._name]
async def _kdl_call(self, node, _ctx):
args, props = compute_dynamic(node, _ctx=_ctx)
subroutine = node.sub
if subroutine:
func = self
for accessor in subroutine:
func = getattr(func, accessor)
else:
func = self.run
for hook in self.hooks.values():
try:
res = hook(self._name, node, _ctx)
if asyncio.iscoroutinefunction(hook):
await res
except Exception as e:
self.logger.warning(f'Failed to run plugin hook: {e}')
self.logger.debug(format_exception(e))
async def _call(self, subroutine, tag, *args, **kwargs):
try:
result = func(*args, _children=node.nodes, _ctx=_ctx, **props)
if subroutine:
func = self
for accessor in subroutine:
func = getattr(func, accessor)
else:
func = self.run
res = func(*args, **kwargs)
if asyncio.iscoroutinefunction(func):
result = await result
res = await res
return res
except Exception as e:
if isinstance(e, KeyboardInterrupt):
raise e
raise PluginError(self._name, str(e)) from e
if node.alias:
_ctx[node.alias] = result
async def _tick(self, *args, **kwargs):
try:
res = self.tick(*args, **kwargs)
@ -118,7 +98,6 @@ class PluginBase(ABC):
raise e
raise PluginError(self._name, str(e)) from e
# Base class helpers
def broadcast(self, event):
"""Send event to every active chat"""
@ -127,10 +106,7 @@ class PluginBase(ABC):
continue
proc.control_pipe.send(event)
def register_hook(self, hook):
self.hooks[self._name] = hook
async def execute_kdl(self, nodes, _ctx={}):
async def execute_kdl(self, nodes, *py_args, _ctx={}, **py_props):
"""
Run other plugins as configured by the passed KDL nodes collection
If this was done in response to an event, pass it as 'event' in _ctx!
@ -138,14 +114,16 @@ class PluginBase(ABC):
_ctx = copy.deepcopy({**self._global_ctx, **_ctx})
for node in nodes:
try:
args, props = compute_dynamic(node, _ctx=_ctx)
target = self.plugins.get(node.name)
if target is None:
self.logger.warning(f'Could not find plugin or builtin with name {node.name}')
break
await target._kdl_call(node, _ctx)
result = await target._call(node.sub, node.tag, *args, *py_args, **props, _ctx=_ctx, **py_props, _children=node.nodes)
if node.alias:
_ctx[node.alias] = result
except Exception as e:
self.logger.warning(f'Failed to execute defered KDL: {e}')
self.logger.debug(format_exception(e))
break
@ -156,7 +134,6 @@ class PluginBase(ABC):
"""
self._event_queue.put_nowait(event)
# User-defined
async def setup(self, *args, **kwargs):
"""Called when plugin is being loaded."""

View file

@ -87,7 +87,7 @@ class JailPlugin(PluginBase):
if isinstance(event, Message):
if self.jail_command.invoked(event):
try:
args, _ = self.jail_command.parse(event.text)
args = self.jail_command.parse(event.text)
end_date = maya.when(args['length'], prefer_dates_from='future')
deets = self.chats[event.via].shared.api.get_user_details(args['username'])
if deets is None:
@ -117,7 +117,7 @@ class JailPlugin(PluginBase):
self.send_to_bus(weewoo)
elif self.unjail_command.invoked(event):
try:
args, _ = self.jail_command.parse(event.text)
args = self.jail_command.parse(event.text)
deets = self.chats[event.via].shared.api.get_user_details(args['username'])
if deets is None:
raise ValueError()

View file

@ -47,7 +47,7 @@ class ShoutoutPlugin(PluginBase):
if isinstance(event, Message):
if self.command and self.command.invoked(event):
try:
args, _ = self.command.parse(event.text)
args = self.command.parse(event.text)
except ArgumentError as e:
msg = SysMessage(self._name, str(e), replies_to=event)
self.chats[event.via].send(msg)

View file

@ -139,7 +139,7 @@ class CommandPlugin(PluginBase):
if self.help_cmd.invoked(event):
try:
args, _ = self.help_cmd.parse(event.text)
args = self.help_cmd.parse(event.text)
except argparse.ArgumentError as e:
msg = SysMessage(self._name, f"{e}. See !help {self.help_cmd.name}", replies_to=event)
self.chats[event.via].send(msg)