* Sending responses back on the backend channel
* Version bump
This commit is contained in:
parent
491c2cd571
commit
462dc2f1c9
4 changed files with 25 additions and 24 deletions
|
@ -5,14 +5,17 @@ import logging
|
||||||
import json
|
import json
|
||||||
import socket
|
import socket
|
||||||
import sys
|
import sys
|
||||||
|
import traceback
|
||||||
import websocket
|
import websocket
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from getopt import getopt
|
from getopt import getopt
|
||||||
|
from .response import Response
|
||||||
|
|
||||||
__author__ = 'Fabio Manganiello <info@fabiomanganiello.com>'
|
__author__ = 'Fabio Manganiello <info@fabiomanganiello.com>'
|
||||||
|
__version__ = '0.3.2'
|
||||||
|
|
||||||
#-----------#
|
#-----------#
|
||||||
|
|
||||||
|
@ -53,7 +56,7 @@ def _init_plugin(plugin_name, reload=False):
|
||||||
return plugin
|
return plugin
|
||||||
|
|
||||||
|
|
||||||
def _exec_func(args, retry=True):
|
def _exec_func(args, backend=None, retry=True):
|
||||||
action = args.pop('action')
|
action = args.pop('action')
|
||||||
tokens = action.split('.')
|
tokens = action.split('.')
|
||||||
module_name = str.join('.', tokens[:-1])
|
module_name = str.join('.', tokens[:-1])
|
||||||
|
@ -72,6 +75,7 @@ def _exec_func(args, retry=True):
|
||||||
else:
|
else:
|
||||||
logging.info('Processed response: {}'.format(response))
|
logging.info('Processed response: {}'.format(response))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
response = Response(output=None, errors=[e, traceback.format_exc()])
|
||||||
logging.exception(e)
|
logging.exception(e)
|
||||||
if retry:
|
if retry:
|
||||||
# Put the action back where it was before retrying
|
# 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))
|
logging.info('Reloading plugin {} and retrying'.format(module_name))
|
||||||
_init_plugin(module_name, reload=True)
|
_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):
|
def on_msg(msg, backend=None):
|
||||||
Thread(target=_exec_func, args=(msg,)).start()
|
Thread(target=_exec_func, args=(msg,backend)).start()
|
||||||
|
|
||||||
|
|
||||||
def parse_config_file(config_file=None):
|
def parse_config_file(config_file=None):
|
||||||
|
@ -170,6 +177,8 @@ def get_device_id():
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
print('Starting platypush v.{}'.format(__version__))
|
||||||
|
|
||||||
debug = False
|
debug = False
|
||||||
config_file = None
|
config_file = None
|
||||||
|
|
||||||
|
@ -193,9 +202,9 @@ Usage: {} [-v] [-h] [-c <config_file>]
|
||||||
|
|
||||||
config = parse_config_file(config_file)
|
config = parse_config_file(config_file)
|
||||||
if debug: config['logging'] = logging.DEBUG
|
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()
|
mq = Queue()
|
||||||
backends = get_backends(config)
|
backends = get_backends(config)
|
||||||
|
@ -206,7 +215,7 @@ Usage: {} [-v] [-h] [-c <config_file>]
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
on_msg(mq.get())
|
on_msg(mq.get(), backend)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import logging
|
import logging
|
||||||
|
import sys
|
||||||
import platypush
|
import platypush
|
||||||
|
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
@ -32,7 +33,8 @@ class Backend(Thread):
|
||||||
self.device_id = platypush.get_device_id()
|
self.device_id = platypush.get_device_id()
|
||||||
|
|
||||||
Thread.__init__(self)
|
Thread.__init__(self)
|
||||||
logging.basicConfig(level=logging.INFO
|
|
||||||
|
logging.basicConfig(stream=sys.stdout, level=platypush.get_logging_level()
|
||||||
if 'logging' not in config
|
if 'logging' not in config
|
||||||
else getattr(logging, config.pop('logging')))
|
else getattr(logging, config.pop('logging')))
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,16 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import logging
|
import logging
|
||||||
import traceback
|
|
||||||
|
|
||||||
|
from platypush import get_logging_level
|
||||||
from platypush.response import Response
|
from platypush.response import Response
|
||||||
|
|
||||||
class Plugin(object):
|
class Plugin(object):
|
||||||
def __init__(self, config):
|
def __init__(self, config):
|
||||||
self.config = 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()):
|
for cls in reversed(self.__class__.mro()):
|
||||||
if cls is not object:
|
if cls is not object:
|
||||||
|
@ -17,21 +19,9 @@ class Plugin(object):
|
||||||
except AttributeError as e:
|
except AttributeError as e:
|
||||||
pass
|
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):
|
def run(self, method, *args, **kwargs):
|
||||||
try:
|
return getattr(self, method)(*args, **kwargs)
|
||||||
res = getattr(self, method)(*args, **kwargs)
|
|
||||||
except Exception as e:
|
|
||||||
res = Response(output=None, errors=[e, traceback.format_exc()])
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
# vim:sw=4:ts=4:et:
|
# vim:sw=4:ts=4:et:
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ class Response(object):
|
||||||
self.errors = errors
|
self.errors = errors
|
||||||
|
|
||||||
def __str__(self):
|
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):
|
def is_error(self):
|
||||||
return len(self.errors) != 0
|
return len(self.errors) != 0
|
||||||
|
|
Loading…
Reference in a new issue