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 ?!?!
Thanks for the answers and help, but I shouldn’t try to re-invent the wheel.
This framework is great:
http://yapsy.sourceforge.net/
Works like a charm.
(It did help to learn more about Python)