runbullet.lib.plugins moved to runbullet.plugins

This commit is contained in:
Fabio Manganiello 2017-11-03 19:56:12 +01:00
parent f07268293c
commit 413bb8ead0
6 changed files with 40 additions and 38 deletions

View file

@ -19,13 +19,9 @@ __author__ = 'Fabio Manganiello <info@fabiomanganiello.com>'
#-----------# #-----------#
curdir = os.path.dirname(os.path.realpath(__file__))
lib_dir = curdir + os.sep + 'lib'
sys.path.insert(0, lib_dir)
modules = {}
plugins = {}
config = {} config = {}
modules = {}
wrkdir = os.path.dirname(os.path.realpath(__file__))
def on_open(ws): def on_open(ws):
@ -40,42 +36,41 @@ def on_error(ws, error):
logging.error(error) logging.error(error)
def _init_plugin(plugin, reload=False): def _init_plugin(plugin_name, reload=False):
module_name = 'plugins.{}'.format(plugin) global modules
if module_name not in modules or reload: global config
try:
modules[module_name] = importlib.import_module(module_name) if plugin_name in modules and not reload:
except ModuleNotFoundError as e: return modules[plugin_name]
logging.warn('No such plugin: {}'.format(plugin))
raise RuntimeError(e) try:
logging.warn(__package__ + '.plugins.' + plugin_name)
module = importlib.import_module(__package__ + '.plugins.' + plugin_name)
except ModuleNotFoundError as e:
logging.warn('No such plugin: {}'.format(plugin_name))
raise RuntimeError(e)
# e.g. plugins.music.mpd main class: MusicMpdPlugin # e.g. plugins.music.mpd main class: MusicMpdPlugin
cls_name = functools.reduce( cls_name = functools.reduce(
lambda a,b: a.title() + b.title(), lambda a,b: a.title() + b.title(),
(plugin.title().split('.')) (plugin_name.title().split('.'))
) + 'Plugin' ) + 'Plugin'
if cls_name not in plugins or reload: plugin_conf = config[plugin_name] if plugin_name in config else {}
plugin_conf = config[plugin] if plugin in config else {}
try: try:
plugins[cls_name] = getattr(modules[module_name], cls_name)(plugin_conf) plugin = getattr(module, cls_name)(plugin_conf)
except AttributeError as e: modules[plugin_name] = plugin
logging.warn('No such class in {}: {}'.format( except AttributeError as e:
module_name, cls_name)) logging.warn('No such class in {}: {}'.format(
raise RuntimeError(e) plugin_name, cls_name))
raise RuntimeError(e)
return plugins[cls_name] return plugin
def _exec_func(body, retry=True): def _exec_func(body, retry=True):
args = {} args = {}
logging.info('Received push addressed to me: {}'.format(body))
if 'plugin' not in body:
logging.warn('No plugin specified')
return
plugin_name = body['plugin'] plugin_name = body['plugin']
if 'args' in body: if 'args' in body:
@ -84,11 +79,11 @@ def _exec_func(body, retry=True):
else body['args'] else body['args']
try: try:
try: plugin = _init_plugin(plugin_name)
plugin = _init_plugin(plugin_name) except RuntimeError as e: # Module/class not found
except RuntimeError as e: # Module/class not found return
return
try:
ret = plugin.run(args) ret = plugin.run(args)
out = None out = None
err = None err = None
@ -113,6 +108,8 @@ def _exec_func(body, retry=True):
def _on_push(ws, data): def _on_push(ws, data):
global config
data = json.loads(data) data = json.loads(data)
if data['type'] == 'tickle' and data['subtype'] == 'push': if data['type'] == 'tickle' and data['subtype'] == 'push':
logging.debug('Received push tickle') logging.debug('Received push tickle')
@ -136,8 +133,11 @@ def _on_push(ws, data):
if 'target' not in body or body['target'] != config['device_id']: if 'target' not in body or body['target'] != config['device_id']:
return # Not for me return # Not for me
logging.info('Received push addressed to me: {}'.format(body))
if 'plugin' not in body: if 'plugin' not in body:
return # No plugin specified logging.warn('No plugin specified')
return
thread = Thread(target=_exec_func, args=(body,)) thread = Thread(target=_exec_func, args=(body,))
thread.start() thread.start()
@ -157,14 +157,13 @@ def parse_config_file(config_file=None):
else: else:
locations = [ locations = [
# ./config.yaml # ./config.yaml
os.path.join(curdir, 'config.yaml'), os.path.join(wrkdir, 'config.yaml'),
# ~/.config/runbullet/config.yaml # ~/.config/runbullet/config.yaml
os.path.join(os.environ['HOME'], '.config', 'runbullet', 'config.yaml'), os.path.join(os.environ['HOME'], '.config', 'runbullet', 'config.yaml'),
# /etc/runbullet/config.yaml # /etc/runbullet/config.yaml
os.path.join(os.sep, 'etc', 'runbullet', 'config.yaml'), os.path.join(os.sep, 'etc', 'runbullet', 'config.yaml'),
] ]
config = {}
for loc in locations: for loc in locations:
try: try:
with open(loc,'r') as f: with open(loc,'r') as f:
@ -179,6 +178,9 @@ def main():
DEBUG = False DEBUG = False
config_file = None config_file = None
plugins_dir = os.path.join(wrkdir, 'plugins')
sys.path.insert(0, plugins_dir)
optlist, args = getopt(sys.argv[1:], 'vh') optlist, args = getopt(sys.argv[1:], 'vh')
for opt, arg in optlist: for opt, arg in optlist:
if opt == '-c': if opt == '-c':

View file

@ -25,7 +25,7 @@ def create_etc_dir():
else: else:
raise raise
plugins = pkg_files('runbullet/lib/plugins') plugins = pkg_files('runbullet/plugins')
create_etc_dir() create_etc_dir()
setup( setup(