Exemple #1
0
    def addImpliedProtocol ( self, proto, adapter = NO_ADAPTER_NEEDED,
                                   depth = 1 ):

        self.__lock.acquire()
        try:
            key = mkRef( proto )
            if not updateWithSimplestAdapter(
                self.__implies, key, adapter, depth
            ):
                return self.__implies[ key ][ 0 ]
        finally:
            self.__lock.release()

        # Always register implied protocol with classes, because they should
        # know if we break the implication link between two protocols
        for klass, ( baseAdapter, d ) in self.__adapters.items():
            api.declareAdapterForType(
                proto, composeAdapters( baseAdapter, self, adapter ), klass,
                depth + d
            )

        if self.__listeners:
            for listener in self.__listeners.keys():    # Must use keys()!
                listener.newProtocolImplied( self, proto, adapter, depth )

        return adapter
Exemple #2
0
    def registerImplementation ( self, klass, adapter = NO_ADAPTER_NEEDED,
                                       depth = 1 ):

        self.__lock.acquire()
        try:
            if not updateWithSimplestAdapter(
                self.__adapters, klass, adapter, depth
            ):
                return self.__adapters[ klass ][ 0 ]
        finally:
            self.__lock.release()

        if adapter is DOES_NOT_SUPPORT:
            # Don't register non-support with implied protocols, because
            # "X implies Y" and "not X" doesn't imply "not Y".  In effect,
            # explicitly registering DOES_NOT_SUPPORT for a type is just a
            # way to "disinherit" a superclass' claim to support something.
            return adapter

        for proto, ( extender, d ) in self.getImpliedProtocols():
            api.declareAdapterForType(
                proto, composeAdapters( adapter, self, extender ), klass,
                depth + d
            )

        return adapter
Exemple #3
0
def __protocolForType ( key ):

    """Recursive implementation of protocolForType; assumes standardized key"""

    __registryLock.acquire()

    try:
        try:
            return registry[ key ]

        except KeyError:
            baseType, methods, implicit = key

            if implicit:
                proto = WeakSubset( baseType, methods )
            else:
                proto = TypeSubset( baseType, methods )

            registry[ key ] = proto

    finally:
        __registryLock.release()

    # declare that proto implies all subset-method protocols
    if len( methods ) > 1:
        for method in methods:
            subset = tuple( [ m for m in methods if m != method ] )
            implied = __protocolForType( ( baseType, subset, implicit ) )
            declareAdapterForProtocol( implied, NO_ADAPTER_NEEDED, proto )

    # declare that explicit form implies implicit form
    if implicit:
        impliedBy = __protocolForType( ( baseType, methods, False ) )
        declareAdapterForProtocol( proto, NO_ADAPTER_NEEDED, impliedBy )

    # declare that baseType implements this protocol
    declareAdapterForType( proto, NO_ADAPTER_NEEDED, baseType )
    return proto

#-- EOF ------------------------------------------------------------------------
Exemple #4
0
def __protocolForType(key):
    """Recursive implementation of protocolForType; assumes standardized key"""

    __registryLock.acquire()

    try:
        try:
            return registry[key]

        except KeyError:
            baseType, methods, implicit = key

            if implicit:
                proto = WeakSubset(baseType, methods)
            else:
                proto = TypeSubset(baseType, methods)

            registry[key] = proto

    finally:
        __registryLock.release()

    # declare that proto implies all subset-method protocols
    if len(methods) > 1:
        for method in methods:
            subset = tuple([m for m in methods if m != method])
            implied = __protocolForType((baseType, subset, implicit))
            declareAdapterForProtocol(implied, NO_ADAPTER_NEEDED, proto)

    # declare that explicit form implies implicit form
    if implicit:
        impliedBy = __protocolForType((baseType, methods, False))
        declareAdapterForProtocol(proto, NO_ADAPTER_NEEDED, impliedBy)

    # declare that baseType implements this protocol
    declareAdapterForType(proto, NO_ADAPTER_NEEDED, baseType)
    return proto
Exemple #5
0
    def addImpliedProtocol(self,proto,adapter=NO_ADAPTER_NEEDED,depth=1):

        self.__lock.acquire()
        try:
            key = mkRef(proto)
            if not updateWithSimplestAdapter(
                self.__implies, key, adapter, depth
            ):
                return self.__implies[key][0]
        finally:
            self.__lock.release()

        # Always register implied protocol with classes, because they should
        # know if we break the implication link between two protocols
        for klass,(baseAdapter,d) in self.__adapters.items():
            api.declareAdapterForType(
                proto,composeAdapters(baseAdapter,self,adapter),klass,depth+d
            )

        if self.__listeners:
            for listener in self.__listeners.keys():    # Must use keys()!
                listener.newProtocolImplied(self, proto, adapter, depth)

        return adapter
Exemple #6
0
    def registerImplementation(self,klass,adapter=NO_ADAPTER_NEEDED,depth=1):

        self.__lock.acquire()
        try:
            if not updateWithSimplestAdapter(
                self.__adapters,klass,adapter,depth
            ):
                return self.__adapters[klass][0]
        finally:
            self.__lock.release()

        if adapter is DOES_NOT_SUPPORT:
            # Don't register non-support with implied protocols, because
            # "X implies Y" and "not X" doesn't imply "not Y".  In effect,
            # explicitly registering DOES_NOT_SUPPORT for a type is just a
            # way to "disinherit" a superclass' claim to support something.
            return adapter

        for proto, (extender,d) in self.getImpliedProtocols():
            api.declareAdapterForType(
                proto, composeAdapters(adapter,self,extender), klass, depth+d
            )

        return adapter