platypush/platypush/plugins/__init__.py

50 lines
1.3 KiB
Python
Raw Normal View History

import sys
2017-12-11 03:53:26 +01:00
import logging
import traceback
2017-12-18 01:10:51 +01:00
from platypush.config import Config
from platypush.message.response import Response
from platypush.utils import get_decorators
2018-06-06 20:09:18 +02:00
def action(f):
def _execute_action(*args, **kwargs):
output = None
errors = []
try:
output = f(*args, **kwargs)
if output and isinstance(output, Response):
errors = output.errors
output = output.output
except Exception as e:
if isinstance(args[0], Plugin):
args[0].logger.exception(e)
errors.append(str(e) + '\n' + traceback.format_exc())
return Response(output=output, errors=errors)
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):
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()))
self.registered_actions = set(get_decorators(self.__class__).get('action', []))
def run(self, method, *args, **kwargs):
if method not in self.registered_actions:
raise RuntimeError('{} is not a registered action on {}'.format(
method, self.__class__.__name__))
return getattr(self, method)(*args, **kwargs)
2017-10-31 09:20:35 +01:00
# vim:sw=4:ts=4:et: