Refactored variable plugin

This commit is contained in:
Fabio Manganiello 2020-09-09 02:14:59 +02:00
parent 4d0d467292
commit a650840429
1 changed files with 6 additions and 14 deletions

View File

@ -18,14 +18,14 @@ class VariablePlugin(Plugin):
_variable_table_name = 'variable' _variable_table_name = 'variable'
def __init__(self, *args, **kwargs): def __init__(self, **kwargs):
""" """
The plugin will create a table named ``variable`` on the database The plugin will create a table named ``variable`` on the database
configured in the :mod:`platypush.plugins.db` plugin. You'll have configured in the :mod:`platypush.plugins.db` plugin. You'll have
to specify a default ``engine`` in your ``db`` plugin configuration. to specify a default ``engine`` in your ``db`` plugin configuration.
""" """
super().__init__(*args, **kwargs) super().__init__(**kwargs)
self.db_plugin = get_plugin('db') self.db_plugin = get_plugin('db')
self.redis_plugin = get_plugin('redis') self.redis_plugin = get_plugin('redis')
@ -64,7 +64,7 @@ class VariablePlugin(Plugin):
*self.db_config['args'], *self.db_config['args'],
**self.db_config['kwargs']).output **self.db_config['kwargs']).output
return {name: rows[0]['value'] if rows else None} return {name: rows[0]['value'] if rows else default_value}
@action @action
def set(self, **kwargs): def set(self, **kwargs):
@ -74,8 +74,8 @@ class VariablePlugin(Plugin):
:param kwargs: Key-value list of variables to set (e.g. ``foo='bar', answer=42``) :param kwargs: Key-value list of variables to set (e.g. ``foo='bar', answer=42``)
""" """
records = [ { 'name': k, 'value': v } records = [{'name': k, 'value': v}
for (k,v) in kwargs.items() ] for (k, v) in kwargs.items()]
self.db_plugin.insert(table=self._variable_table_name, self.db_plugin.insert(table=self._variable_table_name,
records=records, key_columns=['name'], records=records, key_columns=['name'],
@ -86,7 +86,6 @@ class VariablePlugin(Plugin):
return kwargs return kwargs
@action @action
def unset(self, name): def unset(self, name):
""" """
@ -96,7 +95,7 @@ class VariablePlugin(Plugin):
:type name: str :type name: str
""" """
records = [ { 'name': name } ] records = [{'name': name}]
self.db_plugin.delete(table=self._variable_table_name, self.db_plugin.delete(table=self._variable_table_name,
records=records, engine=self.db_config['engine'], records=records, engine=self.db_config['engine'],
@ -105,7 +104,6 @@ class VariablePlugin(Plugin):
return True return True
@action @action
def mget(self, name): def mget(self, name):
""" """
@ -114,8 +112,6 @@ class VariablePlugin(Plugin):
:param name: Variable name :param name: Variable name
:type name: str :type name: str
:param default_value: What will be returned if the variable is not defined (default: None)
:returns: A map in the format ``{"<name>":"<value>"}`` :returns: A map in the format ``{"<name>":"<value>"}``
""" """
@ -134,7 +130,6 @@ class VariablePlugin(Plugin):
self.redis_plugin.mset(**kwargs) self.redis_plugin.mset(**kwargs)
return kwargs return kwargs
@action @action
def munset(self, name): def munset(self, name):
""" """
@ -146,7 +141,6 @@ class VariablePlugin(Plugin):
return self.redis_plugin.delete(name) return self.redis_plugin.delete(name)
@action @action
def expire(self, name, expire): def expire(self, name, expire):
""" """
@ -161,6 +155,4 @@ class VariablePlugin(Plugin):
return self.redis_plugin.expire(name, expire) return self.redis_plugin.expire(name, expire)
# vim:sw=4:ts=4:et: # vim:sw=4:ts=4:et: