Added annotation for vocabulary registry
authorThierry Florac <thierry.florac@onf.fr>
Tue, 15 Nov 2016 10:43:03 +0100
changeset 70 ba0aefcd471a
parent 69 0b859bda300e
child 71 01d01045a2b7
Added annotation for vocabulary registry
src/pyams_utils/vocabulary.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_utils/vocabulary.py	Tue Nov 15 10:43:03 2016 +0100
@@ -0,0 +1,38 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+import logging
+logger = logging.getLogger('PyAMS (utils)')
+
+# import interfaces
+from zope.schema.interfaces import IVocabularyFactory
+
+# import packages
+from zope.interface import directlyProvides
+from zope.schema.vocabulary import getVocabularyRegistry
+
+
+class vocabulary_config:
+    """Class decorator to define a vocabulary"""
+
+    def __init__(self, name):
+        self.name = name
+
+    def __call__(self, klass):
+        logger.debug('Registering class {0} as vocabulary with name "{1}"'.format(str(klass), self.name))
+        directlyProvides(klass, IVocabularyFactory)
+        getVocabularyRegistry().register(self.name, klass)
+        return klass