Python plugins like Minecraft Bukkit
Question: I've been reading this:  
<http://lkubuntu.wordpress.com/2012/10/02/writing-a-python-plugin-api/>  
I'm trying to do something like Minecraft help system.  
Let's say I have my main module, and the help commands are:  

> help  
>  test1  
>  test2  
>

And then, after loading the plugin, I would have the same set, plus the ones
that the plugin has available.  
Also, when processing the commands, how can I do, to distinguish the ones from
internal program to the ones from plugins?!  
So far, I've come up with this:  

    
    
    import imp
    import os
    
    PluginFolder = "./plugins"
    MainModule = "__init__"
    
    def getPlugins():
        plugins = []
        possibleplugins = os.listdir(PluginFolder)
        for i in possibleplugins:
            location = os.path.join(PluginFolder,i)
            if not os.path.isdir(location) or not MainModule + ".py" in os.listdir(location):
                continue
            info = imp.find_module(MainModule, [location])
            plugins.append({"name":i, "info": info})
        return plugins
    
    def loadPlugin(plugin):
        return imp.load_module(MainModule, *plugin["info"])
    
    disponiveis = []
    for i in getPlugins():
        print("Loading plugin "+ i["name"])
        plugin = loadPlugin(i)
        plugin.run()
        disponiveis.append(i["name"])
    
    while 1:
        foo = raw_input(":")
        if foo == 'quit':
            break;
        elif foo in disponiveis:
            print "ok"
        else:
            continue
    

Not much from the original example :|  
My BIG problem is that cycle where it loads all the plugins.  
Currently I have 2 plugins, _hello_ and _testing_. How can I do to have
something like this:

    
    
    send_command(plugin_name, action)
    

Also, the if/elif is kinda lame... Available commands should come from the
plugin.  
Using a _dict_ maybe?!?! And then when loading the plugin, it would add the
aditional commands to that dict ?!?!


Answer: As the last line of the blog entry you cite says "Now, of course, this plugin
API is very simple, and can easily (and should) be extended for your program’s
needs."

The only "api" defined in the sample code is a `plugin.run()` method that
doesn't return anything. So at the very least from your question it sounds
like you need one the describes the available "commands" the loaded plugin
has. One way to do that would be to define another method that all plugins
must provide called `available_commands()` which returns a dictionary of
commands where the keys are all command names and the values are corresponding
functions that do them.

However that could need to be extended with some mechanism to provide command
argument information back to the client application as well. How this might be
done is limited only by your imagination to either invent something yourself
or perhaps learn how others have done it, perhaps by asking a more specific
question here on StackOverflow.

