About a plugin framework!

Calvin Spealman calvin at ironfroggy.com
Tue Jun 8 06:46:49 EDT 2004


Simon Roses Femerling wrote:

> 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.

Its simple: you're getting the name of the class, a string, and trying to
instansiate it. Try something more like this:

...
cn = pxml.GetClassname()
cn = getattr(module_name, cn)
myobj = cn() # Now cn should work, it should be the Test class
myobj.Msg()


You might want to reconsider this, though. Why the XML? Couldn't you just as
easily traverse the objects in the module, via the dir function, to find
all the resident classes? Unless, of course, you have some specific reason,
but if its pure-python, then I don't think you need the XML. 





More information about the Python-list mailing list