Exemplo n.º 1
0
def unload(bot, module_path):
    if module_path in tracking.list:
        plugin = tracking.list[module_path]
        loop = asyncio.get_event_loop()

        if len(plugin["threads"]) == 0:
            all_commands = plugin["commands"]["all"]
            for command_name in all_commands:
                if command_name in command.commands:
                    logger.debug("removing function {}".format(command_name))
                    del command.commands[command_name]
                if command_name in command.admin_commands:
                    logger.debug("deregistering admin command {}".format(command_name))
                    command.admin_commands.remove(command_name)

            for type in plugin["commands"]["tagged"]:
                for command_name in plugin["commands"]["tagged"][type]:
                    if command_name in command.command_tagsets:
                        logger.debug("deregistering tagged command {}".format(command_name))
                        del command.command_tagsets[command_name]

            for type in bot._handlers.pluggables:
                for handler in bot._handlers.pluggables[type]:
                    if handler[2]["module.path"] == module_path:
                        logger.debug("removing handler {} {}".format(type, handler))
                        bot._handlers.pluggables[type].remove(handler)
                        tracking.deregister_handler(handler[0], handler[2]["module.path"])

            shared = plugin["shared"]
            for shared_def in shared:
                id = shared_def[0]
                if id in bot.shared:
                    logger.debug("removing shared {}".format(id))
                    del bot.shared[id]

            if len(plugin["asyncio.task"]) > 0:
                for task in plugin["asyncio.task"]:
                    logger.info("cancelling task: {}".format(task))
                    loop.call_soon_threadsafe(task.cancel)

            if len(plugin["aiohttp.web"]) > 0:
                from sinks import aiohttp_terminate # XXX: needs to be late-imported
                for group in plugin["aiohttp.web"]:
                    yield from aiohttp_terminate(group)

            if len(plugin["commands"]["argument.preprocessors"]) > 0:
                for groupname in plugin["commands"]["argument.preprocessors"]:
                    del command.preprocessors[groupname]

            logger.info("{} unloaded".format(module_path))

            del tracking.list[module_path]

            return True

        else:
            raise RuntimeError("{} has {} thread(s)".format(module_path, len(plugin["threads"])))

    else:
        raise KeyError("{} not found".format(module_path))
Exemplo n.º 2
0
def unload(bot, module_path):
    if module_path in tracking.list:
        plugin = tracking.list[module_path]
        loop = asyncio.get_event_loop()

        if len(plugin["threads"]) == 0:
            all_commands = plugin["commands"]["all"]
            for command_name in all_commands:
                if command_name in command.commands:
                    logger.debug("removing function {}".format(command_name))
                    del command.commands[command_name]
                if command_name in command.admin_commands:
                    logger.debug(
                        "deregistering admin command {}".format(command_name))
                    command.admin_commands.remove(command_name)

            for type in plugin["commands"]["tagged"]:
                for command_name in plugin["commands"]["tagged"][type]:
                    if command_name in command.command_tagsets:
                        logger.debug("deregistering tagged command {}".format(
                            command_name))
                        del command.command_tagsets[command_name]

            for type in bot._handlers.pluggables:
                for handler in bot._handlers.pluggables[type]:
                    if handler[2]["module.path"] == module_path:
                        logger.debug("removing handler {} {}".format(
                            type, handler))
                        bot._handlers.pluggables[type].remove(handler)

            shared = plugin["shared"]
            for shared_def in shared:
                id = shared_def[0]
                if id in bot.shared:
                    logger.debug("removing shared {}".format(id))
                    del bot.shared[id]

            if len(plugin["asyncio.task"]) > 0:
                for task in plugin["asyncio.task"]:
                    logger.info("cancelling task: {}".format(task))
                    loop.call_soon_threadsafe(task.cancel)

            if len(plugin["aiohttp.web"]) > 0:
                from sinks import aiohttp_terminate  # XXX: needs to be late-imported
                for group in plugin["aiohttp.web"]:
                    yield from aiohttp_terminate(group)

            logger.info("{} unloaded".format(module_path))

            del tracking.list[module_path]

            return True

        else:
            raise RuntimeError("{} has {} thread(s)".format(
                module_path, len(plugin["threads"])))

    else:
        raise KeyError("{} not found".format(module_path))
Exemplo n.º 3
0
def unload(bot, module_path):
    if module_path in tracking.list:
        plugin = tracking.list[module_path]
        loop = asyncio.get_event_loop()

        # Look for an optional function finali[sz]e, akin to initiali[sz]e.
        # May be a regular function or a coroutine, optionally taking the bot as an argument.
        public_functions = [
            o for o in getmembers(sys.modules[module_path], isfunction)
        ]
        try:
            for function_name, the_function in public_functions:
                if function_name in ("_finalise", "_finalize"):
                    argc = len(inspect.signature(the_function).parameters)
                    if argc == 0:
                        args = ()
                    elif argc == 1:
                        args = (bot, )
                    else:
                        continue
                    coro = the_function(*args)
                    if asyncio.iscoroutinefunction(the_function):
                        yield from coro
        except Exception as e:
            logger.exception(
                "EXCEPTION during plugin deinit: {}".format(module_path))

        if len(plugin["threads"]) == 0:
            all_commands = plugin["commands"]["all"]
            for command_name in all_commands:
                if command_name in command.commands:
                    logger.debug("removing function {}".format(command_name))
                    del command.commands[command_name]
                if command_name in command.admin_commands:
                    logger.debug(
                        "deregistering admin command {}".format(command_name))
                    command.admin_commands.remove(command_name)

            for type in plugin["commands"]["tagged"]:
                for command_name in plugin["commands"]["tagged"][type]:
                    if command_name in command.command_tagsets:
                        logger.debug("deregistering tagged command {}".format(
                            command_name))
                        del command.command_tagsets[command_name]

            for type in bot._handlers.pluggables:
                for handler in bot._handlers.pluggables[type]:
                    if handler[2]["module.path"] == module_path:
                        logger.debug("removing handler {} {}".format(
                            type, handler))
                        bot._handlers.pluggables[type].remove(handler)
                        tracking.deregister_handler(handler[0],
                                                    handler[2]["module.path"])

            shared = plugin["shared"]
            for shared_def in shared:
                id = shared_def[0]
                if id in bot.shared:
                    logger.debug("removing shared {}".format(id))
                    del bot.shared[id]

            if len(plugin["asyncio.task"]) > 0:
                for task in plugin["asyncio.task"]:
                    logger.info("cancelling task: {}".format(task))
                    loop.call_soon_threadsafe(task.cancel)

            if len(plugin["aiohttp.web"]) > 0:
                from sinks import aiohttp_terminate  # XXX: needs to be late-imported
                for group in plugin["aiohttp.web"]:
                    yield from aiohttp_terminate(group)

            if len(plugin["commands"]["argument.preprocessors"]) > 0:
                for groupname in plugin["commands"]["argument.preprocessors"]:
                    del command.preprocessors[groupname]

            logger.info("{} unloaded".format(module_path))

            del tracking.list[module_path]

            return True

        else:
            raise RuntimeError("{} has {} thread(s)".format(
                module_path, len(plugin["threads"])))

    else:
        raise KeyError("{} not found".format(module_path))