Use annotation for vocabulary declaration
authorThierry Florac <thierry.florac@onf.fr>
Tue, 15 Nov 2016 10:43:55 +0100
changeset 71 01d01045a2b7
parent 70 ba0aefcd471a
child 72 9049384a2bd4
Use annotation for vocabulary declaration
src/pyams_utils/encoding.py
src/pyams_utils/text.py
src/pyams_utils/timezone/vocabulary.py
src/pyams_utils/zodb.py
--- a/src/pyams_utils/encoding.py	Tue Nov 15 10:43:03 2016 +0100
+++ b/src/pyams_utils/encoding.py	Tue Nov 15 10:43:55 2016 +0100
@@ -16,13 +16,14 @@
 # import standard library
 
 # import interfaces
-from zope.schema.interfaces import IVocabularyFactory, IChoice
+from zope.schema.interfaces import IChoice
 
 # import packages
 from pyams_utils.request import check_request
-from zope.interface import provider, implementer
+from pyams_utils.vocabulary import vocabulary_config
+from zope.interface import implementer
 from zope.schema import Choice
-from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary, getVocabularyRegistry
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
 
 from pyams_utils import _
 
@@ -123,7 +124,7 @@
 }
 
 
-@provider(IVocabularyFactory)
+@vocabulary_config(name='PyAMS encodings')
 class EncodingsVocabulary(SimpleVocabulary):
 
     def __init__(self, terms, *interfaces):
@@ -133,8 +134,6 @@
         terms.sort(key=lambda x: x.title)
         super(EncodingsVocabulary, self).__init__(terms, *interfaces)
 
-getVocabularyRegistry().register('PyAMS encodings', EncodingsVocabulary)
-
 
 class IEncodingField(IChoice):
     """Encoding field interface"""
--- a/src/pyams_utils/text.py	Tue Nov 15 10:43:03 2016 +0100
+++ b/src/pyams_utils/text.py	Tue Nov 15 10:43:55 2016 +0100
@@ -21,13 +21,13 @@
 from pyams_utils.interfaces.tales import ITALESExtension
 from pyams_utils.interfaces.text import IHTMLRenderer
 from pyramid.interfaces import IRequest
-from zope.schema.interfaces import IVocabularyFactory
 
 # import packages
 from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
 from pyams_utils.request import check_request
-from zope.interface import provider, Interface
-from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm, getVocabularyRegistry
+from pyams_utils.vocabulary import vocabulary_config
+from zope.interface import Interface
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 
 
 def get_text_start(text, length, max=0):
@@ -124,7 +124,7 @@
             return str(context)
 
 
-@provider(IVocabularyFactory)
+@vocabulary_config(name='PyAMS HTML renderers')
 class RenderersVocabulary(SimpleVocabulary):
     """Text renderers vocabulary"""
 
@@ -135,5 +135,3 @@
         terms = [SimpleTerm(name, name, translate(adapt.title).label)
                  for name, adapt in registry.getAdapters(('', request), IHTMLRenderer)]
         super(RenderersVocabulary, self).__init__(terms)
-
-getVocabularyRegistry().register('PyAMS HTML renderers', RenderersVocabulary)
--- a/src/pyams_utils/timezone/vocabulary.py	Tue Nov 15 10:43:03 2016 +0100
+++ b/src/pyams_utils/timezone/vocabulary.py	Tue Nov 15 10:43:55 2016 +0100
@@ -17,19 +17,16 @@
 import pytz
 
 # import interfaces
-from zope.schema.interfaces import IVocabularyFactory
 
 # import packages
-from zope.interface import provider
-from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary, getVocabularyRegistry
+from pyams_utils.vocabulary import vocabulary_config
+from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
 
 
-@provider(IVocabularyFactory)
+@vocabulary_config(name='PyAMS timezones')
 class TimezonesVocabulary(SimpleVocabulary):
     """Timezones vocabulary"""
 
     def __init__(self, *args, **kw):
         terms = [SimpleTerm(t, t, t) for t in pytz.all_timezones]
         super(TimezonesVocabulary, self).__init__(terms)
-
-getVocabularyRegistry().register('PyAMS timezones', TimezonesVocabulary)
--- a/src/pyams_utils/zodb.py	Tue Nov 15 10:43:03 2016 +0100
+++ b/src/pyams_utils/zodb.py	Tue Nov 15 10:43:55 2016 +0100
@@ -23,21 +23,21 @@
 from ZODB.interfaces import IConnection
 from zope.annotation.interfaces import IAttributeAnnotatable
 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectRemovedEvent
-from zope.schema.interfaces import IVocabularyFactory
 
 # import packages
 from persistent import Persistent
 from pyams_utils.adapter import adapter_config
 from pyams_utils.registry import get_utilities_for
+from pyams_utils.vocabulary import vocabulary_config
 from pyramid.events import subscriber
 from pyramid_zodbconn import get_uris, db_from_uri
 from ZEO import ClientStorage
 from ZODB import DB
 from zope.container.contained import Contained
-from zope.interface import implementer, provider
+from zope.interface import implementer
 from zope.schema import getFieldNames
 from zope.schema.fieldproperty import FieldProperty
-from zope.schema.vocabulary import getVocabularyRegistry, SimpleVocabulary, SimpleTerm
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
 
 
 @adapter_config(context=IPersistent, provides=IConnection)
@@ -149,7 +149,7 @@
     manager.unregisterUtility(event.object, IZEOConnection, name=event.object.name)
 
 
-@provider(IVocabularyFactory)
+@vocabulary_config(name='PyAMS ZEO connections')
 class ZEOConnectionVocabulary(SimpleVocabulary):
     """ZEO connections vocabulary"""
 
@@ -157,8 +157,6 @@
         terms = [SimpleTerm(name, title=util.name) for name, util in get_utilities_for(IZEOConnection)]
         super(ZEOConnectionVocabulary, self).__init__(terms)
 
-getVocabularyRegistry().register('PyAMS ZEO connections', ZEOConnectionVocabulary)
-
 
 def get_connection_from_settings(settings):
     """Load connection matching registry settings"""