Exemplo n.º 1
0
def _implements(name, interfaces, classImplements):
    frame = sys._getframe(2)
    locals = frame.f_locals

    # Try to make sure we were called from a class def. In 2.2.0 we can't
    # check for __module__ since it doesn't seem to be added to the locals
    # until later on.
    if (locals is frame.f_globals) or (
        ('__module__' not in locals) and sys.version_info[:3] > (2, 2, 0)):
        raise TypeError(name+" can be used only from a class definition.")

    if '__implements_advice_data__' in locals:
        raise TypeError(name+" can be used only once in a class definition.")

    locals['__implements_advice_data__'] = interfaces, classImplements
    addClassAdvisor(_implements_advice, depth=3)
Exemplo n.º 2
0
def classProvides(*interfaces):
    """Declare interfaces provided directly by a class

      This function is called in a class definition.

      The arguments are one or more interfaces or interface specifications
      (``IDeclaration`` objects).

      The given interfaces (including the interfaces in the specifications)
      are used to create the class's direct-object interface specification.
      An error will be raised if the module class has an direct interface
      specification. In other words, it is an error to call this function more
      than once in a class definition.

      Note that the given interfaces have nothing to do with the interfaces
      implemented by instances of the class.

      This function is provided for convenience. It provides a more convenient
      way to call directlyProvides for a class. For example::

        classProvides(I1)

      is equivalent to calling::

        directlyProvides(theclass, I1)

      after the class has been created.

      For example:

        >>> from include.zope.interface import Interface
        >>> class IFoo(Interface): pass
        ...
        >>> class IFooFactory(Interface): pass
        ...
        >>> class C(object):
        ...   implements(IFoo)
        ...   classProvides(IFooFactory)
        >>> [i.getName() for i in C.__providedBy__]
        ['IFooFactory']
        >>> [i.getName() for i in C().__providedBy__]
        ['IFoo']

      if equivalent to:

        >>> from include.zope.interface import Interface
        >>> class IFoo(Interface): pass
        ...
        >>> class IFooFactory(Interface): pass
        ...
        >>> class C(object):
        ...   implements(IFoo)
        >>> directlyProvides(C, IFooFactory)
        >>> [i.getName() for i in C.__providedBy__]
        ['IFooFactory']
        >>> [i.getName() for i in C().__providedBy__]
        ['IFoo']

      If classProvides is called outside of a class definition, it fails.

        >>> classProvides(IFooFactory)
        Traceback (most recent call last):
        ...
        TypeError: classProvides can be used only from a class definition.

      """
    frame = sys._getframe(1)
    locals = frame.f_locals

    # Try to make sure we were called from a class def
    if (locals is frame.f_globals) or ('__module__' not in locals):
        raise TypeError("classProvides can be used only from a class definition.")

    if '__provides__' in locals:
        raise TypeError(
            "classProvides can only be used once in a class definition.")

    locals["__provides__"] = _normalizeargs(interfaces)

    addClassAdvisor(_classProvides_advice, depth=2)