[TransWarp] Component hierarchy traversal

Radek Kanovsky rk at dat.cz
Wed Oct 1 11:48:59 EDT 2003


Hi,

I have started to implement traverseComponents(comp) generator, that
traverses given component hierarchy and return one component-path pair
in one iteration. Traversal order is not important. There are some
components dispersed in hierarchy that implements some interface. I need
to find them and call its methods. But I am not sure if my solution is
correct or if it is ever possible doing it such way. My first prototype
is here:

    def traverseComponents (comp, reg=None) :
        if reg is None :
            reg = {}
        yield (comp, binding.getComponentPath(comp))
        reg[comp] = 1
        for a,d in comp.__class__.__all_descriptors__.items() :
            if not isinstance(d, binding.Attribute) :
                continue
            i = comp._getBinding(a)
            try :
                if i in reg :
                    continue
            except TypeError :
                continue
            if not isinstance(i, binding.Component) :
                continue
            if not hasattr(comp.__class__, '__all_descriptors__') :
                return
            for x in traverseComponents(i, reg) :
                yield x
        return

    # Usage
    for c,p in traverseComponents(root) :
        a = adapt(c, interfaces.IResourceComponent, default=None)
        if a is None :
            continue
        a.CreateResource()


Another solution that i have tried was global registry of such
components. Every interfaces.IResourceComponent class has attribute
that is computed upon assembly. Side effect of this computation is that
instances register itself in global registry. Then I have all such
components in registry and I have no need for traversal function. But
some bindings don't have uponAssembly flag set and I don't know how to
force assembling from given root. root.uponAssembly() works only if
whole hierarchy is binded with uponAssembly flag.

Is there some pattern how to solve this?

Regards,

Radek Kanovsky



More information about the PEAK mailing list