From 8fe61217ce6c2ea57a990d21c8479f7fe91464e9 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sat, 29 Apr 2023 11:35:57 +0200 Subject: [PATCH] Added `_db` and `_redis` properties to the Plugin class. Plugins can now access the database and Redis APIs directly without having to run their own `get_plugin` validation logic. --- platypush/plugins/__init__.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/platypush/plugins/__init__.py b/platypush/plugins/__init__.py index 96d7c596f..c57ffd678 100644 --- a/platypush/plugins/__init__.py +++ b/platypush/plugins/__init__.py @@ -67,6 +67,30 @@ class Plugin(EventGenerator, ExtensionWithManifest): # lgtm [py/missing-call-to get_decorators(self.__class__, climb_class_hierarchy=True).get('action', []) ) + @property + def _db(self): + """ + :return: The reference to the :class:`platypush.plugins.db.DbPlugin`. + """ + from platypush.context import get_plugin + from platypush.plugins.db import DbPlugin + + db: DbPlugin = get_plugin(DbPlugin) # type: ignore + assert db, 'db plugin not initialized' + return db + + @property + def _redis(self): + """ + :return: The reference to the :class:`platypush.plugins.redis.RedisPlugin`. + """ + from platypush.context import get_plugin + from platypush.plugins.redis import RedisPlugin + + redis: RedisPlugin = get_plugin(RedisPlugin) # type: ignore + assert redis, 'db plugin not initialized' + return redis + def run(self, method, *args, **kwargs): assert ( method in self.registered_actions