2018-10-26 21:55:49 +02:00
|
|
|
import asyncio
|
2017-12-24 13:15:37 +01:00
|
|
|
import importlib
|
|
|
|
import logging
|
2017-12-24 02:35:45 +01:00
|
|
|
|
2019-01-08 16:33:57 +01:00
|
|
|
from threading import RLock
|
2018-08-22 02:04:16 +02:00
|
|
|
|
2017-12-24 02:35:45 +01:00
|
|
|
from ..config import Config
|
|
|
|
|
2018-06-06 20:09:18 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-12-24 02:35:45 +01:00
|
|
|
# Map: backend_name -> backend_instance
|
|
|
|
backends = {}
|
|
|
|
|
|
|
|
# Map: plugin_name -> plugin_instance
|
|
|
|
plugins = {}
|
|
|
|
|
2018-08-22 02:04:16 +02:00
|
|
|
# Map: plugin_name -> init_lock to make sure that a plugin isn't initialized
|
|
|
|
# multiple times
|
|
|
|
plugins_init_locks = {}
|
|
|
|
|
2018-04-24 20:07:16 +02:00
|
|
|
# Reference to the main application bus
|
|
|
|
main_bus = None
|
|
|
|
|
2017-12-24 13:15:37 +01:00
|
|
|
def register_backends(bus=None, global_scope=False, **kwargs):
|
2017-12-24 02:35:45 +01:00
|
|
|
""" Initialize the backend objects based on the configuration and returns
|
|
|
|
a name -> backend_instance map.
|
|
|
|
Params:
|
|
|
|
bus -- If specific (it usually should), the messages processed by the
|
|
|
|
backends will be posted on this bus.
|
|
|
|
|
|
|
|
kwargs -- Any additional key-value parameters required to initialize the backends
|
|
|
|
"""
|
|
|
|
|
2018-04-24 20:07:16 +02:00
|
|
|
global main_bus
|
|
|
|
if bus:
|
|
|
|
main_bus = bus
|
|
|
|
|
2017-12-24 13:15:37 +01:00
|
|
|
if global_scope:
|
|
|
|
global backends
|
|
|
|
else:
|
|
|
|
backends = {}
|
2017-12-24 02:35:45 +01:00
|
|
|
|
|
|
|
for (name, cfg) in Config.get_backends().items():
|
|
|
|
module = importlib.import_module('platypush.backend.' + name)
|
|
|
|
|
|
|
|
# e.g. backend.pushbullet main class: PushbulletBackend
|
2018-03-04 17:55:48 +01:00
|
|
|
cls_name = ''
|
|
|
|
for token in module.__name__.title().split('.')[2:]:
|
|
|
|
cls_name += token.title()
|
|
|
|
cls_name += 'Backend'
|
2017-12-24 02:35:45 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
b = getattr(module, cls_name)(bus=bus, **cfg, **kwargs)
|
|
|
|
backends[name] = b
|
|
|
|
except AttributeError as e:
|
2018-06-06 20:09:18 +02:00
|
|
|
logger.warning('No such class in {}: {}'.format(
|
2017-12-24 02:35:45 +01:00
|
|
|
module.__name__, cls_name))
|
|
|
|
raise RuntimeError(e)
|
|
|
|
|
|
|
|
return backends
|
|
|
|
|
|
|
|
def get_backend(name):
|
|
|
|
""" Returns the backend instance identified by name if it exists """
|
|
|
|
|
|
|
|
global backends
|
2018-07-15 02:57:12 +02:00
|
|
|
return backends.get(name)
|
2017-12-24 02:35:45 +01:00
|
|
|
|
|
|
|
|
2017-12-27 10:18:51 +01:00
|
|
|
def get_plugin(plugin_name, reload=False):
|
|
|
|
""" Registers a plugin instance by name if not registered already, or
|
|
|
|
returns the registered plugin instance"""
|
|
|
|
global plugins
|
2018-08-22 02:04:16 +02:00
|
|
|
global plugins_init_locks
|
2017-12-27 10:18:51 +01:00
|
|
|
|
2018-08-22 02:11:51 +02:00
|
|
|
if plugin_name not in plugins_init_locks:
|
2019-01-08 16:33:57 +01:00
|
|
|
plugins_init_locks[plugin_name] = RLock()
|
2018-08-22 02:11:51 +02:00
|
|
|
|
2018-09-16 23:45:20 +02:00
|
|
|
if plugin_name in plugins and not reload:
|
|
|
|
return plugins[plugin_name]
|
2017-12-27 10:18:51 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
plugin = importlib.import_module('platypush.plugins.' + plugin_name)
|
2018-02-20 22:58:13 +01:00
|
|
|
except ImportError as e:
|
2018-06-06 20:09:18 +02:00
|
|
|
logger.warning('No such plugin: {}'.format(plugin_name))
|
2017-12-27 10:18:51 +01:00
|
|
|
raise RuntimeError(e)
|
|
|
|
|
|
|
|
# e.g. plugins.music.mpd main class: MusicMpdPlugin
|
2018-01-12 15:18:06 +01:00
|
|
|
cls_name = ''
|
|
|
|
for token in plugin_name.split('.'):
|
|
|
|
cls_name += token.title()
|
|
|
|
cls_name += 'Plugin'
|
2017-12-27 10:18:51 +01:00
|
|
|
|
|
|
|
plugin_conf = Config.get_plugins()[plugin_name] \
|
|
|
|
if plugin_name in Config.get_plugins() else {}
|
|
|
|
|
2019-02-13 15:14:35 +01:00
|
|
|
if 'disabled' in plugin_conf:
|
|
|
|
if plugin_conf['disabled'] is True:
|
|
|
|
return None
|
|
|
|
del plugin_conf['disabled']
|
|
|
|
|
|
|
|
if 'enabled' in plugin_conf:
|
|
|
|
if plugin_conf['enabled'] is False:
|
|
|
|
return None
|
|
|
|
del plugin_conf['enabled']
|
|
|
|
|
2017-12-27 10:18:51 +01:00
|
|
|
try:
|
|
|
|
plugin_class = getattr(plugin, cls_name)
|
|
|
|
except AttributeError as e:
|
2019-07-16 16:51:27 +02:00
|
|
|
logger.warning('No such class in {}: {} [error: {}]'.format(plugin_name, cls_name, str(e)))
|
2017-12-27 10:18:51 +01:00
|
|
|
raise RuntimeError(e)
|
|
|
|
|
2018-09-20 12:59:18 +02:00
|
|
|
with plugins_init_locks[plugin_name]:
|
2018-09-18 19:19:41 +02:00
|
|
|
if plugins.get(plugin_name) and not reload:
|
|
|
|
return plugins[plugin_name]
|
|
|
|
plugins[plugin_name] = plugin_class(**plugin_conf)
|
|
|
|
|
2018-08-22 02:32:53 +02:00
|
|
|
return plugins[plugin_name]
|
2017-12-27 10:18:51 +01:00
|
|
|
|
2019-06-23 02:16:20 +02:00
|
|
|
|
2018-04-24 20:07:16 +02:00
|
|
|
def get_bus():
|
|
|
|
global main_bus
|
|
|
|
return main_bus
|
|
|
|
|
2017-12-27 10:18:51 +01:00
|
|
|
|
2018-10-26 21:55:49 +02:00
|
|
|
def get_or_create_event_loop():
|
|
|
|
try:
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
except RuntimeError:
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
|
|
|
|
return loop
|
|
|
|
|
|
|
|
|
2017-12-24 02:35:45 +01:00
|
|
|
# vim:sw=4:ts=4:et:
|