2018-07-15 20:12:23 +02:00
|
|
|
from platypush.config import Config
|
|
|
|
from platypush.context import get_plugin
|
2018-07-06 02:08:38 +02:00
|
|
|
from platypush.plugins import Plugin, action
|
2018-06-13 22:09:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
class VariablePlugin(Plugin):
|
|
|
|
"""
|
|
|
|
This plugin allows you to manipulate context variables that can be
|
2018-07-20 19:19:55 +02:00
|
|
|
accessed across your tasks. It requires the :mod:`platypush.plugins.db`
|
2018-09-06 09:31:31 +02:00
|
|
|
and :mod:`platypush.plugins.redis` plugins to be enabled, as the variables
|
|
|
|
will be stored either persisted on a local database or on the local Redis instance.
|
2018-07-20 19:19:55 +02:00
|
|
|
|
|
|
|
Requires:
|
|
|
|
|
|
|
|
* **sqlalchemy** (``pip install sqlalchemy``)
|
2018-09-06 09:31:31 +02:00
|
|
|
* **redis** (``pip install redis``)
|
2018-06-13 22:09:28 +02:00
|
|
|
"""
|
|
|
|
|
2018-07-15 20:12:23 +02:00
|
|
|
_variable_table_name = 'variable'
|
|
|
|
|
2018-06-13 22:09:28 +02:00
|
|
|
def __init__(self, *args, **kwargs):
|
2018-07-20 19:19:55 +02:00
|
|
|
"""
|
|
|
|
The plugin will create a table named ``variable`` on the database
|
|
|
|
configured in the :mod:`platypush.plugins.db` plugin. You'll have
|
|
|
|
to specify a default ``engine`` in your ``db`` plugin configuration.
|
|
|
|
"""
|
|
|
|
|
2018-06-13 22:09:28 +02:00
|
|
|
super().__init__(*args, **kwargs)
|
2018-07-15 20:12:23 +02:00
|
|
|
self.db_plugin = get_plugin('db')
|
2018-09-06 09:31:31 +02:00
|
|
|
self.redis_plugin = get_plugin('redis')
|
2018-07-15 20:12:23 +02:00
|
|
|
|
|
|
|
db = Config.get('db')
|
|
|
|
self.db_config = {
|
|
|
|
'engine': db.get('engine'),
|
|
|
|
'args': db.get('args', []),
|
|
|
|
'kwargs': db.get('kwargs', {})
|
|
|
|
}
|
|
|
|
|
|
|
|
self._create_tables()
|
|
|
|
# self._variables = {}
|
|
|
|
|
|
|
|
def _create_tables(self):
|
|
|
|
self.db_plugin.execute("""CREATE TABLE IF NOT EXISTS {}(
|
|
|
|
name varchar(255) not null primary key,
|
|
|
|
value text
|
|
|
|
)""".format(self._variable_table_name))
|
2018-06-13 22:09:28 +02:00
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-06-13 22:09:28 +02:00
|
|
|
def get(self, name, default_value=None):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
2018-09-06 09:31:31 +02:00
|
|
|
Get the value of a variable by name from the local db.
|
2018-06-25 19:57:43 +02:00
|
|
|
|
|
|
|
:param name: Variable name
|
|
|
|
: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>"}``
|
|
|
|
"""
|
|
|
|
|
2018-07-15 20:12:23 +02:00
|
|
|
rows = self.db_plugin.select(table=self._variable_table_name,
|
|
|
|
filter={'name': name},
|
|
|
|
engine=self.db_config['engine'],
|
|
|
|
*self.db_config['args'],
|
|
|
|
**self.db_config['kwargs']).output
|
|
|
|
|
|
|
|
return {name: rows[0]['value'] if rows else None}
|
2018-06-13 22:09:28 +02:00
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-06-13 23:20:21 +02:00
|
|
|
def set(self, **kwargs):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
2018-09-06 09:31:31 +02:00
|
|
|
Set a variable or a set of variables on the local db.
|
2018-06-25 19:57:43 +02:00
|
|
|
|
|
|
|
:param kwargs: Key-value list of variables to set (e.g. ``foo='bar', answer=42``)
|
|
|
|
"""
|
|
|
|
|
2018-07-15 20:12:23 +02:00
|
|
|
records = [ { 'name': k, 'value': v }
|
|
|
|
for (k,v) in kwargs.items() ]
|
|
|
|
|
|
|
|
self.db_plugin.insert(table=self._variable_table_name,
|
|
|
|
records=records, key_columns=['name'],
|
|
|
|
engine=self.db_config['engine'],
|
|
|
|
on_duplicate_update=True,
|
|
|
|
*self.db_config['args'],
|
|
|
|
**self.db_config['kwargs'])
|
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
return kwargs
|
2018-06-13 22:09:28 +02:00
|
|
|
|
2018-07-15 20:12:23 +02:00
|
|
|
|
2018-07-06 02:08:38 +02:00
|
|
|
@action
|
2018-06-13 22:09:28 +02:00
|
|
|
def unset(self, name):
|
2018-06-25 19:57:43 +02:00
|
|
|
"""
|
2018-09-06 09:31:31 +02:00
|
|
|
Unset a variable by name if it's set on the local db.
|
2018-06-25 19:57:43 +02:00
|
|
|
|
|
|
|
:param name: Name of the variable to remove
|
|
|
|
:type name: str
|
|
|
|
"""
|
2018-07-15 20:12:23 +02:00
|
|
|
|
|
|
|
records = [ { 'name': name } ]
|
|
|
|
|
|
|
|
self.db_plugin.delete(table=self._variable_table_name,
|
|
|
|
records=records, engine=self.db_config['engine'],
|
|
|
|
*self.db_config['args'],
|
|
|
|
**self.db_config['kwargs'])
|
|
|
|
|
|
|
|
return True
|
2018-06-13 22:09:28 +02:00
|
|
|
|
|
|
|
|
2018-09-06 09:31:31 +02:00
|
|
|
@action
|
|
|
|
def mget(self, name):
|
|
|
|
"""
|
|
|
|
Get the value of a variable by name from Redis.
|
|
|
|
|
|
|
|
:param name: Variable name
|
|
|
|
: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>"}``
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self.redis_plugin.mget([name])
|
|
|
|
|
|
|
|
@action
|
|
|
|
def mset(self, **kwargs):
|
|
|
|
"""
|
|
|
|
Set a variable or a set of variables on Redis.
|
|
|
|
|
|
|
|
:param kwargs: Key-value list of variables to set (e.g. ``foo='bar', answer=42``)
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self.redis_plugin.mset(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
@action
|
|
|
|
def munset(self, name):
|
|
|
|
"""
|
|
|
|
Unset a Redis variable by name if it's set
|
|
|
|
|
|
|
|
:param name: Name of the variable to remove
|
|
|
|
:type name: str
|
|
|
|
"""
|
|
|
|
|
2018-09-20 12:49:57 +02:00
|
|
|
return self.redis_plugin.delete(name)
|
|
|
|
|
|
|
|
|
|
|
|
@action
|
|
|
|
def expire(self, key, expiration):
|
|
|
|
"""
|
|
|
|
Set a variable expiration on Redis
|
|
|
|
"""
|
|
|
|
|
|
|
|
return self.redis_plugin.expire(key, expiration)
|
2018-09-06 09:31:31 +02:00
|
|
|
|
|
|
|
|
2018-06-13 22:09:28 +02:00
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|