[wxPython-users] Re: XRC and subclassing ... prevents app from closing?

Robin Dunn robin at alldunn.com
Tue Mar 20 13:59:16 PDT 2007


Don Dwiggins wrote:
>>> - Is there a way to force XRC to simply use a class as base class 
>>> instead of it importing a module by itself?
>>
>> No.  It has to have a reference to the class object somehow, and the 
>> way that is done in Python is to import the module and get it from there.
> 
> Random synaptic spark: could the XRC loader be enhanced to take an 
> optional argument consisting of a dictionary of classes to be used to 
> resolve the subclass references (sort of like the globals and locals 
> arguments to eval)?
> 
> 

The entire Python subclass factory is pasted below.  You can easily make 
your own factories that do whatever you want.

#----------------------------------------------------------------------------
#  Create a factory for handling the subclass property of the object tag.


def _my_import(name):
     try:
         mod = __import__(name)
     except ImportError:
         import traceback
         print traceback.format_exc()
         raise
     components = name.split('.')
     for comp in components[1:]:
         mod = getattr(mod, comp)
     return mod


class XmlSubclassFactory_Python(XmlSubclassFactory):
     def __init__(self):
         XmlSubclassFactory.__init__(self)

     def Create(self, className):
         assert className.find('.') != -1, "Module name must be specified!"
         mname = className[:className.rfind('.')]
         cname = className[className.rfind('.')+1:]
         module = _my_import(mname)
         klass = getattr(module, cname)
         inst = klass()
         return inst


XmlResource_AddSubclassFactory(XmlSubclassFactory_Python())

#----------------------------------------------------------------------------

-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list