|
| 1 | +import asyncio |
| 2 | +from operator import itemgetter |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +from cloudbot import hook |
| 6 | +from cloudbot.util import web |
| 7 | + |
| 8 | + |
| 9 | +def gen_markdown_table(headers, rows): |
| 10 | + rows = list(rows) |
| 11 | + rows.insert(0, headers) |
| 12 | + rotated = zip(*reversed(rows)) |
| 13 | + |
| 14 | + sizes = tuple(map(lambda l: max(map(len, l)), rotated)) |
| 15 | + rows.insert(1, tuple(('-' * size) for size in sizes)) |
| 16 | + lines = [ |
| 17 | + "| {} |".format(' | '.join(cell.ljust(sizes[i]) for i, cell in enumerate(row))) |
| 18 | + for row in rows |
| 19 | + ] |
| 20 | + return '\n'.join(lines) |
| 21 | + |
| 22 | + |
| 23 | +@hook.command(permissions=["botcontrol"], autohelp=False) |
| 24 | +def pluginlist(bot): |
| 25 | + """- List all currently loaded plugins""" |
| 26 | + manager = bot.plugin_manager |
| 27 | + plugins = [ |
| 28 | + (plugin.title, str(Path(plugin.file_path).resolve().relative_to(bot.base_dir))) |
| 29 | + for plugin in manager.plugins.values() |
| 30 | + ] |
| 31 | + plugins.sort(key=itemgetter(0)) |
| 32 | + table = gen_markdown_table(["Plugin", "Path"], plugins) |
| 33 | + return web.paste(table, service="hastebin") |
| 34 | + |
| 35 | + |
| 36 | +@asyncio.coroutine |
| 37 | +@hook.command(permissions=["botcontrol"]) |
| 38 | +def pluginload(bot, text, reply): |
| 39 | + """<plugin path> - (Re)load <plugin> manually""" |
| 40 | + manager = bot.plugin_manager |
| 41 | + path = str(Path(text.strip()).resolve()) |
| 42 | + was_loaded = path in manager.plugins |
| 43 | + coro = bot.plugin_manager.load_plugin(path) |
| 44 | + |
| 45 | + try: |
| 46 | + yield from coro |
| 47 | + except Exception: |
| 48 | + reply("Plugin failed to load.") |
| 49 | + raise |
| 50 | + else: |
| 51 | + return "Plugin {}loaded successfully.".format("re" if was_loaded else "") |
| 52 | + |
| 53 | + |
| 54 | +@asyncio.coroutine |
| 55 | +@hook.command(permissions=["botcontrol"]) |
| 56 | +def pluginunload(bot, text): |
| 57 | + """<plugin path> - Unload <plugin> manually""" |
| 58 | + manager = bot.plugin_manager |
| 59 | + path = Path(text.strip()).resolve() |
| 60 | + is_loaded = path in manager.plugins |
| 61 | + |
| 62 | + if not is_loaded: |
| 63 | + return "Plugin not loaded, unable to unload." |
| 64 | + |
| 65 | + if (yield from manager.unload_plugin(path)): |
| 66 | + return "Plugin unloaded successfully." |
| 67 | + |
| 68 | + return "Plugin failed to unload." |
0 commit comments