Python / Plone - 是否可以在py文件的模块级别调用全局实用程序?

时间:2014-11-05 18:42:57

标签: python plone grok

我有一个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站点上的References,查看指令页面,其中包含全局实用程序信息。

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

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

1 个答案:

答案 0 :(得分:2)

你可以做你想做的事情,而不依赖于Zope。

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

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

utilForTesting = TestUtil()
相关问题