From 413bb8ead0924a3c9b65c4b9fc8503623283302a Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Fri, 3 Nov 2017 19:56:12 +0100 Subject: [PATCH] runbullet.lib.plugins moved to runbullet.plugins --- runbullet/__init__.py | 76 ++++++++++--------- runbullet/{lib => }/plugins/__init__.py | 0 runbullet/{lib => }/plugins/music/__init__.py | 0 .../{lib => }/plugins/music/mpd/__init__.py | 0 runbullet/{lib => }/plugins/shell/__init__.py | 0 setup.py | 2 +- 6 files changed, 40 insertions(+), 38 deletions(-) rename runbullet/{lib => }/plugins/__init__.py (100%) rename runbullet/{lib => }/plugins/music/__init__.py (100%) rename runbullet/{lib => }/plugins/music/mpd/__init__.py (100%) rename runbullet/{lib => }/plugins/shell/__init__.py (100%) diff --git a/runbullet/__init__.py b/runbullet/__init__.py index 7b58a887d..e21578c4a 100644 --- a/runbullet/__init__.py +++ b/runbullet/__init__.py @@ -19,13 +19,9 @@ __author__ = 'Fabio Manganiello ' #-----------# -curdir = os.path.dirname(os.path.realpath(__file__)) -lib_dir = curdir + os.sep + 'lib' -sys.path.insert(0, lib_dir) - -modules = {} -plugins = {} config = {} +modules = {} +wrkdir = os.path.dirname(os.path.realpath(__file__)) def on_open(ws): @@ -40,42 +36,41 @@ def on_error(ws, error): logging.error(error) -def _init_plugin(plugin, reload=False): - module_name = 'plugins.{}'.format(plugin) - if module_name not in modules or reload: - try: - modules[module_name] = importlib.import_module(module_name) - except ModuleNotFoundError as e: - logging.warn('No such plugin: {}'.format(plugin)) - raise RuntimeError(e) +def _init_plugin(plugin_name, reload=False): + global modules + global config + + if plugin_name in modules and not reload: + return modules[plugin_name] + + 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 cls_name = functools.reduce( lambda a,b: a.title() + b.title(), - (plugin.title().split('.')) + (plugin_name.title().split('.')) ) + 'Plugin' - if cls_name not in plugins or reload: - plugin_conf = config[plugin] if plugin in config else {} + plugin_conf = config[plugin_name] if plugin_name in config else {} - try: - plugins[cls_name] = getattr(modules[module_name], cls_name)(plugin_conf) - except AttributeError as e: - logging.warn('No such class in {}: {}'.format( - module_name, cls_name)) - raise RuntimeError(e) + 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) - return plugins[cls_name] + return plugin def _exec_func(body, retry=True): 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'] if 'args' in body: @@ -84,11 +79,11 @@ def _exec_func(body, retry=True): else body['args'] try: - try: - plugin = _init_plugin(plugin_name) - except RuntimeError as e: # Module/class not found - return + plugin = _init_plugin(plugin_name) + except RuntimeError as e: # Module/class not found + return + try: ret = plugin.run(args) out = None err = None @@ -113,6 +108,8 @@ def _exec_func(body, retry=True): def _on_push(ws, data): + global config + data = json.loads(data) if data['type'] == 'tickle' and data['subtype'] == 'push': 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']: return # Not for me + logging.info('Received push addressed to me: {}'.format(body)) + if 'plugin' not in body: - return # No plugin specified + logging.warn('No plugin specified') + return thread = Thread(target=_exec_func, args=(body,)) thread.start() @@ -157,14 +157,13 @@ def parse_config_file(config_file=None): else: locations = [ # ./config.yaml - os.path.join(curdir, 'config.yaml'), + os.path.join(wrkdir, 'config.yaml'), # ~/.config/runbullet/config.yaml os.path.join(os.environ['HOME'], '.config', 'runbullet', 'config.yaml'), # /etc/runbullet/config.yaml os.path.join(os.sep, 'etc', 'runbullet', 'config.yaml'), ] - config = {} for loc in locations: try: with open(loc,'r') as f: @@ -179,6 +178,9 @@ def main(): DEBUG = False config_file = None + plugins_dir = os.path.join(wrkdir, 'plugins') + sys.path.insert(0, plugins_dir) + optlist, args = getopt(sys.argv[1:], 'vh') for opt, arg in optlist: if opt == '-c': diff --git a/runbullet/lib/plugins/__init__.py b/runbullet/plugins/__init__.py similarity index 100% rename from runbullet/lib/plugins/__init__.py rename to runbullet/plugins/__init__.py diff --git a/runbullet/lib/plugins/music/__init__.py b/runbullet/plugins/music/__init__.py similarity index 100% rename from runbullet/lib/plugins/music/__init__.py rename to runbullet/plugins/music/__init__.py diff --git a/runbullet/lib/plugins/music/mpd/__init__.py b/runbullet/plugins/music/mpd/__init__.py similarity index 100% rename from runbullet/lib/plugins/music/mpd/__init__.py rename to runbullet/plugins/music/mpd/__init__.py diff --git a/runbullet/lib/plugins/shell/__init__.py b/runbullet/plugins/shell/__init__.py similarity index 100% rename from runbullet/lib/plugins/shell/__init__.py rename to runbullet/plugins/shell/__init__.py diff --git a/setup.py b/setup.py index 0ac515aa2..872eda844 100755 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ def create_etc_dir(): else: raise -plugins = pkg_files('runbullet/lib/plugins') +plugins = pkg_files('runbullet/plugins') create_etc_dir() setup(