diff options
author | olpc user <olpc@localhost.localdomain> | 2010-01-30 13:13:29 (GMT) |
---|---|---|
committer | olpc user <olpc@localhost.localdomain> | 2010-01-30 13:13:29 (GMT) |
commit | f8a54f5c2f4487f264fe4647cec62599db93d063 (patch) | |
tree | 8758b1412c524129dad7e90a4da6b463a85ed96f /zope |
initial save 1-30-2010 bangladesh
Diffstat (limited to 'zope')
33 files changed, 7413 insertions, 0 deletions
diff --git a/zope/__init__.py b/zope/__init__.py new file mode 100644 index 0000000..2e2033b --- /dev/null +++ b/zope/__init__.py @@ -0,0 +1,7 @@ +# this is a namespace package +try: + import pkg_resources + pkg_resources.declare_namespace(__name__) +except ImportError: + import pkgutil + __path__ = pkgutil.extend_path(__path__, __name__) diff --git a/zope/interface/__init__.py b/zope/interface/__init__.py new file mode 100644 index 0000000..f45079b --- /dev/null +++ b/zope/interface/__init__.py @@ -0,0 +1,80 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Interfaces + +This package implements the Python "scarecrow" proposal. + +The package exports two objects, `Interface` and `Attribute` directly. It also +exports several helper methods. Interface is used to create an interface with +a class statement, as in: + + class IMyInterface(Interface): + '''Interface documentation + ''' + + def meth(arg1, arg2): + '''Documentation for meth + ''' + + # Note that there is no self argument + +To find out what you can do with interfaces, see the interface +interface, `IInterface` in the `interfaces` module. + +The package has several public modules: + + o `declarations` provides utilities to declare interfaces on objects. It + also provides a wide range of helpful utilities that aid in managing + declared interfaces. Most of its public names are however imported here. + + o `document` has a utility for documenting an interface as structured text. + + o `exceptions` has the interface-defined exceptions + + o `interfaces` contains a list of all public interfaces for this package. + + o `verify` has utilities for verifying implementations of interfaces. + +See the module doc strings for more information. + +$Id: __init__.py 67630 2006-04-27 00:54:03Z jim $ +""" +__docformat__ = 'restructuredtext' + +from zope.interface.interface import Interface, _wire + +# Need to actually get the interface elements to implement the right interfaces +_wire() +del _wire + +from zope.interface.interface import Attribute, invariant + +from zope.interface.declarations import providedBy, implementedBy +from zope.interface.declarations import classImplements, classImplementsOnly +from zope.interface.declarations import directlyProvidedBy, directlyProvides +from zope.interface.declarations import alsoProvides, implementer +from zope.interface.declarations import implements, implementsOnly +from zope.interface.declarations import classProvides, moduleProvides +from zope.interface.declarations import noLongerProvides, Declaration +from zope.interface.exceptions import Invalid + +# The following are to make spec pickles cleaner +from zope.interface.declarations import Provides + + +from zope.interface.interfaces import IInterfaceDeclaration + +moduleProvides(IInterfaceDeclaration) + +__all__ = ('Interface', 'Attribute') + tuple(IInterfaceDeclaration) diff --git a/zope/interface/_flatten.py b/zope/interface/_flatten.py new file mode 100644 index 0000000..f0df3a3 --- /dev/null +++ b/zope/interface/_flatten.py @@ -0,0 +1,37 @@ +############################################################################## +# +# Copyright (c) 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Adapter-style interface registry + +See Adapter class. + +$Id: _flatten.py 26551 2004-07-15 07:06:37Z srichter $ +""" +from zope.interface import Declaration + +def _flatten(implements, include_None=0): + + try: + r = implements.flattened() + except AttributeError: + if implements is None: + r=() + else: + r = Declaration(implements).flattened() + + if not include_None: + return r + + r = list(r) + r.append(None) + return r diff --git a/zope/interface/adapter.py b/zope/interface/adapter.py new file mode 100644 index 0000000..0cb8949 --- /dev/null +++ b/zope/interface/adapter.py @@ -0,0 +1,644 @@ +############################################################################## +# +# Copyright (c) 2004 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Adapter management + +$Id: adapter.py 67796 2006-05-01 13:55:44Z jim $ +""" + +import weakref +from zope.interface import providedBy, Interface, ro + +_marker = object +class BaseAdapterRegistry(object): + + # List of methods copied from lookup sub-objects: + _delegated = ('lookup', 'queryMultiAdapter', 'lookup1', 'queryAdapter', + 'adapter_hook', 'lookupAll', 'names', + 'subscriptions', 'subscribers') + + # All registries maintain a generation that can be used by verifying + # registries + _generation = 0 + + def __init__(self, bases=()): + + # {order -> {required -> {provided -> {name -> value}}}} + # where "interfaces" is really a nested key. So, for example: + # for order == 0, we have: + # {provided -> {name -> valie}} + # but for order == 2, we have: + # {r1 -> {r2 -> {provided -> {name -> valie}}}} + self._adapters = [] + + # {order -> {required -> {provided -> {name -> [value]}}}} + # where the remarks about adapters above apply + self._subscribers = [] + + # Set, with a reference count, keeping track of the interfaces + # for which we have provided components: + self._provided = {} + + # Looup object to perform lookup. We make this a separate object to + # to make it easier, in the furture, to implement just the lookup + # functionality in C. + self._createLookup() + + # Cache invalidation data. There are really 2 kinds of registries: + + # Invalidating registries have caches that are invalidated + # when they or when base registies change. An invalidating + # registry can only have invalidating registries as bases. + + # Verifying registies can't rely on getting invalidation message, + # so have to check the generations of base registries to determine + # if their cache data are current + + # Base registries: + self.__bases__ = bases + + def _setBases(self, bases): + self.__dict__['__bases__'] = bases + self.ro = ro.ro(self) + self.changed(self) + + __bases__ = property(lambda self: self.__dict__['__bases__'], + lambda self, bases: self._setBases(bases), + ) + + def _createLookup(self): + self._v_lookup = self.LookupClass(self) + for name in self._delegated: + self.__dict__[name] = getattr(self._v_lookup, name) + + def changed(self, originally_changed): + self._generation += 1 + self._v_lookup.changed(originally_changed) + + def register(self, required, provided, name, value): + if value is None: + self.unregister(required, provided, name, value) + return + + required = tuple(map(_convert_None_to_Interface, required)) + name = _normalize_name(name) + order = len(required) + byorder = self._adapters + while len(byorder) <= order: + byorder.append({}) + components = byorder[order] + key = required + (provided,) + + for k in key: + d = components.get(k) + if d is None: + d = {} + components[k] = d + components = d + + if components.get(name) == value: + return + + components[name] = value + + n = self._provided.get(provided, 0) + 1 + self._provided[provided] = n + if n == 1: + self._v_lookup.add_extendor(provided) + + self.changed(self) + + def registered(self, required, provided, name=u''): + required = tuple(map(_convert_None_to_Interface, required)) + name = _normalize_name(name) + order = len(required) + byorder = self._adapters + if len(byorder) <= order: + return None + + components = byorder[order] + key = required + (provided,) + + for k in key: + d = components.get(k) + if d is None: + return None + components = d + + return components.get(name) + + def unregister(self, required, provided, name, value=None): + required = tuple(map(_convert_None_to_Interface, required)) + order = len(required) + byorder = self._adapters + if order >= len(byorder): + return False + components = byorder[order] + key = required + (provided,) + + for k in key: + d = components.get(k) + if d is None: + return + components = d + + old = components.get(name) + if old is None: + return + if (value is not None) and (old != value): + return + + del components[name] + n = self._provided[provided] - 1 + if n == 0: + del self._provided[provided] + self._v_lookup.remove_extendor(provided) + else: + self._provided[provided] = n + + self.changed(self) + + return + + + def subscribe(self, required, provided, value): + required = tuple(map(_convert_None_to_Interface, required)) + name = u'' + order = len(required) + byorder = self._subscribers + while len(byorder) <= order: + byorder.append({}) + components = byorder[order] + key = required + (provided,) + + for k in key: + d = components.get(k) + if d is None: + d = {} + components[k] = d + components = d + + components[name] = components.get(name, ()) + (value, ) + + if provided is not None: + n = self._provided.get(provided, 0) + 1 + self._provided[provided] = n + if n == 1: + self._v_lookup.add_extendor(provided) + + self.changed(self) + + def unsubscribe(self, required, provided, value=None): + required = tuple(map(_convert_None_to_Interface, required)) + order = len(required) + byorder = self._subscribers + if order >= len(byorder): + return + components = byorder[order] + key = required + (provided,) + + for k in key: + d = components.get(k) + if d is None: + return + components = d + + old = components.get(u'') + if not old: + return + + if value is None: + new = () + else: + new = tuple([v for v in old if v != value]) + + if new == old: + return + + components[u''] = new + + if provided is not None: + n = self._provided[provided] + len(new) - len(old) + if n == 0: + del self._provided[provided] + self._v_lookup.remove_extendor(provided) + + self.changed(self) + + # XXX hack to fake out twisted's use of a private api. We need to get them + # to use the new registed method. + def get(self, _): + class XXXTwistedFakeOut: + selfImplied = {} + return XXXTwistedFakeOut + + +_not_in_mapping = object() +class LookupBasePy(object): + + def __init__(self): + self._cache = {} + self._mcache = {} + self._scache = {} + + def changed(self, ignored=None): + self._cache.clear() + self._mcache.clear() + self._scache.clear() + + def _getcache(self, provided, name): + cache = self._cache.get(provided) + if cache is None: + cache = {} + self._cache[provided] = cache + if name: + c = cache.get(name) + if c is None: + c = {} + cache[name] = c + cache = c + return cache + + def lookup(self, required, provided, name=u'', default=None): + cache = self._getcache(provided, name) + if len(required) == 1: + result = cache.get(required[0], _not_in_mapping) + else: + result = cache.get(tuple(required), _not_in_mapping) + + if result is _not_in_mapping: + result = self._uncached_lookup(required, provided, name) + if len(required) == 1: + cache[required[0]] = result + else: + cache[tuple(required)] = result + + if result is None: + return default + + return result + + def lookup1(self, required, provided, name=u'', default=None): + cache = self._getcache(provided, name) + result = cache.get(required, _not_in_mapping) + if result is _not_in_mapping: + return self.lookup((required, ), provided, name, default) + + if result is None: + return default + + return result + + def queryAdapter(self, object, provided, name=u'', default=None): + return self.adapter_hook(provided, object, name, default) + + def adapter_hook(self, provided, object, name=u'', default=None): + required = providedBy(object) + cache = self._getcache(provided, name) + factory = cache.get(required, _not_in_mapping) + if factory is _not_in_mapping: + factory = self.lookup((required, ), provided, name) + + if factory is not None: + result = factory(object) + if result is not None: + return result + + return default + + def lookupAll(self, required, provided): + cache = self._mcache.get(provided) + if cache is None: + cache = {} + self._mcache[provided] = cache + + required = tuple(required) + result = cache.get(required, _not_in_mapping) + if result is _not_in_mapping: + result = self._uncached_lookupAll(required, provided) + cache[required] = result + + return result + + + def subscriptions(self, required, provided): + cache = self._scache.get(provided) + if cache is None: + cache = {} + self._scache[provided] = cache + + required = tuple(required) + result = cache.get(required, _not_in_mapping) + if result is _not_in_mapping: + result = self._uncached_subscriptions(required, provided) + cache[required] = result + + return result + +LookupBase = LookupBasePy + +class VerifyingBasePy(LookupBasePy): + + def changed(self, originally_changed): + LookupBasePy.changed(self, originally_changed) + self._verify_ro = self._registry.ro[1:] + self._verify_generations = [r._generation for r in self._verify_ro] + + def _verify(self): + if ([r._generation for r in self._verify_ro] + != self._verify_generations): + self.changed(None) + + def _getcache(self, provided, name): + self._verify() + return LookupBasePy._getcache(self, provided, name) + + def lookupAll(self, required, provided): + self._verify() + return LookupBasePy.lookupAll(self, required, provided) + + def subscriptions(self, required, provided): + self._verify() + return LookupBasePy.subscriptions(self, required, provided) + +VerifyingBase = VerifyingBasePy + + +try: + import _zope_interface_coptimizations +except ImportError: + pass +else: + from _zope_interface_coptimizations import LookupBase, VerifyingBase + +class AdapterLookupBase(object): + + def __init__(self, registry): + self._registry = registry + self._required = {} + self.init_extendors() + super(AdapterLookupBase, self).__init__() + + def changed(self, ignored=None): + super(AdapterLookupBase, self).changed(None) + for r in self._required.keys(): + r = r() + if r is not None: + r.unsubscribe(self) + self._required.clear() + + + # Extendors + # --------- + + # When given an target interface for an adapter lookup, we need to consider + # adapters for interfaces that extend the target interface. This is + # what the extendors dictionary is about. It tells us all of the + # interfaces that extend an interface for which there are adapters + # registered. + + # We could separate this by order and name, thus reducing the + # number of provided interfaces to search at run time. The tradeoff, + # however, is that we have to store more information. For example, + # is the same interface is provided for multiple names and if the + # interface extends many interfaces, we'll have to keep track of + # a fair bit of information for each name. It's better to + # be space efficient here and be time efficient in the cache + # implementation. + + # TODO: add invalidation when a provided interface changes, in case + # the interface's __iro__ has changed. This is unlikely enough that + # we'll take our chances for now. + + def init_extendors(self): + self._extendors = {} + for p in self._registry._provided: + self.add_extendor(p) + + def add_extendor(self, provided): + _extendors = self._extendors + for i in provided.__iro__: + extendors = _extendors.get(i, ()) + _extendors[i] = ( + [e for e in extendors if provided.isOrExtends(e)] + + + [provided] + + + [e for e in extendors if not provided.isOrExtends(e)] + ) + + def remove_extendor(self, provided): + _extendors = self._extendors + for i in provided.__iro__: + _extendors[i] = [e for e in _extendors.get(i, ()) + if e != provided] + + + def _subscribe(self, *required): + _refs = self._required + for r in required: + ref = r.weakref() + if ref not in _refs: + r.subscribe(self) + _refs[ref] = 1 + + def _uncached_lookup(self, required, provided, name=u''): + result = None + order = len(required) + for registry in self._registry.ro: + byorder = registry._adapters + if order >= len(byorder): + continue + + extendors = registry._v_lookup._extendors.get(provided) + if not extendors: + continue + + components = byorder[order] + result = _lookup(components, required, extendors, name, 0, + order) + if result is not None: + break + + self._subscribe(*required) + + return result + + def queryMultiAdapter(self, objects, provided, name=u'', default=None): + factory = self.lookup(map(providedBy, objects), provided, name) + if factory is None: + return default + + result = factory(*objects) + if result is None: + return default + + return result + + def _uncached_lookupAll(self, required, provided): + order = len(required) + result = {} + for registry in reversed(self._registry.ro): + byorder = registry._adapters + if order >= len(byorder): + continue + extendors = registry._v_lookup._extendors.get(provided) + if not extendors: + continue + components = byorder[order] + _lookupAll(components, required, extendors, result, 0, order) + + self._subscribe(*required) + + return tuple(result.iteritems()) + + def names(self, required, provided): + return [c[0] for c in self.lookupAll(required, provided)] + + def _uncached_subscriptions(self, required, provided): + order = len(required) + result = [] + for registry in reversed(self._registry.ro): + byorder = registry._subscribers + if order >= len(byorder): + continue + + if provided is None: + extendors = (provided, ) + else: + extendors = registry._v_lookup._extendors.get(provided) + if extendors is None: + continue + + _subscriptions(byorder[order], required, extendors, u'', + result, 0, order) + + self._subscribe(*required) + + return result + + def subscribers(self, objects, provided): + subscriptions = self.subscriptions(map(providedBy, objects), provided) + if provided is None: + result = () + for subscription in subscriptions: + subscription(*objects) + else: + result = [] + for subscription in subscriptions: + subscriber = subscription(*objects) + if subscriber is not None: + result.append(subscriber) + return result + +class AdapterLookup(AdapterLookupBase, LookupBase): + pass + +class AdapterRegistry(BaseAdapterRegistry): + + LookupClass = AdapterLookup + + def __init__(self, bases=()): + # AdapterRegisties are invalidating registries, so + # we need to keep track of out invalidating subregistries. + self._v_subregistries = weakref.WeakKeyDictionary() + + super(AdapterRegistry, self).__init__(bases) + + def _addSubregistry(self, r): + self._v_subregistries[r] = 1 + + def _removeSubregistry(self, r): + if r in self._v_subregistries: + del self._v_subregistries[r] + + def _setBases(self, bases): + old = self.__dict__.get('__bases__', ()) + for r in old: + if r not in bases: + r._removeSubregistry(self) + for r in bases: + if r not in old: + r._addSubregistry(self) + + super(AdapterRegistry, self)._setBases(bases) + + def changed(self, originally_changed): + super(AdapterRegistry, self).changed(originally_changed) + + for sub in self._v_subregistries.keys(): + sub.changed(originally_changed) + + +class VerifyingAdapterLookup(AdapterLookupBase, VerifyingBase): + pass + +class VerifyingAdapterRegistry(BaseAdapterRegistry): + + LookupClass = VerifyingAdapterLookup + +def _convert_None_to_Interface(x): + if x is None: + return Interface + else: + return x + +def _normalize_name(name): + if isinstance(name, basestring): + return unicode(name) + + raise TypeError("name must be a regular or unicode string") + +def _lookup(components, specs, provided, name, i, l): + if i < l: + for spec in specs[i].__sro__: + comps = components.get(spec) + if comps: + r = _lookup(comps, specs, provided, name, i+1, l) + if r is not None: + return r + else: + for iface in provided: + comps = components.get(iface) + if comps: + r = comps.get(name) + if r is not None: + return r + + return None + +def _lookupAll(components, specs, provided, result, i, l): + if i < l: + for spec in reversed(specs[i].__sro__): + comps = components.get(spec) + if comps: + _lookupAll(comps, specs, provided, result, i+1, l) + else: + for iface in reversed(provided): + comps = components.get(iface) + if comps: + result.update(comps) + +def _subscriptions(components, specs, provided, name, result, i, l): + if i < l: + for spec in reversed(specs[i].__sro__): + comps = components.get(spec) + if comps: + _subscriptions(comps, specs, provided, name, result, i+1, l) + else: + for iface in reversed(provided): + comps = components.get(iface) + if comps: + comps = comps.get(name) + if comps: + result.extend(comps) diff --git a/zope/interface/advice.py b/zope/interface/advice.py new file mode 100644 index 0000000..2d9e038 --- /dev/null +++ b/zope/interface/advice.py @@ -0,0 +1,192 @@ +############################################################################## +# +# Copyright (c) 2003 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Class advice. + +This module was adapted from 'protocols.advice', part of the Python +Enterprise Application Kit (PEAK). Please notify the PEAK authors +(pje@telecommunity.com and tsarna@sarna.org) if bugs are found or +Zope-specific changes are required, so that the PEAK version of this module +can be kept in sync. + +PEAK is a Python application framework that interoperates with (but does +not require) Zope 3 and Twisted. It provides tools for manipulating UML +models, object-relational persistence, aspect-oriented programming, and more. +Visit the PEAK home page at http://peak.telecommunity.com for more information. + +$Id: advice.py 25177 2004-06-02 13:17:31Z jim $ +""" + +from types import ClassType, FunctionType +import sys + +def getFrameInfo(frame): + """Return (kind,module,locals,globals) for a frame + + 'kind' is one of "exec", "module", "class", "function call", or "unknown". + """ + + f_locals = frame.f_locals + f_globals = frame.f_globals + + sameNamespace = f_locals is f_globals + hasModule = '__module__' in f_locals + hasName = '__name__' in f_globals + + sameName = hasModule and hasName + sameName = sameName and f_globals['__name__']==f_locals['__module__'] + + module = hasName and sys.modules.get(f_globals['__name__']) or None + + namespaceIsModule = module and module.__dict__ is f_globals + + if not namespaceIsModule: + # some kind of funky exec + kind = "exec" + elif sameNamespace and not hasModule: + kind = "module" + elif sameName and not sameNamespace: + kind = "class" + elif not sameNamespace: + kind = "function call" + else: + # How can you have f_locals is f_globals, and have '__module__' set? + # This is probably module-level code, but with a '__module__' variable. + kind = "unknown" + return kind, module, f_locals, f_globals + + +def addClassAdvisor(callback, depth=2): + """Set up 'callback' to be passed the containing class upon creation + + This function is designed to be called by an "advising" function executed + in a class suite. The "advising" function supplies a callback that it + wishes to have executed when the containing class is created. The + callback will be given one argument: the newly created containing class. + The return value of the callback will be used in place of the class, so + the callback should return the input if it does not wish to replace the + class. + + The optional 'depth' argument to this function determines the number of + frames between this function and the targeted class suite. 'depth' + defaults to 2, since this skips this function's frame and one calling + function frame. If you use this function from a function called directly + in the class suite, the default will be correct, otherwise you will need + to determine the correct depth yourself. + + This function works by installing a special class factory function in + place of the '__metaclass__' of the containing class. Therefore, only + callbacks *after* the last '__metaclass__' assignment in the containing + class will be executed. Be sure that classes using "advising" functions + declare any '__metaclass__' *first*, to ensure all callbacks are run.""" + + frame = sys._getframe(depth) + kind, module, caller_locals, caller_globals = getFrameInfo(frame) + + # This causes a problem when zope interfaces are used from doctest. + # In these cases, kind == "exec". + # + #if kind != "class": + # raise SyntaxError( + # "Advice must be in the body of a class statement" + # ) + + previousMetaclass = caller_locals.get('__metaclass__') + defaultMetaclass = caller_globals.get('__metaclass__', ClassType) + + + def advise(name, bases, cdict): + + if '__metaclass__' in cdict: + del cdict['__metaclass__'] + + if previousMetaclass is None: + if bases: + # find best metaclass or use global __metaclass__ if no bases + meta = determineMetaclass(bases) + else: + meta = defaultMetaclass + + elif isClassAdvisor(previousMetaclass): + # special case: we can't compute the "true" metaclass here, + # so we need to invoke the previous metaclass and let it + # figure it out for us (and apply its own advice in the process) + meta = previousMetaclass + + else: + meta = determineMetaclass(bases, previousMetaclass) + + newClass = meta(name,bases,cdict) + + # this lets the callback replace the class completely, if it wants to + return callback(newClass) + + # introspection data only, not used by inner function + advise.previousMetaclass = previousMetaclass + advise.callback = callback + + # install the advisor + caller_locals['__metaclass__'] = advise + + +def isClassAdvisor(ob): + """True if 'ob' is a class advisor function""" + return isinstance(ob,FunctionType) and hasattr(ob,'previousMetaclass') + + +def determineMetaclass(bases, explicit_mc=None): + """Determine metaclass from 1+ bases and optional explicit __metaclass__""" + + meta = [getattr(b,'__class__',type(b)) for b in bases] + + if explicit_mc is not None: + # The explicit metaclass needs to be verified for compatibility + # as well, and allowed to resolve the incompatible bases, if any + meta.append(explicit_mc) + + if len(meta)==1: + # easy case + return meta[0] + + candidates = minimalBases(meta) # minimal set of metaclasses + + if not candidates: + # they're all "classic" classes + return ClassType + + elif len(candidates)>1: + # We could auto-combine, but for now we won't... + raise TypeError("Incompatible metatypes",bases) + + # Just one, return it + return candidates[0] + + +def minimalBases(classes): + """Reduce a list of base classes to its ordered minimum equivalent""" + + classes = [c for c in classes if c is not ClassType] + candidates = [] + + for m in classes: + for n in classes: + if issubclass(n,m) and m is not n: + break + else: + # m has no subclasses in 'classes' + if m in candidates: + candidates.remove(m) # ensure that we're later in the list + candidates.append(m) + + return candidates + diff --git a/zope/interface/common/__init__.py b/zope/interface/common/__init__.py new file mode 100644 index 0000000..b711d36 --- /dev/null +++ b/zope/interface/common/__init__.py @@ -0,0 +1,2 @@ +# +# This file is necessary to make this directory a package. diff --git a/zope/interface/common/idatetime.py b/zope/interface/common/idatetime.py new file mode 100644 index 0000000..c77ea15 --- /dev/null +++ b/zope/interface/common/idatetime.py @@ -0,0 +1,577 @@ +############################################################################## +# Copyright (c) 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +############################################################################## +"""Datetime interfaces. + +This module is called idatetime because if it were called datetime the import +of the real datetime would fail. + +$Id: idatetime.py 25177 2004-06-02 13:17:31Z jim $ +""" + +from zope.interface import Interface, Attribute +from zope.interface import classImplements, directlyProvides + +from datetime import timedelta, date, datetime, time, tzinfo + + +class ITimeDeltaClass(Interface): + """This is the timedelta class interface.""" + + min = Attribute("The most negative timedelta object") + + max = Attribute("The most positive timedelta object") + + resolution = Attribute( + "The smallest difference between non-equal timedelta objects") + + +class ITimeDelta(ITimeDeltaClass): + """Represent the difference between two datetime objects. + + Supported operators: + + - add, subtract timedelta + - unary plus, minus, abs + - compare to timedelta + - multiply, divide by int/long + + In addition, datetime supports subtraction of two datetime objects + returning a timedelta, and addition or subtraction of a datetime + and a timedelta giving a datetime. + + Representation: (days, seconds, microseconds). + """ + + days = Attribute("Days between -999999999 and 999999999 inclusive") + + seconds = Attribute("Seconds between 0 and 86399 inclusive") + + microseconds = Attribute("Microseconds between 0 and 999999 inclusive") + + +class IDateClass(Interface): + """This is the date class interface.""" + + min = Attribute("The earliest representable date") + + max = Attribute("The latest representable date") + + resolution = Attribute( + "The smallest difference between non-equal date objects") + + def today(): + """Return the current local time. + + This is equivalent to date.fromtimestamp(time.time())""" + + def fromtimestamp(timestamp): + """Return the local date from a POSIX timestamp (like time.time()) + + This may raise ValueError, if the timestamp is out of the range of + values supported by the platform C localtime() function. It's common + for this to be restricted to years from 1970 through 2038. Note that + on non-POSIX systems that include leap seconds in their notion of a + timestamp, leap seconds are ignored by fromtimestamp(). + """ + + def fromordinal(ordinal): + """Return the date corresponding to the proleptic Gregorian ordinal. + + January 1 of year 1 has ordinal 1. ValueError is raised unless + 1 <= ordinal <= date.max.toordinal(). + For any date d, date.fromordinal(d.toordinal()) == d. + """ + + +class IDate(IDateClass): + """Represents a date (year, month and day) in an idealized calendar. + + Operators: + + __repr__, __str__ + __cmp__, __hash__ + __add__, __radd__, __sub__ (add/radd only with timedelta arg) + """ + + year = Attribute("Between MINYEAR and MAXYEAR inclusive.") + + month = Attribute("Between 1 and 12 inclusive") + + day = Attribute( + "Between 1 and the number of days in the given month of the given year.") + + def replace(year, month, day): + """Return a date with the same value. + + Except for those members given new values by whichever keyword + arguments are specified. For example, if d == date(2002, 12, 31), then + d.replace(day=26) == date(2000, 12, 26). + """ + + def timetuple(): + """Return a 9-element tuple of the form returned by time.localtime(). + + The hours, minutes and seconds are 0, and the DST flag is -1. + d.timetuple() is equivalent to + (d.year, d.month, d.day, 0, 0, 0, d.weekday(), d.toordinal() - + date(d.year, 1, 1).toordinal() + 1, -1) + """ + + def toordinal(): + """Return the proleptic Gregorian ordinal of the date + + January 1 of year 1 has ordinal 1. For any date object d, + date.fromordinal(d.toordinal()) == d. + """ + + def weekday(): + """Return the day of the week as an integer. + + Monday is 0 and Sunday is 6. For example, + date(2002, 12, 4).weekday() == 2, a Wednesday. + + See also isoweekday(). + """ + + def isoweekday(): + """Return the day of the week as an integer. + + Monday is 1 and Sunday is 7. For example, + date(2002, 12, 4).isoweekday() == 3, a Wednesday. + + See also weekday(), isocalendar(). + """ + + def isocalendar(): + """Return a 3-tuple, (ISO year, ISO week number, ISO weekday). + + The ISO calendar is a widely used variant of the Gregorian calendar. + See http://www.phys.uu.nl/~vgent/calendar/isocalendar.htm for a good + explanation. + + The ISO year consists of 52 or 53 full weeks, and where a week starts + on a Monday and ends on a Sunday. The first week of an ISO year is the + first (Gregorian) calendar week of a year containing a Thursday. This + is called week number 1, and the ISO year of that Thursday is the same + as its Gregorian year. + + For example, 2004 begins on a Thursday, so the first week of ISO year + 2004 begins on Monday, 29 Dec 2003 and ends on Sunday, 4 Jan 2004, so + that date(2003, 12, 29).isocalendar() == (2004, 1, 1) and + date(2004, 1, 4).isocalendar() == (2004, 1, 7). + """ + + def isoformat(): + """Return a string representing the date in ISO 8601 format. + + This is 'YYYY-MM-DD'. + For example, date(2002, 12, 4).isoformat() == '2002-12-04'. + """ + + def __str__(): + """For a date d, str(d) is equivalent to d.isoformat().""" + + def ctime(): + """Return a string representing the date. + + For example date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'. + d.ctime() is equivalent to time.ctime(time.mktime(d.timetuple())) + on platforms where the native C ctime() function + (which time.ctime() invokes, but which date.ctime() does not invoke) + conforms to the C standard. + """ + + def strftime(format): + """Return a string representing the date. + + Controlled by an explicit format string. Format codes referring to + hours, minutes or seconds will see 0 values. + """ + + +class IDateTimeClass(Interface): + """This is the datetime class interface.""" + + min = Attribute("The earliest representable datetime") + + max = Attribute("The latest representable datetime") + + resolution = Attribute( + "The smallest possible difference between non-equal datetime objects") + + def today(): + """Return the current local datetime, with tzinfo None. + + This is equivalent to datetime.fromtimestamp(time.time()). + See also now(), fromtimestamp(). + """ + + def now(tz=None): + """Return the current local date and time. + + If optional argument tz is None or not specified, this is like today(), + but, if possible, supplies more precision than can be gotten from going + through a time.time() timestamp (for example, this may be possible on + platforms supplying the C gettimeofday() function). + + Else tz must be an instance of a class tzinfo subclass, and the current + date and time are converted to tz's time zone. In this case the result + is equivalent to tz.fromutc(datetime.utcnow().replace(tzinfo=tz)). + + See also today(), utcnow(). + """ + + def utcnow(): + """Return the current UTC date and time, with tzinfo None. + + This is like now(), but returns the current UTC date and time, as a + naive datetime object. + + See also now(). + """ + + def fromtimestamp(timestamp, tz=None): + """Return the local date and time corresponding to the POSIX timestamp. + + Same as is returned by time.time(). If optional argument tz is None or + not specified, the timestamp is converted to the platform's local date + and time, and the returned datetime object is naive. + + Else tz must be an instance of a class tzinfo subclass, and the + timestamp is converted to tz's time zone. In this case the result is + equivalent to + tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz)). + + fromtimestamp() may raise ValueError, if the timestamp is out of the + range of values supported by the platform C localtime() or gmtime() + functions. It's common for this to be restricted to years in 1970 + through 2038. Note that on non-POSIX systems that include leap seconds + in their notion of a timestamp, leap seconds are ignored by + fromtimestamp(), and then it's possible to have two timestamps + differing by a second that yield identical datetime objects. + + See also utcfromtimestamp(). + """ + + def utcfromtimestamp(timestamp): + """Return the UTC datetime from the POSIX timestamp with tzinfo None. + + This may raise ValueError, if the timestamp is out of the range of + values supported by the platform C gmtime() function. It's common for + this to be restricted to years in 1970 through 2038. + + See also fromtimestamp(). + """ + + def fromordinal(ordinal): + """Return the datetime from the proleptic Gregorian ordinal. + + January 1 of year 1 has ordinal 1. ValueError is raised unless + 1 <= ordinal <= datetime.max.toordinal(). + The hour, minute, second and microsecond of the result are all 0, and + tzinfo is None. + """ + + def combine(date, time): + """Return a new datetime object. + + Its date members are equal to the given date object's, and whose time + and tzinfo members are equal to the given time object's. For any + datetime object d, d == datetime.combine(d.date(), d.timetz()). + If date is a datetime object, its time and tzinfo members are ignored. + """ + + +class IDateTime(IDate, IDateTimeClass): + """Object contains all the information from a date object and a time object. + """ + + year = Attribute("Year between MINYEAR and MAXYEAR inclusive") + + month = Attribute("Month between 1 and 12 inclusive") + + day = Attribute( + "Day between 1 and the number of days in the given month of the year") + + hour = Attribute("Hour in range(24)") + + minute = Attribute("Minute in range(60)") + + second = Attribute("Second in range(60)") + + microsecond = Attribute("Microsecond in range(1000000)") + + tzinfo = Attribute( + """The object passed as the tzinfo argument to the datetime constructor + or None if none was passed""") + + def date(): + """Return date object with same year, month and day.""" + + def time(): + """Return time object with same hour, minute, second, microsecond. + + tzinfo is None. See also method timetz(). + """ + + def timetz(): + """Return time object with same hour, minute, second, microsecond, + and tzinfo. + + See also method time(). + """ + + def replace(year, month, day, hour, minute, second, microsecond, tzinfo): + """Return a datetime with the same members, except for those members + given new values by whichever keyword arguments are specified. + + Note that tzinfo=None can be specified to create a naive datetime from + an aware datetime with no conversion of date and time members. + """ + + def astimezone(tz): + """Return a datetime object with new tzinfo member tz, adjusting the + date and time members so the result is the same UTC time as self, but + in tz's local time. + + tz must be an instance of a tzinfo subclass, and its utcoffset() and + dst() methods must not return None. self must be aware (self.tzinfo + must not be None, and self.utcoffset() must not return None). + + If self.tzinfo is tz, self.astimezone(tz) is equal to self: no + adjustment of date or time members is performed. Else the result is + local time in time zone tz, representing the same UTC time as self: + after astz = dt.astimezone(tz), astz - astz.utcoffset() + will usually have the same date and time members as dt - dt.utcoffset(). + The discussion of class tzinfo explains the cases at Daylight Saving + Time transition boundaries where this cannot be achieved (an issue only + if tz models both standard and daylight time). + + If you merely want to attach a time zone object tz to a datetime dt + without adjustment of date and time members, use dt.replace(tzinfo=tz). + If you merely want to remove the time zone object from an aware + datetime dt without conversion of date and time members, use + dt.replace(tzinfo=None). + + Note that the default tzinfo.fromutc() method can be overridden in a + tzinfo subclass to effect the result returned by astimezone(). + """ + + def utcoffset(): + """Return the timezone offset in minutes east of UTC (negative west of + UTC).""" + + def dst(): + """Return 0 if DST is not in effect, or the DST offset (in minutes + eastward) if DST is in effect. + """ + + def tzname(): + """Return the timezone name.""" + + def timetuple(): + """Return a 9-element tuple of the form returned by time.localtime().""" + + def utctimetuple(): + """Return UTC time tuple compatilble with time.gmtimr().""" + + def toordinal(): + """Return the proleptic Gregorian ordinal of the date. + + The same as self.date().toordinal(). + """ + + def weekday(): + """Return the day of the week as an integer. + + Monday is 0 and Sunday is 6. The same as self.date().weekday(). + See also isoweekday(). + """ + + def isoweekday(): + """Return the day of the week as an integer. + + Monday is 1 and Sunday is 7. The same as self.date().isoweekday. + See also weekday(), isocalendar(). + """ + + def isocalendar(): + """Return a 3-tuple, (ISO year, ISO week number, ISO weekday). + + The same as self.date().isocalendar(). + """ + + def isoformat(sep='T'): + """Return a string representing the date and time in ISO 8601 format. + + YYYY-MM-DDTHH:MM:SS.mmmmmm or YYYY-MM-DDTHH:MM:SS if microsecond is 0 + + If utcoffset() does not return None, a 6-character string is appended, + giving the UTC offset in (signed) hours and minutes: + + YYYY-MM-DDTHH:MM:SS.mmmmmm+HH:MM or YYYY-MM-DDTHH:MM:SS+HH:MM + if microsecond is 0. + + The optional argument sep (default 'T') is a one-character separator, + placed between the date and time portions of the result. + """ + + def __str__(): + """For a datetime instance d, str(d) is equivalent to d.isoformat(' '). + """ + + def ctime(): + """Return a string representing the date and time. + + datetime(2002, 12, 4, 20, 30, 40).ctime() == 'Wed Dec 4 20:30:40 2002'. + d.ctime() is equivalent to time.ctime(time.mktime(d.timetuple())) on + platforms where the native C ctime() function (which time.ctime() + invokes, but which datetime.ctime() does not invoke) conforms to the + C standard. + """ + + def strftime(format): + """Return a string representing the date and time. + + This is controlled by an explicit format string. + """ + + +class ITimeClass(Interface): + """This is the time class interface.""" + + min = Attribute("The earliest representable time") + + max = Attribute("The latest representable time") + + resolution = Attribute( + "The smallest possible difference between non-equal time objects") + + +class ITime(ITimeClass): + """Represent time with time zone. + + Operators: + + __repr__, __str__ + __cmp__, __hash__ + """ + + hour = Attribute("Hour in range(24)") + + minute = Attribute("Minute in range(60)") + + second = Attribute("Second in range(60)") + + microsecond = Attribute("Microsecond in range(1000000)") + + tzinfo = Attribute( + """The object passed as the tzinfo argument to the time constructor + or None if none was passed.""") + + def replace(hour, minute, second, microsecond, tzinfo): + """Return a time with the same value. + + Except for those members given new values by whichever keyword + arguments are specified. Note that tzinfo=None can be specified + to create a naive time from an aware time, without conversion of the + time members. + """ + + def isoformat(): + """Return a string representing the time in ISO 8601 format. + + That is HH:MM:SS.mmmmmm or, if self.microsecond is 0, HH:MM:SS + If utcoffset() does not return None, a 6-character string is appended, + giving the UTC offset in (signed) hours and minutes: + HH:MM:SS.mmmmmm+HH:MM or, if self.microsecond is 0, HH:MM:SS+HH:MM + """ + + def __str__(): + """For a time t, str(t) is equivalent to t.isoformat().""" + + def strftime(format): + """Return a string representing the time. + + This is controlled by an explicit format string. + """ + + def utcoffset(): + """Return the timezone offset in minutes east of UTC (negative west of + UTC). + + If tzinfo is None, returns None, else returns + self.tzinfo.utcoffset(None), and raises an exception if the latter + doesn't return None or a timedelta object representing a whole number + of minutes with magnitude less than one day. + """ + + def dst(): + """Return 0 if DST is not in effect, or the DST offset (in minutes + eastward) if DST is in effect. + + If tzinfo is None, returns None, else returns self.tzinfo.dst(None), + and raises an exception if the latter doesn't return None, or a + timedelta object representing a whole number of minutes with + magnitude less than one day. + """ + + def tzname(): + """Return the timezone name. + + If tzinfo is None, returns None, else returns self.tzinfo.tzname(None), + or raises an exception if the latter doesn't return None or a string + object. + """ + + +class ITZInfo(Interface): + """Time zone info class. + """ + + def utcoffset(dt): + """Return offset of local time from UTC, in minutes east of UTC. + + If local time is west of UTC, this should be negative. + Note that this is intended to be the total offset from UTC; + for example, if a tzinfo object represents both time zone and DST + adjustments, utcoffset() should return their sum. If the UTC offset + isn't known, return None. Else the value returned must be a timedelta + object specifying a whole number of minutes in the range -1439 to 1439 + inclusive (1440 = 24*60; the magnitude of the offset must be less + than one day). + """ + + def dst(dt): + """Return the daylight saving time (DST) adjustment, in minutes east + of UTC, or None if DST information isn't known. + """ + + def tzname(dt): + """Return the time zone name corresponding to the datetime object as + a string. + """ + + def fromutc(dt): + """Return an equivalent datetime in self's local time.""" + + +classImplements(timedelta, ITimeDelta) +classImplements(date, IDate) +classImplements(datetime, IDateTime) +classImplements(time, ITime) +classImplements(tzinfo, ITZInfo) + +## directlyProvides(timedelta, ITimeDeltaClass) +## directlyProvides(date, IDateClass) +## directlyProvides(datetime, IDateTimeClass) +## directlyProvides(time, ITimeClass) diff --git a/zope/interface/common/interfaces.py b/zope/interface/common/interfaces.py new file mode 100644 index 0000000..a8585b0 --- /dev/null +++ b/zope/interface/common/interfaces.py @@ -0,0 +1,98 @@ +############################################################################## +# +# Copyright (c) 2003 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Interfaces for standard python exceptions + +$Id: interfaces.py 25177 2004-06-02 13:17:31Z jim $ +""" +from zope.interface import Interface +from zope.interface import classImplements + +class IException(Interface): pass +class IStandardError(IException): pass +class IWarning(IException): pass +class ISyntaxError(IStandardError): pass +class ILookupError(IStandardError): pass +class IValueError(IStandardError): pass +class IRuntimeError(IStandardError): pass +class IArithmeticError(IStandardError): pass +class IAssertionError(IStandardError): pass +class IAttributeError(IStandardError): pass +class IDeprecationWarning(IWarning): pass +class IEOFError(IStandardError): pass +class IEnvironmentError(IStandardError): pass +class IFloatingPointError(IArithmeticError): pass +class IIOError(IEnvironmentError): pass +class IImportError(IStandardError): pass +class IIndentationError(ISyntaxError): pass +class IIndexError(ILookupError): pass +class IKeyError(ILookupError): pass +class IKeyboardInterrupt(IStandardError): pass +class IMemoryError(IStandardError): pass +class INameError(IStandardError): pass +class INotImplementedError(IRuntimeError): pass +class IOSError(IEnvironmentError): pass +class IOverflowError(IArithmeticError): pass +class IOverflowWarning(IWarning): pass +class IReferenceError(IStandardError): pass +class IRuntimeWarning(IWarning): pass +class IStopIteration(IException): pass +class ISyntaxWarning(IWarning): pass +class ISystemError(IStandardError): pass +class ISystemExit(IException): pass +class ITabError(IIndentationError): pass +class ITypeError(IStandardError): pass +class IUnboundLocalError(INameError): pass +class IUnicodeError(IValueError): pass +class IUserWarning(IWarning): pass +class IZeroDivisionError(IArithmeticError): pass + +classImplements(ArithmeticError, IArithmeticError) +classImplements(AssertionError, IAssertionError) +classImplements(AttributeError, IAttributeError) +classImplements(DeprecationWarning, IDeprecationWarning) +classImplements(EnvironmentError, IEnvironmentError) +classImplements(EOFError, IEOFError) +classImplements(Exception, IException) +classImplements(FloatingPointError, IFloatingPointError) +classImplements(ImportError, IImportError) +classImplements(IndentationError, IIndentationError) +classImplements(IndexError, IIndexError) +classImplements(IOError, IIOError) +classImplements(KeyboardInterrupt, IKeyboardInterrupt) +classImplements(KeyError, IKeyError) +classImplements(LookupError, ILookupError) +classImplements(MemoryError, IMemoryError) +classImplements(NameError, INameError) +classImplements(NotImplementedError, INotImplementedError) +classImplements(OSError, IOSError) +classImplements(OverflowError, IOverflowError) +classImplements(OverflowWarning, IOverflowWarning) +classImplements(ReferenceError, IReferenceError) +classImplements(RuntimeError, IRuntimeError) +classImplements(RuntimeWarning, IRuntimeWarning) +classImplements(StandardError, IStandardError) +classImplements(StopIteration, IStopIteration) +classImplements(SyntaxError, ISyntaxError) +classImplements(SyntaxWarning, ISyntaxWarning) +classImplements(SystemError, ISystemError) +classImplements(SystemExit, ISystemExit) +classImplements(TabError, ITabError) +classImplements(TypeError, ITypeError) +classImplements(UnboundLocalError, IUnboundLocalError) +classImplements(UnicodeError, IUnicodeError) +classImplements(UserWarning, IUserWarning) +classImplements(ValueError, IValueError) +classImplements(Warning, IWarning) +classImplements(ZeroDivisionError, IZeroDivisionError) + diff --git a/zope/interface/common/mapping.py b/zope/interface/common/mapping.py new file mode 100644 index 0000000..ba29fd8 --- /dev/null +++ b/zope/interface/common/mapping.py @@ -0,0 +1,127 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Mapping Interfaces + +$Id: mapping.py 29359 2005-03-01 15:45:04Z poster $ +""" +from zope.interface import Interface + +class IItemMapping(Interface): + """Simplest readable mapping object + """ + + def __getitem__(key): + """Get a value for a key + + A KeyError is raised if there is no value for the key. + """ + + +class IReadMapping(IItemMapping): + """Basic mapping interface + """ + + def get(key, default=None): + """Get a value for a key + + The default is returned if there is no value for the key. + """ + + def __contains__(key): + """Tell if a key exists in the mapping.""" + + +class IWriteMapping(Interface): + """Mapping methods for changing data""" + + def __delitem__(key): + """Delete a value from the mapping using the key.""" + + def __setitem__(key, value): + """Set a new item in the mapping.""" + + +class IEnumerableMapping(IReadMapping): + """Mapping objects whose items can be enumerated. + """ + + def keys(): + """Return the keys of the mapping object. + """ + + def __iter__(): + """Return an iterator for the keys of the mapping object. + """ + + def values(): + """Return the values of the mapping object. + """ + + def items(): + """Return the items of the mapping object. + """ + + def __len__(): + """Return the number of items. + """ + +class IMapping(IWriteMapping, IEnumerableMapping): + ''' Simple mapping interface ''' + +class IIterableMapping(IEnumerableMapping): + + def iterkeys(): + "iterate over keys; equivalent to __iter__" + + def itervalues(): + "iterate over values" + + def iteritems(): + "iterate over items" + +class IClonableMapping(Interface): + + def copy(): + "return copy of dict" + +class IExtendedReadMapping(IIterableMapping): + + def has_key(key): + """Tell if a key exists in the mapping; equivalent to __contains__""" + +class IExtendedWriteMapping(IWriteMapping): + + def clear(): + "delete all items" + + def update(d): + " Update D from E: for k in E.keys(): D[k] = E[k]" + + def setdefault(key, default=None): + "D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D" + + def pop(k, *args): + """remove specified key and return the corresponding value + *args may contain a single default value, or may not be supplied. + If key is not found, default is returned if given, otherwise + KeyError is raised""" + + def popitem(): + """remove and return some (key, value) pair as a + 2-tuple; but raise KeyError if mapping is empty""" + +class IFullMapping( + IExtendedReadMapping, IExtendedWriteMapping, IClonableMapping, IMapping): + ''' Full mapping interface ''' # IMapping included so tests for IMapping + # succeed with IFullMapping diff --git a/zope/interface/common/sequence.py b/zope/interface/common/sequence.py new file mode 100644 index 0000000..054d137 --- /dev/null +++ b/zope/interface/common/sequence.py @@ -0,0 +1,152 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Sequence Interfaces + +$Id: sequence.py 39752 2005-10-30 20:16:09Z srichter $ +""" +__docformat__ = 'restructuredtext' +from zope import interface + +class IMinimalSequence(interface.Interface): + + def __getitem__(index): + """`x.__getitem__(index)` <==> `x[index]` + + Declaring this interface does not specify whether `__getitem__` + supports slice objects.""" + + def __iter__(): + """`x.__iter__()` <==> `iter(x)`""" + +class IFiniteSequence(IMinimalSequence): + + def __len__(): + """`x.__len__()` <==> `len(x)`""" + +class IReadSequence(IFiniteSequence): + """read interface shared by tuple and list""" + + def __contains__(item): + """`x.__contains__(item)` <==> `item in x`""" + + def __lt__(other): + """`x.__lt__(other)` <==> `x < other`""" + + def __le__(other): + """`x.__le__(other)` <==> `x <= other`""" + + def __eq__(other): + """`x.__eq__(other)` <==> `x == other`""" + + def __ne__(other): + """`x.__ne__(other)` <==> `x != other`""" + + def __gt__(other): + """`x.__gt__(other)` <==> `x > other`""" + + def __ge__(other): + """`x.__ge__(other)` <==> `x >= other`""" + + def __add__(other): + """`x.__add__(other)` <==> `x + other`""" + + def __mul__(n): + """`x.__mul__(n)` <==> `x * n`""" + + def __rmul__(n): + """`x.__rmul__(n)` <==> `n * x`""" + + def __getslice__(i, j): + """`x.__getslice__(i, j)` <==> `x[i:j]` + + Use of negative indices is not supported. + + Deprecated since Python 2.0 but still a part of `UserList`. + """ + +class IExtendedReadSequence(IReadSequence): + """Full read interface for lists""" + + def count(item): + """Return number of occurrences of value""" + + def index(item, *args): + """Return first index of value + + `L.index(value, [start, [stop]])` -> integer""" + +class IUniqueMemberWriteSequence(interface.Interface): + """The write contract for a sequence that may enforce unique members""" + + def __setitem__(index, item): + """`x.__setitem__(index, item)` <==> `x[index] = item` + + Declaring this interface does not specify whether `__setitem__` + supports slice objects. + """ + + def __delitem__(index): + """`x.__delitem__(index)` <==> `del x[index]` + + Declaring this interface does not specify whether `__delitem__` + supports slice objects. + """ + + def __setslice__(i, j, other): + """`x.__setslice__(i, j, other)` <==> `x[i:j]=other` + + Use of negative indices is not supported. + + Deprecated since Python 2.0 but still a part of `UserList`. + """ + + def __delslice__(i, j): + """`x.__delslice__(i, j)` <==> `del x[i:j]` + + Use of negative indices is not supported. + + Deprecated since Python 2.0 but still a part of `UserList`. + """ + def __iadd__(y): + """`x.__iadd__(y)` <==> `x += y`""" + + def append(item): + """Append item to end""" + + def insert(index, item): + """Insert item before index""" + + def pop(index=-1): + """Remove and return item at index (default last)""" + + def remove(item): + """Remove first occurrence of value""" + + def reverse(): + """Reverse *IN PLACE*""" + + def sort(cmpfunc=None): + """Stable sort *IN PLACE*; `cmpfunc(x, y)` -> -1, 0, 1""" + + def extend(iterable): + """Extend list by appending elements from the iterable""" + +class IWriteSequence(IUniqueMemberWriteSequence): + """Full write contract for sequences""" + + def __imul__(n): + """`x.__imul__(n)` <==> `x *= n`""" + +class ISequence(IReadSequence, IWriteSequence): + """Full sequence contract""" diff --git a/zope/interface/declarations.py b/zope/interface/declarations.py new file mode 100644 index 0000000..99e7898 --- /dev/null +++ b/zope/interface/declarations.py @@ -0,0 +1,1386 @@ +############################################################################## +# Copyright (c) 2003 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +############################################################################## +"""Implementation of interface declarations + +There are three flavors of declarations: + + - Declarations are used to simply name declared interfaces. + + - ImplementsDeclarations are used to express the interfaces that a + class implements (that instances of the class provides). + + Implements specifications support inheriting interfaces. + + - ProvidesDeclarations are used to express interfaces directly + provided by objects. + + +$Id: declarations.py 70112 2006-09-12 04:51:16Z baijum $ +""" +__docformat__ = 'restructuredtext' + +import sys +import types +import weakref +from zope.interface.interface import InterfaceClass, Specification +from ro import mergeOrderings, ro +import exceptions +from types import ClassType, ModuleType +from zope.interface.advice import addClassAdvisor + +# Registry of class-implementation specifications +BuiltinImplementationSpecifications = {} + +class Declaration(Specification): + """Interface declarations""" + + def __init__(self, *interfaces): + Specification.__init__(self, _normalizeargs(interfaces)) + + def changed(self, originally_changed): + Specification.changed(self, originally_changed) + try: + del self._v_attrs + except AttributeError: + pass + + def __contains__(self, interface): + """Test whether an interface is in the specification + + for example: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> class I2(I1): pass + ... + >>> class I3(Interface): pass + ... + >>> class I4(I3): pass + ... + >>> spec = Declaration(I2, I3) + >>> spec = Declaration(I4, spec) + >>> int(I1 in spec) + 0 + >>> int(I2 in spec) + 1 + >>> int(I3 in spec) + 1 + >>> int(I4 in spec) + 1 + """ + return self.extends(interface) and interface in self.interfaces() + + def __iter__(self): + """Return an iterator for the interfaces in the specification + + for example: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> class I2(I1): pass + ... + >>> class I3(Interface): pass + ... + >>> class I4(I3): pass + ... + >>> spec = Declaration(I2, I3) + >>> spec = Declaration(I4, spec) + >>> i = iter(spec) + >>> i.next().getName() + 'I4' + >>> i.next().getName() + 'I2' + >>> i.next().getName() + 'I3' + >>> list(i) + [] + """ + return self.interfaces() + + def flattened(self): + """Return an iterator of all included and extended interfaces + + for example: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> class I2(I1): pass + ... + >>> class I3(Interface): pass + ... + >>> class I4(I3): pass + ... + >>> spec = Declaration(I2, I3) + >>> spec = Declaration(I4, spec) + >>> i = spec.flattened() + >>> i.next().getName() + 'I4' + >>> i.next().getName() + 'I2' + >>> i.next().getName() + 'I1' + >>> i.next().getName() + 'I3' + >>> i.next().getName() + 'Interface' + >>> list(i) + [] + + """ + return iter(self.__iro__) + + def __sub__(self, other): + """Remove interfaces from a specification + + Examples: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> class I2(I1): pass + ... + >>> class I3(Interface): pass + ... + >>> class I4(I3): pass + ... + >>> spec = Declaration() + >>> [iface.getName() for iface in spec] + [] + >>> spec -= I1 + >>> [iface.getName() for iface in spec] + [] + >>> spec -= Declaration(I1, I2) + >>> [iface.getName() for iface in spec] + [] + >>> spec = Declaration(I2, I4) + >>> [iface.getName() for iface in spec] + ['I2', 'I4'] + >>> [iface.getName() for iface in spec - I4] + ['I2'] + >>> [iface.getName() for iface in spec - I1] + ['I4'] + >>> [iface.getName() for iface + ... in spec - Declaration(I3, I4)] + ['I2'] + + """ + + return Declaration( + *[i for i in self.interfaces() + if not [j for j in other.interfaces() + if i.extends(j, 0)] + ] + ) + + def __add__(self, other): + """Add two specifications or a specification and an interface + + Examples: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> class I2(I1): pass + ... + >>> class I3(Interface): pass + ... + >>> class I4(I3): pass + ... + >>> spec = Declaration() + >>> [iface.getName() for iface in spec] + [] + >>> [iface.getName() for iface in spec+I1] + ['I1'] + >>> [iface.getName() for iface in I1+spec] + ['I1'] + >>> spec2 = spec + >>> spec += I1 + >>> [iface.getName() for iface in spec] + ['I1'] + >>> [iface.getName() for iface in spec2] + [] + >>> spec2 += Declaration(I3, I4) + >>> [iface.getName() for iface in spec2] + ['I3', 'I4'] + >>> [iface.getName() for iface in spec+spec2] + ['I1', 'I3', 'I4'] + >>> [iface.getName() for iface in spec2+spec] + ['I3', 'I4', 'I1'] + + """ + + seen = {} + result = [] + for i in self.interfaces(): + if i not in seen: + seen[i] = 1 + result.append(i) + for i in other.interfaces(): + if i not in seen: + seen[i] = 1 + result.append(i) + + return Declaration(*result) + + __radd__ = __add__ + + +############################################################################## +# +# Implementation specifications +# +# These specify interfaces implemented by instances of classes + +class Implements(Declaration): + + # class whose specification should be used as additional base + inherit = None + + # interfaces actually declared for a class + declared = () + + __name__ = '?' + + def __repr__(self): + return '<implementedBy %s>' % (self.__name__) + + def __reduce__(self): + return implementedBy, (self.inherit, ) + +def implementedByFallback(cls): + """Return the interfaces implemented for a class' instances + + The value returned is an IDeclaration. + + for example: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> class I2(I1): pass + ... + >>> class I3(Interface): pass + ... + >>> class I4(I3): pass + ... + >>> class C1(object): + ... implements(I2) + >>> class C2(C1): + ... implements(I3) + >>> [i.getName() for i in implementedBy(C2)] + ['I3', 'I2'] + + Really, any object should be able to receive a successful answer, even + an instance: + + >>> class Callable(object): + ... def __call__(self): + ... return self + + >>> implementedBy(Callable()) + <implementedBy zope.interface.declarations.?> + + Note that the name of the spec ends with a '?', because the `Callable` + instance does not have a `__name__` attribute. + """ + # This also manages storage of implementation specifications + + try: + spec = cls.__dict__.get('__implemented__') + except AttributeError: + + # we can't get the class dict. This is probably due to a + # security proxy. If this is the case, then probably no + # descriptor was installed for the class. + + # We don't want to depend directly on zope.security in + # zope.interface, but we'll try to make reasonable + # accommodations in an indirect way. + + # We'll check to see if there's an implements: + + spec = getattr(cls, '__implemented__', None) + if spec is None: + # There's no spec stred in the class. Maybe its a builtin: + spec = BuiltinImplementationSpecifications.get(cls) + if spec is not None: + return spec + return _empty + + if spec.__class__ == Implements: + # we defaulted to _empty or there was a spec. Good enough. + # Return it. + return spec + + # TODO: need old style __implements__ compatibility? + # Hm, there's an __implemented__, but it's not a spec. Must be + # an old-style declaration. Just compute a spec for it + return Declaration(*_normalizeargs((spec, ))) + + if isinstance(spec, Implements): + return spec + + if spec is None: + spec = BuiltinImplementationSpecifications.get(cls) + if spec is not None: + return spec + + # TODO: need old style __implements__ compatibility? + if spec is not None: + # old-style __implemented__ = foo declaration + spec = (spec, ) # tuplefy, as it might be just an int + spec = Implements(*_normalizeargs(spec)) + spec.inherit = None # old-style implies no inherit + del cls.__implemented__ # get rid of the old-style declaration + else: + try: + bases = cls.__bases__ + except AttributeError: + if not callable(cls): + raise TypeError("ImplementedBy called for non-factory", cls) + bases = () + + spec = Implements(*[implementedBy(c) for c in bases]) + spec.inherit = cls + + spec.__name__ = (getattr(cls, '__module__', '?') or '?') + \ + '.' + (getattr(cls, '__name__', '?') or '?') + + try: + cls.__implemented__ = spec + if not hasattr(cls, '__providedBy__'): + cls.__providedBy__ = objectSpecificationDescriptor + + if (isinstance(cls, DescriptorAwareMetaClasses) + and + '__provides__' not in cls.__dict__): + # Make sure we get a __provides__ descriptor + cls.__provides__ = ClassProvides( + cls, + getattr(cls, '__class__', type(cls)), + ) + + except TypeError: + if not isinstance(cls, type): + raise TypeError("ImplementedBy called for non-type", cls) + BuiltinImplementationSpecifications[cls] = spec + + return spec + +implementedBy = implementedByFallback + +def classImplementsOnly(cls, *interfaces): + """Declare the only interfaces implemented by instances of a class + + The arguments after the class are one or more interfaces or interface + specifications (``IDeclaration`` objects). + + The interfaces given (including the interfaces in the specifications) + replace any previous declarations. + + Consider the following example: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> class I2(Interface): pass + ... + >>> class I3(Interface): pass + ... + >>> class I4(Interface): pass + ... + >>> class A(object): + ... implements(I3) + >>> class B(object): + ... implements(I4) + >>> class C(A, B): + ... pass + >>> classImplementsOnly(C, I1, I2) + >>> [i.getName() for i in implementedBy(C)] + ['I1', 'I2'] + + Instances of ``C`` provide only ``I1``, ``I2``, and regardless of + whatever interfaces instances of ``A`` and ``B`` implement. + """ + spec = implementedBy(cls) + spec.declared = () + spec.inherit = None + classImplements(cls, *interfaces) + +def classImplements(cls, *interfaces): + """Declare additional interfaces implemented for instances of a class + + The arguments after the class are one or more interfaces or + interface specifications (``IDeclaration`` objects). + + The interfaces given (including the interfaces in the specifications) + are added to any interfaces previously declared. + + Consider the following example: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> class I2(Interface): pass + ... + >>> class I3(Interface): pass + ... + >>> class I4(Interface): pass + ... + >>> class I5(Interface): pass + ... + >>> class A(object): + ... implements(I3) + >>> class B(object): + ... implements(I4) + >>> class C(A, B): + ... pass + >>> classImplements(C, I1, I2) + >>> [i.getName() for i in implementedBy(C)] + ['I1', 'I2', 'I3', 'I4'] + >>> classImplements(C, I5) + >>> [i.getName() for i in implementedBy(C)] + ['I1', 'I2', 'I5', 'I3', 'I4'] + + Instances of ``C`` provide ``I1``, ``I2``, ``I5``, and whatever + interfaces instances of ``A`` and ``B`` provide. + """ + + spec = implementedBy(cls) + spec.declared += tuple(_normalizeargs(interfaces)) + + # compute the bases + bases = [] + seen = {} + for b in spec.declared: + if b not in seen: + seen[b] = 1 + bases.append(b) + + if spec.inherit is not None: + + for c in spec.inherit.__bases__: + b = implementedBy(c) + if b not in seen: + seen[b] = 1 + bases.append(b) + + spec.__bases__ = tuple(bases) + +def _implements_advice(cls): + interfaces, classImplements = cls.__dict__['__implements_advice_data__'] + del cls.__implements_advice_data__ + classImplements(cls, *interfaces) + return cls + + +class implementer: + + def __init__(self, *interfaces): + self.interfaces = interfaces + + def __call__(self, ob): + if isinstance(ob, DescriptorAwareMetaClasses): + raise TypeError("Can't use implementer with classes. Use one of " + "the class-declaration functions instead." + ) + spec = Implements(*self.interfaces) + try: + ob.__implemented__ = spec + except AttributeError: + raise TypeError("Can't declare implements", ob) + return ob + +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) + +def implements(*interfaces): + """Declare interfaces implemented by instances of a class + + This function is called in a class definition. + + The arguments are one or more interfaces or interface + specifications (IDeclaration objects). + + The interfaces given (including the interfaces in the + specifications) are added to any interfaces previously + declared. + + Previous declarations include declarations for base classes + unless implementsOnly was used. + + This function is provided for convenience. It provides a more + convenient way to call classImplements. For example:: + + implements(I1) + + is equivalent to calling:: + + classImplements(C, I1) + + after the class has been created. + + Consider the following example:: + + + >>> from zope.interface import Interface + >>> class IA1(Interface): pass + ... + >>> class IA2(Interface): pass + ... + >>> class IB(Interface): pass + ... + >>> class IC(Interface): pass + ... + >>> class A(object): implements(IA1, IA2) + ... + >>> class B(object): implements(IB) + ... + + >>> class C(A, B): + ... implements(IC) + + >>> ob = C() + >>> int(IA1 in providedBy(ob)) + 1 + >>> int(IA2 in providedBy(ob)) + 1 + >>> int(IB in providedBy(ob)) + 1 + >>> int(IC in providedBy(ob)) + 1 + + Instances of ``C`` implement ``I1``, ``I2``, and whatever interfaces + instances of ``A`` and ``B`` implement. + + """ + _implements("implements", interfaces, classImplements) + +def implementsOnly(*interfaces): + """Declare the only interfaces implemented by instances of a class + + This function is called in a class definition. + + The arguments are one or more interfaces or interface + specifications (IDeclaration objects). + + Previous declarations including declarations for base classes + are overridden. + + This function is provided for convenience. It provides a more + convenient way to call classImplementsOnly. For example:: + + implementsOnly(I1) + + is equivalent to calling:: + + classImplementsOnly(I1) + + after the class has been created. + + Consider the following example:: + + >>> from zope.interface import Interface + >>> class IA1(Interface): pass + ... + >>> class IA2(Interface): pass + ... + >>> class IB(Interface): pass + ... + >>> class IC(Interface): pass + ... + >>> class A(object): implements(IA1, IA2) + ... + >>> class B(object): implements(IB) + ... + + >>> class C(A, B): + ... implementsOnly(IC) + + >>> ob = C() + >>> int(IA1 in providedBy(ob)) + 0 + >>> int(IA2 in providedBy(ob)) + 0 + >>> int(IB in providedBy(ob)) + 0 + >>> int(IC in providedBy(ob)) + 1 + + + Instances of ``C`` implement ``IC``, regardless of what + instances of ``A`` and ``B`` implement. + + """ + _implements("implementsOnly", interfaces, classImplementsOnly) + +############################################################################## +# +# Instance declarations + +class Provides(Declaration): # Really named ProvidesClass + """Implement __provides__, the instance-specific specification + + When an object is pickled, we pickle the interfaces that it implements. + """ + + def __init__(self, cls, *interfaces): + self.__args = (cls, ) + interfaces + self._cls = cls + Declaration.__init__(self, *(interfaces + (implementedBy(cls), ))) + + def __reduce__(self): + return Provides, self.__args + + __module__ = 'zope.interface' + + def __get__(self, inst, cls): + """Make sure that a class __provides__ doesn't leak to an instance + + For example: + + >>> from zope.interface import Interface + >>> class IFooFactory(Interface): pass + ... + + >>> class C(object): + ... pass + + >>> C.__provides__ = ProvidesClass(C, IFooFactory) + >>> [i.getName() for i in C.__provides__] + ['IFooFactory'] + >>> getattr(C(), '__provides__', 0) + 0 + + """ + if inst is None and cls is self._cls: + # We were accessed through a class, so we are the class' + # provides spec. Just return this object, but only if we are + # being called on the same class that we were defined for: + return self + + raise AttributeError('__provides__') + +ProvidesClass = Provides + +# Registry of instance declarations +# This is a memory optimization to allow objects to share specifications. +InstanceDeclarations = weakref.WeakValueDictionary() + +def Provides(*interfaces): + """Cache instance declarations + + Instance declarations are shared among instances that have the same + declaration. The declarations are cached in a weak value dictionary. + + (Note that, in the examples below, we are going to make assertions about + the size of the weakvalue dictionary. For the assertions to be + meaningful, we need to force garbage collection to make sure garbage + objects are, indeed, removed from the system. Depending on how Python + is run, we may need to make multiple calls to be sure. We provide a + collect function to help with this: + + >>> import gc + >>> def collect(): + ... for i in range(4): + ... gc.collect() + + ) + + >>> collect() + >>> before = len(InstanceDeclarations) + + >>> class C(object): + ... pass + + >>> from zope.interface import Interface + >>> class I(Interface): + ... pass + + >>> c1 = C() + >>> c2 = C() + + >>> len(InstanceDeclarations) == before + 1 + + >>> directlyProvides(c1, I) + >>> len(InstanceDeclarations) == before + 1 + 1 + + >>> directlyProvides(c2, I) + >>> len(InstanceDeclarations) == before + 1 + 1 + + >>> del c1 + >>> collect() + >>> len(InstanceDeclarations) == before + 1 + 1 + + >>> del c2 + >>> collect() + >>> len(InstanceDeclarations) == before + 1 + """ + + spec = InstanceDeclarations.get(interfaces) + if spec is None: + spec = ProvidesClass(*interfaces) + InstanceDeclarations[interfaces] = spec + + return spec +Provides.__safe_for_unpickling__ = True + + +DescriptorAwareMetaClasses = ClassType, type +def directlyProvides(object, *interfaces): + """Declare interfaces declared directly for an object + + The arguments after the object are one or more interfaces or interface + specifications (``IDeclaration`` objects). + + The interfaces given (including the interfaces in the specifications) + replace interfaces previously declared for the object. + + Consider the following example: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> class I2(Interface): pass + ... + >>> class IA1(Interface): pass + ... + >>> class IA2(Interface): pass + ... + >>> class IB(Interface): pass + ... + >>> class IC(Interface): pass + ... + >>> class A(object): implements(IA1, IA2) + ... + >>> class B(object): implements(IB) + ... + + >>> class C(A, B): + ... implements(IC) + + >>> ob = C() + >>> directlyProvides(ob, I1, I2) + >>> int(I1 in providedBy(ob)) + 1 + >>> int(I2 in providedBy(ob)) + 1 + >>> int(IA1 in providedBy(ob)) + 1 + >>> int(IA2 in providedBy(ob)) + 1 + >>> int(IB in providedBy(ob)) + 1 + >>> int(IC in providedBy(ob)) + 1 + + The object, ``ob`` provides ``I1``, ``I2``, and whatever interfaces + instances have been declared for instances of ``C``. + + To remove directly provided interfaces, use ``directlyProvidedBy`` and + subtract the unwanted interfaces. For example: + + >>> directlyProvides(ob, directlyProvidedBy(ob)-I2) + >>> int(I1 in providedBy(ob)) + 1 + >>> int(I2 in providedBy(ob)) + 0 + + removes I2 from the interfaces directly provided by ``ob``. The object, + ``ob`` no longer directly provides ``I2``, although it might still + provide ``I2`` if it's class implements ``I2``. + + To add directly provided interfaces, use ``directlyProvidedBy`` and + include additional interfaces. For example: + + >>> int(I2 in providedBy(ob)) + 0 + >>> directlyProvides(ob, directlyProvidedBy(ob), I2) + + adds ``I2`` to the interfaces directly provided by ob:: + + >>> int(I2 in providedBy(ob)) + 1 + + """ + + # We need to avoid setting this attribute on meta classes that + # don't support descriptors. + # We can do away with this check when we get rid of the old EC + cls = getattr(object, '__class__', None) + if cls is not None and getattr(cls, '__class__', None) is cls: + # It's a meta class (well, at least it it could be an extension class) + if not isinstance(object, DescriptorAwareMetaClasses): + raise TypeError("Attempt to make an interface declaration on a " + "non-descriptor-aware class") + + interfaces = _normalizeargs(interfaces) + if cls is None: + cls = type(object) + + issub = False + for damc in DescriptorAwareMetaClasses: + if issubclass(cls, damc): + issub = True + break + if issub: + # we have a class or type. We'll use a special descriptor + # that provides some extra caching + object.__provides__ = ClassProvides(object, cls, *interfaces) + else: + object.__provides__ = Provides(cls, *interfaces) + + +def alsoProvides(object, *interfaces): + """Declare interfaces declared directly for an object + + The arguments after the object are one or more interfaces or interface + specifications (``IDeclaration`` objects). + + The interfaces given (including the interfaces in the specifications) are + added to the interfaces previously declared for the object. + + Consider the following example: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> class I2(Interface): pass + ... + >>> class IA1(Interface): pass + ... + >>> class IA2(Interface): pass + ... + >>> class IB(Interface): pass + ... + >>> class IC(Interface): pass + ... + >>> class A(object): implements(IA1, IA2) + ... + >>> class B(object): implements(IB) + ... + + >>> class C(A, B): + ... implements(IC) + + >>> ob = C() + >>> directlyProvides(ob, I1) + >>> int(I1 in providedBy(ob)) + 1 + >>> int(I2 in providedBy(ob)) + 0 + >>> int(IA1 in providedBy(ob)) + 1 + >>> int(IA2 in providedBy(ob)) + 1 + >>> int(IB in providedBy(ob)) + 1 + >>> int(IC in providedBy(ob)) + 1 + + >>> alsoProvides(ob, I2) + >>> int(I1 in providedBy(ob)) + 1 + >>> int(I2 in providedBy(ob)) + 1 + >>> int(IA1 in providedBy(ob)) + 1 + >>> int(IA2 in providedBy(ob)) + 1 + >>> int(IB in providedBy(ob)) + 1 + >>> int(IC in providedBy(ob)) + 1 + + The object, ``ob`` provides ``I1``, ``I2``, and whatever interfaces + instances have been declared for instances of ``C``. Notice that the + alsoProvides just extends the provided interfaces. + """ + directlyProvides(object, directlyProvidedBy(object), *interfaces) + +def noLongerProvides(object, interface): + """ + This removes a directly provided interface from an object. + Consider the following two interfaces: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> class I2(Interface): pass + ... + + ``I1`` is provided through the class, ``I2`` is directly provided + by the object: + + >>> class C(object): + ... implements(I1) + >>> c = C() + >>> alsoProvides(c, I2) + >>> I2.providedBy(c) + True + + Remove I2 from c again: + + >>> noLongerProvides(c, I2) + >>> I2.providedBy(c) + False + + Removing an interface that is provided through the class is not possible: + + >>> noLongerProvides(c, I1) + Traceback (most recent call last): + ... + ValueError: Can only remove directly provided interfaces. + + """ + directlyProvides(object, directlyProvidedBy(object)-interface) + if interface.providedBy(object): + raise ValueError("Can only remove directly provided interfaces.") + +class ClassProvidesBasePy(object): + + def __get__(self, inst, cls): + if cls is self._cls: + # We only work if called on the class we were defined for + + if inst is None: + # We were accessed through a class, so we are the class' + # provides spec. Just return this object as is: + return self + + return self._implements + + raise AttributeError('__provides__') + +ClassProvidesBase = ClassProvidesBasePy + +# Try to get C base: +try: + import _zope_interface_coptimizations +except ImportError: + pass +else: + from _zope_interface_coptimizations import ClassProvidesBase + + +class ClassProvides(Declaration, ClassProvidesBase): + """Special descriptor for class __provides__ + + The descriptor caches the implementedBy info, so that + we can get declarations for objects without instance-specific + interfaces a bit quicker. + + For example: + + >>> from zope.interface import Interface + >>> class IFooFactory(Interface): + ... pass + >>> class IFoo(Interface): + ... pass + >>> class C(object): + ... implements(IFoo) + ... classProvides(IFooFactory) + >>> [i.getName() for i in C.__provides__] + ['IFooFactory'] + + >>> [i.getName() for i in C().__provides__] + ['IFoo'] + """ + + def __init__(self, cls, metacls, *interfaces): + self._cls = cls + self._implements = implementedBy(cls) + self.__args = (cls, metacls, ) + interfaces + Declaration.__init__(self, *(interfaces + (implementedBy(metacls), ))) + + def __reduce__(self): + return self.__class__, self.__args + + # Copy base-class method for speed + __get__ = ClassProvidesBase.__get__ + +def directlyProvidedBy(object): + """Return the interfaces directly provided by the given object + + The value returned is an ``IDeclaration``. + """ + provides = getattr(object, "__provides__", None) + if (provides is None # no spec + or + # We might have gotten the implements spec, as an + # optimization. If so, it's like having only one base, that we + # lop off to exclude class-supplied declarations: + isinstance(provides, Implements) + ): + return _empty + + # Strip off the class part of the spec: + return Declaration(provides.__bases__[:-1]) + +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 directlyProvidedByProvides for a class. For example:: + + classProvides(I1) + + is equivalent to calling:: + + directlyProvides(theclass, I1) + + after the class has been created. + + For example: + + >>> from 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 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) + +def _classProvides_advice(cls): + interfaces = cls.__dict__['__provides__'] + del cls.__provides__ + directlyProvides(cls, *interfaces) + return cls + +def moduleProvides(*interfaces): + """Declare interfaces provided by a module + + This function is used in a module 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 module's direct-object interface specification. An + error will be raised if the module already has an interface specification. + In other words, it is an error to call this function more than once in a + module definition. + + This function is provided for convenience. It provides a more convenient + way to call directlyProvides. For example:: + + moduleImplements(I1) + + is equivalent to:: + + directlyProvides(sys.modules[__name__], I1) + """ + frame = sys._getframe(1) + locals = frame.f_locals + + # Try to make sure we were called from a class def + if (locals is not frame.f_globals) or ('__name__' not in locals): + raise TypeError( + "moduleProvides can only be used from a module definition.") + + if '__provides__' in locals: + raise TypeError( + "moduleProvides can only be used once in a module definition.") + + locals["__provides__"] = Provides(ModuleType, + *_normalizeargs(interfaces)) + +############################################################################## +# +# Declaration querying support + +def ObjectSpecification(direct, cls): + """Provide object specifications + + These combine information for the object and for it's classes. + + For example: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> class I2(Interface): pass + ... + >>> class I3(Interface): pass + ... + >>> class I31(I3): pass + ... + >>> class I4(Interface): pass + ... + >>> class I5(Interface): pass + ... + >>> class A(object): implements(I1) + ... + >>> class B(object): __implemented__ = I2 + ... + >>> class C(A, B): implements(I31) + ... + >>> c = C() + >>> directlyProvides(c, I4) + >>> [i.getName() for i in providedBy(c)] + ['I4', 'I31', 'I1', 'I2'] + >>> [i.getName() for i in providedBy(c).flattened()] + ['I4', 'I31', 'I3', 'I1', 'I2', 'Interface'] + >>> int(I1 in providedBy(c)) + 1 + >>> int(I3 in providedBy(c)) + 0 + >>> int(providedBy(c).extends(I3)) + 1 + >>> int(providedBy(c).extends(I31)) + 1 + >>> int(providedBy(c).extends(I5)) + 0 + >>> class COnly(A, B): implementsOnly(I31) + ... + >>> class D(COnly): implements(I5) + ... + >>> c = D() + >>> directlyProvides(c, I4) + >>> [i.getName() for i in providedBy(c)] + ['I4', 'I5', 'I31'] + >>> [i.getName() for i in providedBy(c).flattened()] + ['I4', 'I5', 'I31', 'I3', 'Interface'] + >>> int(I1 in providedBy(c)) + 0 + >>> int(I3 in providedBy(c)) + 0 + >>> int(providedBy(c).extends(I3)) + 1 + >>> int(providedBy(c).extends(I1)) + 0 + >>> int(providedBy(c).extends(I31)) + 1 + >>> int(providedBy(c).extends(I5)) + 1 + """ + + return Provides(cls, direct) + +def getObjectSpecification(ob): + + provides = getattr(ob, '__provides__', None) + if provides is not None: + return provides + + try: + cls = ob.__class__ + except AttributeError: + # We can't get the class, so just consider provides + return _empty + + return implementedBy(cls) + +def providedBy(ob): + + # Here we have either a special object, an old-style declaration + # or a descriptor + + # Try to get __providedBy__ + try: + r = ob.__providedBy__ + except AttributeError: + # Not set yet. Fall back to lower-level thing that computes it + return getObjectSpecification(ob) + + try: + # We might have gotten a descriptor from an instance of a + # class (like an ExtensionClass) that doesn't support + # descriptors. We'll make sure we got one by trying to get + # the only attribute, which all specs have. + r.extends + + except AttributeError: + + # The object's class doesn't understand descriptors. + # Sigh. We need to get an object descriptor, but we have to be + # careful. We want to use the instance's __provides__, if + # there is one, but only if it didn't come from the class. + + try: + r = ob.__provides__ + except AttributeError: + # No __provides__, so just fall back to implementedBy + return implementedBy(ob.__class__) + + # We need to make sure we got the __provides__ from the + # instance. We'll do this by making sure we don't get the same + # thing from the class: + + try: + cp = ob.__class__.__provides__ + except AttributeError: + # The ob doesn't have a class or the class has no + # provides, assume we're done: + return r + + if r is cp: + # Oops, we got the provides from the class. This means + # the object doesn't have it's own. We should use implementedBy + return implementedBy(ob.__class__) + + return r + +class ObjectSpecificationDescriptorPy(object): + """Implement the `__providedBy__` attribute + + The `__providedBy__` attribute computes the interfaces peovided by + an object. + """ + + def __get__(self, inst, cls): + """Get an object specification for an object + + For example: + + >>> from 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'] + + """ + + # Get an ObjectSpecification bound to either an instance or a class, + # depending on how we were accessed. + + if inst is None: + return getObjectSpecification(cls) + + provides = getattr(inst, '__provides__', None) + if provides is not None: + return provides + + return implementedBy(cls) + +ObjectSpecificationDescriptor = ObjectSpecificationDescriptorPy + +############################################################################## + +def _normalizeargs(sequence, output = None): + """Normalize declaration arguments + + Normalization arguments might contain Declarions, tuples, or single + interfaces. + + Anything but individial interfaces or implements specs will be expanded. + """ + if output is None: + output = [] + + cls = sequence.__class__ + if InterfaceClass in cls.__mro__ or Implements in cls.__mro__: + output.append(sequence) + else: + for v in sequence: + _normalizeargs(v, output) + + return output + +_empty = Declaration() + +try: + import _zope_interface_coptimizations +except ImportError: + pass +else: + from _zope_interface_coptimizations import implementedBy, providedBy + from _zope_interface_coptimizations import getObjectSpecification + from _zope_interface_coptimizations import ObjectSpecificationDescriptor + +objectSpecificationDescriptor = ObjectSpecificationDescriptor() diff --git a/zope/interface/document.py b/zope/interface/document.py new file mode 100644 index 0000000..282dadf --- /dev/null +++ b/zope/interface/document.py @@ -0,0 +1,107 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +""" Pretty-Print an Interface object as structured text (Yum) + +This module provides a function, asStructuredText, for rendering an +interface as structured text. + +$Id: document.py 39768 2005-10-31 13:57:35Z tlotze $ +""" +import zope.interface + +def asStructuredText(I, munge=0): + """ Output structured text format. Note, this will whack any existing + 'structured' format of the text. """ + + r = [I.getName()] + outp = r.append + level = 1 + + if I.getDoc(): + outp(_justify_and_indent(_trim_doc_string(I.getDoc()), level)) + + bases = [base + for base in I.__bases__ + if base is not zope.interface.Interface + ] + if bases: + outp(_justify_and_indent("This interface extends:", level, munge)) + level += 1 + for b in bases: + item = "o %s" % b.getName() + outp(_justify_and_indent(_trim_doc_string(item), level, munge)) + level -= 1 + + namesAndDescriptions = I.namesAndDescriptions() + namesAndDescriptions.sort() + + outp(_justify_and_indent("Attributes:", level, munge)) + level += 1 + for name, desc in namesAndDescriptions: + if not hasattr(desc, 'getSignatureString'): # ugh... + item = "%s -- %s" % (desc.getName(), + desc.getDoc() or 'no documentation') + outp(_justify_and_indent(_trim_doc_string(item), level, munge)) + level -= 1 + + outp(_justify_and_indent("Methods:", level, munge)) + level += 1 + for name, desc in namesAndDescriptions: + if hasattr(desc, 'getSignatureString'): # ugh... + item = "%s%s -- %s" % (desc.getName(), + desc.getSignatureString(), + desc.getDoc() or 'no documentation') + outp(_justify_and_indent(_trim_doc_string(item), level, munge)) + + return "\n\n".join(r) + "\n\n" + + +def _trim_doc_string(text): + """ Trims a doc string to make it format + correctly with structured text. """ + + lines = text.replace('\r\n', '\n').split('\n') + nlines = [lines.pop(0)] + if lines: + min_indent = min([len(line) - len(line.lstrip()) + for line in lines]) + for line in lines: + nlines.append(line[min_indent:]) + + return '\n'.join(nlines) + + +def _justify_and_indent(text, level, munge=0, width=72): + """ indent and justify text, rejustify (munge) if specified """ + + indent = " " * level + + if munge: + lines = [] + line = indent + text = text.split() + + for word in text: + line = ' '.join([line, word]) + if len(line) > width: + lines.append(line) + line = indent + else: + lines.append(line) + + return '\n'.join(lines) + + else: + return indent + \ + text.strip().replace("\r\n", "\n") .replace("\n", "\n" + indent) diff --git a/zope/interface/exceptions.py b/zope/interface/exceptions.py new file mode 100644 index 0000000..c19171b --- /dev/null +++ b/zope/interface/exceptions.py @@ -0,0 +1,69 @@ +############################################################################## +# +# Copyright (c) 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Interface-specific exceptions + +$Id: exceptions.py 67174 2006-04-20 14:11:55Z flindner $ +""" + +class Invalid(Exception): + """A specification is violated + """ + +class DoesNotImplement(Invalid): + """ This object does not implement """ + def __init__(self, interface): + self.interface = interface + + def __str__(self): + return """An object does not implement interface %(interface)s + + """ % self.__dict__ + +class BrokenImplementation(Invalid): + """An attribute is not completely implemented. + """ + + def __init__(self, interface, name): + self.interface=interface + self.name=name + + def __str__(self): + return """An object has failed to implement interface %(interface)s + + The %(name)s attribute was not provided. + """ % self.__dict__ + +class BrokenMethodImplementation(Invalid): + """An method is not completely implemented. + """ + + def __init__(self, method, mess): + self.method=method + self.mess=mess + + def __str__(self): + return """The implementation of %(method)s violates its contract + because %(mess)s. + """ % self.__dict__ + +class InvalidInterface(Exception): + """The interface has invalid contents + """ + +class BadImplements(TypeError): + """An implementation assertion is invalid + + because it doesn't contain an interface or a sequence of valid + implementation assertions. + """ diff --git a/zope/interface/interface.py b/zope/interface/interface.py new file mode 100644 index 0000000..3eff8b6 --- /dev/null +++ b/zope/interface/interface.py @@ -0,0 +1,821 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Interface object implementation + +$Id: interface.py 67761 2006-04-30 13:56:44Z jim $ +""" + +from __future__ import generators + +import sys +import warnings +import weakref +from types import FunctionType +from ro import ro +from zope.interface.exceptions import Invalid + +CO_VARARGS = 4 +CO_VARKEYWORDS = 8 +TAGGED_DATA = '__interface_tagged_values__' + +_decorator_non_return = object() + +def invariant(call): + f_locals = sys._getframe(1).f_locals + tags = f_locals.setdefault(TAGGED_DATA, {}) + invariants = tags.setdefault('invariants', []) + invariants.append(call) + return _decorator_non_return + +class Element(object): + + # We can't say this yet because we don't have enough + # infrastructure in place. + # + #implements(IElement) + + def __init__(self, __name__, __doc__=''): + """Create an 'attribute' description + """ + if not __doc__ and __name__.find(' ') >= 0: + __doc__ = __name__ + __name__ = None + + self.__name__=__name__ + self.__doc__=__doc__ + self.__tagged_values = {} + + def getName(self): + """ Returns the name of the object. """ + return self.__name__ + + def getDoc(self): + """ Returns the documentation for the object. """ + return self.__doc__ + + def getTaggedValue(self, tag): + """ Returns the value associated with 'tag'. """ + return self.__tagged_values[tag] + + def queryTaggedValue(self, tag, default=None): + """ Returns the value associated with 'tag'. """ + return self.__tagged_values.get(tag, default) + + def getTaggedValueTags(self): + """ Returns a list of all tags. """ + return self.__tagged_values.keys() + + def setTaggedValue(self, tag, value): + """ Associates 'value' with 'key'. """ + self.__tagged_values[tag] = value + +class SpecificationBasePy(object): + + def providedBy(self, ob): + """Is the interface implemented by an object + + >>> from zope.interface import * + >>> class I1(Interface): + ... pass + >>> class C(object): + ... implements(I1) + >>> c = C() + >>> class X(object): + ... pass + >>> x = X() + >>> I1.providedBy(x) + False + >>> I1.providedBy(C) + False + >>> I1.providedBy(c) + True + >>> directlyProvides(x, I1) + >>> I1.providedBy(x) + True + >>> directlyProvides(C, I1) + >>> I1.providedBy(C) + True + + """ + spec = providedBy(ob) + return self in spec._implied + + def implementedBy(self, cls): + """Test whether the specification is implemented by a class or factory. + Raise TypeError if argument is neither a class nor a callable.""" + spec = implementedBy(cls) + return self in spec._implied + + def isOrExtends(self, interface): + """Is the interface the same as or extend the given interface + + Examples:: + + >>> from zope.interface import Interface + >>> from zope.interface.declarations import Declaration + >>> class I1(Interface): pass + ... + >>> class I2(I1): pass + ... + >>> class I3(Interface): pass + ... + >>> class I4(I3): pass + ... + >>> spec = Declaration() + >>> int(spec.extends(Interface)) + 1 + >>> spec = Declaration(I2) + >>> int(spec.extends(Interface)) + 1 + >>> int(spec.extends(I1)) + 1 + >>> int(spec.extends(I2)) + 1 + >>> int(spec.extends(I3)) + 0 + >>> int(spec.extends(I4)) + 0 + + """ + return interface in self._implied + + __call__ = isOrExtends + +SpecificationBase = SpecificationBasePy + +_marker = object() +class InterfaceBasePy(object): + """Base class that wants to be replaced with a C base :) + """ + + def __call__(self, obj, alternate=_marker): + """Adapt an object to the interface + """ + conform = getattr(obj, '__conform__', None) + if conform is not None: + adapter = self._call_conform(conform) + if adapter is not None: + return adapter + + adapter = self.__adapt__(obj) + + if adapter is not None: + return adapter + elif alternate is not _marker: + return alternate + else: + raise TypeError("Could not adapt", obj, self) + + def __adapt__(self, obj): + """Adapt an object to the reciever + """ + if self.providedBy(obj): + return obj + + for hook in adapter_hooks: + adapter = hook(self, obj) + if adapter is not None: + return adapter + +InterfaceBase = InterfaceBasePy + +adapter_hooks = [] + +try: + import _zope_interface_coptimizations +except ImportError: + pass +else: + from _zope_interface_coptimizations import SpecificationBase + from _zope_interface_coptimizations import InterfaceBase, adapter_hooks + +class Specification(SpecificationBase): + """Specifications + + An interface specification is used to track interface declarations + and component registrations. + + This class is a base class for both interfaces themselves and for + interface specifications (declarations). + + Specifications are mutable. If you reassign their cases, their + relations with other specifications are adjusted accordingly. + + For example: + + >>> from zope.interface import Interface + >>> class I1(Interface): + ... pass + >>> class I2(I1): + ... pass + >>> class I3(I2): + ... pass + + >>> [i.__name__ for i in I1.__bases__] + ['Interface'] + + >>> [i.__name__ for i in I2.__bases__] + ['I1'] + + >>> I3.extends(I1) + 1 + + >>> I2.__bases__ = (Interface, ) + + >>> [i.__name__ for i in I2.__bases__] + ['Interface'] + + >>> I3.extends(I1) + 0 + + """ + + # Copy some base class methods for speed + isOrExtends = SpecificationBase.isOrExtends + providedBy = SpecificationBase.providedBy + + ######################################################################### + # BBB 2004-07-13: Backward compatabilty. These methods have been + # deprecated in favour of providedBy and implementedBy. + + def isImplementedByInstancesOf(self, cls): + warnings.warn( + "isImplementedByInstancesOf has been renamed to implementedBy", + DeprecationWarning, stacklevel=2, + ) + return self.implementedBy(cls) + + def isImplementedBy(self, ob): + warnings.warn( + "isImplementedBy has been renamed to providedBy", + DeprecationWarning, stacklevel=2, + ) + return self.providedBy(ob) + # + ######################################################################### + + def __init__(self, bases=()): + self._implied = {} + self.dependents = weakref.WeakKeyDictionary() + self.__bases__ = tuple(bases) + + def subscribe(self, dependent): + self.dependents[dependent] = self.dependents.get(dependent, 0) + 1 + + def unsubscribe(self, dependent): + n = self.dependents.get(dependent, 0) - 1 + if not n: + del self.dependents[dependent] + elif n > 0: + self.dependents[dependent] = n + else: + raise KeyError(dependent) + + def __setBases(self, bases): + # Register ourselves as a dependent of our old bases + for b in self.__bases__: + b.unsubscribe(self) + + # Register ourselves as a dependent of our bases + self.__dict__['__bases__'] = bases + for b in bases: + b.subscribe(self) + + self.changed(self) + + __bases__ = property( + + lambda self: self.__dict__.get('__bases__', ()), + __setBases, + ) + + def changed(self, originally_changed): + """We, or something we depend on, have changed + """ + + implied = self._implied + implied.clear() + + ancestors = ro(self) + + try: + if Interface not in ancestors: + ancestors.append(Interface) + except NameError: + pass # defining Interface itself + + self.__sro__ = tuple(ancestors) + self.__iro__ = tuple([ancestor for ancestor in ancestors + if isinstance(ancestor, InterfaceClass) + ]) + + for ancestor in ancestors: + # We directly imply our ancestors: + implied[ancestor] = () + + # Now, advise our dependents of change: + for dependent in self.dependents.keys(): + dependent.changed(originally_changed) + + + def interfaces(self): + """Return an iterator for the interfaces in the specification + + for example:: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> class I2(I1): pass + ... + >>> class I3(Interface): pass + ... + >>> class I4(I3): pass + ... + >>> spec = Specification((I2, I3)) + >>> spec = Specification((I4, spec)) + >>> i = spec.interfaces() + >>> i.next().getName() + 'I4' + >>> i.next().getName() + 'I2' + >>> i.next().getName() + 'I3' + >>> list(i) + [] + """ + seen = {} + for base in self.__bases__: + for interface in base.interfaces(): + if interface not in seen: + seen[interface] = 1 + yield interface + + + def extends(self, interface, strict=True): + """Does the specification extend the given interface? + + Test whether an interface in the specification extends the + given interface + + Examples:: + + >>> from zope.interface import Interface + >>> from zope.interface.declarations import Declaration + >>> class I1(Interface): pass + ... + >>> class I2(I1): pass + ... + >>> class I3(Interface): pass + ... + >>> class I4(I3): pass + ... + >>> spec = Declaration() + >>> int(spec.extends(Interface)) + 1 + >>> spec = Declaration(I2) + >>> int(spec.extends(Interface)) + 1 + >>> int(spec.extends(I1)) + 1 + >>> int(spec.extends(I2)) + 1 + >>> int(spec.extends(I3)) + 0 + >>> int(spec.extends(I4)) + 0 + >>> I2.extends(I2) + 0 + >>> I2.extends(I2, False) + 1 + >>> I2.extends(I2, strict=False) + 1 + + """ + return ((interface in self._implied) + and + ((not strict) or (self != interface)) + ) + + def weakref(self, callback=None): + return weakref.ref(self, callback) + + def get(self, name, default=None): + """Query for an attribute description + """ + try: + attrs = self._v_attrs + except AttributeError: + attrs = self._v_attrs = {} + attr = attrs.get(name) + if attr is None: + for iface in self.__iro__: + attr = iface.direct(name) + if attr is not None: + attrs[name] = attr + break + + if attr is None: + return default + else: + return attr + +class InterfaceClass(Element, InterfaceBase, Specification): + """Prototype (scarecrow) Interfaces Implementation.""" + + # We can't say this yet because we don't have enough + # infrastructure in place. + # + #implements(IInterface) + + def __init__(self, name, bases=(), attrs=None, __doc__=None, + __module__=None): + + if attrs is None: + attrs = {} + + if __module__ is None: + __module__ = attrs.get('__module__') + if isinstance(__module__, str): + 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__ = sys._getframe(1).f_globals['__name__'] + except (AttributeError, KeyError): + pass + + self.__module__ = __module__ + + d = attrs.get('__doc__') + if d is not None: + if not isinstance(d, Attribute): + if __doc__ is None: + __doc__ = d + del attrs['__doc__'] + + if __doc__ is None: + __doc__ = '' + + Element.__init__(self, name, __doc__) + + tagged_data = attrs.pop(TAGGED_DATA, None) + if tagged_data is not None: + for key, val in tagged_data.items(): + self.setTaggedValue(key, val) + + for base in bases: + if not isinstance(base, InterfaceClass): + raise TypeError('Expected base interfaces') + + Specification.__init__(self, bases) + + # Make sure that all recorded attributes (and methods) are of type + # `Attribute` and `Method` + for name, attr in attrs.items(): + if isinstance(attr, Attribute): + attr.interface = self + if not attr.__name__: + attr.__name__ = name + elif isinstance(attr, FunctionType): + attrs[name] = fromFunction(attr, self, name=name) + elif attr is _decorator_non_return: + del attrs[name] + else: + raise InvalidInterface("Concrete attribute, " + name) + + self.__attrs = attrs + + self.__identifier__ = "%s.%s" % (self.__module__, self.__name__) + + def interfaces(self): + """Return an iterator for the interfaces in the specification + + for example:: + + >>> from zope.interface import Interface + >>> class I1(Interface): pass + ... + >>> + >>> i = I1.interfaces() + >>> i.next().getName() + 'I1' + >>> list(i) + [] + """ + yield self + + def getBases(self): + return self.__bases__ + + def isEqualOrExtendedBy(self, other): + """Same interface or extends?""" + return self == other or other.extends(self) + + def names(self, all=False): + """Return the attribute names defined by the interface.""" + if not all: + return self.__attrs.keys() + + r = self.__attrs.copy() + + for base in self.__bases__: + r.update(dict.fromkeys(base.names(all))) + + return r.keys() + + def __iter__(self): + return iter(self.names(all=True)) + + def namesAndDescriptions(self, all=False): + """Return attribute names and descriptions defined by interface.""" + if not all: + return self.__attrs.items() + + r = {} + for base in self.__bases__[::-1]: + r.update(dict(base.namesAndDescriptions(all))) + + r.update(self.__attrs) + + return r.items() + + def getDescriptionFor(self, name): + """Return the attribute description for the given name.""" + r = self.get(name) + if r is not None: + return r + + raise KeyError(name) + + __getitem__ = getDescriptionFor + + def __contains__(self, name): + return self.get(name) is not None + + def direct(self, name): + return self.__attrs.get(name) + + def queryDescriptionFor(self, name, default=None): + return self.get(name, default) + + def deferred(self): + """Return a defered class corresponding to the interface.""" + if hasattr(self, "_deferred"): return self._deferred + + klass={} + exec "class %s: pass" % self.__name__ in klass + klass=klass[self.__name__] + + self.__d(klass.__dict__) + + self._deferred=klass + + return klass + + def validateInvariants(self, obj, errors=None): + """validate object to defined invariants.""" + for call in self.queryTaggedValue('invariants', []): + try: + call(obj) + except Invalid, e: + if errors is None: + raise + else: + errors.append(e) + for base in self.__bases__: + try: + base.validateInvariants(obj, errors) + except Invalid: + if errors is None: + raise + if errors: + raise Invalid(errors) + + def _getInterface(self, ob, name): + """Retrieve a named interface.""" + return None + + def __d(self, dict): + + for k, v in self.__attrs.items(): + if isinstance(v, Method) and not (k in dict): + dict[k]=v + + for b in self.__bases__: + b.__d(dict) + + def __repr__(self): + try: + return self._v_repr + except AttributeError: + name = self.__name__ + m = self.__module__ + if m: + name = '%s.%s' % (m, name) + r = "<%s %s>" % (self.__class__.__name__, name) + self._v_repr = r + return r + + def _call_conform(self, conform): + try: + return conform(self) + except TypeError: + # We got a TypeError. It might be an error raised by + # the __conform__ implementation, or *we* may have + # made the TypeError by calling an unbound method + # (object is a class). In the later case, we behave + # as though there is no __conform__ method. We can + # detect this case by checking whether there is more + # than one traceback object in the traceback chain: + if sys.exc_info()[2].tb_next is not None: + # There is more than one entry in the chain, so + # reraise the error: + raise + # This clever trick is from Phillip Eby + + return None + + def __reduce__(self): + return self.__name__ + + def __cmp(self, o1, o2): + # Yes, I did mean to name this __cmp, rather than __cmp__. + # It is a private method used by __lt__ and __gt__. + # I don't want to override __eq__ because I want the default + # __eq__, which is really fast. + """Make interfaces sortable + + TODO: It would ne nice if: + + More specific interfaces should sort before less specific ones. + Otherwise, sort on name and module. + + But this is too complicated, and we're going to punt on it + for now. + + For now, sort on interface and module name. + + None is treated as a pseudo interface that implies the loosest + contact possible, no contract. For that reason, all interfaces + sort before None. + + """ + if o1 == o2: + return 0 + + if o1 is None: + return 1 + if o2 is None: + return -1 + + n1 = (getattr(o1, '__name__', ''), + getattr(getattr(o1, '__module__', None), '__name__', '')) + n2 = (getattr(o2, '__name__', ''), + getattr(getattr(o2, '__module__', None), '__name__', '')) + + return cmp(n1, n2) + + def __lt__(self, other): + c = self.__cmp(self, other) + #print '<', self, other, c < 0, c + return c < 0 + + def __gt__(self, other): + c = self.__cmp(self, other) + #print '>', self, other, c > 0, c + return c > 0 + + +Interface = InterfaceClass("Interface", __module__ = 'zope.interface') + +class Attribute(Element): + """Attribute descriptions + """ + + # We can't say this yet because we don't have enough + # infrastructure in place. + # + # implements(IAttribute) + + interface = None + + +class Method(Attribute): + """Method interfaces + + The idea here is that you have objects that describe methods. + This provides an opportunity for rich meta-data. + """ + + # We can't say this yet because we don't have enough + # infrastructure in place. + # + # implements(IMethod) + + def __call__(self, *args, **kw): + raise BrokenImplementation(self.interface, self.__name__) + + def getSignatureInfo(self): + return {'positional': self.positional, + 'required': self.required, + 'optional': self.optional, + 'varargs': self.varargs, + 'kwargs': self.kwargs, + } + + def getSignatureString(self): + sig = [] + for v in self.positional: + sig.append(v) + if v in self.optional.keys(): + sig[-1] += "=" + `self.optional[v]` + if self.varargs: + sig.append("*" + self.varargs) + if self.kwargs: + sig.append("**" + self.kwargs) + + return "(%s)" % ", ".join(sig) + + +def fromFunction(func, interface=None, imlevel=0, name=None): + name = name or func.__name__ + method = Method(name, func.__doc__) + defaults = func.func_defaults or () + code = func.func_code + # Number of positional arguments + na = code.co_argcount-imlevel + names = code.co_varnames[imlevel:] + opt = {} + # Number of required arguments + nr = na-len(defaults) + if nr < 0: + defaults=defaults[-nr:] + nr = 0 + + # Determine the optional arguments. + opt.update(dict(zip(names[nr:], defaults))) + + method.positional = names[:na] + method.required = names[:nr] + method.optional = opt + + argno = na + + # Determine the function's variable argument's name (i.e. *args) + if code.co_flags & CO_VARARGS: + method.varargs = names[argno] + argno = argno + 1 + else: + method.varargs = None + + # Determine the function's keyword argument's name (i.e. **kw) + if code.co_flags & CO_VARKEYWORDS: + method.kwargs = names[argno] + else: + method.kwargs = None + + method.interface = interface + + for key, value in func.__dict__.items(): + method.setTaggedValue(key, value) + + return method + + +def fromMethod(meth, interface=None, name=None): + func = meth.im_func + return fromFunction(func, interface, imlevel=1, name=name) + + +# Now we can create the interesting interfaces and wire them up: +def _wire(): + from zope.interface.declarations import classImplements + + from zope.interface.interfaces import IAttribute + classImplements(Attribute, IAttribute) + + from zope.interface.interfaces import IMethod + classImplements(Method, IMethod) + + from zope.interface.interfaces import IInterface, ISpecification + classImplements(InterfaceClass, IInterface) + classImplements(Specification, ISpecification) + +# We import this here to deal with module dependencies. +from zope.interface.declarations import providedBy, implementedBy +from zope.interface.exceptions import InvalidInterface +from zope.interface.exceptions import BrokenImplementation diff --git a/zope/interface/interfaces.py b/zope/interface/interfaces.py new file mode 100644 index 0000000..6deac8c --- /dev/null +++ b/zope/interface/interfaces.py @@ -0,0 +1,730 @@ +############################################################################## +# +# Copyright (c) 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Interface Package Interfaces + +$Id: interfaces.py 67803 2006-05-01 15:20:47Z jim $ +""" +__docformat__ = 'restructuredtext' + + +from zope.interface import Interface +from zope.interface.interface import Attribute + +class IElement(Interface): + """Objects that have basic documentation and tagged values. + """ + + __name__ = Attribute('__name__', 'The object name') + __doc__ = Attribute('__doc__', 'The object doc string') + + def getTaggedValue(tag): + """Returns the value associated with `tag`. + + Raise a `KeyError` of the tag isn't set. + """ + + def queryTaggedValue(tag, default=None): + """Returns the value associated with `tag`. + + Return the default value of the tag isn't set. + """ + + def getTaggedValueTags(): + """Returns a list of all tags.""" + + def setTaggedValue(tag, value): + """Associates `value` with `key`.""" + + +class IAttribute(IElement): + """Attribute descriptors""" + + interface = Attribute('interface', + 'Stores the interface instance in which the ' + 'attribute is located.') + + +class IMethod(IAttribute): + """Method attributes""" + + def getSignatureInfo(): + """Returns the signature information. + + This method returns a dictionary with the following keys: + + o `positional` - All positional arguments. + + o `required` - A list of all required arguments. + + o `optional` - A list of all optional arguments. + + o `varargs` - The name of the varargs argument. + + o `kwargs` - The name of the kwargs argument. + """ + + def getSignatureString(): + """Return a signature string suitable for inclusion in documentation. + + This method returns the function signature string. For example, if you + have `func(a, b, c=1, d='f')`, then the signature string is `(a, b, + c=1, d='f')`. + """ + +class ISpecification(Interface): + """Object Behavioral specifications""" + + def extends(other, strict=True): + """Test whether a specification extends another + + The specification extends other if it has other as a base + interface or if one of it's bases extends other. + + If strict is false, then the specification extends itself. + """ + + def isOrExtends(other): + """Test whether the specification is or extends another + """ + + def weakref(callback=None): + """Return a weakref to the specification + + This method is, regrettably, needed to allow weakrefs to be + computed to security-proxied specifications. While the + zope.interface package does not require zope.security or + zope.proxy, it has to be able to coexist with it. + + """ + + __bases__ = Attribute("""Base specifications + + A tuple if specifications from which this specification is + directly derived. + + """) + + __sro__ = Attribute("""Specification-resolution order + + A tuple of the specification and all of it's ancestor + specifications from most specific to least specific. + + (This is similar to the method-resolution order for new-style classes.) + """) + + def get(name, default=None): + """Look up the description for a name + + If the named attribute is not defined, the default is + returned. + """ + + +class IInterface(ISpecification, IElement): + """Interface objects + + Interface objects describe the behavior of an object by containing + useful information about the object. This information includes: + + o Prose documentation about the object. In Python terms, this + is called the "doc string" of the interface. In this element, + you describe how the object works in prose language and any + other useful information about the object. + + o Descriptions of attributes. Attribute descriptions include + the name of the attribute and prose documentation describing + the attributes usage. + + o Descriptions of methods. Method descriptions can include: + + - Prose "doc string" documentation about the method and its + usage. + + - A description of the methods arguments; how many arguments + are expected, optional arguments and their default values, + the position or arguments in the signature, whether the + method accepts arbitrary arguments and whether the method + accepts arbitrary keyword arguments. + + o Optional tagged data. Interface objects (and their attributes and + methods) can have optional, application specific tagged data + associated with them. Examples uses for this are examples, + security assertions, pre/post conditions, and other possible + information you may want to associate with an Interface or its + attributes. + + Not all of this information is mandatory. For example, you may + only want the methods of your interface to have prose + documentation and not describe the arguments of the method in + exact detail. Interface objects are flexible and let you give or + take any of these components. + + Interfaces are created with the Python class statement using + either Interface.Interface or another interface, as in:: + + from zope.interface import Interface + + class IMyInterface(Interface): + '''Interface documentation''' + + def meth(arg1, arg2): + '''Documentation for meth''' + + # Note that there is no self argument + + class IMySubInterface(IMyInterface): + '''Interface documentation''' + + def meth2(): + '''Documentation for meth2''' + + You use interfaces in two ways: + + o You assert that your object implement the interfaces. + + There are several ways that you can assert that an object + implements an interface: + + 1. Call zope.interface.implements in your class definition. + + 2. Call zope.interfaces.directlyProvides on your object. + + 3. Call 'zope.interface.classImplements' to assert that instances + of a class implement an interface. + + For example:: + + from zope.interface import classImplements + + classImplements(some_class, some_interface) + + This approach is useful when it is not an option to modify + the class source. Note that this doesn't affect what the + class itself implements, but only what its instances + implement. + + o You query interface meta-data. See the IInterface methods and + attributes for details. + + """ + + def providedBy(object): + """Test whether the interface is implemented by the object + + Return true of the object asserts that it implements the + interface, including asserting that it implements an extended + interface. + """ + + def implementedBy(class_): + """Test whether the interface is implemented by instances of the class + + Return true of the class asserts that its instances implement the + interface, including asserting that they implement an extended + interface. + """ + + def names(all=False): + """Get the interface attribute names + + Return a sequence of the names of the attributes, including + methods, included in the interface definition. + + Normally, only directly defined attributes are included. If + a true positional or keyword argument is given, then + attributes defined by base classes will be included. + """ + + def namesAndDescriptions(all=False): + """Get the interface attribute names and descriptions + + Return a sequence of the names and descriptions of the + attributes, including methods, as name-value pairs, included + in the interface definition. + + Normally, only directly defined attributes are included. If + a true positional or keyword argument is given, then + attributes defined by base classes will be included. + """ + + def __getitem__(name): + """Get the description for a name + + If the named attribute is not defined, a KeyError is raised. + """ + + def direct(name): + """Get the description for the name if it was defined by the interface + + If the interface doesn't define the name, returns None. + """ + + def validateInvariants(obj, errors=None): + """Validate invariants + + Validate object to defined invariants. If errors is None, + raises first Invalid error; if errors is a list, appends all errors + to list, then raises Invalid with the errors as the first element + of the "args" tuple.""" + + def __contains__(name): + """Test whether the name is defined by the interface""" + + def __iter__(): + """Return an iterator over the names defined by the interface + + The names iterated include all of the names defined by the + interface directly and indirectly by base interfaces. + """ + + __module__ = Attribute("""The name of the module defining the interface""") + +class IDeclaration(ISpecification): + """Interface declaration + + Declarations are used to express the interfaces implemented by + classes or provided by objects. + """ + + def __contains__(interface): + """Test whether an interface is in the specification + + Return true if the given interface is one of the interfaces in + the specification and false otherwise. + """ + + def __iter__(): + """Return an iterator for the interfaces in the specification + """ + + def flattened(): + """Return an iterator of all included and extended interfaces + + An iterator is returned for all interfaces either included in + or extended by interfaces included in the specifications + without duplicates. The interfaces are in "interface + resolution order". The interface resolution order is such that + base interfaces are listed after interfaces that extend them + and, otherwise, interfaces are included in the order that they + were defined in the specification. + """ + + def __sub__(interfaces): + """Create an interface specification with some interfaces excluded + + The argument can be an interface or an interface + specifications. The interface or interfaces given in a + specification are subtracted from the interface specification. + + Removing an interface that is not in the specification does + not raise an error. Doing so has no effect. + + Removing an interface also removes sub-interfaces of the interface. + + """ + + def __add__(interfaces): + """Create an interface specification with some interfaces added + + The argument can be an interface or an interface + specifications. The interface or interfaces given in a + specification are added to the interface specification. + + Adding an interface that is already in the specification does + not raise an error. Doing so has no effect. + """ + + def __nonzero__(): + """Return a true value of the interface specification is non-empty + """ + +class IInterfaceDeclaration(Interface): + """Declare and check the interfaces of objects + + The functions defined in this interface are used to declare the + interfaces that objects provide and to query the interfaces that have + been declared. + + Interfaces can be declared for objects in two ways: + + - Interfaces are declared for instances of the object's class + + - Interfaces are declared for the object directly. + + The interfaces declared for an object are, therefore, the union of + interfaces declared for the object directly and the interfaces + declared for instances of the object's class. + + Note that we say that a class implements the interfaces provided + by it's instances. An instance can also provide interfaces + directly. The interfaces provided by an object are the union of + the interfaces provided directly and the interfaces implemented by + the class. + """ + + def providedBy(ob): + """Return the interfaces provided by an object + + This is the union of the interfaces directly provided by an + object and interfaces implemented by it's class. + + The value returned is an IDeclaration. + """ + + def implementedBy(class_): + """Return the interfaces implemented for a class' instances + + The value returned is an IDeclaration. + """ + + def classImplements(class_, *interfaces): + """Declare additional interfaces implemented for instances of a class + + The arguments after the class are one or more interfaces or + interface specifications (IDeclaration objects). + + The interfaces given (including the interfaces in the + specifications) are added to any interfaces previously + declared. + + Consider the following example:: + + class C(A, B): + ... + + classImplements(C, I1, I2) + + + Instances of ``C`` provide ``I1``, ``I2``, and whatever interfaces + instances of ``A`` and ``B`` provide. + """ + + def implementer(*interfaces): + """Create a decorator for declaring interfaces implemented by a facory + + A callable is returned that makes an implements declaration on + objects passed to it. + """ + + def classImplementsOnly(class_, *interfaces): + """Declare the only interfaces implemented by instances of a class + + The arguments after the class are one or more interfaces or + interface specifications (IDeclaration objects). + + The interfaces given (including the interfaces in the + specifications) replace any previous declarations. + + Consider the following example:: + + class C(A, B): + ... + + classImplements(C, IA, IB. IC) + classImplementsOnly(C. I1, I2) + + Instances of ``C`` provide only ``I1``, ``I2``, and regardless of + whatever interfaces instances of ``A`` and ``B`` implement. + """ + + def directlyProvidedBy(object): + """Return the interfaces directly provided by the given object + + The value returned is an IDeclaration. + """ + + def directlyProvides(object, *interfaces): + """Declare interfaces declared directly for an object + + The arguments after the object are one or more interfaces or + interface specifications (IDeclaration objects). + + The interfaces given (including the interfaces in the + specifications) replace interfaces previously + declared for the object. + + Consider the following example:: + + class C(A, B): + ... + + ob = C() + directlyProvides(ob, I1, I2) + + The object, ``ob`` provides ``I1``, ``I2``, and whatever interfaces + instances have been declared for instances of ``C``. + + To remove directly provided interfaces, use ``directlyProvidedBy`` and + subtract the unwanted interfaces. For example:: + + directlyProvides(ob, directlyProvidedBy(ob)-I2) + + removes I2 from the interfaces directly provided by + ``ob``. The object, ``ob`` no longer directly provides ``I2``, + although it might still provide ``I2`` if it's class + implements ``I2``. + + To add directly provided interfaces, use ``directlyProvidedBy`` and + include additional interfaces. For example:: + + directlyProvides(ob, directlyProvidedBy(ob), I2) + + adds I2 to the interfaces directly provided by ob. + """ + + def alsoProvides(object, *interfaces): + """Declare additional interfaces directly for an object:: + + alsoProvides(ob, I1) + + is equivalent to:: + + directivelyProvides(ob, directlyProvidedBy(ob), I1) + """ + + def noLongerProvides(object, interface): + """Remove an interface from the list of an object's directly + provided interfaces:: + + noLongerProvides(ob, I1) + + is equivalent to:: + + directlyProvides(ob, directlyProvidedBy(ob)-I1) + + with the exception that if ``I1`` is an interface that is + provided by ``ob`` through the class's implementation, + ValueError is raised. + """ + + def implements(*interfaces): + """Declare interfaces implemented by instances of a class + + This function is called in a class definition. + + The arguments are one or more interfaces or interface + specifications (IDeclaration objects). + + The interfaces given (including the interfaces in the + specifications) are added to any interfaces previously + declared. + + Previous declarations include declarations for base classes + unless implementsOnly was used. + + This function is provided for convenience. It provides a more + convenient way to call classImplements. For example:: + + implements(I1) + + is equivalent to calling:: + + classImplements(C, I1) + + after the class has been created. + + Consider the following example:: + + class C(A, B): + implements(I1, I2) + + + Instances of ``C`` implement ``I1``, ``I2``, and whatever interfaces + instances of ``A`` and ``B`` implement. + """ + + def implementsOnly(*interfaces): + """Declare the only interfaces implemented by instances of a class + + This function is called in a class definition. + + The arguments are one or more interfaces or interface + specifications (IDeclaration objects). + + Previous declarations including declarations for base classes + are overridden. + + This function is provided for convenience. It provides a more + convenient way to call classImplementsOnly. For example:: + + implementsOnly(I1) + + is equivalent to calling:: + + classImplementsOnly(I1) + + after the class has been created. + + Consider the following example:: + + class C(A, B): + implementsOnly(I1, I2) + + + Instances of ``C`` implement ``I1``, ``I2``, regardless of what + instances of ``A`` and ``B`` implement. + """ + + 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. + """ + + def moduleProvides(*interfaces): + """Declare interfaces provided by a module + + This function is used in a module 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 module's direct-object + interface specification. An error will be raised if the module + already has an interface specification. In other words, it is + an error to call this function more than once in a module + definition. + + This function is provided for convenience. It provides a more + convenient way to call directlyProvides for a module. For example:: + + moduleImplements(I1) + + is equivalent to:: + + directlyProvides(sys.modules[__name__], I1) + """ + + def Declaration(*interfaces): + """Create an interface specification + + The arguments are one or more interfaces or interface + specifications (IDeclaration objects). + + A new interface specification (IDeclaration) with + the given interfaces is returned. + """ + +class IAdapterRegistry(Interface): + """Provide an interface-based registry for adapters + + This registry registers objects that are in some sense "from" a + sequence of specification to an interface and a name. + + No specific semantics are assumed for the registered objects, + however, the most common application will be to register factories + that adapt objects providing required specifications to a provided + interface. + """ + + def register(required, provided, name, value): + """Register a value + + A value is registered for a *sequence* of required specifications, a + provided interface, and a name. + """ + + def registered(required, provided, name=u''): + """Return the component registered for the given interfaces and name + + Unlike the lookup method, this methods won't retrieve + components registered for more specific required interfaces or + less specific provided interfaces. + + If no component was registered exactly for the given + interfaces and name, then None is returned. + + """ + + def lookup(required, provided, name='', default=None): + """Lookup a value + + A value is looked up based on a *sequence* of required + specifications, a provided interface, and a name. + """ + + def queryMultiAdapter(objects, provided, name=u'', default=None): + """Adapt a sequence of objects to a named, provided, interface + """ + + def lookup1(required, provided, name=u'', default=None): + """Lookup a value using a single required interface + + A value is looked up based on a single required + specifications, a provided interface, and a name. + """ + + def queryAdapter(object, provided, name=u'', default=None): + """Adapt an object using a registered adapter factory. + """ + + def adapter_hook(provided, object, name=u'', default=None): + """Adapt an object using a registered adapter factory. + """ + + def lookupAll(required, provided): + """Find all adapters from the required to the provided interfaces + + An iterable object is returned that provides name-value two-tuples. + """ + + def names(required, provided): + """Return the names for which there are registered objects + """ + + def subscribe(required, provided, subscriber, name=u''): + """Register a subscriber + + A subscriber is registered for a *sequence* of required + specifications, a provided interface, and a name. + + Multiple subscribers may be registered for the same (or + equivalent) interfaces. + """ + + def subscriptions(required, provided, name=u''): + """Get a sequence of subscribers + + Subscribers for a *sequence* of required interfaces, and a provided + interface are returned. + """ + + def subscribers(objects, provided, name=u''): + """Get a sequence of subscription adapters + """ diff --git a/zope/interface/ro.py b/zope/interface/ro.py new file mode 100644 index 0000000..e8e6115 --- /dev/null +++ b/zope/interface/ro.py @@ -0,0 +1,63 @@ +############################################################################## +# +# Copyright (c) 2003 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Compute a resolution order for an object and it's bases + +$Id: ro.py 25177 2004-06-02 13:17:31Z jim $ +""" + +def ro(object): + """Compute a "resolution order" for an object + """ + return mergeOrderings([_flatten(object, [])]) + +def mergeOrderings(orderings, seen=None): + """Merge multiple orderings so that within-ordering order is preserved + + Orderings are constrained in such a way that if an object appears + in two or more orderings, then the suffix that begins with the + object must be in both orderings. + + For example: + + >>> _mergeOrderings([ + ... ['x', 'y', 'z'], + ... ['q', 'z'], + ... [1, 3, 5], + ... ['z'] + ... ]) + ['x', 'y', 'q', 1, 3, 5, 'z'] + + """ + + if seen is None: + seen = {} + result = [] + orderings.reverse() + for ordering in orderings: + ordering = list(ordering) + ordering.reverse() + for o in ordering: + if o not in seen: + seen[o] = 1 + result.append(o) + + result.reverse() + return result + +def _flatten(ob, result): + result.append(ob) + for base in ob.__bases__: + _flatten(base, result) + + return result diff --git a/zope/interface/tests/__init__.py b/zope/interface/tests/__init__.py new file mode 100644 index 0000000..b711d36 --- /dev/null +++ b/zope/interface/tests/__init__.py @@ -0,0 +1,2 @@ +# +# This file is necessary to make this directory a package. diff --git a/zope/interface/tests/dummy.py b/zope/interface/tests/dummy.py new file mode 100644 index 0000000..f4a4f9d --- /dev/null +++ b/zope/interface/tests/dummy.py @@ -0,0 +1,25 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Dummy Module + +$Id$ +""" +from zope.interface import moduleProvides +from zope.interface.tests.ifoo import IFoo +from zope.interface import moduleProvides + +moduleProvides(IFoo) + +def bar(baz): + pass diff --git a/zope/interface/tests/ifoo.py b/zope/interface/tests/ifoo.py new file mode 100644 index 0000000..6ae2231 --- /dev/null +++ b/zope/interface/tests/ifoo.py @@ -0,0 +1,28 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""IFoo test module + +$Id$ +""" +from zope.interface import Interface + +class IFoo(Interface): + """ + Dummy interface for unit tests. + """ + + def bar(baz): + """ + Just a note. + """ diff --git a/zope/interface/tests/m1.py b/zope/interface/tests/m1.py new file mode 100644 index 0000000..86adad2 --- /dev/null +++ b/zope/interface/tests/m1.py @@ -0,0 +1,23 @@ +############################################################################## +# +# Copyright (c) 2004 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Test module that declares an interface + +$Id$ +""" +from zope.interface import Interface, moduleProvides + +class I1(Interface): pass +class I2(Interface): pass + +moduleProvides(I1, I2) diff --git a/zope/interface/tests/m2.py b/zope/interface/tests/m2.py new file mode 100644 index 0000000..16762dd --- /dev/null +++ b/zope/interface/tests/m2.py @@ -0,0 +1,17 @@ +############################################################################## +# +# Copyright (c) 2004 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Test module that doesn't declare an interface + +$Id$ +""" diff --git a/zope/interface/tests/odd.py b/zope/interface/tests/odd.py new file mode 100644 index 0000000..7e31f30 --- /dev/null +++ b/zope/interface/tests/odd.py @@ -0,0 +1,129 @@ +############################################################################## +# +# Copyright (c) 2003 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Odd meta class that doesn't subclass type. + +This is used for testing support for ExtensionClass in new interfaces. + + >>> class A(object): + ... __metaclass__ = MetaClass + ... a = 1 + ... + >>> A.__name__ + 'A' + >>> A.__bases__ + () + >>> class B(object): + ... __metaclass__ = MetaClass + ... b = 1 + ... + >>> class C(A, B): pass + ... + >>> C.__name__ + 'C' + >>> int(C.__bases__ == (A, B)) + 1 + >>> a = A() + >>> aa = A() + >>> a.a + 1 + >>> aa.a + 1 + >>> aa.a = 2 + >>> a.a + 1 + >>> aa.a + 2 + >>> c = C() + >>> c.a + 1 + >>> c.b + 1 + >>> c.b = 2 + >>> c.b + 2 + >>> C.c = 1 + >>> c.c + 1 + >>> from types import ClassType + >>> int(isinstance(C, (type, ClassType))) + 0 + >>> int(C.__class__.__class__ is C.__class__) + 1 + +$Id: odd.py 38178 2005-08-30 21:50:19Z mj $ +""" + +# class OddClass is an odd meta class + +class MetaMetaClass(type): + + def __getattribute__(self, name): + if name == '__class__': + return self + return type.__getattribute__(self, name) + + +class MetaClass(object): + """Odd classes + """ + __metaclass__ = MetaMetaClass + + def __init__(self, name, bases, dict): + self.__name__ = name + self.__bases__ = bases + self.__dict__.update(dict) + + def __call__(self): + return OddInstance(self) + + def __getattr__(self, name): + for b in self.__bases__: + v = getattr(b, name, self) + if v is not self: + return v + raise AttributeError(name) + + def __repr__(self): + return "<odd class %s at %s>" % (self.__name__, hex(id(self))) + +class OddInstance(object): + + def __init__(self, cls): + self.__dict__['__class__'] = cls + + def __getattribute__(self, name): + dict = object.__getattribute__(self, '__dict__') + if name == '__dict__': + return dict + v = dict.get(name, self) + if v is not self: + return v + return getattr(dict['__class__'], name) + + def __setattr__(self, name, v): + self.__dict__[name] = v + + def __delattr__(self, name): + del self.__dict__[name] + + def __repr__(self): + return "<odd %s instance at %s>" % ( + self.__class__.__name__, hex(id(self))) + + + +# DocTest: +if __name__ == "__main__": + import doctest, __main__ + doctest.testmod(__main__, isprivate=lambda *a: False) diff --git a/zope/interface/tests/test_adapter.py b/zope/interface/tests/test_adapter.py new file mode 100644 index 0000000..a8d1d94 --- /dev/null +++ b/zope/interface/tests/test_adapter.py @@ -0,0 +1,335 @@ +############################################################################## +# +# Copyright (c) 2003 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Adapter registry tests + +$Id: test_adapter.py 67795 2006-05-01 13:55:42Z jim $ +""" +import unittest +import zope.interface +from zope.interface.adapter import AdapterRegistry +import zope.interface + + +class IF0(zope.interface.Interface): + pass +class IF1(IF0): + pass + +class IB0(zope.interface.Interface): + pass +class IB1(IB0): + pass + +class IR0(zope.interface.Interface): + pass +class IR1(IR0): + pass + +def test_multi_adapter_get_best_match(): + """ + >>> registry = AdapterRegistry() + + >>> class IB2(IB0): + ... pass + >>> class IB3(IB2, IB1): + ... pass + >>> class IB4(IB1, IB2): + ... pass + + >>> registry.register([None, IB1], IR0, '', 'A1') + >>> registry.register([None, IB0], IR0, '', 'A0') + >>> registry.register([None, IB2], IR0, '', 'A2') + + >>> registry.lookup((IF1, IB1), IR0, '') + 'A1' + >>> registry.lookup((IF1, IB2), IR0, '') + 'A2' + >>> registry.lookup((IF1, IB0), IR0, '') + 'A0' + >>> registry.lookup((IF1, IB3), IR0, '') + 'A2' + >>> registry.lookup((IF1, IB4), IR0, '') + 'A1' + """ + +def test_multi_adapter_lookupAll_get_best_matches(): + """ + >>> registry = AdapterRegistry() + + >>> class IB2(IB0): + ... pass + >>> class IB3(IB2, IB1): + ... pass + >>> class IB4(IB1, IB2): + ... pass + + >>> registry.register([None, IB1], IR0, '', 'A1') + >>> registry.register([None, IB0], IR0, '', 'A0') + >>> registry.register([None, IB2], IR0, '', 'A2') + + >>> tuple(registry.lookupAll((IF1, IB1), IR0))[0][1] + 'A1' + >>> tuple(registry.lookupAll((IF1, IB2), IR0))[0][1] + 'A2' + >>> tuple(registry.lookupAll((IF1, IB0), IR0))[0][1] + 'A0' + >>> tuple(registry.lookupAll((IF1, IB3), IR0))[0][1] + 'A2' + >>> tuple(registry.lookupAll((IF1, IB4), IR0))[0][1] + 'A1' + """ + + +def test_multi_adapter_w_default(): + """ + >>> registry = AdapterRegistry() + + >>> registry.register([None, None], IB1, 'bob', 'A0') + + >>> registry.lookup((IF1, IR1), IB0, 'bob') + 'A0' + + >>> registry.register([None, IR0], IB1, 'bob', 'A1') + + >>> registry.lookup((IF1, IR1), IB0, 'bob') + 'A1' + + >>> registry.lookup((IF1, IR1), IB0, 'bruce') + + >>> registry.register([None, IR1], IB1, 'bob', 'A2') + >>> registry.lookup((IF1, IR1), IB0, 'bob') + 'A2' + """ + +def test_multi_adapter_w_inherited_and_multiple_registrations(): + """ + >>> registry = AdapterRegistry() + + >>> class IX(zope.interface.Interface): + ... pass + + >>> registry.register([IF0, IR0], IB1, 'bob', 'A1') + >>> registry.register([IF1, IX], IB1, 'bob', 'AX') + + >>> registry.lookup((IF1, IR1), IB0, 'bob') + 'A1' + """ + +def test_named_adapter_with_default(): + """Query a named simple adapter + + >>> registry = AdapterRegistry() + + If we ask for a named adapter, we won't get a result unless there + is a named adapter, even if the object implements the interface: + + >>> registry.lookup([IF1], IF0, 'bob') + + >>> registry.register([None], IB1, 'bob', 'A1') + >>> registry.lookup([IF1], IB0, 'bob') + 'A1' + + >>> registry.lookup([IF1], IB0, 'bruce') + + >>> registry.register([None], IB0, 'bob', 'A2') + >>> registry.lookup([IF1], IB0, 'bob') + 'A2' + """ + +def test_multi_adapter_gets_closest_provided(): + """ + >>> registry = AdapterRegistry() + >>> registry.register([IF1, IR0], IB0, 'bob', 'A1') + >>> registry.register((IF1, IR0), IB1, 'bob', 'A2') + >>> registry.lookup((IF1, IR1), IB0, 'bob') + 'A1' + + >>> registry = AdapterRegistry() + >>> registry.register([IF1, IR0], IB1, 'bob', 'A2') + >>> registry.register([IF1, IR0], IB0, 'bob', 'A1') + >>> registry.lookup([IF1, IR0], IB0, 'bob') + 'A1' + + >>> registry = AdapterRegistry() + >>> registry.register([IF1, IR0], IB0, 'bob', 'A1') + >>> registry.register([IF1, IR1], IB1, 'bob', 'A2') + >>> registry.lookup([IF1, IR1], IB0, 'bob') + 'A2' + + >>> registry = AdapterRegistry() + >>> registry.register([IF1, IR1], IB1, 'bob', 2) + >>> registry.register([IF1, IR0], IB0, 'bob', 1) + >>> registry.lookup([IF1, IR1], IB0, 'bob') + 2 + """ + +def test_multi_adapter_check_non_default_dont_hide_default(): + """ + >>> registry = AdapterRegistry() + + >>> class IX(zope.interface.Interface): + ... pass + + + >>> registry.register([None, IR0], IB0, 'bob', 1) + >>> registry.register([IF1, IX], IB0, 'bob', 2) + >>> registry.lookup([IF1, IR1], IB0, 'bob') + 1 + """ + +def test_adapter_hook_with_factory_producing_None(): + """ + >>> registry = AdapterRegistry() + >>> default = object() + + >>> class Object1(object): + ... zope.interface.implements(IF0) + >>> class Object2(object): + ... zope.interface.implements(IF0) + + >>> def factory(context): + ... if isinstance(context, Object1): + ... return 'adapter' + ... return None + + >>> registry.register([IF0], IB0, '', factory) + + >>> registry.adapter_hook(IB0, Object1()) + 'adapter' + >>> registry.adapter_hook(IB0, Object2()) is None + True + >>> registry.adapter_hook(IB0, Object2(), default=default) is default + True + """ + +def test_adapter_registry_update_upon_interface_bases_change(): + """ + Let's first create a adapter registry and a simple adaptation hook: + + >>> globalRegistry = AdapterRegistry() + + >>> def _hook(iface, ob, lookup=globalRegistry.lookup1): + ... factory = lookup(zope.interface.providedBy(ob), iface) + ... if factory is None: + ... return None + ... else: + ... return factory(ob) + + >>> zope.interface.interface.adapter_hooks.append(_hook) + + Now we create some interfaces and an implementation: + + >>> class IX(zope.interface.Interface): + ... pass + + >>> class IY(zope.interface.Interface): + ... pass + + >>> class X(object): + ... pass + + >>> class Y(object): + ... zope.interface.implements(IY) + ... def __init__(self, original): + ... self.original=original + + and register an adapter: + + >>> globalRegistry.register((IX,), IY, '', Y) + + at first, we still expect the adapter lookup from `X` to `IY` to fail: + + >>> IY(X()) #doctest: +NORMALIZE_WHITESPACE +ELLIPSIS + Traceback (most recent call last): + ... + TypeError: ('Could not adapt', + <zope.interface.tests.test_adapter.X object at ...>, + <InterfaceClass zope.interface.tests.test_adapter.IY>) + + But after we declare an interface on the class `X`, it should pass: + + >>> zope.interface.classImplementsOnly(X, IX) + + >>> IY(X()) #doctest: +ELLIPSIS + <zope.interface.tests.test_adapter.Y object at ...> + + >>> hook = zope.interface.interface.adapter_hooks.pop() + """ + + +def test_changing_declarations(): + """ + + If we change declarations for a class, those adapter lookup should + eflect the changes: + + >>> class I1(zope.interface.Interface): + ... pass + >>> class I2(zope.interface.Interface): + ... pass + + >>> registry = AdapterRegistry() + >>> registry.register([I1], I2, '', 42) + + >>> class C: + ... pass + + >>> registry.lookup([zope.interface.implementedBy(C)], I2, '') + + >>> zope.interface.classImplements(C, I1) + + >>> registry.lookup([zope.interface.implementedBy(C)], I2, '') + 42 + """ + +def test_correct_multi_adapter_lookup(): + """ + >>> registry = AdapterRegistry() + >>> registry.register([IF0, IB1], IR0, '', 'A01') + >>> registry.register([IF1, IB0], IR0, '', 'A10') + >>> registry.lookup((IF1, IB1), IR0, '') + 'A10' + """ + +def test_duplicate_bases(): + """ +There was a bug that caused problems if a spec had multiple bases: + + >>> class I(zope.interface.Interface): + ... pass + >>> class I2(I, I): + ... pass + >>> registry = AdapterRegistry() + >>> registry.register([I2], IR0, 'x', 'X') + >>> registry.lookup((I2, ), IR0, 'x') + 'X' + >>> registry.register([I2], IR0, 'y', 'Y') + >>> registry.lookup((I2, ), IR0, 'x') + 'X' + >>> registry.lookup((I2, ), IR0, 'y') + 'Y' +""" + + +def test_suite(): + from zope.testing import doctest, doctestunit + return unittest.TestSuite(( + doctestunit.DocFileSuite('../adapter.txt', '../human.txt', + '../human.ru.txt', 'foodforthought.txt', + globs={'__name__': '__main__'}), + doctest.DocTestSuite(), + )) + +if __name__ == '__main__': + unittest.main(defaultTest='test_suite') diff --git a/zope/interface/tests/test_advice.py b/zope/interface/tests/test_advice.py new file mode 100644 index 0000000..7f61a06 --- /dev/null +++ b/zope/interface/tests/test_advice.py @@ -0,0 +1,178 @@ + +############################################################################## +# +# Copyright (c) 2003 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Tests for advice + +This module was adapted from 'protocols.tests.advice', part of the Python +Enterprise Application Kit (PEAK). Please notify the PEAK authors +(pje@telecommunity.com and tsarna@sarna.org) if bugs are found or +Zope-specific changes are required, so that the PEAK version of this module +can be kept in sync. + +PEAK is a Python application framework that interoperates with (but does +not require) Zope 3 and Twisted. It provides tools for manipulating UML +models, object-relational persistence, aspect-oriented programming, and more. +Visit the PEAK home page at http://peak.telecommunity.com for more information. + +$Id: test_advice.py 40836 2005-12-16 22:40:51Z benji_york $ +""" + +import unittest +from unittest import TestCase, makeSuite, TestSuite +from zope.interface.advice import * +from types import ClassType +import sys + +def ping(log, value): + + def pong(klass): + log.append((value,klass)) + return [klass] + + addClassAdvisor(pong) + +class ClassicClass: + __metaclass__ = ClassType + classLevelFrameInfo = getFrameInfo(sys._getframe()) + +class NewStyleClass: + __metaclass__ = type + classLevelFrameInfo = getFrameInfo(sys._getframe()) + +moduleLevelFrameInfo = getFrameInfo(sys._getframe()) + +class FrameInfoTest(TestCase): + + classLevelFrameInfo = getFrameInfo(sys._getframe()) + + def checkModuleInfo(self): + kind, module, f_locals, f_globals = moduleLevelFrameInfo + self.assertEquals(kind, "module") + for d in module.__dict__, f_locals, f_globals: + self.assert_(d is globals()) + + def checkClassicClassInfo(self): + kind, module, f_locals, f_globals = ClassicClass.classLevelFrameInfo + self.assertEquals(kind, "class") + + self.assert_(f_locals is ClassicClass.__dict__) # ??? + for d in module.__dict__, f_globals: + self.assert_(d is globals()) + + def checkNewStyleClassInfo(self): + kind, module, f_locals, f_globals = NewStyleClass.classLevelFrameInfo + self.assertEquals(kind, "class") + + for d in module.__dict__, f_globals: + self.assert_(d is globals()) + + def checkCallInfo(self): + kind, module, f_locals, f_globals = getFrameInfo(sys._getframe()) + self.assertEquals(kind, "function call") + self.assert_(f_locals is locals()) # ??? + for d in module.__dict__, f_globals: + self.assert_(d is globals()) + + +class AdviceTests(TestCase): + + def checkOrder(self): + log = [] + class Foo(object): + ping(log, 1) + ping(log, 2) + ping(log, 3) + + # Strip the list nesting + for i in 1,2,3: + self.assert_(isinstance(Foo, list)) + Foo, = Foo + + self.assertEquals(log, [(1, Foo), (2, [Foo]), (3, [[Foo]])]) + + def TODOcheckOutside(self): + # Disabled because the check does not work with doctest tests. + try: + ping([], 1) + except SyntaxError: + pass + else: + raise AssertionError( + "Should have detected advice outside class body" + ) + + def checkDoubleType(self): + if sys.hexversion >= 0x02030000: + return # you can't duplicate bases in 2.3 + class aType(type,type): + ping([],1) + aType, = aType + self.assert_(aType.__class__ is type) + + def checkSingleExplicitMeta(self): + + class M(type): + pass + + class C(M): + __metaclass__ = M + ping([],1) + + C, = C + self.assert_(C.__class__ is M) + + + def checkMixedMetas(self): + + class M1(type): pass + class M2(type): pass + + class B1: __metaclass__ = M1 + class B2: __metaclass__ = M2 + + try: + class C(B1,B2): + ping([],1) + except TypeError: + pass + else: + raise AssertionError("Should have gotten incompatibility error") + + class M3(M1,M2): pass + + class C(B1,B2): + __metaclass__ = M3 + ping([],1) + + self.assert_(isinstance(C,list)) + C, = C + self.assert_(isinstance(C,M3)) + + def checkMetaOfClass(self): + + class metameta(type): + pass + + class meta(type): + __metaclass__ = metameta + + self.assertEquals(determineMetaclass((meta, type)), metameta) + +TestClasses = (AdviceTests, FrameInfoTest) + +def test_suite(): + return TestSuite([makeSuite(t,'check') for t in TestClasses]) + +if __name__ == '__main__': + unittest.main(defaultTest='test_suite') diff --git a/zope/interface/tests/test_declarations.py b/zope/interface/tests/test_declarations.py new file mode 100644 index 0000000..29a7270 --- /dev/null +++ b/zope/interface/tests/test_declarations.py @@ -0,0 +1,416 @@ +############################################################################## +# +# Copyright (c) 2003 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Test the new API for making and checking interface declarations + +$Id: test_declarations.py 67630 2006-04-27 00:54:03Z jim $ +""" +import unittest +from zope.interface import * +from zope.testing.doctestunit import DocTestSuite +from zope.interface import Interface + +class I1(Interface): pass +class I2(Interface): pass +class I3(Interface): pass +class I4(Interface): pass +class I5(Interface): pass + +class A(object): + implements(I1) +class B(object): + implements(I2) +class C(A, B): + implements(I3) + +class COnly(A, B): + implementsOnly(I3) + +class COnly_old(A, B): + __implemented__ = I3 + +class D(COnly): + implements(I5) + +def test_ObjectSpecification_Simple(): + """ + >>> c = C() + >>> directlyProvides(c, I4) + >>> [i.__name__ for i in providedBy(c)] + ['I4', 'I3', 'I1', 'I2'] + """ + +def test_ObjectSpecification_Simple_w_only(): + """ + >>> c = COnly() + >>> directlyProvides(c, I4) + >>> [i.__name__ for i in providedBy(c)] + ['I4', 'I3'] + """ + +def test_ObjectSpecification_Simple_old_style(): + """ + >>> c = COnly_old() + >>> directlyProvides(c, I4) + >>> [i.__name__ for i in providedBy(c)] + ['I4', 'I3'] + """ + + +class Test(unittest.TestCase): + + # Note that most of the tests are in the doc strings of the + # declarations module. + + def test_backward_compat(self): + + class C1(object): __implemented__ = I1 + class C2(C1): __implemented__ = I2, I5 + class C3(C2): __implemented__ = I3, C2.__implemented__ + + self.assert_(C3.__implemented__.__class__ is tuple) + + self.assertEqual( + [i.getName() for i in providedBy(C3())], + ['I3', 'I2', 'I5'], + ) + + class C4(C3): + implements(I4) + + self.assertEqual( + [i.getName() for i in providedBy(C4())], + ['I4', 'I3', 'I2', 'I5'], + ) + + self.assertEqual( + [i.getName() for i in C4.__implemented__], + ['I4', 'I3', 'I2', 'I5'], + ) + + # Note that C3.__implemented__ should now be a sequence of interfaces + self.assertEqual( + [i.getName() for i in C3.__implemented__], + ['I3', 'I2', 'I5'], + ) + self.failIf(C3.__implemented__.__class__ is tuple) + + def test_module(self): + import zope.interface.tests.m1 + import zope.interface.tests.m2 + directlyProvides(zope.interface.tests.m2, + zope.interface.tests.m1.I1, + zope.interface.tests.m1.I2, + ) + self.assertEqual(list(providedBy(zope.interface.tests.m1)), + list(providedBy(zope.interface.tests.m2)), + ) + + def test_builtins(self): + # Setup + + intspec = implementedBy(int) + olddeclared = intspec.declared + + classImplements(int, I1) + class myint(int): + implements(I2) + + x = 42 + self.assertEqual([i.getName() for i in providedBy(x)], + ['I1']) + + x = myint(42) + directlyProvides(x, I3) + self.assertEqual([i.getName() for i in providedBy(x)], + ['I3', 'I2', 'I1']) + + # cleanup + intspec.declared = olddeclared + classImplements(int) + + x = 42 + self.assertEqual([i.getName() for i in providedBy(x)], + []) + + +def test_signature_w_no_class_interfaces(): + """ + >>> from zope.interface import * + >>> class C(object): + ... pass + >>> c = C() + >>> list(providedBy(c)) + [] + + >>> class I(Interface): + ... pass + >>> directlyProvides(c, I) + >>> list(providedBy(c)) == list(directlyProvidedBy(c)) + 1 + """ + +def test_classImplement_on_deeply_nested_classes(): + """This test is in response to a bug found, which is why it's a bit + contrived + + >>> from zope.interface import * + >>> class B1(object): + ... pass + >>> class B2(B1): + ... pass + >>> class B3(B2): + ... pass + >>> class D(object): + ... implements() + >>> class S(B3, D): + ... implements() + + This failed due to a bug in the code for finding __providedBy__ + descriptors for old-style classes. + + """ + +def test_pickle_provides_specs(): + """ + >>> from pickle import dumps, loads + >>> a = A() + >>> I2.providedBy(a) + 0 + >>> directlyProvides(a, I2) + >>> I2.providedBy(a) + 1 + >>> a2 = loads(dumps(a)) + >>> I2.providedBy(a2) + 1 + + """ + +def test_that_we_dont_inherit_class_provides(): + """ + >>> class X(object): + ... classProvides(I1) + >>> class Y(X): + ... pass + >>> [i.__name__ for i in X.__provides__] + ['I1'] + >>> Y.__provides__ + Traceback (most recent call last): + ... + AttributeError: __provides__ + + """ + +def test_that_we_dont_inherit_provides_optimizations(): + """ + + When we make a declaration for a class, we install a __provides__ + descriptors that provides a default for instances that don't have + instance-specific declarations: + + >>> class A(object): + ... implements(I1) + + >>> class B(object): + ... implements(I2) + + >>> [i.__name__ for i in A().__provides__] + ['I1'] + >>> [i.__name__ for i in B().__provides__] + ['I2'] + + But it's important that we don't use this for subclasses without + declarations. This would cause incorrect results: + + >>> class X(A, B): + ... pass + + >>> X().__provides__ + Traceback (most recent call last): + ... + AttributeError: __provides__ + + However, if we "induce" a declaration, by calling implementedBy + (even indirectly through providedBy): + + >>> [i.__name__ for i in providedBy(X())] + ['I1', 'I2'] + + + then the optimization will work: + + >>> [i.__name__ for i in X().__provides__] + ['I1', 'I2'] + + """ + +def test_classProvides_before_implements(): + """Special descriptor for class __provides__ + + The descriptor caches the implementedBy info, so that + we can get declarations for objects without instance-specific + interfaces a bit quicker. + + For example:: + + >>> from zope.interface import Interface + >>> class IFooFactory(Interface): + ... pass + >>> class IFoo(Interface): + ... pass + >>> class C(object): + ... classProvides(IFooFactory) + ... implements(IFoo) + >>> [i.getName() for i in C.__provides__] + ['IFooFactory'] + + >>> [i.getName() for i in C().__provides__] + ['IFoo'] + """ + +def test_getting_spec_for_proxied_builtin_class(): + """ + + In general, we should be able to get a spec + for a proxied class if someone has declared or + asked for a spec before. + + We don't want to depend on proxies in this (zope.interface) + package, but we do want to work with proxies. Proxies have the + effect that a class's __dict__ cannot be gotten. Further, for + built-in classes, we can't save, and thus, cannot get, any class + attributes. We'll emulate this by treating a plain object as a class: + + >>> cls = object() + + We'll create an implements specification: + + >>> import zope.interface.declarations + >>> impl = zope.interface.declarations.Implements(I1, I2) + + Now, we'll emulate a declaration for a built-in type by putting + it in BuiltinImplementationSpecifications: + + >>> zope.interface.declarations.BuiltinImplementationSpecifications[ + ... cls] = impl + + Now, we should be able to get it back: + + >>> implementedBy(cls) is impl + True + + Of course, we don't want to leave it there. :) + + >>> del zope.interface.declarations.BuiltinImplementationSpecifications[ + ... cls] + + """ + +def test_declaration_get(): + """ + We can get definitions from a declaration: + + >>> import zope.interface + >>> class I1(zope.interface.Interface): + ... a11 = zope.interface.Attribute('a11') + ... a12 = zope.interface.Attribute('a12') + >>> class I2(zope.interface.Interface): + ... a21 = zope.interface.Attribute('a21') + ... a22 = zope.interface.Attribute('a22') + ... a12 = zope.interface.Attribute('a212') + >>> class I11(I1): + ... a11 = zope.interface.Attribute('a111') + + >>> decl = Declaration(I11, I2) + >>> decl.get('a11') is I11.get('a11') + True + >>> decl.get('a12') is I1.get('a12') + True + >>> decl.get('a21') is I2.get('a21') + True + >>> decl.get('a22') is I2.get('a22') + True + >>> decl.get('a') + >>> decl.get('a', 42) + 42 + + We get None even with no interfaces: + + >>> decl = Declaration() + >>> decl.get('a11') + >>> decl.get('a11', 42) + 42 + + We get new data if e change interface bases: + + >>> decl.__bases__ = I11, I2 + >>> decl.get('a11') is I11.get('a11') + True + """ + +def test_classImplements_after_classImplementsOnly_issue_402(): + """http://www.zope.org/Collectors/Zope3-dev/402 + +>>> from zope.interface import * +>>> class I1(Interface): +... pass +>>> class I2(Interface): +... pass +>>> class C: +... implements(I1) +>>> class C2: +... implementsOnly(I2) +>>> class I3(Interface): +... pass + +>>> [i.__name__ for i in providedBy(C2()).__iro__] +['I2', 'Interface'] + +>>> classImplements(C2, I3) +>>> [i.__name__ for i in providedBy(C2()).__iro__] +['I2', 'I3', 'Interface'] + +>>> class I4(Interface): +... pass +>>> classImplements(C2, I4) +>>> [i.__name__ for i in providedBy(C2()).__iro__] +['I2', 'I3', 'I4', 'Interface'] + + +""" + +def test_picklability_of_implements_specifications(): + """ + + Sometimes, we need to pickle implements specs. We should be able + to do so as long as the class is picklable. + + >>> import pickle + >>> pickle.loads(pickle.dumps(implementedBy(C))) is implementedBy(C) + True + + + """ + + +def test_suite(): + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(Test)) + suite.addTest(DocTestSuite("zope.interface.declarations")) + suite.addTest(DocTestSuite()) + + return suite + + +if __name__ == '__main__': + unittest.main() diff --git a/zope/interface/tests/test_document.py b/zope/interface/tests/test_document.py new file mode 100644 index 0000000..a5bd879 --- /dev/null +++ b/zope/interface/tests/test_document.py @@ -0,0 +1,71 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Documentation tests. + +$Id: test_document.py 26560 2004-07-15 21:38:42Z srichter $ +""" +from unittest import TestCase, main, makeSuite + +from zope.interface import Interface, Attribute + +class Test(TestCase): + + def testBlech(self): + from zope.interface.document import asStructuredText + + self.assertEqual(asStructuredText(I2), '''\ +I2 + + I2 doc + + This interface extends: + + o _I1 + + Attributes: + + a1 -- no documentation + + a2 -- a2 doc + + Methods: + + f21() -- f21 doc + + f22() -- no documentation + + f23() -- f23 doc + +''') + + +def test_suite(): + return makeSuite(Test) + +class _I1(Interface): + def f11(): pass + def f12(): pass + +class I2(_I1): + "I2 doc" + + a1 = Attribute('a1') + a2 = Attribute('a2', 'a2 doc') + + def f21(): "f21 doc" + def f22(): pass + def f23(): "f23 doc" + +if __name__=='__main__': + main(defaultTest='test_suite') diff --git a/zope/interface/tests/test_element.py b/zope/interface/tests/test_element.py new file mode 100644 index 0000000..33edc3f --- /dev/null +++ b/zope/interface/tests/test_element.py @@ -0,0 +1,43 @@ +############################################################################## +# +# Copyright (c) 2003 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Test Element meta-class. + +$Id: test_element.py 39768 2005-10-31 13:57:35Z tlotze $ +""" + +import unittest +from zope.interface.interface import Element + +class TestElement(unittest.TestCase): + + def test_taggedValues(self): + """Test that we can update tagged values of more than one element + """ + + e1 = Element("foo") + e2 = Element("bar") + e1.setTaggedValue("x", 1) + e2.setTaggedValue("x", 2) + self.assertEqual(e1.getTaggedValue("x"), 1) + self.assertEqual(e2.getTaggedValue("x"), 2) + + +def test_suite(): + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(TestElement)) + return suite + + +if __name__ == '__main__': + unittest.main(defaultTest='test_suite') diff --git a/zope/interface/tests/test_interface.py b/zope/interface/tests/test_interface.py new file mode 100644 index 0000000..f444c51 --- /dev/null +++ b/zope/interface/tests/test_interface.py @@ -0,0 +1,352 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Test Interface implementation + +$Id: test_interface.py 67761 2006-04-30 13:56:44Z jim $ +""" +import sys +import unittest +from zope.testing.doctestunit import DocTestSuite +from zope.interface.tests.unitfixtures import * # hehehe +from zope.interface.exceptions import BrokenImplementation, Invalid +from zope.interface import implementedBy, providedBy +from zope.interface import Interface, directlyProvides, Attribute +from zope import interface + +class InterfaceTests(unittest.TestCase): + + def testInterfaceSetOnAttributes(self): + self.assertEqual(FooInterface['foobar'].interface, + FooInterface) + self.assertEqual(FooInterface['aMethod'].interface, + FooInterface) + + def testClassImplements(self): + self.assert_(IC.implementedBy(C)) + + self.assert_(I1.implementedBy(A)) + self.assert_(I1.implementedBy(B)) + self.assert_(not I1.implementedBy(C)) + self.assert_(I1.implementedBy(D)) + self.assert_(I1.implementedBy(E)) + + self.assert_(not I2.implementedBy(A)) + self.assert_(I2.implementedBy(B)) + self.assert_(not I2.implementedBy(C)) + + # No longer after interfacegeddon + # self.assert_(not I2.implementedBy(D)) + + self.assert_(not I2.implementedBy(E)) + + def testUtil(self): + self.assert_(IC in implementedBy(C)) + self.assert_(I1 in implementedBy(A)) + self.assert_(not I1 in implementedBy(C)) + self.assert_(I2 in implementedBy(B)) + self.assert_(not I2 in implementedBy(C)) + + self.assert_(IC in providedBy(C())) + self.assert_(I1 in providedBy(A())) + self.assert_(not I1 in providedBy(C())) + self.assert_(I2 in providedBy(B())) + self.assert_(not I2 in providedBy(C())) + + + def testObjectImplements(self): + self.assert_(IC.providedBy(C())) + + self.assert_(I1.providedBy(A())) + self.assert_(I1.providedBy(B())) + self.assert_(not I1.providedBy(C())) + self.assert_(I1.providedBy(D())) + self.assert_(I1.providedBy(E())) + + self.assert_(not I2.providedBy(A())) + self.assert_(I2.providedBy(B())) + self.assert_(not I2.providedBy(C())) + + # Not after interface geddon + # self.assert_(not I2.providedBy(D())) + + self.assert_(not I2.providedBy(E())) + + def testDeferredClass(self): + a = A() + self.assertRaises(BrokenImplementation, a.ma) + + + def testInterfaceExtendsInterface(self): + self.assert_(BazInterface.extends(BobInterface)) + self.assert_(BazInterface.extends(BarInterface)) + self.assert_(BazInterface.extends(FunInterface)) + self.assert_(not BobInterface.extends(FunInterface)) + self.assert_(not BobInterface.extends(BarInterface)) + self.assert_(BarInterface.extends(FunInterface)) + self.assert_(not BarInterface.extends(BazInterface)) + + def testVerifyImplementation(self): + from zope.interface.verify import verifyClass + self.assert_(verifyClass(FooInterface, Foo)) + self.assert_(Interface.providedBy(I1)) + + def test_names(self): + names = list(_I2.names()); names.sort() + self.assertEqual(names, ['f21', 'f22', 'f23']) + names = list(_I2.names(all=True)); names.sort() + self.assertEqual(names, ['a1', 'f11', 'f12', 'f21', 'f22', 'f23']) + + def test_namesAndDescriptions(self): + names = [nd[0] for nd in _I2.namesAndDescriptions()]; names.sort() + self.assertEqual(names, ['f21', 'f22', 'f23']) + names = [nd[0] for nd in _I2.namesAndDescriptions(1)]; names.sort() + self.assertEqual(names, ['a1', 'f11', 'f12', 'f21', 'f22', 'f23']) + + for name, d in _I2.namesAndDescriptions(1): + self.assertEqual(name, d.__name__) + + def test_getDescriptionFor(self): + self.assertEqual(_I2.getDescriptionFor('f11').__name__, 'f11') + self.assertEqual(_I2.getDescriptionFor('f22').__name__, 'f22') + self.assertEqual(_I2.queryDescriptionFor('f33', self), self) + self.assertRaises(KeyError, _I2.getDescriptionFor, 'f33') + + def test___getitem__(self): + self.assertEqual(_I2['f11'].__name__, 'f11') + self.assertEqual(_I2['f22'].__name__, 'f22') + self.assertEqual(_I2.get('f33', self), self) + self.assertRaises(KeyError, _I2.__getitem__, 'f33') + + def test___contains__(self): + self.failUnless('f11' in _I2) + self.failIf('f33' in _I2) + + def test___iter__(self): + names = list(iter(_I2)) + names.sort() + self.assertEqual(names, ['a1', 'f11', 'f12', 'f21', 'f22', 'f23']) + + def testAttr(self): + description = _I2.getDescriptionFor('a1') + self.assertEqual(description.__name__, 'a1') + self.assertEqual(description.__doc__, 'This is an attribute') + + def testFunctionAttributes(self): + # Make sure function attributes become tagged values. + meth = _I1['f12'] + self.assertEqual(meth.getTaggedValue('optional'), 1) + + def testInvariant(self): + # set up + o = InvariantC() + directlyProvides(o, IInvariant) + # a helper + def errorsEqual(self, o, error_len, error_msgs, interface=None): + if interface is None: + interface = IInvariant + self.assertRaises(Invalid, interface.validateInvariants, o) + e = [] + try: + interface.validateInvariants(o, e) + except Invalid, error: + self.assertEquals(error.args[0], e) + else: + self._assert(0) # validateInvariants should always raise + # Invalid + self.assertEquals(len(e), error_len) + msgs = [error.args[0] for error in e] + msgs.sort() + for msg in msgs: + self.assertEquals(msg, error_msgs.pop(0)) + # the tests + self.assertEquals(IInvariant.getTaggedValue('invariants'), + [ifFooThenBar]) + self.assertEquals(IInvariant.validateInvariants(o), None) + o.bar = 27 + self.assertEquals(IInvariant.validateInvariants(o), None) + o.foo = 42 + self.assertEquals(IInvariant.validateInvariants(o), None) + del o.bar + errorsEqual(self, o, 1, ['If Foo, then Bar!']) + # nested interfaces with invariants: + self.assertEquals(ISubInvariant.getTaggedValue('invariants'), + [BarGreaterThanFoo]) + o = InvariantC() + directlyProvides(o, ISubInvariant) + o.foo = 42 + # even though the interface has changed, we should still only have one + # error. + errorsEqual(self, o, 1, ['If Foo, then Bar!'], ISubInvariant) + # however, if we set foo to 0 (Boolean False) and bar to a negative + # number then we'll get the new error + o.foo = 2 + o.bar = 1 + errorsEqual(self, o, 1, ['Please, Boo MUST be greater than Foo!'], + ISubInvariant) + # and if we set foo to a positive number and boo to 0, we'll + # get both errors! + o.foo = 1 + o.bar = 0 + errorsEqual(self, o, 2, ['If Foo, then Bar!', + 'Please, Boo MUST be greater than Foo!'], + ISubInvariant) + # for a happy ending, we'll make the invariants happy + o.foo = 1 + o.bar = 2 + self.assertEquals(IInvariant.validateInvariants(o), None) # woohoo + # now we'll do two invariants on the same interface, + # just to make sure that a small + # multi-invariant interface is at least minimally tested. + o = InvariantC() + directlyProvides(o, IInvariant) + o.foo = 42 + old_invariants = IInvariant.getTaggedValue('invariants') + invariants = old_invariants[:] + invariants.append(BarGreaterThanFoo) # if you really need to mutate, + # then this would be the way to do it. Probably a bad idea, though. :-) + IInvariant.setTaggedValue('invariants', invariants) + # + # even though the interface has changed, we should still only have one + # error. + errorsEqual(self, o, 1, ['If Foo, then Bar!']) + # however, if we set foo to 0 (Boolean False) and bar to a negative + # number then we'll get the new error + o.foo = 2 + o.bar = 1 + errorsEqual(self, o, 1, ['Please, Boo MUST be greater than Foo!']) + # and if we set foo to a positive number and boo to 0, we'll + # get both errors! + o.foo = 1 + o.bar = 0 + errorsEqual(self, o, 2, ['If Foo, then Bar!', + 'Please, Boo MUST be greater than Foo!']) + # for another happy ending, we'll make the invariants happy again + o.foo = 1 + o.bar = 2 + self.assertEquals(IInvariant.validateInvariants(o), None) # bliss + # clean up + IInvariant.setTaggedValue('invariants', old_invariants) + + def test___doc___element(self): + class I(Interface): + "xxx" + + self.assertEqual(I.__doc__, "xxx") + self.assertEqual(list(I), []) + + class I(Interface): + "xxx" + + __doc__ = Attribute('the doc') + + self.assertEqual(I.__doc__, "") + self.assertEqual(list(I), ['__doc__']) + + def testIssue228(self): + # Test for http://collector.zope.org/Zope3-dev/228 + class I(Interface): + "xxx" + class Bad: + __providedBy__ = None + # Old style classes don't have a '__class__' attribute + self.failUnlessRaises(AttributeError, I.providedBy, Bad) + + +class _I1(Interface): + + a1 = Attribute("This is an attribute") + + def f11(): pass + def f12(): pass + f12.optional = 1 + +class _I1_(_I1): pass +class _I1__(_I1_): pass + +class _I2(_I1__): + def f21(): pass + def f22(): pass + f23 = f22 + + + +if sys.version_info >= (2, 4): + def test_invariant_as_decorator(): + """Invaiants can be deined in line + + >>> class IRange(interface.Interface): + ... min = interface.Attribute("Lower bound") + ... max = interface.Attribute("Upper bound") + ... + ... @interface.invariant + ... def range_invariant(ob): + ... if ob.max < ob.min: + ... raise Invalid('max < min') + + + >>> class Range(object): + ... interface.implements(IRange) + ... + ... def __init__(self, min, max): + ... self.min, self.max = min, max + + >>> IRange.validateInvariants(Range(1,2)) + >>> IRange.validateInvariants(Range(1,1)) + >>> IRange.validateInvariants(Range(2,1)) + Traceback (most recent call last): + ... + Invalid: max < min + + + """ + +def duplicate_bases_management(): + """ +There was a bug that surfaced when an interface was repeated in +a set of bases and the bases were changed: + + >>> class I(interface.Interface): + ... pass + + >>> class I2(I, I): + ... pass + + >>> I2.__bases__ = (I,) + + +""" + +def test_suite(): + from zope.testing import doctest + suite = unittest.makeSuite(InterfaceTests) + suite.addTest(doctest.DocTestSuite("zope.interface.interface")) + if sys.version_info >= (2, 4): + suite.addTest(doctest.DocTestSuite()) + suite.addTest(doctest.DocFileSuite( + '../README.txt', + globs={'__name__': '__main__'}, + optionflags=doctest.NORMALIZE_WHITESPACE, + )) + suite.addTest(doctest.DocFileSuite( + '../README.ru.txt', + globs={'__name__': '__main__'}, + optionflags=doctest.NORMALIZE_WHITESPACE, + )) + return suite + +def main(): + unittest.TextTestRunner().run(test_suite()) + +if __name__=="__main__": + main() diff --git a/zope/interface/tests/test_odd_declarations.py b/zope/interface/tests/test_odd_declarations.py new file mode 100644 index 0000000..4b6f399 --- /dev/null +++ b/zope/interface/tests/test_odd_declarations.py @@ -0,0 +1,204 @@ +############################################################################## +# +# Copyright (c) 2003 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Test interface declarations against ExtensionClass-like classes. + +These tests are to make sure we do something sane in the presense of +classic ExtensionClass classes and instances. + +$Id: test_odd_declarations.py 40836 2005-12-16 22:40:51Z benji_york $ +""" +import unittest, odd +from zope.interface import Interface, implements, implementsOnly +from zope.interface import directlyProvides, providedBy, directlyProvidedBy +from zope.interface import classImplements, classImplementsOnly, implementedBy + +class I1(Interface): pass +class I2(Interface): pass +class I3(Interface): pass +class I31(I3): pass +class I4(Interface): pass +class I5(Interface): pass + +class Odd(object): __metaclass__ = odd.MetaClass + +class B(Odd): __implemented__ = I2 + + +# TODO: We are going to need more magic to make classProvides work with odd +# classes. This will work in the next iteration. For now, we'll use +# a different mechanism. + +# from zope.interface import classProvides + +class A(Odd): + implements(I1) + +class C(A, B): + implements(I31) + + +class Test(unittest.TestCase): + + def test_ObjectSpecification(self): + c = C() + directlyProvides(c, I4) + self.assertEqual([i.getName() for i in providedBy(c)], + ['I4', 'I31', 'I1', 'I2'] + ) + self.assertEqual([i.getName() for i in providedBy(c).flattened()], + ['I4', 'I31', 'I3', 'I1', 'I2', 'Interface'] + ) + self.assert_(I1 in providedBy(c)) + self.failIf(I3 in providedBy(c)) + self.assert_(providedBy(c).extends(I3)) + self.assert_(providedBy(c).extends(I31)) + self.failIf(providedBy(c).extends(I5)) + + class COnly(A, B): + implementsOnly(I31) + + class D(COnly): + implements(I5) + + classImplements(D, I5) + + c = D() + directlyProvides(c, I4) + self.assertEqual([i.getName() for i in providedBy(c)], + ['I4', 'I5', 'I31']) + self.assertEqual([i.getName() for i in providedBy(c).flattened()], + ['I4', 'I5', 'I31', 'I3', 'Interface']) + self.failIf(I1 in providedBy(c)) + self.failIf(I3 in providedBy(c)) + self.assert_(providedBy(c).extends(I3)) + self.failIf(providedBy(c).extends(I1)) + self.assert_(providedBy(c).extends(I31)) + self.assert_(providedBy(c).extends(I5)) + + class COnly(A, B): __implemented__ = I31 + class D(COnly): + implements(I5) + + classImplements(D, I5) + c = D() + directlyProvides(c, I4) + self.assertEqual([i.getName() for i in providedBy(c)], + ['I4', 'I5', 'I31']) + self.assertEqual([i.getName() for i in providedBy(c).flattened()], + ['I4', 'I5', 'I31', 'I3', 'Interface']) + self.failIf(I1 in providedBy(c)) + self.failIf(I3 in providedBy(c)) + self.assert_(providedBy(c).extends(I3)) + self.failIf(providedBy(c).extends(I1)) + self.assert_(providedBy(c).extends(I31)) + self.assert_(providedBy(c).extends(I5)) + + def test_classImplements(self): + class A(Odd): + implements(I3) + + class B(Odd): + implements(I4) + + class C(A, B): + pass + classImplements(C, I1, I2) + self.assertEqual([i.getName() for i in implementedBy(C)], + ['I1', 'I2', 'I3', 'I4']) + classImplements(C, I5) + self.assertEqual([i.getName() for i in implementedBy(C)], + ['I1', 'I2', 'I5', 'I3', 'I4']) + + def test_classImplementsOnly(self): + class A(Odd): + implements(I3) + + class B(Odd): + implements(I4) + + class C(A, B): + pass + classImplementsOnly(C, I1, I2) + self.assertEqual([i.__name__ for i in implementedBy(C)], + ['I1', 'I2']) + + + def test_directlyProvides(self): + class IA1(Interface): pass + class IA2(Interface): pass + class IB(Interface): pass + class IC(Interface): pass + class A(Odd): + implements(IA1, IA2) + + class B(Odd): + implements(IB) + + class C(A, B): + implements(IC) + + + ob = C() + directlyProvides(ob, I1, I2) + self.assert_(I1 in providedBy(ob)) + self.assert_(I2 in providedBy(ob)) + self.assert_(IA1 in providedBy(ob)) + self.assert_(IA2 in providedBy(ob)) + self.assert_(IB in providedBy(ob)) + self.assert_(IC in providedBy(ob)) + + directlyProvides(ob, directlyProvidedBy(ob)-I2) + self.assert_(I1 in providedBy(ob)) + self.failIf(I2 in providedBy(ob)) + self.failIf(I2 in providedBy(ob)) + directlyProvides(ob, directlyProvidedBy(ob), I2) + self.assert_(I2 in providedBy(ob)) + + def test_directlyProvides_fails_for_odd_class(self): + self.assertRaises(TypeError, directlyProvides, C, I5) + + # see above + def TODO_test_classProvides_fails_for_odd_class(self): + try: + class A(Odd): + classProvides(I1) + except TypeError: + pass # Sucess + self.assert_(False, + "Shouldn't be able to use directlyProvides on odd class." + ) + + def test_implementedBy(self): + class I2(I1): pass + + class C1(Odd): + implements(I2) + + class C2(C1): + implements(I3) + + self.assertEqual([i.getName() for i in implementedBy(C2)], + ['I3', 'I2']) + + + + +def test_suite(): + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(Test)) + return suite + + +if __name__ == '__main__': + unittest.main() diff --git a/zope/interface/tests/test_sorting.py b/zope/interface/tests/test_sorting.py new file mode 100644 index 0000000..004485e --- /dev/null +++ b/zope/interface/tests/test_sorting.py @@ -0,0 +1,49 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Test interface sorting + +$Id: test_sorting.py 25177 2004-06-02 13:17:31Z jim $ +""" + +from unittest import TestCase, TestSuite, main, makeSuite + +from zope.interface import Interface + +class I1(Interface): pass +class I2(I1): pass +class I3(I1): pass +class I4(Interface): pass +class I5(I4): pass +class I6(I2): pass + + +class Test(TestCase): + + def test(self): + l = [I1, I3, I5, I6, I4, I2] + l.sort() + self.assertEqual(l, [I1, I2, I3, I4, I5, I6]) + + def test_w_None(self): + l = [I1, None, I3, I5, None, I6, I4, I2] + l.sort() + self.assertEqual(l, [I1, I2, I3, I4, I5, I6, None, None]) + +def test_suite(): + return TestSuite(( + makeSuite(Test), + )) + +if __name__=='__main__': + main(defaultTest='test_suite') diff --git a/zope/interface/tests/test_verify.py b/zope/interface/tests/test_verify.py new file mode 100644 index 0000000..d4fac9a --- /dev/null +++ b/zope/interface/tests/test_verify.py @@ -0,0 +1,196 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Interface Verify tests + +$Id: test_verify.py 26866 2004-08-02 20:57:00Z jim $ +""" +from zope.interface import Interface, implements, classImplements, Attribute +from zope.interface.verify import verifyClass, verifyObject +from zope.interface.exceptions import DoesNotImplement, BrokenImplementation +from zope.interface.exceptions import BrokenMethodImplementation + +import unittest + +class Test(unittest.TestCase): + + def testNotImplemented(self): + + class C(object): pass + + class I(Interface): pass + + self.assertRaises(DoesNotImplement, verifyClass, I, C) + + classImplements(C, I) + + verifyClass(I, C) + + def testMissingAttr(self): + + class I(Interface): + def f(): pass + + class C(object): + implements(I) + + self.assertRaises(BrokenImplementation, verifyClass, I, C) + + C.f=lambda self: None + + verifyClass(I, C) + + def testMissingAttr_with_Extended_Interface(self): + + class II(Interface): + def f(): + pass + + class I(II): + pass + + class C(object): + implements(I) + + self.assertRaises(BrokenImplementation, verifyClass, I, C) + + C.f=lambda self: None + + verifyClass(I, C) + + def testWrongArgs(self): + + class I(Interface): + def f(a): pass + + class C(object): + def f(self, b): pass + + implements(I) + + # We no longer require names to match. + #self.assertRaises(BrokenMethodImplementation, verifyClass, I, C) + + C.f=lambda self, a: None + + verifyClass(I, C) + + C.f=lambda self, **kw: None + + self.assertRaises(BrokenMethodImplementation, verifyClass, I, C) + + C.f=lambda self, a, *args: None + + verifyClass(I, C) + + C.f=lambda self, a, *args, **kw: None + + verifyClass(I, C) + + C.f=lambda self, *args: None + + verifyClass(I, C) + + def testExtraArgs(self): + + class I(Interface): + def f(a): pass + + class C(object): + def f(self, a, b): pass + + implements(I) + + self.assertRaises(BrokenMethodImplementation, verifyClass, I, C) + + C.f=lambda self, a: None + + verifyClass(I, C) + + C.f=lambda self, a, b=None: None + + verifyClass(I, C) + + def testNoVar(self): + + class I(Interface): + def f(a, *args): pass + + class C(object): + def f(self, a): pass + + implements(I) + + self.assertRaises(BrokenMethodImplementation, verifyClass, I, C) + + C.f=lambda self, a, *foo: None + + verifyClass(I, C) + + def testNoKW(self): + + class I(Interface): + def f(a, **args): pass + + class C(object): + def f(self, a): pass + + implements(I) + + self.assertRaises(BrokenMethodImplementation, verifyClass, I, C) + + C.f=lambda self, a, **foo: None + + verifyClass(I, C) + + def testModule(self): + + from zope.interface.tests.ifoo import IFoo + from zope.interface.tests import dummy + + verifyObject(IFoo, dummy) + + def testMethodForAttr(self): + + class IFoo(Interface): + foo = Attribute("The foo Attribute") + + + class Foo: + implements(IFoo) + + def foo(self): + pass + + verifyClass(IFoo, Foo) + + def testNonMethodForMethod(self): + + class IBar(Interface): + def foo(): + pass + + class Bar: + implements(IBar) + + foo = 1 + + self.assertRaises(BrokenMethodImplementation, verifyClass, IBar, Bar) + + +def test_suite(): + loader=unittest.TestLoader() + return loader.loadTestsFromTestCase(Test) + +if __name__=='__main__': + unittest.TextTestRunner().run(test_suite()) diff --git a/zope/interface/tests/unitfixtures.py b/zope/interface/tests/unitfixtures.py new file mode 100644 index 0000000..1007b19 --- /dev/null +++ b/zope/interface/tests/unitfixtures.py @@ -0,0 +1,142 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Unit Test Fixtures + +$Id: unitfixtures.py 26898 2004-08-04 09:38:12Z hdima $ +""" +from zope.interface import Interface, invariant +from zope.interface.interface import Attribute +from zope.interface.exceptions import Invalid + +class mytest(Interface): + pass + +class C(object): + def m1(self, a, b): + "return 1" + return 1 + + def m2(self, a, b): + "return 2" + return 2 + +# testInstancesOfClassImplements + +# YAGNI IC=Interface.impliedInterface(C) +class IC(Interface): + def m1(a, b): + "return 1" + + def m2(a, b): + "return 2" + + + +C.__implemented__=IC + +class I1(Interface): + def ma(): + "blah" + +class I2(I1): pass + +class I3(Interface): pass + +class I4(Interface): pass + +class A(I1.deferred()): + __implemented__=I1 + +class B(object): + __implemented__=I2, I3 + +class D(A, B): pass + +class E(A, B): + __implemented__ = A.__implemented__, C.__implemented__ + + +class FooInterface(Interface): + """ This is an Abstract Base Class """ + + foobar = Attribute("fuzzed over beyond all recognition") + + def aMethod(foo, bar, bingo): + """ This is aMethod """ + + def anotherMethod(foo=6, bar="where you get sloshed", bingo=(1,3,)): + """ This is anotherMethod """ + + def wammy(zip, *argues): + """ yadda yadda """ + + def useless(**keywords): + """ useless code is fun! """ + +class Foo(object): + """ A concrete class """ + + __implemented__ = FooInterface, + + foobar = "yeah" + + def aMethod(self, foo, bar, bingo): + """ This is aMethod """ + return "barf!" + + def anotherMethod(self, foo=6, bar="where you get sloshed", bingo=(1,3,)): + """ This is anotherMethod """ + return "barf!" + + def wammy(self, zip, *argues): + """ yadda yadda """ + return "barf!" + + def useless(self, **keywords): + """ useless code is fun! """ + return "barf!" + +foo_instance = Foo() + +class Blah(object): + pass + +new = Interface.__class__ +FunInterface = new('FunInterface') +BarInterface = new('BarInterface', [FunInterface]) +BobInterface = new('BobInterface') +BazInterface = new('BazInterface', [BobInterface, BarInterface]) + +# fixtures for invariant tests +def ifFooThenBar(obj): + if getattr(obj, 'foo', None) and not getattr(obj, 'bar', None): + raise Invalid('If Foo, then Bar!') +class IInvariant(Interface): + foo = Attribute('foo') + bar = Attribute('bar; must eval to Boolean True if foo does') + invariant(ifFooThenBar) +def BarGreaterThanFoo(obj): + foo = getattr(obj, 'foo', None) + bar = getattr(obj, 'bar', None) + if foo is not None and isinstance(foo, type(bar)): + # type checking should be handled elsewhere (like, say, + # schema); these invariants should be intra-interface + # constraints. This is a hacky way to do it, maybe, but you + # get the idea + if not bar > foo: + raise Invalid('Please, Boo MUST be greater than Foo!') +class ISubInvariant(IInvariant): + invariant(BarGreaterThanFoo) +class InvariantC(object): + pass diff --git a/zope/interface/verify.py b/zope/interface/verify.py new file mode 100644 index 0000000..ae1b37a --- /dev/null +++ b/zope/interface/verify.py @@ -0,0 +1,111 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 Zope Corporation and Contributors. +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# +############################################################################## +"""Verify interface implementations + +$Id: verify.py 37426 2005-07-26 06:24:15Z hdima $ +""" +from zope.interface.exceptions import BrokenImplementation, DoesNotImplement +from zope.interface.exceptions import BrokenMethodImplementation +from types import FunctionType, MethodType +from zope.interface.interface import fromMethod, fromFunction, Method + +# This will be monkey-patched when running under Zope 2, so leave this +# here: +MethodTypes = (MethodType, ) + + +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 == 'c': + tester = iface.implementedBy + else: + tester = iface.providedBy + + if not tentative and not tester(candidate): + raise DoesNotImplement(iface) + + # Here the `desc` is either an `Attribute` or `Method` instance + for name, desc in iface.namesAndDescriptions(1): + if not hasattr(candidate, name): + if (not isinstance(desc, Method)) and vtype == 'c': + # We can't verify non-methods on classes, since the + # class may provide attrs in it's __init__. + continue + + raise BrokenImplementation(iface, name) + + attr = getattr(candidate, name) + if not isinstance(desc, Method): + # If it's not a method, there's nothing else we can test + continue + + if isinstance(attr, FunctionType): + # should never get here, since classes should not provide functions + meth = fromFunction(attr, iface, name=name) + elif (isinstance(attr, MethodTypes) + and type(attr.im_func) is FunctionType): + meth = fromMethod(attr, iface, name) + else: + if not callable(attr): + raise BrokenMethodImplementation(name, "Not a method") + # sigh, it's callable, but we don't know how to intrspect it, so + # we have to give it a pass. + continue + + # Make sure that the required and implemented method signatures are + # the same. + desc = desc.getSignatureInfo() + meth = meth.getSignatureInfo() + + mess = _incompat(desc, meth) + if mess: + raise BrokenMethodImplementation(name, mess) + + return True + +def verifyClass(iface, candidate, tentative=0): + return _verify(iface, candidate, tentative, vtype='c') + +def verifyObject(iface, candidate, tentative=0): + return _verify(iface, candidate, tentative, vtype='o') + +def _incompat(required, implemented): + #if (required['positional'] != + # implemented['positional'][:len(required['positional'])] + # and implemented['kwargs'] is None): + # return 'imlementation has different argument names' + if len(implemented['required']) > len(required['required']): + return 'implementation requires too many arguments' + if ((len(implemented['positional']) < len(required['positional'])) + and not implemented['varargs']): + return "implementation doesn't allow enough arguments" + if required['kwargs'] and not implemented['kwargs']: + return "implementation doesn't support keyword arguments" + if required['varargs'] and not implemented['varargs']: + return "implementation doesn't support variable arguments" |