From: Benjamin Braatz Date: Wed, 10 Nov 2021 10:47:21 +0000 (+0100) Subject: Remove old-style callback (plus multiple fixes). X-Git-Tag: v0.3.0~4 X-Git-Url: http://git.graph-it.com/?a=commitdiff_plain;h=0d92096ebe8d6934e289d08ad71fc644df24d8c5;p=graphit%2Fcontrolpi.git Remove old-style callback (plus multiple fixes). --- diff --git a/controlpi/__init__.py b/controlpi/__init__.py index 78e1ed6..b50ae88 100644 --- a/controlpi/__init__.py +++ b/controlpi/__init__.py @@ -137,15 +137,7 @@ async def test(conf: Dict[str, PluginConf], Incorrect plugin configurations can also be tested by this: >>> asyncio.run(test( ... {"Example Init": {"plugin": "Init"}}, [])) - 'messages' is a required property - - Failed validating 'required' in schema: - {'properties': {'messages': {'items': {'type': 'object'}, - 'type': 'array'}}, - 'required': ['messages']} - - On instance: - {'plugin': 'Init'} + data must contain ['messages'] properties Configuration for 'Example Init' is not valid. """ message_bus = MessageBus() @@ -158,7 +150,7 @@ async def test(conf: Dict[str, PluginConf], return print(f"test(): {message}") message_bus.register('test()', 'Test', - [MessageTemplate()], [MessageTemplate()], log) + [MessageTemplate()], [([MessageTemplate()], log)]) coroutines = _process_conf(message_bus, conf) for coroutine in coroutines: diff --git a/controlpi/__main__.py b/controlpi/__main__.py index 94ff899..4569c7c 100644 --- a/controlpi/__main__.py +++ b/controlpi/__main__.py @@ -52,7 +52,10 @@ def read_configuration() -> Dict[str, PluginConf]: class ConfigHandler(pyinotify.ProcessEvent): + """Handler for changes of the configuration file on disk.""" + def process_IN_MODIFY(self, event): + """Cancel all tasks if configuration file changed.""" if event.pathname == os.path.abspath(sys.argv[1]): print(f"Configuration file modified: {event.pathname}") for task in asyncio.all_tasks(): @@ -61,6 +64,7 @@ class ConfigHandler(pyinotify.ProcessEvent): async def add_config_change_handler() -> pyinotify.AsyncioNotifier: + """Add handler for configuration file.""" wm = pyinotify.WatchManager() loop = asyncio.get_running_loop() notifier = pyinotify.AsyncioNotifier(wm, loop, diff --git a/controlpi/baseplugin.py b/controlpi/baseplugin.py index 904521d..75704ad 100644 --- a/controlpi/baseplugin.py +++ b/controlpi/baseplugin.py @@ -57,7 +57,7 @@ when using the system in production: ... bus = MessageBus() ... p = BusPlugin(bus, 'Bus Test', {}) ... bus.register('Test', 'TestPlugin', -... [{}], [{'sender': {'const': 'Bus Test'}}], log) +... [{}], [([{'sender': {'const': 'Bus Test'}}], log)]) ... bus_task = asyncio.create_task(bus.run()) ... asyncio.create_task(p.run()) ... await bus.send({'sender': 'Test', 'target': 'Bus Test', 'key': 'v'}) diff --git a/controlpi/messagebus.py b/controlpi/messagebus.py index 54579fe..3c94ecf 100644 --- a/controlpi/messagebus.py +++ b/controlpi/messagebus.py @@ -935,6 +935,7 @@ class TemplateRegistry: return result def get_callbacks(self, message: Message) -> List[MessageCallback]: + """Get all callbacks registered for templates matching a message.""" result = [] for client in self._callbacks: for callback in self._callbacks[client]: @@ -1110,10 +1111,8 @@ class MessageBus: def register(self, client: str, plugin: str, sends: Iterable[MessageTemplate], - receives: Union[Iterable[MessageTemplate], - Iterable[Tuple[Iterable[MessageTemplate], - MessageCallback]]], - callback: Optional[MessageCallback] = None) -> None: + receives: Iterable[Tuple[Iterable[MessageTemplate], + MessageCallback]]) -> None: """Register a client at the message bus. >>> async def callback(message): @@ -1152,13 +1151,9 @@ class MessageBus: for template in sends: self._send_reg.insert(template, client) event['sends'] = self._send_reg.get_templates(client) - if callback: - for template in receives: + for (templates, callback) in receives: + for template in templates: self._recv_reg.insert(template, client, callback) - else: - for (templates, callback) in receives: - for template in templates: - self._recv_reg.insert(template, client, callback) event['receives'] = self._recv_reg.get_templates(client) self._queue.put_nowait(event)