Date Tags Python

This time i focused on plugin API management. To me the best way to handle this is to use interfaces.

I wasn’t motivated enough to dig in PEAKPyProtocols so i picked up an ASPN recipe, did some magic-cooking and came up with a base Plugin class and an IPlugin interface. Each plugin developer shall use both this way:

from framework.plugin import IPlugin, Plugin

class IFoo(IPlugin):

    def echo(self, message):
        pass

class Foo(Plugin):
    __implements__ = IFoo

    def echo(self, message):
        return message

In the plugin loader you are now able to detect ‘false’ plugins and partially implemented plugins:

from framework.plugin import InterfaceOmission, Plugin

for entrypoint in pkg_resources.iter_entry_points("my.plugins"):
   try:
      plugin_class = entrypoint.load()
   except InterfaceOmission, omission:
      print omission
      continue

   assert issubclass(plugin_class,Plugin), '%r is not a valid Plugin!' % plugin_class

I’ve put the code in the PythonFR SVN repository, hoping it would eventually be useful for anybody else than me ;-) I don’t think the code will evolve much more, it remains simple enough so that it can be reused.


Comments

comments powered by Disqus