[Zope] How can a method know if it was acquired?
Dieter Maurer
dieter@handshake.de
Mon, 7 Jul 2003 19:55:01 +0200
Itai Tavor wrote at 2003-7-7 20:00 +1000:
> I got a folderish python product with the following method:
>
> def SKU(self):
> """ Return our SKU for the catalog """
> return self.sku
>
> The problem is that when objects contained in instances of this product
> are cataloged, they acquire the SKU method, so they also get the sku
> property. That is bad. So what I need is something like this:
>
> def SKU(self):
> """ Return our SKU for the catalog """
> if (I was acquired):
> return None
> else:
> return self.sku
>
> But I can't figure out how to do that. Help?
I fear this is impossible and would not be the right way either.
The right way (in my view) would be to make indexes manageable
and allow to set 3 boolean properties:
ALLOW_ACQUISITION= 1 # to indicate that attributes may be acquired
HIDE_EXCEPTIONS= 1 # silently ignore (most) exceptions during calls
ALLOW_CALL= 1 # indicate that attributes are called when callable
I have implemented an "IndexBase" class that determines the value
of object "obj" for index "id" in the following way:
def getValue(self,obj,default= None):
'''the value of *obj* with respect to this index.'''
marker= self
# determine base value
id= self.id
if self.ALLOW_ACQUISITION:
val= getattr(obj,id,default)
elif hasattr(aq_base(obj), id): val= getattr(obj,id)
else: val= default
if safe_callable(val):
if not self.ALLOW_CALL: return default
try: val= val()
except EXCEPTIONS_NOT_TO_BE_CAUGHT: raise
except:
if self.HIDE_EXCEPTIONS: return default
raise
return val
Dieter