在启动时查询注册表时的ComponentLookupError

前端之家收集整理的这篇文章主要介绍了在启动时查询注册表时的ComponentLookupError前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在构建一个应用程序,使用collective.lead(trunk)查询外部关系数据库中的某些数据.用户可以在自定义Plone控制面板工具中修改数据库连接设置(我按照Aspeli的Professional Plone Development书中的示例).以这种方式查询数据库设置.

我的产品base.zcml为数据库设置了一个实用程序:

<include package="plone.app.registry" />
<include package="collective.lead" />

<i18n:registerTranslations directory="locales" />

<utility
  provides="collective.lead.interfaces.IDatabase"
  factory=".dbsettings.CalculatorDatabase"
  name="test.calc.db"
  />

dbsettings.py有:

from zope.component import getUtility
from plone.registry.interfaces import IRegistry

class CalculatorDatabase(Database):

    @property
    def _url(self):
        registry = getUtility(IRegistry)
        settings = registry.forInterface(IDatabaseSettings)
        return URL(
            drivername=settings.drivername,username=settings.username,password=settings.password,host=settings.hostname,port=settings.port,database=settings.database,)

这会在运行时引发ComponentLookupError异常:

File "/home/zope/envs/test-web/src/test.calc/test/calc/dbsettings.py",line 38,in _url
    registry = getUtility(IRegistry)
File "/home/zope/envs/test-web/eggs/zope.component-3.7.1-py2.6.egg/zope/component/_api.py",line 171,in getUtility
    raise ComponentLookupError(interface,name)
zope.configuration.config.ConfigurationExecutionError: <class 'zope.component.interfaces.ComponentLookupError'>: (<InterfaceClass plone.registry.interfaces.IRegistry>,'')
in:
File "/home/zope/envs/test-web/src/test.calc/test/calc/configure.zcml",line 26.2-30.6
<utility
  provides="collective.lead.interfaces.IDatabase"
  factory=".dbsettings.CalculatorDatabase"
  name="test.calc.db"
  />

为什么在运行时找不到注册表?我究竟做错了什么?

谢谢.

史蒂夫提到的是问题的根源,因此我只会针对您无法进行异常处理的情况添加解决方法.在类似的场景中,我需要注册一个依赖于存储在注册表中的设置的实用程序.没有这些设置就无法注册该实用程序,所以我绝对希望事情按顺序发生.

解决方法是不在zcml中注册该实用程序,而是在以下订阅者中执行此操作:

<subscriber
    for="Products.CMFPlone.interfaces.IPloneSiteRoot
         zope.app.publication.interfaces.IBeforeTraverseEvent"
    handler=".component.setupStuff"
    />

这将保证setupStuff可以访问所有本地实用程序.另请注意,在查询实用程序是否已存在之后,setupStuff也会快速返回,因为订阅者将在每个请求上触发.

原文链接:https://www.f2er.com/windows/364320.html

猜你在找的Windows相关文章