About a plugin framework!

Discussion in 'Python' started by Simon Roses Femerling, Jun 8, 2004.

  1. 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
    Simon Roses Femerling, Jun 8, 2004
    #1
    1. Advertising

  2. Simon Roses Femerling

    Guyon Morée Guest

    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

    ==========================================================
    "Simon Roses Femerling" <> wrote in message
    news:...
    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
    Guyon Morée, Jun 8, 2004
    #2
    1. Advertising

  3. Simon Roses Femerling

    Peter Otten Guest

    Simon Roses Femerling wrote:

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

    >>> class Test: pass

    ....
    >>> Test()

    <__main__.Test instance at 0x40296acc>
    >>> "Test"()

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: 'str' object is not callable
    >>> u"Test"()

    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:

    >>> getattr(__main__, u"Test")()

    <__main__.Test instance at 0x40296bcc>


    > 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
    Peter Otten, Jun 8, 2004
    #3
  4. Simon Roses Femerling

    j_mckitrick Guest

    > fp = open(f)
    > exec(fp) in globals()
    > str = xml


    What do the second and third lines do here?

    jonathon
    j_mckitrick, Jun 8, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PilotYid
    Replies:
    1
    Views:
    603
    Andrew Thompson
    Oct 31, 2005
  2. Marcin Cenkier
    Replies:
    1
    Views:
    5,385
    Marcin Cenkier
    Apr 12, 2006
  3. William

    C++ plugin framework

    William, Apr 25, 2004, in forum: C++
    Replies:
    1
    Views:
    3,228
    Claudio Puviani
    Apr 25, 2004
  4. Jimmy
    Replies:
    0
    Views:
    494
    Jimmy
    Mar 15, 2007
  5. Replies:
    1
    Views:
    711
Loading...

Share This Page