我有下一节课:
@implementer(ISocial)
class SocialVKSelenium:
pass当我把它添加到zope注册表时:
gsm = getGlobalSiteManager()
gsm.registerAdapter(SocialVKSelenium)我得到了:TypeError: The adapter factory doesn't have a __component_adapts__ attribute and no required specifications were specified
当我添加那里适配器(IOther)时,注册工作正常,但没有。为什么会发生这种事?
发布于 2017-01-30 17:58:00
您需要在类或注册表上提供上下文。
我怀疑您没有将整个问题集进行通信--适配器是一个组件,它适应指定的接口的对象,并提供另一个组件。您的示例未能指定所适应的上下文是什么,也就是说,在您的适配器对象的类构造过程中会采用什么类型的对象?
例如,这可以很好地工作:
from zope.interface import Interface, implements
from zope.component import getGlobalSiteManager, adapts
class IWeight(Interface):
pass
class IVolume(Interface):
pass
class WeightToVolume(object):
implements(IVolume)
adapts(IWeight)
#...
gsm = getGlobalSiteManager()
gsm.registerAdapter(WeightToVolume)虽然您可以为此使用修饰器(实现者/适配器)语法,但按照惯例,对于属于类而不是函数的适配器工厂,更倾向于使用实现/适配器。
至少,如果适配器没有声明它在类或工厂函数本身上所适应的内容,则需要告诉注册表。在最广泛的情况下,这可能看起来是:
gsm.registerAdapter(MyAdapterClassHere, required=(Interface,))当然,上面的示例是一个适配器,它声称可以调整任何上下文,除非您知道需要它的原因,否则不建议这样做。
https://stackoverflow.com/questions/41925655
复制相似问题