跳转到主要内容

基于实用工具的词汇注册表

项目描述

zope.vocabularyregistry

Latest release Supported Python versions https://github.com/zopefoundation/zope.vocabularyregistry/actions/workflows/tests.yml/badge.svg https://coveralls.io/repos/github/zopefoundation/zope.vocabularyregistry/badge.svg?branch=master

本Zope 3包提供了一个使用实用程序查找词汇表的zope.schema词汇注册表。

基于组件的词汇注册表

本包基于组件架构为zope.schema提供了一个词汇注册表。

当导入zope.vocabularyregistry包时,它将替换zope.schema的简单词汇注册表,因此这是自动完成的。我们只需要提供词汇工厂实用程序

>>> import zope.vocabularyregistry
>>> from zope.component import provideUtility
>>> from zope.schema.interfaces import IVocabularyFactory
>>> from zope.schema.vocabulary import SimpleTerm
>>> from zope.schema.vocabulary import SimpleVocabulary
>>> def makeVocabularyFactory(*values):
...     def vocabularyFactory(context=None):
...         terms = [SimpleTerm(v) for v in values]
...         return SimpleVocabulary(terms)
...     return vocabularyFactory
>>> zope.component.provideUtility(
...     makeVocabularyFactory(1, 2), IVocabularyFactory,
...     name='SomeVocabulary')

现在我们可以使用标准的zope.schema方式来获取词汇

>>> from zope.schema.vocabulary import getVocabularyRegistry
>>> vr = getVocabularyRegistry()
>>> voc = vr.get(None, 'SomeVocabulary')
>>> [term.value for term in voc]
[1, 2]

如果未找到词汇,则会引发VocabularyRegistryError

>>> try:
...     vr.get(None, 'NotAvailable')
... except LookupError as error:
...     print("%s.%s: %s" % (error.__module__, error.__class__.__name__, error))
zope.schema.vocabulary.VocabularyRegistryError: unknown vocabulary: 'NotAvailable'

我们还可以使用在本地组件注册表中定义的词汇。让我们定义一些带有词汇的本地站点。

>>> import zope.component.hooks
>>> from zope.component import globalregistry
>>> from zope.component.globalregistry import getGlobalSiteManager
>>> from zope.interface.registry import Components
>>> class LocalSite(object):
...   def __init__(self, name):
...      self.sm = Components(
...          name=name, bases=(globalregistry.getGlobalSiteManager(), ))
...
...   def getSiteManager(self):
...       return self.sm
>>> local_site_even = LocalSite('local_site_even')
>>> local_site_even.sm.registerUtility(
...     makeVocabularyFactory(4, 6, 8), IVocabularyFactory,
...     name='SomeVocabulary', event=False)
>>> local_site_odd = LocalSite('local_site_odd')
>>> local_site_odd.sm.registerUtility(
...     makeVocabularyFactory(3, 5, 7), IVocabularyFactory,
...     name='SomeVocabulary', event=False)

本地组件注册表中定义的词汇可以通过两种方式访问。

  1. 在站点内部使用注册表。

>>> with zope.component.hooks.site(local_site_even):
...     voc = getVocabularyRegistry().get(None, 'SomeVocabulary')
...     [term.value for term in voc]
[4, 6, 8]
  1. 绑定到一个可以用于查找本地站点管理器的上下文。

>>> from zope.interface.interfaces import IComponentLookup
>>> zope.component.provideAdapter(
...    lambda number: ((local_site_even, local_site_odd)[number % 2]).sm,
...    adapts=(int, ), provides=IComponentLookup)
>>> context = 4
>>> voc = getVocabularyRegistry().get(context, 'SomeVocabulary')
>>> [term.value for term in voc]
[4, 6, 8]

绑定到上下文的优先级高于活动站点,因此我们可以从其他站点查找词汇。

>>> context = 7
>>> with zope.component.hooks.site(local_site_even):
...     voc = getVocabularyRegistry().get(context, 'SomeVocabulary')
...     [term.value for term in voc]
[3, 5, 7]

如果我们无法为给定上下文找到本地站点,则当前活动站点被使用。

>>> from zope.interface.interfaces import ComponentLookupError
>>> def raisingGetSiteManager(context=None):
...    if context == 42:
...        raise ComponentLookupError(context)
...    return zope.component.hooks.getSiteManager(context)
>>> hook = zope.component.getSiteManager.sethook(raisingGetSiteManager)
>>> context = 42
>>> with zope.component.hooks.site(local_site_odd):
...     voc = getVocabularyRegistry().get(context, 'SomeVocabulary')
...     [term.value for term in voc]
[3, 5, 7]

配置

本包提供的配置确保了词汇注册表的建立

>>> from zope.configuration import xmlconfig
>>> _ = xmlconfig.string(r"""
... <configure xmlns="http://namespaces.zope.org/zope" i18n_domain="zope">
...   <include package="zope.vocabularyregistry" />
... </configure>
... """)

变更记录

1.2.0 (2021-11-26)

  • 添加了对Python 3.8、3.9和3.10的支持。

1.1.1 (2018-12-03)

  • 针对1.1.0中引入的新功能的重要错误修复:如果无法为提供的上下文查找本地站点管理器,则回退到活动站点管理器。

1.1.0 (2018-11-30)

  • 确保在查找词汇工厂时提供上下文。

  • 取消对Python 2.6和3.3的支持。

  • 添加了对Python 3.5、3.6、3.7、PyPy和PyPy3的支持。

1.0.0 (2013-03-01)

  • 添加了对Python 3.3的支持。

  • 将废弃的zope.interface.implements用法替换为等效的zope.interface.implementer装饰器。

  • 取消对Python 2.4和2.5的支持。

  • 首次发布,与zope.app.schema无关。

项目详情


下载文件

下载适用于您平台的应用程序。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源分布

zope.vocabularyregistry-1.2.0.tar.gz (8.3 kB 查看哈希值)

上传时间

构建分布

zope.vocabularyregistry-1.2.0-py2.py3-none-any.whl (10.1 kB 查看哈希值)

上传时间 Python 2 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面