add full support for command-based plugins (#27)

pull/74/head
Mallory Hancock 2019-09-18 09:28:22 -07:00
parent 1aa17eebfb
commit 9797f731a7
1 changed files with 38 additions and 18 deletions

View File

@ -1,7 +1,7 @@
from enum import Enum
from functools import wraps
plugins = {}
cmds = {}
lstnrs = {}
@ -22,15 +22,43 @@ class Output:
except AttributeError:
return msg
class _BasePlugin:
enabled = True
class Command:
def enable(self):
self.enabled = True
def disable(self):
self.enabled = False
class Listener(_BasePlugin):
def __init__(self, name, run=None):
self.name = name
if run:
self.run = run
def __str__(self):
return self.name
def run(self):
pass
def add_listener(self):
lstnrs[self.name] = self
plugins[self.name] = self
class Command(_BasePlugin):
def __init__(self, cmd, **kwargs):
self.cmd = cmd
self.help_txt = kwargs.get('help_txt', '')
self.help_text = kwargs.get('help_text', 'N/A')
self.ops = kwargs.get('ops', False)
self.ops_msg = kwargs.get('ops_msg', '')
self.enabled = True
self.run = kwargs.get('run', self.run)
def __str__(self):
return self.cmd
def run(self, msg):
pass
@ -40,20 +68,12 @@ class Command:
self.ops_msg = ops_msg
def update_plugin(self, **kwargs):
self.help_text = kwargs.get('help_text')
self.help_text = kwargs.get('help_text', 'N/A')
self.run = kwargs.get('run', self.run)
def add_command(self):
cmds[self.cmd] = self
def enable(self):
self.enabled = True
def disable(self):
self.enabled = False
def __str__(self):
return self.cmd
plugins[self.cmd] = self
def action(msg):
@ -66,7 +86,7 @@ def message(msg):
def _add_command(command, help_text, func):
if command not in cmds:
cmds[command] = Command(command, help_txt=help_text, run=func)
Command(command, help_text=help_text, run=func).add_command()
else:
cmds[command].update_plugin(help_text=help_text, run=func)
print(cmds)
@ -74,13 +94,13 @@ def _add_command(command, help_text, func):
def _ops_plugin(command, ops_msg, func):
if command not in cmds:
cmds[command] = Command(command, ops=True, ops_msg=ops_msg)
Command(command, ops=True, ops_msg=ops_msg).add_command()
else:
cmds[command].enable_ops(ops_msg)
def _add_listener(name, func):
lstnrs[name] = func
Listener(name, run=func).add_listener()
def clear_plugins():
@ -88,7 +108,7 @@ def clear_plugins():
lstnrs.clear()
def register(command, help_text=None):
def register(command, help_text='N/A'):
@wraps(command)
def register_for_command(func):
_add_command(command, help_text, func)