Remove old-style callback (plus multiple fixes).
authorBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 10 Nov 2021 10:47:21 +0000 (11:47 +0100)
committerBenjamin Braatz <benjamin.braatz@graph-it.com>
Wed, 10 Nov 2021 10:47:21 +0000 (11:47 +0100)
controlpi/__init__.py
controlpi/__main__.py
controlpi/baseplugin.py
controlpi/messagebus.py

index 78e1ed6571b9e33719b12165c8013b474add9687..b50ae881c6a533aff2d775351264477d0f8ce242 100644 (file)
@@ -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
-    <BLANKLINE>
-    Failed validating 'required' in schema:
-        {'properties': {'messages': {'items': {'type': 'object'},
-                                     'type': 'array'}},
-         'required': ['messages']}
-    <BLANKLINE>
-    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:
index 94ff899979b90166b5664674e837d49e172758aa..4569c7c0c836ae6eeff6222fa536296176022bad 100644 (file)
@@ -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,
index 904521d2ef2fa57b1a306153083a5dc7dfa3466d..75704ad799201b867968836dbd280604e5bc7e56 100644 (file)
@@ -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'})
index 54579fedde34739fe2453573ea69b87635dafe09..3c94ecf9ef1892b2066d1bf140dd6c743faa10d8 100644 (file)
@@ -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)