=========================
zope.vocabularyregistry
=========================
.. image:: https://img.shields.io/pypi/v/zope.vocabularyregistry.svg
:target: https://pypi.python.org/pypi/zope.vocabularyregistry/
:alt: Latest release
.. image:: https://img.shields.io/pypi/pyversions/zope.vocabularyregistry.svg
:target: https://pypi.org/project/zope.vocabularyregistry/
:alt: Supported Python versions
.. image:: https://github.com/zopefoundation/zope.vocabularyregistry/actions/workflows/tests.yml/badge.svg
:target: https://github.com/zopefoundation/zope.vocabularyregistry/actions/workflows/tests.yml
.. image:: https://coveralls.io/repos/github/zopefoundation/zope.vocabularyregistry/badge.svg?branch=master
:target: https://coveralls.io/github/zopefoundation/zope.vocabularyregistry?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]
2. 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``.
Raw data
{
"_id": null,
"home_page": "http://github.com/zopefoundation/zope.vocabularyregistry",
"name": "zope.vocabularyregistry",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "zope3 schema vocabulary registry",
"author": "Zope Foundation and Contributors",
"author_email": "zope-dev@zope.dev",
"download_url": "https://files.pythonhosted.org/packages/26/19/2f4f43f7b6c1789b5436dffefcfb3c744a1a445c981f6ba8d271e186d704/zope_vocabularyregistry-3.0.tar.gz",
"platform": null,
"description": "=========================\n zope.vocabularyregistry\n=========================\n\n.. image:: https://img.shields.io/pypi/v/zope.vocabularyregistry.svg\n :target: https://pypi.python.org/pypi/zope.vocabularyregistry/\n :alt: Latest release\n\n.. image:: https://img.shields.io/pypi/pyversions/zope.vocabularyregistry.svg\n :target: https://pypi.org/project/zope.vocabularyregistry/\n :alt: Supported Python versions\n\n.. image:: https://github.com/zopefoundation/zope.vocabularyregistry/actions/workflows/tests.yml/badge.svg\n :target: https://github.com/zopefoundation/zope.vocabularyregistry/actions/workflows/tests.yml\n\n.. image:: https://coveralls.io/repos/github/zopefoundation/zope.vocabularyregistry/badge.svg?branch=master\n :target: https://coveralls.io/github/zopefoundation/zope.vocabularyregistry?branch=master\n\n\nThis Zope 3 package provides a ``zope.schema`` vocabulary registry that uses\nutilities to look up vocabularies.\n\n\n=====================================\n Component-based Vocabulary Registry\n=====================================\n\nThis package provides a vocabulary registry for zope.schema,\nbased on the component architecture.\n\nIt replaces the zope.schema's simple vocabulary registry\nwhen ``zope.vocabularyregistry`` package is imported, so it's done\nautomatically. All we need is provide vocabulary factory\nutilities:\n\n >>> import zope.vocabularyregistry\n >>> from zope.component import provideUtility\n >>> from zope.schema.interfaces import IVocabularyFactory\n >>> from zope.schema.vocabulary import SimpleTerm\n >>> from zope.schema.vocabulary import SimpleVocabulary\n\n >>> def makeVocabularyFactory(*values):\n ... def vocabularyFactory(context=None):\n ... terms = [SimpleTerm(v) for v in values]\n ... return SimpleVocabulary(terms)\n ... return vocabularyFactory\n\n >>> zope.component.provideUtility(\n ... makeVocabularyFactory(1, 2), IVocabularyFactory,\n ... name='SomeVocabulary')\n\nNow we can get the vocabulary using standard zope.schema\nway:\n\n >>> from zope.schema.vocabulary import getVocabularyRegistry\n >>> vr = getVocabularyRegistry()\n >>> voc = vr.get(None, 'SomeVocabulary')\n >>> [term.value for term in voc]\n [1, 2]\n\n\nIf vocabulary is not found, VocabularyRegistryError is raised.\n\n >>> try:\n ... vr.get(None, 'NotAvailable')\n ... except LookupError as error:\n ... print(\"%s.%s: %s\" % (error.__module__, error.__class__.__name__, error))\n zope.schema.vocabulary.VocabularyRegistryError: unknown vocabulary: 'NotAvailable'\n\n\nWe can also use vocabularies defined in local component registries.\nLet's define some local sites with a vocabulary.\n\n >>> import zope.component.hooks\n >>> from zope.component import globalregistry\n >>> from zope.component.globalregistry import getGlobalSiteManager\n\n >>> from zope.interface.registry import Components\n >>> class LocalSite(object):\n ... def __init__(self, name):\n ... self.sm = Components(\n ... name=name, bases=(globalregistry.getGlobalSiteManager(), ))\n ...\n ... def getSiteManager(self):\n ... return self.sm\n\n >>> local_site_even = LocalSite('local_site_even')\n >>> local_site_even.sm.registerUtility(\n ... makeVocabularyFactory(4, 6, 8), IVocabularyFactory,\n ... name='SomeVocabulary', event=False)\n\n >>> local_site_odd = LocalSite('local_site_odd')\n >>> local_site_odd.sm.registerUtility(\n ... makeVocabularyFactory(3, 5, 7), IVocabularyFactory,\n ... name='SomeVocabulary', event=False)\n\n\nVocabularies defined in local component registries can be accessed\nin two ways.\n\n1. Using the registry from within a site.\n\n >>> with zope.component.hooks.site(local_site_even):\n ... voc = getVocabularyRegistry().get(None, 'SomeVocabulary')\n ... [term.value for term in voc]\n [4, 6, 8]\n\n2. Binding to a context that can be used to look up a local site manager.\n\n >>> from zope.interface.interfaces import IComponentLookup\n >>> zope.component.provideAdapter(\n ... lambda number: ((local_site_even, local_site_odd)[number % 2]).sm,\n ... adapts=(int, ), provides=IComponentLookup)\n\n >>> context = 4\n >>> voc = getVocabularyRegistry().get(context, 'SomeVocabulary')\n >>> [term.value for term in voc]\n [4, 6, 8]\n\nBinding to a context takes precedence over active site, so we can look\nup vocabularies from other sites.\n\n >>> context = 7\n >>> with zope.component.hooks.site(local_site_even):\n ... voc = getVocabularyRegistry().get(context, 'SomeVocabulary')\n ... [term.value for term in voc]\n [3, 5, 7]\n\n\nIf we cannot find a local site for given context, currently active\nsite is used.\n\n >>> from zope.interface.interfaces import ComponentLookupError\n >>> def raisingGetSiteManager(context=None):\n ... if context == 42:\n ... raise ComponentLookupError(context)\n ... return zope.component.hooks.getSiteManager(context)\n >>> hook = zope.component.getSiteManager.sethook(raisingGetSiteManager)\n\n >>> context = 42\n >>> with zope.component.hooks.site(local_site_odd):\n ... voc = getVocabularyRegistry().get(context, 'SomeVocabulary')\n ... [term.value for term in voc]\n [3, 5, 7]\n\n\nConfiguration\n=============\n\nThis package provides configuration that ensures the vocabulary\nregistry is established:\n\n\n >>> from zope.configuration import xmlconfig\n >>> _ = xmlconfig.string(r\"\"\"\n ... <configure xmlns=\"http://namespaces.zope.org/zope\" i18n_domain=\"zope\">\n ... <include package=\"zope.vocabularyregistry\" />\n ... </configure>\n ... \"\"\")\n\n\n=========\n CHANGES\n=========\n\n3.0 (2025-09-12)\n================\n\n- Replace ``pkg_resources`` namespace with PEP 420 native namespace.\n\n\n2.0 (2025-09-04)\n================\n\n- Add support for Python 3.11, 3.12, 3.13.\n\n- Drop support for Python 2.7, 3.5, 3.6, 3.7, 3.8.\n\n\n1.2.0 (2021-11-26)\n==================\n\n- Add support for Python 3.8, 3.9, and 3.10.\n\n\n1.1.1 (2018-12-03)\n==================\n\n- Important bugfix for the new feature introduced in 1.1.0: Fall back to\n active site manager if no local site manager can be looked up for provided\n context.\n\n\n1.1.0 (2018-11-30)\n==================\n\n- Ensure that a context is provided when looking up the vocabulary factory.\n\n- Drop support for Python 2.6 and 3.3.\n\n- Add support for Python 3.5, 3.6, 3.7, PyPy and PyPy3.\n\n\n1.0.0 (2013-03-01)\n==================\n\n- Added support for Python 3.3.\n\n- Replaced deprecated ``zope.interface.implements`` usage with equivalent\n ``zope.interface.implementer`` decorator.\n\n- Dropped support for Python 2.4 and 2.5.\n\n- Initial release independent of ``zope.app.schema``.\n",
"bugtrack_url": null,
"license": "ZPL-2.1",
"summary": "Utility-based Vocabulary Registry",
"version": "3.0",
"project_urls": {
"Homepage": "http://github.com/zopefoundation/zope.vocabularyregistry"
},
"split_keywords": [
"zope3",
"schema",
"vocabulary",
"registry"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "887ddcac00615916dbda1fe6005e44297f25934b90531c24529c52f52fc79a21",
"md5": "5f9becaa2ac7f03d4b08d6821bb5d43a",
"sha256": "6f68b46ca211aa1e0eeede215a3f29205d26c1624bec08aef7bf0a44c8ee553d"
},
"downloads": -1,
"filename": "zope_vocabularyregistry-3.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "5f9becaa2ac7f03d4b08d6821bb5d43a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 9468,
"upload_time": "2025-09-12T07:52:19",
"upload_time_iso_8601": "2025-09-12T07:52:19.039201Z",
"url": "https://files.pythonhosted.org/packages/88/7d/dcac00615916dbda1fe6005e44297f25934b90531c24529c52f52fc79a21/zope_vocabularyregistry-3.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "26192f4f43f7b6c1789b5436dffefcfb3c744a1a445c981f6ba8d271e186d704",
"md5": "c2e8b57146d0240f907e7707e773b7bc",
"sha256": "23ea2f64e7d5aa2ec2b538904349d9c64d038c3bef645687c4a15bdb12e7c8fe"
},
"downloads": -1,
"filename": "zope_vocabularyregistry-3.0.tar.gz",
"has_sig": false,
"md5_digest": "c2e8b57146d0240f907e7707e773b7bc",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 9663,
"upload_time": "2025-09-12T07:52:19",
"upload_time_iso_8601": "2025-09-12T07:52:19.892966Z",
"url": "https://files.pythonhosted.org/packages/26/19/2f4f43f7b6c1789b5436dffefcfb3c744a1a445c981f6ba8d271e186d704/zope_vocabularyregistry-3.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-09-12 07:52:19",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "zopefoundation",
"github_project": "zope.vocabularyregistry",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "zope.vocabularyregistry"
}