def test_plugin_with_exception_callback(self): plugin = Plugin(exceptions=0) @plugin.on_after_processed() async def _(message, env): if env.exception: plugin.exceptions += 1 @plugin.on_has_text() async def _(message, env): raise Exception app = Kutana() app.register_plugins([plugin]) loop = asyncio.get_event_loop() set_logger_level(logging.CRITICAL) loop.run_until_complete(app.process(DebugManager(), "message")) set_logger_level(logging.ERROR) self.assertEqual(plugin.exceptions, 1)
class KutanaTest(unittest.TestCase): def setUp(self): self.app = None self.target = [] @contextmanager def debug_manager(self, queue): if self.app is None: self.app = Kutana() self.manager = DebugManager(*queue) self.app.add_manager(self.manager) self.plugin = Plugin() self.plugins = [self.plugin] try: yield self.plugin finally: for plugin in self.plugins: self.app.register_plugins([plugin]) self.app.run() self.assertEqual(self.target, self.manager.replies)
def test_load_plugins(self): loaded_plugins = load_plugins("test/test_plugins/") loaded_plugins.sort(key=lambda plugin: plugin.name) self.assertEqual(len(loaded_plugins), 3) self.assertEqual(loaded_plugins[0].name, "Memory") self.assertEqual(loaded_plugins[1].name, "My file") self.assertEqual(loaded_plugins[2].name, "My file twice") app = Kutana() app.register_plugins(loaded_plugins) loop = asyncio.get_event_loop() loop.run_until_complete(app.startup()) loop.run_until_complete(app.process(DebugManager(), "message")) self.assertEqual(loaded_plugins[0].memory, "message") self.assertEqual(loaded_plugins[1].my_file, ":)") self.assertEqual(loaded_plugins[2].my_file, ":):)")
import json from kutana import Kutana, VKManager, load_plugins, TGManager # Load configuration with open("configuration.json") as fh: config = json.load(fh) # Create application app = Kutana() # Add manager to application app.add_manager(VKManager(config["vk_token"])) app.add_manager(TGManager(config["tg_token"])) # Load and register plugins app.register_plugins(load_plugins("plugins/")) if __name__ == "__main__": # Run application app.run()