Example #1
0
def proxyForInterface(iface, originalAttribute='original'):
    """
	Create a class which proxies all method calls which adhere to an interface
	to another provider of that interface.

	This function is intended for creating specialized proxies. The typical way
	to use it is by subclassing the result::

	  class MySpecializedProxy(proxyForInterface(IFoo)):
		  def someInterfaceMethod(self, arg):
			  if arg == 3:
				  return 3
			  return self.original.someInterfaceMethod(arg)

	@param iface: The Interface to which the resulting object will conform, and
		which the wrapped object must provide.

	@param originalAttribute: name of the attribute used to save the original
		object in the resulting class. Default to C{original}.
	@type originalAttribute: C{str}

	@return: A class whose constructor takes the original object as its only
		argument. Constructing the class creates the proxy.
	"""
    def __init__(self, original):
        setattr(self, originalAttribute, original)

    contents = {"__init__": __init__}
    for name in iface:
        contents[name] = _ProxyDescriptor(name, originalAttribute)
    proxy = type("(Proxy for %s)" % (reflect.qual(iface), ), (object, ),
                 contents)
    declarations.classImplements(proxy, iface)
    return proxy
Example #2
0
def proxyForInterface(iface, originalAttribute='original'):
	"""
	Create a class which proxies all method calls which adhere to an interface
	to another provider of that interface.

	This function is intended for creating specialized proxies. The typical way
	to use it is by subclassing the result::

	  class MySpecializedProxy(proxyForInterface(IFoo)):
		  def someInterfaceMethod(self, arg):
			  if arg == 3:
				  return 3
			  return self.original.someInterfaceMethod(arg)

	@param iface: The Interface to which the resulting object will conform, and
		which the wrapped object must provide.

	@param originalAttribute: name of the attribute used to save the original
		object in the resulting class. Default to C{original}.
	@type originalAttribute: C{str}

	@return: A class whose constructor takes the original object as its only
		argument. Constructing the class creates the proxy.
	"""
	def __init__(self, original):
		setattr(self, originalAttribute, original)
	contents = {"__init__": __init__}
	for name in iface:
		contents[name] = _ProxyDescriptor(name, originalAttribute)
	proxy = type("(Proxy for %s)"
				 % (reflect.qual(iface),), (object,), contents)
	declarations.classImplements(proxy, iface)
	return proxy
Example #3
0
def _wire():
    from lib.zope.interface.declarations import classImplements

    from lib.zope.interface.interfaces import IAttribute

    classImplements(Attribute, IAttribute)

    from lib.zope.interface.interfaces import IMethod

    classImplements(Method, IMethod)

    from lib.zope.interface.interfaces import IInterface

    classImplements(InterfaceClass, IInterface)

    from lib.zope.interface.interfaces import ISpecification

    classImplements(Specification, ISpecification)