[Zope-dev] More useful CatalogAware class?

Michel Pelletier michel@digicool.com
Mon, 12 Jun 2000 13:12:33 -0700


Amos and I and the guys at Activestate were discussing cataloging at a
recent training sessions and we came up with the following possible
successor to CatalogAware.

The idea is that a CatalogAware object looks for two methods that return
sequecnes of Catalogs that many be interested in this object.  The first
is class defined method 'getClassCatalogs', the second is an instance
defined method 'getLocationCatalogs' that the instance can get from its
classes, its instance dict, or acquisition.

The idea is that you provide methods that define the policy for finding
Catalogs.  The default class defined 'getClassCatalogs' is identical
behavior to the old CatalogAware, but you can and probably will override
it.

Thoughts?

-Michel

"""
This is a candidate successor to the CatalogAware class.
"""

class CatalogAware:

    meta_type = 'CatalogAware'
    default_catalog = 'Catalog'

    def getClassCatalogs(self):
        """ You want to over ride this method if you don't like the
        default policy of acquire a Catalog named 'Catalog' """
        if hasattr(self, self.default_catalog):
            return [getattr(self, self.default_catalog)]

    def manage_afterAdd(self, item, container):
        self.catalog_object()
        for object in self.objectValues():
            try: s=object._p_changed
            except: s=0
            object.manage_afterAdd(item, container)
            if s is None: object._p_deactivate()

    def manage_afterClone(self, item):
        self.catalog_object()
        for object in self.objectValues():
            try: s=object._p_changed
            except: s=0
            object.manage_afterClone(item)
            if s is None: object._p_deactivate()

    def manage_beforeDelete(self, item, container):
        self.uncatalog_object()
        for object in self.objectValues():
            try: s=object._p_changed
            except: s=0
            object.manage_beforeDelete(item, container)
            if s is None: object._p_deactivate()

    def get_catalogs(self):
        cats = []
        # first, try and call a class defined method to get
        # catalogs... we don't want to acquire...
        if hasattr(self.aq_base, getClassCatalogs):
            for cat in self.getClassCatalogs():
                if cat not in cats:
                    cats.append(cat)
       
        if hasattr(self, getLocationCatalogs):
            # next, try and call a method that can be class defined,
            # instance defined, or acquired.
            for cat in self.getLocationCatalogs():
                if cat not in cats:
                    cats.append(cat)
        return cats

    def catalog_object(self):
        """ Call this to catalog yourself """
        for catalog in self.get_catalogs():
            catalog.catalog_object(self, self.getPhysicalPath())

    def uncatalog_object(self):
        """ Call this to uncatalog yourself """
        for catalog in self.get_catalogs():
            catalog.uncatalog_object(self.getPhysicalPath())        

    def recatalog_object(self):
        self.uncatalog_object()
        self.catalog_object()