Config push on main object instead of plugin.
authorBenjamin Braatz <bb@bbraatz.eu>
Tue, 27 Jul 2021 02:01:32 +0000 (04:01 +0200)
committerBenjamin Braatz <bb@bbraatz.eu>
Tue, 27 Jul 2021 02:01:32 +0000 (04:01 +0200)
conf-conf.json
controlpi/messagebus.py
controlpi_plugins/conf.py [deleted file]

index a856a70ee5b92341f74aa0a7336a765519452dbf..8026f6a46440c24a1555f2376180b4103f0fbb42 100644 (file)
@@ -1,13 +1,9 @@
 {
-    "Conf": {
-        "plugin": "Conf"
-    },
     "Test Procedure": {
         "plugin": "Init",
         "messages": [
-            { "target": "Conf", "command": "push",
+            { "target": "", "command": "push conf",
               "conf": {
-                  "Conf": { "plugin": "Conf" },
                   "Example State": { "plugin": "State" },
                   "Debug Logger": { "plugin": "Log", "filter": [{}] }
               } }
index 51129e5f55f590db53a2ff340ad5d40caa2208f7..d6b9cc8e036194e3299a1c3c4b222019473950e8 100644 (file)
@@ -84,6 +84,7 @@ Client 1: {'sender': '', 'target': 'Client 1'}
 import asyncio
 import json
 import jsonschema  # type: ignore
+import sys
 
 from typing import Union, Dict, List, Any, Iterable, Callable, Coroutine
 MessageValue = Union[None, str, int, float, bool, Dict[str, Any], List[Any]]
@@ -1169,15 +1170,28 @@ class MessageBus:
             message = await self._queue.get()
             if ('target' in message and
                     message['target'] == '' and
-                    'command' in message and
-                    message['command'] == 'get clients'):
-                for client in self._plugins:
-                    answer = Message('')
-                    answer['client'] = client
-                    answer['plugin'] = self._plugins[client]
-                    answer['sends'] = self._send_reg.get_templates(client)
-                    answer['receives'] = self._recv_reg.get_templates(client)
-                    await self._queue.put(answer)
+                    'command' in message):
+                if message['command'] == 'get clients':
+                    for client in self._plugins:
+                        answer = Message('')
+                        answer['client'] = client
+                        answer['plugin'] = self._plugins[client]
+                        answer['sends'] = self._send_reg.get_templates(client)
+                        answer['receives'] = self._recv_reg.get_templates(client)
+                        await self._queue.put(answer)
+                elif message['command'] == 'push conf':
+                    conf = {}
+                    try:
+                        with open(sys.argv[1]) as conf_file:
+                            conf = json.load(conf_file)
+                    except (IndexError, FileNotFoundError, json.decoder.JSONDecodeError):
+                        pass
+                    if conf == message['conf']:
+                        await self.bus.send(Message(self.name, {'event': 'unchanged'}))
+                    else:
+                        with open(sys.argv[1], 'w') as conf_file:
+                            json.dump(message['conf'], conf_file)
+                        await self.bus.send(Message(self.name, {'event': 'changed'}))
             for client in self._recv_reg.get(message):
                 await self._callbacks[client](message)
             self._queue.task_done()
diff --git a/controlpi_plugins/conf.py b/controlpi_plugins/conf.py
deleted file mode 100644 (file)
index 8590a45..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-"""Provide plugin to push new configuration on system.
-
-"""
-import asyncio
-import json
-import sys
-
-from controlpi import BasePlugin, Message, MessageTemplate
-
-
-class Conf(BasePlugin):
-    """Replace configuration by command."""
-
-    CONF_SCHEMA = {'properties': {}}
-    """Empty configuration schema."""
-
-    async def _push(self, message: Message) -> None:
-        """Update configuration if changed."""
-        conf = {}
-        try:
-            with open(sys.argv[1]) as conf_file:
-                conf = json.load(conf_file)
-        except (IndexError, FileNotFoundError, json.decoder.JSONDecodeError):
-            pass
-        if conf == message['conf']:
-            await self.bus.send(Message(self.name, {'event': 'unchanged'}))
-        else:
-            with open(sys.argv[1], 'w') as conf_file:
-                json.dump(message['conf'], conf_file)
-            await self.bus.send(Message(self.name, {'event': 'changed'}))
-
-    def process_conf(self) -> None:
-        """Register plugin as bus client."""
-        receives = [MessageTemplate({'target': {'const': self.name},
-                                     'command': {'const': 'push'},
-                                     'conf': {'type': 'object'}})]
-        sends = [MessageTemplate({'event': {'const': 'changed'}}),
-                 MessageTemplate({'event': {'const': 'unchanged'}})]
-        self.bus.register(self.name, 'Wait', sends, receives, self._push)
-
-    async def run(self) -> None:
-        """Run no code proactively."""
-        pass