I'm working on an IM client in Python and my idea was to make it fully modular. It seemed to me that the obvious approach is to make all plugins derive from a Plugin class (with ProtocolPlugin, UIPlugin subclasses) and to be loaded on program startup by yet another object, the PluginManager. It works as following: each plugin .py file is executed (via execfile()) by the PluginManager, creates an object deriving from the Plugin class and calls registerPlugin method in the PluginManager with that object:

the main file:

class PluginManager:
    def registerPlugin(self, newplugin):
        newplugin.UIObject = self.UI
        self.Plugins[newplugin.name] = newplugin
        
        if isinstance(newplugin, ThreadedPlugin):
            self.Protocols[newplugin.name].start()

        self.triggerPluginRegistered()

    def loadPlugin(pluginfile):
        execfile(pluginfile)

the plugin file:

class someProtocol(ProtocolPlugin): 
    pass # self refers to the PluginManager since the code is ran inside it 

self.registerProtocol(someProtocol())

Such design turns out not only to be rather clumsy but also leads to all sorts of problems and conundrums (eg. imports in the plugin files work strangely; I have to include a reference to the UI object in each plugin etc.). Are there any obvious improvements/simplifications? I'd rather keep the flexibility provided by modular design (easy to implement different user interfaces etc.)

TIA,
Grzegorz Slodkowicz

Is this part of your doctorial theses in computer science?

Is this part of your doctorial theses in computer science?

No it's not. I do my homework myself ;)

each plugin .py file is executed (via execfile()) by the PluginManager

I am not 100% sure what you are doing, but wouldn't you want the PluginManager to be an inherited class. It should also have methods to keep track of all plugins created, which "self.Plugins[newplugin.name] = newplugin" should provide, if that is your problem, and as well should be able to pass them to any other object asking for them. If you have several separate classes calling the plugin manager, then you want a wrapper program of some sort that can pass the identity of the single PluginManager instance to those classes, so they are all using the same instance. Info can also be stored on disk in a single database that any process can access, but that is likely overkill. If I understand what you are trying to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.