From bf6e5784f22612031ba7f6859e74d14971334fd2 Mon Sep 17 00:00:00 2001 From: iou1name Date: Tue, 8 Oct 2019 09:06:52 -0400 Subject: [PATCH] refactor help --- loader.py | 12 +----------- modules/help.py | 40 +++++++++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/loader.py b/loader.py index 0b3d39e..d9cb339 100755 --- a/loader.py +++ b/loader.py @@ -30,7 +30,7 @@ def unload_module(bot, name): bot.unregister_callable(obj) del old_module - delattr(sys.modules['modules'], name) + delattr(sys.modules['modules'], name.rpartition('.')[2]) del sys.modules[name] @@ -77,14 +77,6 @@ def process_callable(func, config): Sets various helper atributes about a given function. """ prefix = config.core.prefix - doc = func.__doc__ - if doc: - doc = doc.strip() - doc = doc.replace("\t", "") - doc = doc.replace("\n\n", "\x00") - doc = doc.replace("\n", " ") - doc = doc.replace("\x00", "\n") - func._docs = {} func.example = getattr(func, "example", [(None, None)]) func.thread = getattr(func, "thread", True) @@ -108,5 +100,3 @@ def process_callable(func, config): if ex_input[0] != prefix: ex_input = prefix + ex_input func.example[n] = (ex_input, example[1]) - if doc: - func._docs = (doc, func.example) diff --git a/modules/help.py b/modules/help.py index 5374bd2..4f4675e 100755 --- a/modules/help.py +++ b/modules/help.py @@ -7,27 +7,41 @@ import random from module import commands, example +def clean_docstring(doc): + """Cleans the docstring up a bit.""" + if not doc: + return '' + doc = doc.strip() + doc = doc.replace("\t", "") + doc = doc.replace("\n\n", "\x00") + doc = doc.replace("\n", " ") + doc = doc.replace("\x00", "\n") + return doc + @commands('help', 'commands') @example('.help tell') def help(bot, trigger): """Shows a command's documentation, and possibly an example.""" if trigger.group(2): - name = trigger.group(2) - name = name.lower() - if name not in bot.commands: + command = trigger.group(2) + command = command.lower() + if command not in bot.commands: return bot.msg("Command not found.") - cmd = bot.commands[name] - docstring, examples = cmd.doc - if examples: - ex = random.choice(examples) - bot.msg(docstring) - if cmd.aliases: - bot.msg("Aliases: " + ", ".join(cmd.aliases)) - if ex[0]: + func = bot.commands[command] + doc = clean_docstring(func.__doc__) + bot.msg(doc) + + aliases = [c for c, f in bot.commands.items() if f == func] + aliases.remove(command) + if aliases: + bot.msg("Aliases: " + ", ".join(aliases)) + + if hasattr(func, 'example'): + ex = random.choice(func.example) bot.msg("Ex. In: " + ex[0]) - if ex[1]: - bot.msg("Ex. Out: " + ex[1]) + if ex[1]: + bot.msg("Ex. Out: " + ex[1]) else: if trigger.owner: