Ejemplo n.º 1
0
    def __init__(self,
                 name,
                 bases=(),
                 attrs=None,
                 __doc__=None,
                 __module__=None):

        if __module__ is None:
            if attrs is not None and attrs.has_key('__module__'):
                __module__ = attrs['__module__']
                del attrs['__module__']
            else:
                try:
                    # Figure out what module defined the interface.
                    # This is how cPython figures out the module of
                    # a class, but of course it does it in C. :-/
                    __module__ = currentframe().f_back.f_globals['__name__']
                except (AttributeError, KeyError):
                    pass
        self.__module__ = __module__

        for b in bases:
            if not isInstance(b, Interface):
                raise TypeError, 'Expected base interfaces'
        self.__bases__ = bases

        if attrs is None: attrs = {}
        if attrs.has_key('__doc__'):
            if __doc__ is None: __doc__ = attrs['__doc__']
            del attrs['__doc__']

        if __doc__ is not None:
            self.__doc__ = __doc__
        else:
            self.__doc__ = ""

        Element.__init__(self, name, __doc__)

        for k, v in attrs.items():
            if isInstance(v, Attribute):
                v.interface = name
                if not v.__name__:
                    v.__name__ = k
            elif isinstance(v, FunctionType):
                attrs[k] = fromFunction(v, name)
            else:
                raise Exceptions.InvalidInterface("Concrete attribute, %s" % k)

        self.__attrs = attrs
Ejemplo n.º 2
0
def _verify(iface, candidate, tentative=0, vtype=None):
    """

    Verify that 'candidate' might correctly implements 'iface'.
    This involves:

      o Making sure the candidate defines all the necessary methods

      o Making sure the methods have the correct signature

      o Making sure the candidate asserts that it implements the interface

    Note that this isn't the same as verifying that the class does
    implement the interface.

    If optional tentative is true, suppress the "is implemented by" test.
    """

    if vtype is "c":
        tester = iface.isImplementedByInstancesOf
    else:
        tester = iface.isImplementedBy

    if not tentative and not tester(candidate):
        raise DoesNotImplement(iface)

    for n, d in iface.namesAndDescriptions(1):
        if not hasattr(candidate, n):
            raise BrokenImplementation(iface, n)

        attr = getattr(candidate, n)
        if type(attr) is FunctionType:
            # should never get here
            meth = fromFunction(attr, n)
        elif type(attr) in MethodTypes:
            meth = fromMethod(attr, n)
        else:
            continue  # must be an attribute...

        d = d.getSignatureInfo()
        meth = meth.getSignatureInfo()

        mess = _incompat(d, meth)
        if mess:
            raise BrokenMethodImplementation(n, mess)

    return 1
Ejemplo n.º 3
0
def _verify(iface, candidate, tentative=0, vtype=None):
    """

    Verify that 'candidate' might correctly implements 'iface'.
    This involves:

      o Making sure the candidate defines all the necessary methods

      o Making sure the methods have the correct signature

      o Making sure the candidate asserts that it implements the interface

    Note that this isn't the same as verifying that the class does
    implement the interface.

    If optional tentative is true, suppress the "is implemented by" test.
    """

    if vtype is 'c':
        tester = iface.isImplementedByInstancesOf
    else:
        tester = iface.isImplementedBy

    if not tentative and not tester(candidate):
        raise DoesNotImplement(iface)

    for n, d in iface.namesAndDescriptions(1):
        if not hasattr(candidate, n):
            raise BrokenImplementation(iface, n)

        attr = getattr(candidate, n)
        if type(attr) is FunctionType:
            # should never get here
            meth = fromFunction(attr, n)
        elif type(attr) in MethodTypes:
            meth = fromMethod(attr, n)
        else:
            continue  # must be an attribute...

        d = d.getSignatureInfo()
        meth = meth.getSignatureInfo()

        mess = _incompat(d, meth)
        if mess:
            raise BrokenMethodImplementation(n, mess)

    return 1
Ejemplo n.º 4
0
    def __init__(self, name, bases=(), attrs=None, __doc__=None,
                 __module__=None):

        if __module__ is None:
            if attrs is not None and attrs.has_key('__module__'):
                __module__ = attrs['__module__']
                del attrs['__module__']
            else:
                try:
                    # Figure out what module defined the interface.
                    # This is how cPython figures out the module of
                    # a class, but of course it does it in C. :-/
                    __module__ = currentframe().f_back.f_globals['__name__']
                except (AttributeError, KeyError):
                    pass
        self.__module__ = __module__

        for b in bases:
            if not isInstance(b, Interface):
                raise TypeError, 'Expected base interfaces'
        self.__bases__=bases

        if attrs is None: attrs={}
        if attrs.has_key('__doc__'):
            if __doc__ is None: __doc__=attrs['__doc__']
            del attrs['__doc__']

        if __doc__ is not None:
            self.__doc__=__doc__
        else:
            self.__doc__ = ""

        Element.__init__(self, name, __doc__)

        for k, v in attrs.items():
            if isInstance(v, Attribute):
                v.interface=name
                if not v.__name__:
                    v.__name__ = k
            elif isinstance(v, FunctionType):
                attrs[k]=fromFunction(v, name)
            else:
                raise Exceptions.InvalidInterface(
                    "Concrete attribute, %s" % k)

        self.__attrs = attrs