48
46
# Attribute that registerAdapter looks at. Is this supposed to be public?
49
47
ALLOW_DUPLICATES = 0
49
# Define a function to find the registered adapter factory, using either a
50
# version of Zope Interface which has the `registered' method or an older
51
# version which does not.
52
if getattr(AdapterRegistry, 'registered', None) is None:
53
def _registered(registry, required, provided):
55
Return the adapter factory for the given parameters in the given
56
registry, or None if there is not one.
58
return registry.get(required).selfImplied.get(provided, {}).get('')
60
def _registered(registry, required, provided):
62
Return the adapter factory for the given parameters in the given
63
registry, or None if there is not one.
65
return registry.registered([required], provided)
51
68
def registerAdapter(adapterFactory, origInterface, *interfaceClasses):
52
69
"""Register an adapter class.
65
82
origInterface = declarations.implementedBy(origInterface)
67
84
for interfaceClass in interfaceClasses:
68
factory = self.get(origInterface).selfImplied.get(interfaceClass, {}).get('')
69
if (factory and not ALLOW_DUPLICATES):
85
factory = _registered(self, origInterface, interfaceClass)
86
if factory is not None and not ALLOW_DUPLICATES:
70
87
raise ValueError("an adapter (%s) was already registered." % (factory, ))
71
88
for interfaceClass in interfaceClasses:
72
89
self.register([origInterface], interfaceClass, '', adapterFactory)
115
134
warnings.warn("components.fixClassImplements doesn't do anything in Twisted 2.3, stop calling it.", ComponentsDeprecationWarning, stacklevel=2)
118
def getAdapterClass(klass, interfaceClass, default):
121
Use getAdapterFactory instead.
123
warnings.warn("components.getAdapterClass is deprecated in Twisted 2.3. Use components.getAdapterFactory instead.", ComponentsDeprecationWarning, stacklevel=2)
124
return getAdapterFactory(klass, interfaceClass, default)
126
def getAdapterClassWithInheritance(klass, interfaceClass, default):
129
Use getAdapterFactory instead.
131
warnings.warn("components.getAdapterClassWithInheritance is deprecated in Twisted 2.3. Use components.getAdapterFactory instead.", ComponentsDeprecationWarning, stacklevel=2)
132
return getAdapterFactory(klass, interfaceClass, default)
135
def getRegistry(r=None):
136
138
"""Returns the Twisted global
137
139
C{zope.interface.adapter.AdapterRegistry} instance.
140
warnings.warn("components.getRegistry's argument is deprecated in Twisted 2.3 and has been ignored since 2.0.", ComponentsDeprecationWarning, stacklevel=2)
141
141
return globalRegistry
143
143
# FIXME: deprecate attribute somehow?
144
144
CannotAdapt = TypeError
148
"""Default value for functions which raise if default not passed.
151
class MetaInterface(interface.InterfaceClass):
152
def __init__(self, name, bases=(), attrs=None, __doc__=None,
154
if attrs is not None:
155
if __module__ == None and attrs.has_key('__module__'):
156
__module__ = attrs['__module__']
157
del attrs['__module__']
158
if __doc__ == None and attrs.has_key('__doc__'):
159
__doc__ = attrs['__doc__']
162
for k, v in attrs.items():
163
if isinstance(v, types.FunctionType):
164
# Look here: the following line is the only useful thing
165
# this function is actually doing. z.i interfaces don't
166
# specify self as the first argument, but t.p.c interfaces
167
# do, so we need to use the imlevel=1 argument.
168
attrs[k] = interface.fromFunction(v, name, name=k, imlevel=1)
169
elif not isinstance(v, interface.Attribute):
170
raise interface.InvalidInterface("Concrete attribute, %s" % k)
172
# BEHOLD A GREAT EVIL SHALL COME UPON THEE
173
if __module__ == None:
174
__module__ = sys._getframe(1).f_globals['__name__']
176
if not __module__.startswith("twisted."):
177
# Don't warn for use within twisted, because twisted classes must
178
# not be switched to z.i.Interface yet, b/c we forgot to deprecate
179
# the "default" kwarg to __call__. Also when t.p.c.Interface is
180
# removed, the rest of twisted can be updated simultaneously. But
181
# everyone else should just use z.i.Interface directly now.
182
warnings.warn("twisted.python.components.Interface is deprecated in Twisted 2.3. Use zope.interface.Interface instead. (also note that you should not use 'self' as the first arg on function decls in z.i.Interfaces).", ComponentsDeprecationWarning, stacklevel=2)
184
return interface.InterfaceClass.__init__(self, name, bases, attrs, __doc__, __module__)
186
def __call__(self, adaptable, alternate=_Nothing, default=_Nothing):
187
if default is not _Nothing:
188
warnings.warn("The 'default=' keyword to Interface.__call__ is deprecated in Twisted 2.3. Use a 2nd positional argument instead.", ComponentsDeprecationWarning, stacklevel=2)
190
if alternate is not _Nothing:
191
raise RuntimeError("Cannot specify both default and alternate to Interface.__call__.")
194
if alternate is _Nothing:
195
return interface.InterfaceClass.__call__(self, adaptable)
197
return interface.InterfaceClass.__call__(self, adaptable, alternate)
200
Interface = MetaInterface("Interface", __module__="twisted.python.components")
204
147
"""I am the default implementation of an Adapter for some interface.
347
285
return self._adapterCache[k]
349
287
adapter = interface.__adapt__(self)
350
if adapter is not None and adapter is not _Nothing and not (
288
if adapter is not None and not (
351
289
hasattr(adapter, "temporaryAdapter") and
352
290
adapter.temporaryAdapter):
353
291
self._adapterCache[k] = adapter
354
292
if (hasattr(adapter, "multiComponent") and
355
293
adapter.multiComponent):
356
294
self.addComponent(adapter)
359
300
def __conform__(self, interface):
360
301
return self.getComponent(interface)