diff --git a/src/octoprint/plugin/core.py b/src/octoprint/plugin/core.py index 0b9da5cbb..d92ed0a80 100644 --- a/src/octoprint/plugin/core.py +++ b/src/octoprint/plugin/core.py @@ -630,30 +630,46 @@ class PluginManager(object): def plugin_hooks(self): return {key: map(lambda v: (v[1], v[2]), value) for key, value in self._plugin_hooks.items()} - def find_plugins(self, existing=None, ignore_uninstalled=True): + def find_plugins(self, existing=None, ignore_uninstalled=True, incl_all_found=False): + added, found = self._find_plugins(existing=existing, ignore_uninstalled=ignore_uninstalled) + if incl_all_found: + return added, found + else: + return added + + def _find_plugins(self, existing=None, ignore_uninstalled=True): if existing is None: existing = dict(self.plugins) - result = OrderedDict() + result_added = OrderedDict() + result_found = [] + if self.plugin_folders: try: - result.update(self._find_plugins_from_folders(self.plugin_folders, - existing, - ignored_uninstalled=ignore_uninstalled)) + added, found = self._find_plugins_from_folders(self.plugin_folders, + existing, + ignored_uninstalled=ignore_uninstalled) + result_added.update(added) + result_found += found except: self.logger.exception("Error fetching plugins from folders") + if self.plugin_entry_points: - existing.update(result) + existing.update(result_added) try: - result.update(self._find_plugins_from_entry_points(self.plugin_entry_points, - existing, - ignore_uninstalled=ignore_uninstalled)) + added, found = self._find_plugins_from_entry_points(self.plugin_entry_points, + existing, + ignore_uninstalled=ignore_uninstalled) + result_added.update(added) + result_found += found except: self.logger.exception("Error fetching plugins from entry points") - return result + + return result_added, result_found def _find_plugins_from_folders(self, folders, existing, ignored_uninstalled=True): - result = OrderedDict() + added = OrderedDict() + found = [] for folder in folders: try: @@ -691,7 +707,8 @@ class PluginManager(object): # whatever this is, we ignore it continue - if key in existing or key in result or (ignored_uninstalled and key in self.marked_plugins["uninstalled"]): + found.append(key) + if key in existing or key in added or (ignored_uninstalled and key in self.marked_plugins["uninstalled"]): # plugin is already defined, ignore it continue @@ -702,16 +719,17 @@ class PluginManager(object): plugin.origin = FolderOrigin("folder", folder) plugin.managable = not flagged_readonly and not actual_readonly plugin.enabled = False - result[key] = plugin + added[key] = plugin except: self.logger.exception("Error processing folder entry {!r} from folder {}".format(entry, folder)) except: self.logger.exception("Error processing folder {}".format(folder)) - return result + return added, found def _find_plugins_from_entry_points(self, groups, existing, ignore_uninstalled=True): - result = OrderedDict() + added = OrderedDict() + found = [] # let's make sure we have a current working set ... working_set = pkg_resources.WorkingSet() @@ -745,7 +763,8 @@ class PluginManager(object): module_name = entry_point.module_name version = entry_point.dist.version - if key in existing or key in result or (ignore_uninstalled and key in self.marked_plugins["uninstalled"]): + found.append(key) + if key in existing or key in added or (ignore_uninstalled and key in self.marked_plugins["uninstalled"]): # plugin is already defined or marked as uninstalled, ignore it continue @@ -782,11 +801,11 @@ class PluginManager(object): module_name, plugin.location)) - result[key] = plugin + added[key] = plugin except: self.logger.exception("Error processing entry point {!r} for group {}".format(entry_point, group)) - return result + return added, found def _import_plugin_from_module(self, key, folder=None, module_name=None, name=None, version=None, summary=None, author=None, url=None, license=None, bundled=False): @@ -869,11 +888,26 @@ class PluginManager(object): if force_reload is None: force_reload = [] - plugins = self.find_plugins(existing=dict((k, v) for k, v in self.plugins.items() if not k in force_reload)) - self.disabled_plugins.update(plugins) + added, found = self.find_plugins(existing=dict((k, v) for k, v in self.plugins.items() if not k in force_reload), + incl_all_found=True) + + # let's clean everything we DIDN'T find first + removed = [key for key in self.enabled_plugins.keys() + self.disabled_plugins.keys() if key not in found] + for key in removed: + try: + del self.enabled_plugins[key] + except KeyError: + pass + + try: + del self.disabled_plugins[key] + except KeyError: + pass + + self.disabled_plugins.update(added) # 1st pass: loading the plugins - for name, plugin in plugins.items(): + for name, plugin in added.items(): try: if not plugin.blacklisted and not plugin.forced_disabled: self.load_plugin(name, plugin, startup=startup, initialize_implementation=initialize_implementations) @@ -887,7 +921,7 @@ class PluginManager(object): force_reload=force_reload) # 2nd pass: enabling those plugins that need enabling - for name, plugin in plugins.items(): + for name, plugin in added.items(): try: if plugin.loaded and not plugin.forced_disabled: if plugin.blacklisted: