[Zope-CMF] ZClass content type question...

Tres Seaver tseaver@zope.com
01 Sep 2002 09:02:31 -0400


On Sat, 2002-08-31 at 18:29, sean.upton@uniontrib.com wrote:

> Perhaps this is the wrong list for this question... I am currently using
> ZClasses to extend some Python products I have written for use as CMF
> content types.  I am subclassing my content class (which provides its own
> DublinCore and other metadata implementations) and also PortalContent.  My
> concern is the places where there are method name/implementation overlaps
> between these two superclasses.
> 
> Normally, outside of using a ZClass, I would just override the method, and
> call the method in the appropriate superclass.  I thought I might be able to
> do this by using an external method inside my ZClass, but I'm not sure of
> the best way to get a reference to the superclasses.  I've tried:
> 	myClass = self.__class__
> 	bases = myClass.__bases__
> 	...which will get something like this:
> (<extension class ZClasses.ZClass.PersistentClass at 851a680>, <extension
> class Products.ZXPContent.NITFCoreImpl.NITFCoreImpl at 8c32c28>, <extension
> class Products.CMFCore.PortalContent.PortalContent at 8683dd0>, <extension
> class OFS.SimpleItem.SimpleItem at 8471170>)
> 
> If I have my external method do something like this, I assume I can do this:
> 
> import string
> def Date(self):
> 	myClass = self.__class__
> 	bases = myClass.__bases__
> 	for base in bases:
> 		if string.find(base.__name__, 'NITFCoreImpl') != -1:
> 			useThisBase = base
> 			break
> 	return useThisBase.Date(self)
> 
> I assume that I can do this wherever I need to, but is this the best
> approach to doing this, or is there something more simple that I'm not aware
> of - or simply not seeing?

Another option would be to create a "protected" interface in the
NITFCoreImpl class, intended to ease such lookups for extenders, e.g.::

  class NITFCoreImpl:

      #....

      security.declareProtected( View, 'Date' )
      def Date( self ):
          """ Pick the date out of NITF's PubDate element."""
          #....

      # Alias for use by subclasses when extending Date.
      security.declareProtected( View, 'NITF_Date' )
      NITF_Date = Date

Actually, come to think of it, I might define the "main" method as
'NITF_Date', and then alias that to 'Date'.

This would allow you to define Date in the ZClass as a PythonScript,
since it doesn't require grubbing around in __class__.__bases__.

Tres.
-- 
===============================================================
Tres Seaver                                tseaver@zope.com
Zope Corporation      "Zope Dealers"       http://www.zope.com