[Zope3-Users] factories for an object

Lorenzo Gil Sanchez lgs at sicem.biz
Fri Jun 1 16:01:03 EDT 2007


Hi,

after looking the zope.component API I realized that there is a function
to get all the factories for an interface but there is no such thing for
an object.

Usually an object provides one or more interfaces, and one interface can
have a lot of factories registered for it. Merging this information I
want the factories that implements *all* the interfaces an object
provides.

Basically I want to know how (e.g. what factory was used) an object was
created by introspection.

I wrote this small function:

def getFactoriesForObject(obj):
    factories = []
    ifaces = tuple(zope.interface.providedBy(obj))
    for iface in ifaces:
        for name, factory in zope.component.getFactoriesFor(iface):
            factoryIfaces = tuple(factory.getInterfaces())
            if ifaces == factoryIfaces and name not in factories:
                factories.append(name)
    return factories

I works quite well for content types objects (the ones I'm interested
in) but I'm curious about why this function return two factories in many
cases:

>>> folder = zope.app.folder.folder.Folder()
>>> getFactoriesForObject(folder)
[u'zope.app.content.Folder',
'BrowserAdd__zope.app.folder.folder.Folder']

Is that because the <browser:addMenuItem> ZCML directive adding always a
factory for its class attribute?

I'm curious about what zope gurus thing about my function and if anybody
think if could be useful in zope.component.

Best regards

Lorenzo



More information about the Zope3-users mailing list