About a plugin framework!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Roses Femerling

    About a plugin framework!

    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.ParseX ML()
    pxml.BeginParse (str)
    cn = pxml.GetClassna me()
    mymod = cn() <-- Here is the error
    mymod.Msg()

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

    The error is:

    Traceback (most recent call last):
    File "blackout.p y", 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

    #2
    Re: About a plugin framework!

    Hi,

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


    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" <simonroses@gra nisla.com> wrote in message
    news:mailman.69 3.1086683764.69 49.python-list@python.org ...
    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.ParseX ML()
    pxml.BeginParse (str)
    cn = pxml.GetClassna me()
    mymod = cn() <-- Here is the error
    mymod.Msg()

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

    The error is:

    Traceback (most recent call last):
    File "blackout.p y", 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


    Comment

    • Peter Otten

      #3
      Re: About a plugin framework!

      Simon Roses Femerling wrote:
      [color=blue]
      > 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>
      > """[/color]

      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.

      [color=blue]
      > class Test:
      > def Msg(self):
      > print "Hello"
      >
      > ---------------------------------
      >
      > --- MyApp.py -----------
      >
      > fp = open(f)
      > exec(fp) in globals()
      > str = __xml__
      > pxml = parsexml.ParseX ML()
      > pxml.BeginParse (str)
      > cn = pxml.GetClassna me()
      > mymod = cn() <-- Here is the error[/color]

      Which of the following would you expect to succed?
      [color=blue][color=green][color=darkred]
      >>> class Test: pass[/color][/color][/color]
      ....[color=blue][color=green][color=darkred]
      >>> Test()[/color][/color][/color]
      <__main__.Tes t instance at 0x40296acc>[color=blue][color=green][color=darkred]
      >>> "Test"()[/color][/color][/color]
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      TypeError: 'str' object is not callable[color=blue][color=green][color=darkred]
      >>> u"Test"()[/color][/color][/color]
      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:
      [color=blue][color=green][color=darkred]
      >>> getattr(__main_ _, u"Test")()[/color][/color][/color]
      <__main__.Tes t instance at 0x40296bcc>

      [color=blue]
      > mymod.Msg()
      >
      > ----------------------------------
      >
      > The error is:
      >
      > Traceback (most recent call last):
      > File "blackout.p y", 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.[/color]

      Why?
      [color=blue]
      > Besides this anyone have suggestions for a better plugin framework ? Maybe
      > using imp module to dynamically import modules ?[/color]

      If a plugin is in the python path, just do

      plugin = __import__(plug inName)

      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





      Comment

      • j_mckitrick

        #4
        Re: About a plugin framework!

        > fp = open(f)[color=blue]
        > exec(fp) in globals()
        > str = xml[/color]

        What do the second and third lines do here?

        jonathon

        Comment

        Working...