Skip to main content

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

This Zope 3 package provides a zope.schema vocabulary registry that uses utilities to look up vocabularies.

Component-based Vocabulary Registry

This package provides a vocabulary registry for zope.schema, based on the component architecture.

It replaces the zope.schema’s simple vocabulary registry when zope.vocabularyregistry package is imported, so it’s done automatically. All we need is provide vocabulary factory utilities:

>>> 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')

Now we can get the vocabulary using standard zope.schema way:

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

If vocabulary is not found, VocabularyRegistryError is raised.

>>> 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'

We can also use vocabularies defined in local component registries. Let’s define some local sites with a vocabulary.

>>> 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)

Vocabularies defined in local component registries can be accessed in two ways.

  1. Using the registry from within a site.

>>> with zope.component.hooks.site(local_site_even):
...     voc = getVocabularyRegistry().get(None, 'SomeVocabulary')
...     [term.value for term in voc]
[4, 6, 8]
  1. Binding to a context that can be used to look up a local site manager.

>>> 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]

Binding to a context takes precedence over active site, so we can look up vocabularies from other sites.

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

If we cannot find a local site for given context, currently active site is used.

>>> 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]

Configuration

This package provides configuration that ensures the vocabulary registry is established:

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

CHANGES

3.0 (2025-09-12)

  • Replace pkg_resources namespace with PEP 420 native namespace.

2.0 (2025-09-04)

  • Add support for Python 3.11, 3.12, 3.13.

  • Drop support for Python 2.7, 3.5, 3.6, 3.7, 3.8.

1.2.0 (2021-11-26)

  • Add support for Python 3.8, 3.9, and 3.10.

1.1.1 (2018-12-03)

  • Important bugfix for the new feature introduced in 1.1.0: Fall back to active site manager if no local site manager can be looked up for provided context.

1.1.0 (2018-11-30)

  • Ensure that a context is provided when looking up the vocabulary factory.

  • Drop support for Python 2.6 and 3.3.

  • Add support for Python 3.5, 3.6, 3.7, PyPy and PyPy3.

1.0.0 (2013-03-01)

  • Added support for Python 3.3.

  • Replaced deprecated zope.interface.implements usage with equivalent zope.interface.implementer decorator.

  • Dropped support for Python 2.4 and 2.5.

  • Initial release independent of zope.app.schema.

Release files for zope.vocabularyregistry 3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for zope.vocabularyregistry 3.0
File Size Uploaded
zope_vocabularyregistry-3.0.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for zope.vocabularyregistry 3.0
File Interpreter ABI Platform
zope_vocabularyregistry-3.0-py3-none-any.whl Python 3 none any Details

Total release size:19.1 kB

Release files / zope_vocabularyregistry-3.0.tar.gz

Download URL zope_vocabularyregistry-3.0.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
23ea2f64e7d5aa2ec2b538904349d9c64d038c3bef645687c4a15bdb12e7c8fe
BLAKE2b-256 checksum
How to use checksums
26192f4f43f7b6c1789b5436dffefcfb3c744a1a445c981f6ba8d271e186d704
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release files / zope_vocabularyregistry-3.0-py3-none-any.whl

Download URL zope_vocabularyregistry-3.0-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f68b46ca211aa1e0eeede215a3f29205d26c1624bec08aef7bf0a44c8ee553d
BLAKE2b-256 checksum
How to use checksums
887ddcac00615916dbda1fe6005e44297f25934b90531c24529c52f52fc79a21
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

3.0 This release

2 release files

2.0

2 release files

1.2.0

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page