From: Benjamin Braatz Date: Tue, 27 Jul 2021 02:35:37 +0000 (+0200) Subject: Add translated keys to Alias plugin. X-Git-Tag: v0.3.0~31 X-Git-Url: http://git.graph-it.com/?a=commitdiff_plain;h=ad9888e7acb2fe749c692fdf6e4f5b50ef3134f5;p=graphit%2Fcontrolpi.git Add translated keys to Alias plugin. --- diff --git a/controlpi_plugins/util.py b/controlpi_plugins/util.py index cf6e869..9928d18 100644 --- a/controlpi_plugins/util.py +++ b/controlpi_plugins/util.py @@ -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 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']} On instance: @@ -336,7 +342,12 @@ class Alias(BasePlugin): 'to' is a required property 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']} 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']],