[PEAK] Adapting based on property

Peter Parente parente at cs.unc.edu
Wed Jun 29 07:12:00 EDT 2005


Hi,

I'd like to use pyprotocols to adapt objects returned by a third party  
library to a set
of interfaces specified in my system. The external library returns  
instances of type
Accessible via a number of methods. The Accessible object is a client side  
proxy for
some server side object.

The Accessible object is a generic interface to a slew of different server  
side objects.
The Role attribute of the accessible object says exactly what kind of  
object it is
representing on the server side and, thus, what its various values mean.  
For instance,
the Value property of an Accessible object with a Role of 'list' is the  
length of the
list while the Value property of an Accessible object with a Role of  
'tree' is the
number of levels in the tree.

What's I'd like to do is adapt instances of the generic Accessible objects  
to more
specific interfaces based on the Role attribute. For example, I'd like to  
have IList
protocol with a method of getLength. Then I'd like to have an adapters like
AccessibleListViewAsList and AccessibleComboboxAsList that implement the  
IList interface
for various list-like objects.

Here's my problem.

In the pyprotocols documentation, I only see a way to declare a class as  
an adapter for
a type of object. But in this case, everything is an Accessible and so  
declaring two
adapters causes an "ambiguous adapter choice" error:

class AccessibleListViewAsList(Adapter):
	advise(instancesProvide=[IList], asAdapterForTypes=[Accessible])

	def getLength(self):
		return self.subject.Value

class AccessibleComboboxAsList(Adapter):
	advise(instancesProvide=[IList], asAdapterForTypes=[Accessible])

	def getLength(self):
		return self.subject.child['list'].Value

Notice the implementations need to be different so a single adapter will  
not suffice.

I'm guessing what I really need is an adapter factory that does something  
like the following pseudocode:

def accessibleAdapterFactory(obj):
	# get object role
	role = obj.Role
	# look for adapters for given role and type
	return protocol.registry.findAdapterFor(role=role, type=type(obj))

Is such a thing possible with pyprotocols? Is there another solution?

Thanks,
Pete



More information about the PEAK mailing list