From 462dc2f1c90900064ea71ab43da96521da98ecd7 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Wed, 13 Dec 2017 04:14:46 +0100 Subject: [PATCH] * Sending responses back on the backend channel * Version bump --- platypush/__init__.py | 23 ++++++++++++++++------- platypush/backend/__init__.py | 4 +++- platypush/plugins/__init__.py | 20 +++++--------------- platypush/response/__init__.py | 2 +- 4 files changed, 25 insertions(+), 24 deletions(-) diff --git a/platypush/__init__.py b/platypush/__init__.py index dde4037a1f..49760fdcd1 100644 --- a/platypush/__init__.py +++ b/platypush/__init__.py @@ -5,14 +5,17 @@ import logging import json import socket import sys +import traceback import websocket import yaml from queue import Queue from threading import Thread from getopt import getopt +from .response import Response __author__ = 'Fabio Manganiello ' +__version__ = '0.3.2' #-----------# @@ -53,7 +56,7 @@ def _init_plugin(plugin_name, reload=False): return plugin -def _exec_func(args, retry=True): +def _exec_func(args, backend=None, retry=True): action = args.pop('action') tokens = action.split('.') module_name = str.join('.', tokens[:-1]) @@ -72,6 +75,7 @@ def _exec_func(args, retry=True): else: logging.info('Processed response: {}'.format(response)) except Exception as e: + response = Response(output=None, errors=[e, traceback.format_exc()]) logging.exception(e) if retry: # Put the action back where it was before retrying @@ -79,11 +83,14 @@ def _exec_func(args, retry=True): logging.info('Reloading plugin {} and retrying'.format(module_name)) _init_plugin(module_name, reload=True) - _exec_func(args, retry=False) + _exec_func(args, backend, retry=False) + finally: + if backend: + backend.send_msg({ 'response':str(response) }) -def on_msg(msg): - Thread(target=_exec_func, args=(msg,)).start() +def on_msg(msg, backend=None): + Thread(target=_exec_func, args=(msg,backend)).start() def parse_config_file(config_file=None): @@ -170,6 +177,8 @@ def get_device_id(): def main(): + print('Starting platypush v.{}'.format(__version__)) + debug = False config_file = None @@ -193,9 +202,9 @@ Usage: {} [-v] [-h] [-c ] config = parse_config_file(config_file) if debug: config['logging'] = logging.DEBUG - logging.info('Configuration dump: {}'.format(config)) - logging.basicConfig(level=get_logging_level()) + logging.basicConfig(level=get_logging_level(), stream=sys.stdout) + logging.debug('Configuration dump: {}'.format(config)) mq = Queue() backends = get_backends(config) @@ -206,7 +215,7 @@ Usage: {} [-v] [-h] [-c ] while True: try: - on_msg(mq.get()) + on_msg(mq.get(), backend) except KeyboardInterrupt: return diff --git a/platypush/backend/__init__.py b/platypush/backend/__init__.py index 6501fd6495..22f70a148f 100644 --- a/platypush/backend/__init__.py +++ b/platypush/backend/__init__.py @@ -1,4 +1,5 @@ import logging +import sys import platypush from threading import Thread @@ -32,7 +33,8 @@ class Backend(Thread): self.device_id = platypush.get_device_id() Thread.__init__(self) - logging.basicConfig(level=logging.INFO + + logging.basicConfig(stream=sys.stdout, level=platypush.get_logging_level() if 'logging' not in config else getattr(logging, config.pop('logging'))) diff --git a/platypush/plugins/__init__.py b/platypush/plugins/__init__.py index 441e4ffbd5..bbaacf8504 100644 --- a/platypush/plugins/__init__.py +++ b/platypush/plugins/__init__.py @@ -1,14 +1,16 @@ import os import sys import logging -import traceback +from platypush import get_logging_level from platypush.response import Response class Plugin(object): def __init__(self, config): self.config = config - self._set_logging() + logging.basicConfig(stream=sys.stdout, level=get_logging_level() + if 'logging' not in config + else getattr(logging, config.pop('logging'))) for cls in reversed(self.__class__.mro()): if cls is not object: @@ -17,21 +19,9 @@ class Plugin(object): except AttributeError as e: pass - def _set_logging(self): - if 'logging' in self.config: - self._logging = self.config.pop('logging') - else: - self._logging = logging.INFO - - logging.basicConfig(level=self._logging) - def run(self, method, *args, **kwargs): - try: - res = getattr(self, method)(*args, **kwargs) - except Exception as e: - res = Response(output=None, errors=[e, traceback.format_exc()]) + return getattr(self, method)(*args, **kwargs) - return res # vim:sw=4:ts=4:et: diff --git a/platypush/response/__init__.py b/platypush/response/__init__.py index a3c0e2c373..e845b24a5d 100644 --- a/platypush/response/__init__.py +++ b/platypush/response/__init__.py @@ -6,7 +6,7 @@ class Response(object): self.errors = errors def __str__(self): - return json.dumps({ 'output': self.output, 'error': self.errors }) + return json.dumps({ 'output': self.output, 'errors': self.errors }) def is_error(self): return len(self.errors) != 0