Add translated keys to Alias plugin.
authorBenjamin Braatz <bb@bbraatz.eu>
Tue, 27 Jul 2021 02:35:37 +0000 (04:35 +0200)
committerBenjamin Braatz <bb@bbraatz.eu>
Tue, 27 Jul 2021 02:35:37 +0000 (04:35 +0200)
controlpi_plugins/util.py

index cf6e86926f10dd876ad306706f4dbebaf9dda17b..9928d18708fd8e8d4885665e56c931312bbf3e9d 100644 (file)
@@ -305,22 +305,23 @@ class Alias(BasePlugin):
     >>> asyncio.run(controlpi.test(
     ...     {"Test Alias": {"plugin": "Alias",
     ...                     "from": {"id": {"const": 42}},
-    ...                     "to": {"id": "translated"}}},
-    ...     [{"id": 42, "content": "Test Message"},
-    ...      {"id": 42, "content": "Second Message"}]))
+    ...                     "to": {"id": "translated"},
+    ...                     "translate": [{'from': "old", "to": "new"}]}},
+    ...     [{"id": 42, "content": "Test Message", "old": "content"},
+    ...      {"id": 42, "content": "Second Message", "old": "content"}]))
     ... # doctest: +NORMALIZE_WHITESPACE
     test(): {'sender': '', 'event': 'registered',
              'client': 'Test Alias', 'plugin': 'Alias',
              'sends': [{'id': {'const': 'translated'}}],
              'receives': [{'id': {'const': 42}}]}
     test(): {'sender': 'test()', 'id': 42,
-             'content': 'Test Message'}
+             'content': 'Test Message', 'old': 'content'}
     test(): {'sender': 'Test Alias', 'id': 'translated',
-             'content': 'Test Message'}
+             'content': 'Test Message', 'new': 'content'}
     test(): {'sender': 'test()', 'id': 42,
-             'content': 'Second Message'}
+             'content': 'Second Message', 'old': 'content'}
     test(): {'sender': 'Test Alias', 'id': 'translated',
-             'content': 'Second Message'}
+             'content': 'Second Message', 'new': 'content'}
 
     The "from" and "to" keys are required:
     >>> asyncio.run(controlpi.test(
@@ -328,7 +329,12 @@ class Alias(BasePlugin):
     'from' is a required property
     <BLANKLINE>
     Failed validating 'required' in schema:
-        {'properties': {'from': {'type': 'object'}, 'to': {'type': 'object'}},
+        {'properties': {'from': {'type': 'object'},
+                        'to': {'type': 'object'},
+                        'translate': {'items': {'properties': {'from': {'type': 'string'},
+                                                               'to': {'type': 'string'}},
+                                                'type': 'object'},
+                                      'type': 'array'}},
          'required': ['from', 'to']}
     <BLANKLINE>
     On instance:
@@ -336,7 +342,12 @@ class Alias(BasePlugin):
     'to' is a required property
     <BLANKLINE>
     Failed validating 'required' in schema:
-        {'properties': {'from': {'type': 'object'}, 'to': {'type': 'object'}},
+        {'properties': {'from': {'type': 'object'},
+                        'to': {'type': 'object'},
+                        'translate': {'items': {'properties': {'from': {'type': 'string'},
+                                                               'to': {'type': 'string'}},
+                                                'type': 'object'},
+                                      'type': 'array'}},
          'required': ['from', 'to']}
     <BLANKLINE>
     On instance:
@@ -367,7 +378,11 @@ class Alias(BasePlugin):
     """
 
     CONF_SCHEMA = {'properties': {'from': {'type': 'object'},
-                                  'to': {'type': 'object'}},
+                                  'to': {'type': 'object'},
+                                  'translate': {'type': 'array',
+                                                'items': {'type': 'object',
+                                                          'properties': {'from': {'type': 'string'},
+                                                                         'to': {'type': 'string'}}}}},
                    'required': ['from', 'to']}
     """Schema for Alias plugin configuration.
 
@@ -383,11 +398,18 @@ class Alias(BasePlugin):
         alias_message.update(self.conf['to'])
         for key in message:
             if key != 'sender' and key not in self.conf['from']:
-                alias_message[key] = message[key]
+                if key in self._translate:
+                    alias_message[self._translate[key]] = message[key]
+                else:
+                    alias_message[key] = message[key]
         await self.bus.send(alias_message)
 
     def process_conf(self) -> None:
         """Register plugin as bus client."""
+        self._translate = {}
+        if 'translate' in self.conf:
+            for pair in self.conf['translate']:
+                self._translate[pair['from']] = pair['to']
         self.bus.register(self.name, 'Alias',
                           [MessageTemplate.from_message(self.conf['to'])],
                           [self.conf['from']],