En Tue, 03 Nov 2009 09:07:01 -0300, Henning Bredel <henning.bre...@gmx.de> escribió:
On Tue, 03 Nov 2009 10:18:29 +0000, Steven D'Aprano wrote:

You need to give some actual examples of what you are trying to do, and
what you are expecting to happen. How is initialized() being called?

Example: Assume a framework which offers common functionality for a plugin
or a module a user can choose at the beginning. The framework does not
know the concrete type of the plugin so it is possible to extend it by
implementing a well known interface or abstract class.

The framework reads the plugin directory, loads each module and creates
buttons for each plugin with a callback method for initializing. To use
common functionality of the framework, initialization method takes it as
the parent parameter.

Then forget about the code you read in that blog post, doesn't apply to your use case.

I think this listing makes the most sense to you:

  # initialize all plugins
  self._plugin_modules = _load_plugins() # imp loading here
  LOGGER.debug(ActionProvider.plugins) # print what was loaded
  for plugin in ActionProvider.plugins: # create button for each
      app_button = gtk.Button(plugin.title)
      LOGGER.debug('Title of plugin: %s' % plugin.title)
      app_button.connect("clicked",
                         plugin.initialize(plugin, self),
                         None)
      self.set_canvas(app_button)
      app_button.show()

Try something like this:

--- begin plugin.py ---
class Plugin(object):
    "Every plugin class should have a docstring"
    def __init__(self, manager):
        pass
--- end plugin.py ---

--- begin pluginmgr.py --
import os.path
from glob import glob
from plugin import Plugin

class PluginManager:
    def __init__(self, plugin_directory):
        self.plugin_directory = plugin_directory
        self.plugins = []

    def load_all(self):
        for fn in glob(os.path.join(self.plugin_directory, '*.py')):
            namespace = {}
            execfile(fn, namespace)
            for name, obj in namespace.items():
                if (isinstance(obj, type) and
                    issubclass(obj, Plugin) and
                obj is not Plugin):
                    # obj is a Plugin subclass
                    cls = obj
                    print cls.__name__, fn
                    print cls.__doc__
                    print
                    plugin = cls(self)  # call the constructor
                    self.plugins.append(plugin)

if __name__ == '__main__':
    mgr = PluginManager(r'd:\\temp\\plugins')
    mgr.load_all()
    print mgr.plugins
--- end pluginmgr.py ---

--- begin one.py in the plugins directory ---
from plugin import Plugin

class MyPlugin(Plugin):
    """The most wonderful plugin in the world.
    This plugin does this, and that,
    and also that, too.
    It's very nice, I assure you."""

class Second(Plugin):
    """My second plugin. I don't know what
    is this for, but it works."""
--- end one.py ---

Plugin is the base class; all plugins must inherit from it. PluginMgr scans the plugin directory, executes all modules it finds there (be careful...), and looks for Plugin subclasses. Then creates an instance of each Plugin subclass.

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to