Example #1
0
def rskel(iface, print_iface=1):
    name = "%s.%s" % (iface.__module__, iface.__name__)

    file = resolve(iface.__module__).__file__
    if file.endswith('pyc'):
        file = file[:-1]
    order = guessOrder(open(file))
    namesAndDescriptions =  getAttributesInOrder(iface, order)

    if namesAndDescriptions and print_iface:
        print
        print "    ######################################"
        print "    # from:", name

    for aname, ades in namesAndDescriptions:
        if isInstance(ades, Method):
            sig = ades.getSignatureString()[1:-1]
            if sig: sig = "self, %s" % sig
            else:   sig = "self"
            print
            print "    def %s(%s):" % (aname, sig)
            print "        'See %s'" % name

        elif isInstance(ades, Attribute):
            print
            print "    # See %s" % name
            print "    %s = None" %aname

    for base in iface.__bases__:
        if base.__name__ not in ('Interface',):
            rskel(base)
Example #2
0
def rskel(iface, print_iface=1):
    name = "%s.%s" % (iface.__module__, iface.__name__)

    file = resolve(iface.__module__).__file__
    if file.endswith('pyc'):
        file = file[:-1]
    order = guessOrder(open(file))
    namesAndDescriptions = getAttributesInOrder(iface, order)

    if namesAndDescriptions and print_iface:
        print
        print "    ######################################"
        print "    # from:", name

    for aname, ades in namesAndDescriptions:
        if isInstance(ades, Method):
            sig = ades.getSignatureString()[1:-1]
            if sig: sig = "self, %s" % sig
            else: sig = "self"
            print
            print "    def %s(%s):" % (aname, sig)
            print "        'See %s'" % name

        elif isInstance(ades, Attribute):
            print
            print "    # See %s" % name
            print "    %s = None" % aname

    for base in iface.__bases__:
        if base.__name__ not in ('Interface', ):
            rskel(base)
Example #3
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
Example #4
0
    def __d(self, dict):

        for k, v in self.__attrs.items():
            if isInstance(v, Method) and not dict.has_key(k):
                dict[k]=v

        for b in self.__bases__: b.__d(dict)
    def __d(self, dict):

        for k, v in self.__attrs.items():
            if isInstance(v, Method) and not dict.has_key(k):
                dict[k]=v

        for b in self.__bases__: b.__d(dict)
Example #6
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
Example #7
0
def visitImplements(implements, object, visitor, getInterface=None):
    """
    Visits the interfaces described by an __implements__ attribute,
    invoking the visitor for each interface object.
    If the visitor returns anything true, the loop stops.
    This does not, and should not, visit superinterfaces.
    """
    # this allows us to work with proxy wrappers in Python 2.2,
    # yet remain compatible with earlier versions of python.
    implements_class = getattr(implements, '__class__', None)

    if implements_class == InterfaceClass or \
       isInstance(implements, InterfaceClass):
        return visitor(implements)
    elif implements == CLASS_INTERFACES:
        klass = getattr(object, '__class__', None)
        if klass is not None:
            i = getImplementsOfInstances(klass)
            if i:
                return visitImplements(i, object, visitor, getInterface)
    elif implements_class == StringType or type(implements) is StringType:
        if getInterface is not None:
            # Look up a named interface.
            i = getInterface(object, implements)
            if i is not None:
                return visitImplements(i, object, visitor, getInterface)
    elif implements_class == TupleType or type(implements) is TupleType:
        for i in implements:
            r = visitImplements(i, object, visitor, getInterface)
            if r:
                # If the visitor returns anything true, stop.
                return r
    else:
        if implements_class is not None and \
           type(implements) != implements_class:
            raise Exceptions.BadImplements(
                """__implements__ should be an interface or tuple,
                not a %s pretending to be a %s"""
                % (type(implements).__name__, implements_class.__name__)
                )
        raise Exceptions.BadImplements(
            """__implements__ should be an interface or tuple,
            not a %s""" % type(implements).__name__)
    return 0
Example #8
0
def visitImplements(implements, object, visitor, getInterface=None):
    """
    Visits the interfaces described by an __implements__ attribute,
    invoking the visitor for each interface object.
    If the visitor returns anything true, the loop stops.
    This does not, and should not, visit superinterfaces.
    """
    # this allows us to work with proxy wrappers in Python 2.2,
    # yet remain compatible with earlier versions of python.
    implements_class = getattr(implements, '__class__', None)

    if implements_class == InterfaceClass or \
       isInstance(implements, InterfaceClass):
        return visitor(implements)
    elif implements == CLASS_INTERFACES:
        klass = getattr(object, '__class__', None)
        if klass is not None:
            i = getImplementsOfInstances(klass)
            if i:
                return visitImplements(i, object, visitor, getInterface)
    elif implements_class == StringType or type(implements) is StringType:
        if getInterface is not None:
            # Look up a named interface.
            i = getInterface(object, implements)
            if i is not None:
                return visitImplements(i, object, visitor, getInterface)
    elif implements_class == TupleType or type(implements) is TupleType:
        for i in implements:
            r = visitImplements(i, object, visitor, getInterface)
            if r:
                # If the visitor returns anything true, stop.
                return r
    else:
        if implements_class is not None and \
           type(implements) != implements_class:
            raise Exceptions.BadImplements(
                """__implements__ should be an interface or tuple,
                not a %s pretending to be a %s"""
                % (type(implements).__name__, implements_class.__name__)
                )
        raise Exceptions.BadImplements(
            """__implements__ should be an interface or tuple,
            not a %s""" % type(implements).__name__)
    return 0