0

我有一个用于 plone 站点的产品,其模块包含一个实用程序类,并且在将/应该使用此实用程序的模块中,我试图在模块级别设置它。

在包含实用程序(my.product.testutility)的模块中,我有这个:

from five import grok
from zope.interface import Interface

class ITestUtil(Interface):
    """Interface of utility
    """

    def returnTest(self):
        """return a string for now
        """ 

class TestUtil(object):
    """Utility class test
    """
    grok.implements(ITestUtil)

    def returnTest(self):
        return "testing"

grok.global_utility(TestUtil, name="TestUtility")

在将使用此实用程序 (my.product.stringtesting) 的模块中:

from five import grok
from my.package.testutility import ITestUtil
from zope import component, schema
from Products.CMFCore.interfaces import ISiteRoot

utilForTesting = component.getUtility(ITestUtil, name="TestUtility")

class IStringTest(Interface):
     ......


class View(grok.View):
     def returnStringForTest(self):
         return utilForTesting.returnTest()

我还有一个模板文件,它会调用 returnStringForTest 在渲染页面上显示字符串。

不幸的是,我最终得到了这个错误: ComponentLookupError: (< InterfaceClass my.product.testutility.ITestUtil >, "TestUtility")

我确实尝试了几种不同的方法,例如使用 grok.GlobalUtility 作为基础,而不是使其成为通过 grok.global_utility 注册它的对象。在测试这个时,我确实使用它删除了类中的 name 参数。

我试图遵循的文档是 grok 网站上的参考资料,查看包含全局实用程序信息的指令页面。

另外,我正在使用 grok 0.9。编辑:我使用的 Plone 版本是 Plone 4,我使用的 python 版本是 2.7。

是否可以像我尝试的那样在模块级别设置实用程序?

4

1 回答 1

3

你可以做你想做的事,完全不依赖 Zope。

您可以更改 my.product.stringtesting 中的行:从

utilForTesting = component.getUtility(ITestUtil, name="TestUtility")

utilForTesting = TestUtil()
于 2014-11-08T10:03:30.677 回答