From 23b851e9d7d8ba3d84b6fb321daf38804e308c8a Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 29 Apr 2023 15:24:58 +0200 Subject: [PATCH] `variable.status` robustness fix. `entities.transform_entities` will pass back an empty list instead of an empty dict if no entities were found, and the function should be able to handle it. --- platypush/plugins/variable/__init__.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/platypush/plugins/variable/__init__.py b/platypush/plugins/variable/__init__.py index 020c4ee431..870f108d88 100644 --- a/platypush/plugins/variable/__init__.py +++ b/platypush/plugins/variable/__init__.py @@ -1,4 +1,4 @@ -from typing import Collection, Dict, Optional +from typing import Collection, Dict, Iterable, Optional, Union from typing_extensions import override # from platypush.common.db import Base @@ -115,22 +115,36 @@ class VariablePlugin(Plugin, EntityManager): return self._redis.expire(name, expire) @override - def transform_entities(self, entities: dict) -> Collection[Variable]: + def transform_entities( + self, entities: Union[dict, Iterable] + ) -> Collection[Variable]: + variables = ( + [ + { + 'name': name, + 'value': value, + } + for name, value in entities.items() + ] + if isinstance(entities, dict) + else entities + ) + return super().transform_entities( [ - Variable(id=name, name=name, value=value) - for name, value in entities.items() + Variable(id=var['name'], name=var['name'], value=var['value']) + for var in variables ] ) @override @action - def status(self): + def status(self, *_, **__): variables = { name: value for name, value in self._db_vars.items() if value is not None } - super().publish_entities(variables) + self.publish_entities(variables) return variables