Пример #1
0
    def __init__(self, baseProtocol, context = None):

        self.baseProtocol = baseProtocol
        self.context = context

        # Note: Protocol is a ``classic'' class, so we don't use super()
        Protocol.__init__(self)

        api.declareAdapterForProtocol(self,NO_ADAPTER_NEEDED,baseProtocol)
Пример #2
0
def sequenceOf ( baseProtocol ):

    """Return a protocol representing an sequence of a given base protocol"""

    key = ( sequenceOf, baseProtocol )

    __registryLock.acquire()

    try:
        try:
            return registry[ key ]

        except KeyError:
            proto = registry[ key ] = SequenceProtocol( baseProtocol )
            declareAdapterForProtocol(
                proto, lambda o: ADAPT_SEQUENCE( o, proto ), IBasicSequence
            )

            return proto
    finally:
        __registryLock.release()
Пример #3
0
def sequenceOf(baseProtocol):
    """Return a protocol representing an sequence of a given base protocol"""

    key = (sequenceOf, baseProtocol)

    __registryLock.acquire()

    try:

        try:
            return registry[key]

        except KeyError:
            proto = registry[key] = SequenceProtocol(baseProtocol)
            declareAdapterForProtocol(proto,
                                      lambda o: ADAPT_SEQUENCE(o, proto),
                                      IBasicSequence)
            return proto

    finally:
        __registryLock.release()
Пример #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

#-- EOF ------------------------------------------------------------------------
Пример #5
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