2017-11-03 02:32:32 +01:00
|
|
|
import functools
|
2017-10-31 10:24:15 +01:00
|
|
|
import importlib
|
2017-10-29 05:17:42 +01:00
|
|
|
import os
|
|
|
|
import logging
|
2017-10-30 02:54:16 +01:00
|
|
|
import socket
|
|
|
|
import sys
|
2017-12-13 04:14:46 +01:00
|
|
|
import traceback
|
2017-11-03 01:56:27 +01:00
|
|
|
import yaml
|
2017-10-29 05:17:42 +01:00
|
|
|
|
2017-11-09 05:04:48 +01:00
|
|
|
from queue import Queue
|
2017-10-31 10:24:15 +01:00
|
|
|
from threading import Thread
|
2017-10-30 02:54:16 +01:00
|
|
|
from getopt import getopt
|
2017-12-13 04:21:26 +01:00
|
|
|
|
2017-12-17 16:15:44 +01:00
|
|
|
from .message.request import Request
|
2017-12-13 04:21:26 +01:00
|
|
|
from .message.response import Response
|
2017-10-29 05:17:42 +01:00
|
|
|
|
2017-11-03 01:56:27 +01:00
|
|
|
__author__ = 'Fabio Manganiello <info@fabiomanganiello.com>'
|
2017-12-16 05:00:29 +01:00
|
|
|
__version__ = '0.3.3'
|
2017-11-03 01:56:27 +01:00
|
|
|
|
|
|
|
#-----------#
|
|
|
|
|
2017-11-03 15:06:29 +01:00
|
|
|
config = {}
|
2017-11-03 19:56:12 +01:00
|
|
|
modules = {}
|
|
|
|
wrkdir = os.path.dirname(os.path.realpath(__file__))
|
2017-11-03 02:42:56 +01:00
|
|
|
|
2017-10-29 05:17:42 +01:00
|
|
|
|
2017-11-03 19:56:12 +01:00
|
|
|
def _init_plugin(plugin_name, reload=False):
|
|
|
|
global modules
|
|
|
|
global config
|
|
|
|
|
|
|
|
if plugin_name in modules and not reload:
|
|
|
|
return modules[plugin_name]
|
|
|
|
|
|
|
|
try:
|
|
|
|
module = importlib.import_module(__package__ + '.plugins.' + plugin_name)
|
|
|
|
except ModuleNotFoundError as e:
|
|
|
|
logging.warn('No such plugin: {}'.format(plugin_name))
|
|
|
|
raise RuntimeError(e)
|
2017-10-31 10:24:15 +01:00
|
|
|
|
2017-11-03 04:36:24 +01:00
|
|
|
# e.g. plugins.music.mpd main class: MusicMpdPlugin
|
2017-11-03 02:42:56 +01:00
|
|
|
cls_name = functools.reduce(
|
|
|
|
lambda a,b: a.title() + b.title(),
|
2017-11-03 19:56:12 +01:00
|
|
|
(plugin_name.title().split('.'))
|
2017-11-03 02:42:56 +01:00
|
|
|
) + 'Plugin'
|
|
|
|
|
2017-11-03 19:56:12 +01:00
|
|
|
plugin_conf = config[plugin_name] if plugin_name in config else {}
|
2017-11-03 15:06:29 +01:00
|
|
|
|
2017-11-03 19:56:12 +01:00
|
|
|
try:
|
|
|
|
plugin = getattr(module, cls_name)(plugin_conf)
|
|
|
|
modules[plugin_name] = plugin
|
|
|
|
except AttributeError as e:
|
|
|
|
logging.warn('No such class in {}: {}'.format(
|
|
|
|
plugin_name, cls_name))
|
|
|
|
raise RuntimeError(e)
|
2017-11-03 04:08:47 +01:00
|
|
|
|
2017-11-03 19:56:12 +01:00
|
|
|
return plugin
|
2017-11-03 04:08:47 +01:00
|
|
|
|
2017-11-03 02:42:56 +01:00
|
|
|
|
2017-12-17 16:15:44 +01:00
|
|
|
def _execute_request(request, retry=True):
|
|
|
|
tokens = request.action.split('.')
|
2017-11-04 12:28:15 +01:00
|
|
|
module_name = str.join('.', tokens[:-1])
|
|
|
|
method_name = tokens[-1:][0]
|
|
|
|
|
2017-11-03 16:48:48 +01:00
|
|
|
try:
|
2017-11-04 12:28:15 +01:00
|
|
|
plugin = _init_plugin(module_name)
|
2017-11-03 19:56:12 +01:00
|
|
|
except RuntimeError as e: # Module/class not found
|
2017-11-09 05:04:48 +01:00
|
|
|
logging.exception(e)
|
2017-11-03 19:56:12 +01:00
|
|
|
return
|
2017-10-31 10:24:15 +01:00
|
|
|
|
2017-11-03 19:56:12 +01:00
|
|
|
try:
|
2017-12-17 16:15:44 +01:00
|
|
|
response = plugin.run(method=method_name, **request.args)
|
2017-12-13 03:37:28 +01:00
|
|
|
if response and response.is_error():
|
|
|
|
logging.warn('Response processed with errors: {}'.format(response))
|
|
|
|
else:
|
|
|
|
logging.info('Processed response: {}'.format(response))
|
2017-11-03 04:08:47 +01:00
|
|
|
except Exception as e:
|
2017-12-17 16:15:44 +01:00
|
|
|
response = Response(output=None, errors=[str(e), traceback.format_exc()])
|
2017-11-03 04:08:47 +01:00
|
|
|
logging.exception(e)
|
|
|
|
if retry:
|
2017-11-04 12:28:15 +01:00
|
|
|
logging.info('Reloading plugin {} and retrying'.format(module_name))
|
|
|
|
_init_plugin(module_name, reload=True)
|
2017-12-17 16:15:44 +01:00
|
|
|
_execute_request(request, retry=False)
|
2017-12-13 04:14:46 +01:00
|
|
|
finally:
|
2017-12-17 16:15:44 +01:00
|
|
|
# Send the response on the backend that received the request
|
|
|
|
if request.backend and request.origin:
|
|
|
|
response.target = request.origin
|
|
|
|
request.backend.send_response(response)
|
2017-10-31 10:24:15 +01:00
|
|
|
|
|
|
|
|
2017-12-16 07:01:25 +01:00
|
|
|
def on_msg(msg):
|
2017-12-17 16:15:44 +01:00
|
|
|
if isinstance(msg, Request):
|
|
|
|
logging.info('Processing request: {}'.format(msg))
|
|
|
|
Thread(target=_execute_request, args=(msg,)).start()
|
|
|
|
elif isinstance(msg, Response):
|
|
|
|
logging.info('Received response: {}'.format(msg))
|
2017-10-30 02:54:16 +01:00
|
|
|
|
|
|
|
|
2017-11-03 15:06:29 +01:00
|
|
|
def parse_config_file(config_file=None):
|
|
|
|
global config
|
|
|
|
|
|
|
|
if config_file:
|
|
|
|
locations = [config_file]
|
|
|
|
else:
|
|
|
|
locations = [
|
|
|
|
# ./config.yaml
|
2017-11-03 19:56:12 +01:00
|
|
|
os.path.join(wrkdir, 'config.yaml'),
|
2017-12-11 20:30:57 +01:00
|
|
|
# ~/.config/platypush/config.yaml
|
|
|
|
os.path.join(os.environ['HOME'], '.config', 'platypush', 'config.yaml'),
|
|
|
|
# /etc/platypush/config.yaml
|
|
|
|
os.path.join(os.sep, 'etc', 'platypush', 'config.yaml'),
|
2017-11-03 15:06:29 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
for loc in locations:
|
|
|
|
try:
|
|
|
|
with open(loc,'r') as f:
|
|
|
|
config = yaml.load(f)
|
|
|
|
except FileNotFoundError as e:
|
|
|
|
pass
|
|
|
|
|
2017-12-11 10:05:45 +01:00
|
|
|
for section in config:
|
|
|
|
if 'disabled' in config[section] and config[section]['disabled']:
|
|
|
|
del config[section]
|
|
|
|
|
2017-12-13 03:37:28 +01:00
|
|
|
if 'logging' not in config:
|
|
|
|
config['logging'] = logging.INFO
|
|
|
|
else:
|
|
|
|
config['logging'] = getattr(logging, config['logging'].upper())
|
|
|
|
|
|
|
|
if 'device_id' not in config:
|
|
|
|
config['device_id'] = socket.gethostname()
|
|
|
|
|
2017-11-03 15:06:29 +01:00
|
|
|
return config
|
2017-11-03 12:01:20 +01:00
|
|
|
|
2017-11-03 15:06:29 +01:00
|
|
|
|
2017-12-17 16:15:44 +01:00
|
|
|
def init_backends(config, bus=None):
|
2017-12-11 23:09:45 +01:00
|
|
|
backends = {}
|
2017-11-09 05:04:48 +01:00
|
|
|
|
|
|
|
for k in config.keys():
|
2017-12-17 16:15:44 +01:00
|
|
|
if not k.startswith('backend.'): continue
|
|
|
|
|
|
|
|
module = importlib.import_module(__package__ + '.' + k)
|
|
|
|
|
|
|
|
# e.g. backend.pushbullet main class: PushbulletBackend
|
|
|
|
cls_name = functools.reduce(
|
|
|
|
lambda a,b: a.title() + b.title(),
|
|
|
|
(module.__name__.title().split('.')[2:])
|
|
|
|
) + 'Backend'
|
|
|
|
|
|
|
|
# Ignore the pusher attribute here
|
|
|
|
if 'pusher' in config[k]: del config[k]['pusher']
|
|
|
|
|
|
|
|
try:
|
|
|
|
b = getattr(module, cls_name)(bus=bus, **config[k])
|
|
|
|
name = '.'.join((k.split('.'))[1:])
|
|
|
|
backends[name] = b
|
|
|
|
except AttributeError as e:
|
|
|
|
logging.warn('No such class in {}: {}'.format(
|
|
|
|
module.__name__, cls_name))
|
|
|
|
raise RuntimeError(e)
|
2017-11-09 05:04:48 +01:00
|
|
|
|
|
|
|
return backends
|
|
|
|
|
2017-12-11 23:09:45 +01:00
|
|
|
|
|
|
|
def get_default_pusher_backend(config):
|
|
|
|
backends = ['.'.join((k.split('.'))[1:])
|
|
|
|
for k in config.keys() if k.startswith('backend.')
|
|
|
|
and 'pusher' in config[k] and config[k]['pusher'] is True]
|
|
|
|
|
|
|
|
return backends[0] if backends else None
|
|
|
|
|
|
|
|
|
2017-12-13 03:37:28 +01:00
|
|
|
def get_logging_level():
|
|
|
|
global config
|
|
|
|
return config['logging']
|
|
|
|
|
|
|
|
|
2017-11-09 05:04:48 +01:00
|
|
|
def get_device_id():
|
|
|
|
global config
|
2017-12-13 01:07:46 +01:00
|
|
|
return config['device_id'] if 'device_id' in config else None
|
2017-11-09 05:04:48 +01:00
|
|
|
|
|
|
|
|
2017-11-03 15:06:29 +01:00
|
|
|
def main():
|
2017-12-13 04:14:46 +01:00
|
|
|
print('Starting platypush v.{}'.format(__version__))
|
|
|
|
|
2017-12-13 03:37:28 +01:00
|
|
|
debug = False
|
2017-11-03 15:06:29 +01:00
|
|
|
config_file = None
|
2017-10-30 02:54:16 +01:00
|
|
|
|
2017-11-03 19:56:12 +01:00
|
|
|
plugins_dir = os.path.join(wrkdir, 'plugins')
|
|
|
|
sys.path.insert(0, plugins_dir)
|
|
|
|
|
2017-10-30 02:54:16 +01:00
|
|
|
optlist, args = getopt(sys.argv[1:], 'vh')
|
|
|
|
for opt, arg in optlist:
|
2017-11-03 01:56:27 +01:00
|
|
|
if opt == '-c':
|
|
|
|
config_file = arg
|
2017-10-30 02:54:16 +01:00
|
|
|
if opt == '-v':
|
2017-12-13 03:37:28 +01:00
|
|
|
debug = True
|
2017-10-30 02:54:16 +01:00
|
|
|
elif opt == '-h':
|
|
|
|
print('''
|
2017-11-03 01:56:27 +01:00
|
|
|
Usage: {} [-v] [-h] [-c <config_file>]
|
2017-10-30 02:54:16 +01:00
|
|
|
-v Enable debug mode
|
|
|
|
-h Show this help
|
2017-11-03 01:56:27 +01:00
|
|
|
-c Path to the configuration file (default: ./config.yaml)
|
2017-10-30 02:54:16 +01:00
|
|
|
'''.format(sys.argv[0]))
|
2017-10-30 02:59:35 +01:00
|
|
|
return
|
2017-10-30 02:54:16 +01:00
|
|
|
|
2017-11-03 15:06:29 +01:00
|
|
|
config = parse_config_file(config_file)
|
2017-12-13 03:37:28 +01:00
|
|
|
if debug: config['logging'] = logging.DEBUG
|
2017-11-03 01:56:27 +01:00
|
|
|
|
2017-12-13 04:14:46 +01:00
|
|
|
logging.basicConfig(level=get_logging_level(), stream=sys.stdout)
|
|
|
|
logging.debug('Configuration dump: {}'.format(config))
|
2017-10-30 02:54:16 +01:00
|
|
|
|
2017-12-17 16:15:44 +01:00
|
|
|
bus = Queue()
|
|
|
|
backends = init_backends(config, bus)
|
2017-10-29 05:17:42 +01:00
|
|
|
|
2017-12-11 23:09:45 +01:00
|
|
|
for backend in backends.values():
|
2017-11-09 05:04:48 +01:00
|
|
|
backend.start()
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
2017-12-17 16:15:44 +01:00
|
|
|
on_msg(bus.get())
|
2017-11-09 05:04:48 +01:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
return
|
2017-10-29 05:17:42 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|
2017-10-29 20:45:19 +01:00
|
|
|
|
2017-11-04 12:28:15 +01:00
|
|
|
# vim:sw=4:ts=4:et:
|
|
|
|
|