Also use docstrings to check if a command has help available, allow one-string
denotation of multiple event hooks
This commit is contained in:
parent
ebc77fa501
commit
4d41848b57
2 changed files with 29 additions and 16 deletions
|
@ -173,15 +173,16 @@ class Module(object):
|
||||||
command = event["message_split"][0].lower()
|
command = event["message_split"][0].lower()
|
||||||
self.message(event, command)
|
self.message(event, command)
|
||||||
|
|
||||||
|
def _get_help(self, hook):
|
||||||
|
return hook.kwargs.get("help", None) or hook.function.__doc__
|
||||||
|
|
||||||
def help(self, event):
|
def help(self, event):
|
||||||
if event["args"]:
|
if event["args"]:
|
||||||
command = event["args_split"][0].lower()
|
command = event["args_split"][0].lower()
|
||||||
if command in self.events.on("received").on(
|
if command in self.events.on("received").on(
|
||||||
"command").get_children():
|
"command").get_children():
|
||||||
hooks = self.events.on("received.command").on(command).get_hooks()
|
hooks = self.events.on("received.command").on(command).get_hooks()
|
||||||
kwargs = hooks[0].kwargs
|
help = self._get_help(hooks[0])
|
||||||
help = hooks[0].kwargs.get("help", None
|
|
||||||
) or hooks[0].function.__doc__
|
|
||||||
|
|
||||||
if help:
|
if help:
|
||||||
event["stdout"].write("%s: %s" % (command, help.strip()))
|
event["stdout"].write("%s: %s" % (command, help.strip()))
|
||||||
|
@ -193,7 +194,8 @@ class Module(object):
|
||||||
help_available = []
|
help_available = []
|
||||||
for child in self.events.on("received.command").get_children():
|
for child in self.events.on("received.command").get_children():
|
||||||
hooks = self.events.on("received.command").on(child).get_hooks()
|
hooks = self.events.on("received.command").on(child).get_hooks()
|
||||||
if hooks and "help" in hooks[0].kwargs:
|
|
||||||
|
if hooks and self._get_help(hooks[0]):
|
||||||
help_available.append(child)
|
help_available.append(child)
|
||||||
help_available = sorted(help_available)
|
help_available = sorted(help_available)
|
||||||
event["stdout"].write("Commands: %s" % ", ".join(help_available))
|
event["stdout"].write("Commands: %s" % ", ".join(help_available))
|
||||||
|
|
|
@ -7,7 +7,8 @@ PRIORITY_LOW = 3
|
||||||
PRIORITY_MONITOR = 4
|
PRIORITY_MONITOR = 4
|
||||||
|
|
||||||
DEFAULT_PRIORITY = PRIORITY_MEDIUM
|
DEFAULT_PRIORITY = PRIORITY_MEDIUM
|
||||||
DEFAULT_DELIMITER = "."
|
DEFAULT_EVENT_DELIMITER = "."
|
||||||
|
DEFAULT_MULTI_DELIMITER = "|"
|
||||||
|
|
||||||
class Event(object):
|
class Event(object):
|
||||||
def __init__(self, bot, name, **kwargs):
|
def __init__(self, bot, name, **kwargs):
|
||||||
|
@ -62,7 +63,8 @@ class EventHookContext(object):
|
||||||
**kwargs):
|
**kwargs):
|
||||||
self._parent._context_hook(self.context, function, priority, replay,
|
self._parent._context_hook(self.context, function, priority, replay,
|
||||||
kwargs)
|
kwargs)
|
||||||
def on(self, subevent, *extra_subevents, delimiter=DEFAULT_DELIMITER):
|
def on(self, subevent, *extra_subevents,
|
||||||
|
delimiter=DEFAULT_EVENT_DELIMITER):
|
||||||
return self._parent._context_on(self.context, subevent,
|
return self._parent._context_on(self.context, subevent,
|
||||||
extra_subevents, delimiter)
|
extra_subevents, delimiter)
|
||||||
def call_for_result(self, default=None, **kwargs):
|
def call_for_result(self, default=None, **kwargs):
|
||||||
|
@ -97,7 +99,7 @@ class EventHook(object):
|
||||||
while not parent == None and not parent.name == None:
|
while not parent == None and not parent.name == None:
|
||||||
path.append(parent.name)
|
path.append(parent.name)
|
||||||
parent = parent.parent
|
parent = parent.parent
|
||||||
return DEFAULT_DELIMITER.join(path[::-1])
|
return DEFAULT_EVENT_DELIMITER.join(path[::-1])
|
||||||
|
|
||||||
def new_context(self, context):
|
def new_context(self, context):
|
||||||
return EventHookContext(self, context)
|
return EventHookContext(self, context)
|
||||||
|
@ -122,29 +124,38 @@ class EventHook(object):
|
||||||
self._call(kwargs)
|
self._call(kwargs)
|
||||||
self._stored_events = None
|
self._stored_events = None
|
||||||
|
|
||||||
def on(self, subevent, *extra_subevents, delimiter=DEFAULT_DELIMITER):
|
def _make_multiple_hook(self, source, context, events):
|
||||||
|
multiple_event_hook = MultipleEventHook()
|
||||||
|
for event in events:
|
||||||
|
event_hook = source.get_child(event)
|
||||||
|
if not context == None:
|
||||||
|
event_hook = event_hook.new_context(context)
|
||||||
|
multiple_event_hook._add(event_hook)
|
||||||
|
return multiple_event_hook
|
||||||
|
|
||||||
|
def on(self, subevent, *extra_subevents,
|
||||||
|
delimiter=DEFAULT_EVENT_DELIMITER):
|
||||||
return self._on(subevent, extra_subevents, None, delimiter)
|
return self._on(subevent, extra_subevents, None, delimiter)
|
||||||
def _context_on(self, context, subevent, extra_subevents,
|
def _context_on(self, context, subevent, extra_subevents,
|
||||||
delimiter=DEFAULT_DELIMITER):
|
delimiter=DEFAULT_EVENT_DELIMITER):
|
||||||
return self._on(subevent, extra_subevents, context, delimiter)
|
return self._on(subevent, extra_subevents, context, delimiter)
|
||||||
def _on(self, subevent, extra_subevents, context, delimiter):
|
def _on(self, subevent, extra_subevents, context, delimiter):
|
||||||
if delimiter in subevent:
|
if delimiter in subevent:
|
||||||
event_chain = subevent.split(delimiter)
|
event_chain = subevent.split(delimiter)
|
||||||
event_obj = self
|
event_obj = self
|
||||||
for event_name in event_chain:
|
for event_name in event_chain:
|
||||||
|
if DEFAULT_MULTI_DELIMITER in event_name:
|
||||||
|
return self._make_multiple_hook(self, context,
|
||||||
|
event_name.split(DEFAULT_MULTI_DELIMITER))
|
||||||
|
|
||||||
event_obj = event_obj.get_child(event_name)
|
event_obj = event_obj.get_child(event_name)
|
||||||
if not context == None:
|
if not context == None:
|
||||||
return event_obj.new_context(context)
|
return event_obj.new_context(context)
|
||||||
return event_obj
|
return event_obj
|
||||||
|
|
||||||
if extra_subevents:
|
if extra_subevents:
|
||||||
multiple_event_hook = MultipleEventHook()
|
return self._make_multiple_hook(self, context,
|
||||||
for extra_subevent in (subevent,)+extra_subevents:
|
(subevent,)+extra_subevents)
|
||||||
child = self.get_child(extra_subevent)
|
|
||||||
if not context == None:
|
|
||||||
child = child.new_context(context)
|
|
||||||
multiple_event_hook._add(child)
|
|
||||||
return multiple_event_hook
|
|
||||||
|
|
||||||
child = self.get_child(subevent)
|
child = self.get_child(subevent)
|
||||||
if not context == None:
|
if not context == None:
|
||||||
|
|
Loading…
Reference in a new issue