"""
attribute_cleaner

A class to remove an attribute from an object at the end of a 
transaction. This is useful for attributes that hold
transaction-specific caches.

Normal operation is::

    if self._v_cache is None:
        self._v_cache = create_data_to_cache()
        attribute_cleaner(self,'_v_cache')
"""

class attribute_cleaner:
    def __init__(self,client,attr):
        self.client = client
        self.attr = attr
        get_transaction().register(self)

    def sortKey(self):
        return repr(self)

    def ClearCache(self,*args):
        try:
            delattr(self.client, self.attr)
        except AttributeError:
            pass
        except KeyError:
            pass

    tpc_finish = tpc_abort = abort = abort_sub = ClearCache

    def tpc_begin(self,transaction,subtransaction=None): pass
    def commit(self,object,transaction): pass
    def tpc_vote(self,transaction):      pass
    def commit_sub(self,transaction):    pass

