def add_plugin(self, plugin, override=False, no_fail=False): """ Add the plugin ``plugin``. :param plugin: A subclass of `Plugin` to attempt to add. :param override: If true, override an already added plugin. """ if not callable(plugin): raise TypeError('plugin must be callable; got %r' % plugin) # Find the base class or raise SubclassError: for base in plugin.bases: if issubclass(base, self.bases): break else: raise errors.PluginSubclassError( plugin=plugin, bases=self.bases, ) # Check override: prev = self.__plugins_by_key.get(plugin.full_name) if prev: if not override: if no_fail: return else: # Must use override=True to override: raise errors.PluginOverrideError( base=base.__name__, name=plugin.name, plugin=plugin, ) self.__plugins.remove(prev) self.__next[plugin] = prev else: if override: if no_fail: return else: # There was nothing already registered to override: raise errors.PluginMissingOverrideError( base=base.__name__, name=plugin.name, plugin=plugin, ) # The plugin is okay, add to sub_d: self.__plugins.add(plugin) self.__plugins_by_key[plugin.full_name] = plugin
def add_plugin(self, klass, override=False): """ Add the plugin ``klass``. :param klass: A subclass of `Plugin` to attempt to add. :param override: If true, override an already added plugin. """ if not inspect.isclass(klass): raise TypeError('plugin must be a class; got %r' % klass) # Find the base class or raise SubclassError: found = False for base in self.bases: if not issubclass(klass, base): continue sub_d = self.__plugins.setdefault(base, {}) found = True if sub_d.get(klass.__name__) is klass: continue # Check override: if klass.__name__ in sub_d: if not override: # Must use override=True to override: raise errors.PluginOverrideError( base=base.__name__, name=klass.__name__, plugin=klass, ) else: if override: # There was nothing already registered to override: raise errors.PluginMissingOverrideError( base=base.__name__, name=klass.__name__, plugin=klass, ) # The plugin is okay, add to sub_d: sub_d[klass.__name__] = klass if not found: raise errors.PluginSubclassError( plugin=klass, bases=self.bases, )