About a plugin framework!

  • Thread starter Simon Roses Femerling
  • Start date
S

Simon Roses Femerling

Dear pythonnians :)

Hopeful somebody can help me about implementing plugin support.

I'm working on a python/wxpython app that needs plugin support. My problem is:

The way my app works is a python module (plugin) that contains (imbedded) XML defining
the classname and some extra information and the app will be load the module using the classname:

Example:

------ mymodule.py ----

__xml__ ="""
<data>
<classname>Test</classname>
</data>
"""

class Test:
def Msg(self):
print "Hello"

---------------------------------

--- MyApp.py -----------

fp = open(f)
exec(fp) in globals()
str = __xml__
pxml = parsexml.ParseXML()
pxml.BeginParse(str)
cn = pxml.GetClassname()
mymod = cn() <-- Here is the error
mymod.Msg()

----------------------------------

The error is:

Traceback (most recent call last):
File "blackout.py", line 503, in onAttackMod
mymod = cn()
TypeError: 'unicode' object is not callable

Any suggestions ? How can achieve this ?
Each module (plugin) can have a different class name defined in the XML data.

Besides this anyone have suggestions for a better plugin framework ? Maybe using imp module to dynamically import
modules ?

Any examples ?

Thx for any help!

Sincerely

SRF
 
G

Guyon Morée

Hi,

change: cn = pxml.GetClassname()
into: cn = globals()[pxml.GetClassname()]


your line returns a string, the name of your class, which is not callable
the correct line looks up the name in your globals() dict and returns a
callable.


cheers,
guyon

==========================================================
Dear pythonnians :)

Hopeful somebody can help me about implementing plugin support.

I'm working on a python/wxpython app that needs plugin support. My problem
is:

The way my app works is a python module (plugin) that contains (imbedded)
XML defining
the classname and some extra information and the app will be load the module
using the classname:

Example:

------ mymodule.py ----

__xml__ ="""
<data>
<classname>Test</classname>
</data>
"""

class Test:
def Msg(self):
print "Hello"

---------------------------------

--- MyApp.py -----------

fp = open(f)
exec(fp) in globals()
str = __xml__
pxml = parsexml.ParseXML()
pxml.BeginParse(str)
cn = pxml.GetClassname()
mymod = cn() <-- Here is the error
mymod.Msg()

----------------------------------

The error is:

Traceback (most recent call last):
File "blackout.py", line 503, in onAttackMod
mymod = cn()
TypeError: 'unicode' object is not callable

Any suggestions ? How can achieve this ?
Each module (plugin) can have a different class name defined in the XML
data.

Besides this anyone have suggestions for a better plugin framework ? Maybe
using imp module to dynamically import
modules ?

Any examples ?

Thx for any help!

Sincerely

SRF
 
P

Peter Otten

Simon said:
The way my app works is a python module (plugin) that contains (imbedded)
XML defining the classname and some extra information and the app will be
load the module using the classname:

Example:

------ mymodule.py ----

__xml__ ="""
<data>
<classname>Test</classname>
</data>
"""

The xml stuff unnecessarily complicates things. Use a list with the names of
exposed classes

__all__ = ["Test"]

If there is only one I'd rather follow a naming convention, i. e. the class
name "Test" would be mandatory.

class Test:
def Msg(self):
print "Hello"

---------------------------------

--- MyApp.py -----------

fp = open(f)
exec(fp) in globals()
str = __xml__
pxml = parsexml.ParseXML()
pxml.BeginParse(str)
cn = pxml.GetClassname()
mymod = cn() <-- Here is the error

Which of the following would you expect to succed?
Traceback (most recent call last):
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'unicode' object is not callable

So the name is not enough - unicode or not. You have to get hold of the
class object, but that is no big deal:
mymod.Msg()

----------------------------------

The error is:

Traceback (most recent call last):
File "blackout.py", line 503, in onAttackMod
mymod = cn()
TypeError: 'unicode' object is not callable

Any suggestions ? How can achieve this ?
Each module (plugin) can have a different class name defined in the XML
data.

Why?
Besides this anyone have suggestions for a better plugin framework ? Maybe
using imp module to dynamically import modules ?

If a plugin is in the python path, just do

plugin = __import__(pluginName)

and then retrieve the class

pluginClass = getattr(plugin, plugin.__all__[0])

Depending on whether you allow multiple plugins/classes at a time and how
you want to access them from your code you'd need registries (basically
python dictionaries) for plugin modules and clases.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top