2017-11-03 04:08:47 +01:00
|
|
|
import sys
|
2017-12-11 03:53:26 +01:00
|
|
|
import logging
|
2018-07-06 02:08:38 +02:00
|
|
|
import traceback
|
2017-12-13 03:37:28 +01:00
|
|
|
|
2018-11-01 19:42:40 +01:00
|
|
|
from functools import wraps
|
|
|
|
|
2017-12-18 01:10:51 +01:00
|
|
|
from platypush.config import Config
|
2017-12-13 04:21:26 +01:00
|
|
|
from platypush.message.response import Response
|
2018-07-06 02:08:38 +02:00
|
|
|
from platypush.utils import get_decorators
|
2017-11-03 04:08:47 +01:00
|
|
|
|
2018-07-05 09:15:53 +02:00
|
|
|
def action(f):
|
2018-11-01 19:42:40 +01:00
|
|
|
@wraps(f)
|
2018-07-05 09:15:53 +02:00
|
|
|
def _execute_action(*args, **kwargs):
|
2018-07-06 02:08:38 +02:00
|
|
|
output = None
|
|
|
|
errors = []
|
|
|
|
|
2019-01-13 20:09:25 +01:00
|
|
|
output = f(*args, **kwargs)
|
|
|
|
if output and isinstance(output, Response):
|
|
|
|
errors = output.errors \
|
|
|
|
if isinstance(output.errors, list) else [output.errors]
|
|
|
|
output = output.output
|
|
|
|
elif isinstance(output, tuple) and len(output) == 2:
|
|
|
|
errors = output[1] \
|
|
|
|
if isinstance(output[1], list) else [output[1]]
|
|
|
|
|
|
|
|
if len(errors) == 1 and errors[0] is None: errors = []
|
|
|
|
output = output[0]
|
2018-07-06 02:08:38 +02:00
|
|
|
|
|
|
|
return Response(output=output, errors=errors)
|
|
|
|
|
2018-07-16 22:56:07 +02:00
|
|
|
# Propagate the docstring
|
|
|
|
_execute_action.__doc__ = f.__doc__
|
2018-07-05 09:15:53 +02:00
|
|
|
return _execute_action
|
|
|
|
|
|
|
|
|
2017-10-31 09:20:35 +01:00
|
|
|
class Plugin(object):
|
2017-12-18 01:10:51 +01:00
|
|
|
""" Base plugin class """
|
2017-11-03 15:06:29 +01:00
|
|
|
|
2017-12-18 01:10:51 +01:00
|
|
|
def __init__(self, **kwargs):
|
2018-06-14 02:19:55 +02:00
|
|
|
self.logger = logging.getLogger(self.__class__.__name__)
|
2018-06-06 20:09:18 +02:00
|
|
|
if 'logging' in kwargs:
|
|
|
|
self.logger.setLevel(getattr(logging, kwargs['logging'].upper()))
|
2017-11-03 04:08:47 +01:00
|
|
|
|
2018-07-17 01:23:12 +02:00
|
|
|
self.registered_actions = set(
|
|
|
|
get_decorators(self.__class__, climb_class_hierarchy=True)
|
|
|
|
.get('action', [])
|
|
|
|
)
|
2018-07-06 02:08:38 +02:00
|
|
|
|
2017-11-04 12:28:15 +01:00
|
|
|
def run(self, method, *args, **kwargs):
|
2018-07-06 02:08:38 +02:00
|
|
|
if method not in self.registered_actions:
|
|
|
|
raise RuntimeError('{} is not a registered action on {}'.format(
|
|
|
|
method, self.__class__.__name__))
|
|
|
|
|
2017-12-13 04:14:46 +01:00
|
|
|
return getattr(self, method)(*args, **kwargs)
|
2017-12-13 03:37:28 +01:00
|
|
|
|
2017-10-31 09:20:35 +01:00
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|