2018-10-10 17:25:03 +00:00
|
|
|
from enum import Enum
|
2019-02-16 18:40:10 +00:00
|
|
|
from functools import wraps
|
2018-10-10 17:25:03 +00:00
|
|
|
|
|
|
|
|
2018-03-15 19:04:58 +00:00
|
|
|
cmds = {}
|
|
|
|
lstnrs = {}
|
2017-12-06 18:45:34 +00:00
|
|
|
|
2017-09-27 21:55:00 +00:00
|
|
|
|
2018-10-10 17:25:03 +00:00
|
|
|
class OutputType(Enum):
|
|
|
|
Message = 'message'
|
|
|
|
Action = 'action'
|
|
|
|
|
|
|
|
|
2017-09-27 21:55:00 +00:00
|
|
|
class Output:
|
|
|
|
def __init__(self, msg_type, msg):
|
|
|
|
self.msg_type = msg_type
|
2017-11-27 05:10:40 +00:00
|
|
|
self.msg = self.sanitize(msg)
|
|
|
|
|
|
|
|
def sanitize(self, msg):
|
2017-12-06 18:45:34 +00:00
|
|
|
try:
|
|
|
|
return msg.splitlines()
|
|
|
|
except AttributeError:
|
|
|
|
return msg
|
2017-09-27 21:55:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def action(msg):
|
2018-10-10 17:25:03 +00:00
|
|
|
return Output(OutputType.Action, msg)
|
2017-09-27 21:55:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def message(msg):
|
2018-10-10 17:25:03 +00:00
|
|
|
return Output(OutputType.Message, msg)
|
2017-09-27 21:55:00 +00:00
|
|
|
|
|
|
|
|
2018-11-02 20:53:04 +00:00
|
|
|
def _add_plugin(command, help_text, func):
|
2019-02-16 18:40:10 +00:00
|
|
|
if command not in cmds:
|
|
|
|
cmds[command] = {}
|
|
|
|
cmds[command].update({
|
2018-11-02 20:53:04 +00:00
|
|
|
'run': func,
|
|
|
|
'help': help_text
|
2019-02-16 18:40:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
def _ops_plugin(command, ops_msg, func):
|
|
|
|
if command not in cmds:
|
|
|
|
cmds[command] = {}
|
|
|
|
cmds[command].update({
|
|
|
|
'ops': True,
|
|
|
|
'ops_msg': ops_msg,
|
|
|
|
})
|
2018-03-15 19:04:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _add_listener(name, func):
|
|
|
|
lstnrs[name] = func
|
2017-09-27 21:55:00 +00:00
|
|
|
|
2017-09-27 23:54:08 +00:00
|
|
|
|
2017-11-30 11:12:33 +00:00
|
|
|
def clear_plugins():
|
|
|
|
cmds.clear()
|
2017-12-06 19:05:57 +00:00
|
|
|
lstnrs.clear()
|
2017-11-30 11:12:33 +00:00
|
|
|
|
|
|
|
|
2018-11-02 20:53:04 +00:00
|
|
|
def register(command, help_text=None):
|
2019-02-16 18:40:10 +00:00
|
|
|
@wraps(command)
|
2017-09-27 23:54:08 +00:00
|
|
|
def register_for_command(func):
|
2018-11-02 20:53:04 +00:00
|
|
|
_add_plugin(command, help_text, func)
|
2017-09-27 23:54:08 +00:00
|
|
|
return func
|
|
|
|
return register_for_command
|
|
|
|
|
2017-12-06 18:45:34 +00:00
|
|
|
|
|
|
|
def listener(name):
|
|
|
|
def register_as_listener(func):
|
2018-03-15 19:04:58 +00:00
|
|
|
_add_listener(name, func)
|
2017-12-06 18:45:34 +00:00
|
|
|
return func
|
|
|
|
return register_as_listener
|
2019-02-16 18:40:10 +00:00
|
|
|
|
|
|
|
def ops(command, msg=None):
|
|
|
|
@wraps(command)
|
|
|
|
def register_ops_command(func):
|
|
|
|
_ops_plugin(command, msg, func)
|
|
|
|
return func
|
|
|
|
return register_ops_command
|