src/pyams_utils/vocabulary.py
branchdev-tf
changeset 408 cf2304af0fab
parent 367 2c95d34496f5
equal deleted inserted replaced
407:0037199881fb 408:cf2304af0fab
    13 """PyAMS_utils.vocabulary module
    13 """PyAMS_utils.vocabulary module
    14 
    14 
    15 This module is used to handle vocabularies.
    15 This module is used to handle vocabularies.
    16 """
    16 """
    17 
    17 
    18 __docformat__ = 'restructuredtext'
       
    19 
       
    20 import logging
    18 import logging
    21 
    19 
    22 import venusian
    20 import venusian
    23 from zope.interface import directlyProvides
    21 from zope.interface import directlyProvides
    24 from zope.schema.interfaces import IVocabularyFactory
    22 from zope.schema.interfaces import IVocabularyFactory
    25 from zope.schema.vocabulary import getVocabularyRegistry
    23 from zope.schema.vocabulary import getVocabularyRegistry
    26 
    24 
    27 
    25 
    28 logger = logging.getLogger('PyAMS (utils)')
    26 __docformat__ = 'restructuredtext'
    29 
    27 
    30 
    28 
    31 class vocabulary_config:
    29 LOGGER = logging.getLogger('PyAMS (utils)')
       
    30 
       
    31 
       
    32 class vocabulary_config:  # pylint: disable=invalid-name
    32     """Class decorator to define a vocabulary
    33     """Class decorator to define a vocabulary
    33 
    34 
    34     :param str name: name of the registered vocabulary
    35     :param str name: name of the registered vocabulary
    35 
    36 
    36     This is, for example, how a vocabulary of registered ZEO connections utilities is created:
    37     This is, for example, how a vocabulary of registered ZEO connections utilities is created:
    46         @vocabulary_config(name='PyAMS ZEO connections')
    47         @vocabulary_config(name='PyAMS ZEO connections')
    47         class ZEOConnectionVocabulary(SimpleVocabulary):
    48         class ZEOConnectionVocabulary(SimpleVocabulary):
    48             '''ZEO connections vocabulary'''
    49             '''ZEO connections vocabulary'''
    49 
    50 
    50             def __init__(self, context=None):
    51             def __init__(self, context=None):
    51                 terms = [SimpleTerm(name, title=util.name) for name, util in get_utilities_for(IZEOConnection)]
    52                 terms = [SimpleTerm(name, title=util.name)
       
    53                          for name, util in get_utilities_for(IZEOConnection)]
    52                 super(ZEOConnectionVocabulary, self).__init__(terms)
    54                 super(ZEOConnectionVocabulary, self).__init__(terms)
    53 
    55 
    54     You can then use such a vocabulary in any schema field:
    56     You can then use such a vocabulary in any schema field:
    55 
    57 
    56     .. code-block:: python
    58     .. code-block:: python
    75 
    77 
    76     def __call__(self, wrapped):
    78     def __call__(self, wrapped):
    77         settings = self.__dict__.copy()
    79         settings = self.__dict__.copy()
    78         depth = settings.pop('_depth', 0)
    80         depth = settings.pop('_depth', 0)
    79 
    81 
    80         def callback(context, name, ob):
    82         def callback(context, name, obj):  # pylint: disable=unused-argument
    81             logger.debug('Registering class {0} as vocabulary with name "{1}"'.format(str(ob), self.name))
    83             LOGGER.debug('Registering class {0} as vocabulary with name "{1}"'.format(
    82             directlyProvides(ob, IVocabularyFactory)
    84                 str(obj), self.name))
    83             getVocabularyRegistry().register(self.name, ob)
    85             directlyProvides(obj, IVocabularyFactory)
       
    86             getVocabularyRegistry().register(self.name, obj)
    84 
    87 
    85         info = self.venusian.attach(wrapped, callback, category='pyams_vocabulary',
    88         info = self.venusian.attach(wrapped, callback, category='pyams_vocabulary',
    86                                     depth=depth + 1)
    89                                     depth=depth + 1)
    87 
    90 
    88         if info.scope == 'class':
    91         if info.scope == 'class':  # pylint: disable=no-member
    89             # if the decorator was attached to a method in a class, or
    92             # if the decorator was attached to a method in a class, or
    90             # otherwise executed at class scope, we need to set an
    93             # otherwise executed at class scope, we need to set an
    91             # 'attr' into the settings if one isn't already in there
    94             # 'attr' into the settings if one isn't already in there
    92             if settings.get('attr') is None:
    95             if settings.get('attr') is None:
    93                 settings['attr'] = wrapped.__name__
    96                 settings['attr'] = wrapped.__name__
    94 
    97 
    95         settings['_info'] = info.codeinfo  # fbo "action_method"
    98         settings['_info'] = info.codeinfo  # pylint: disable=no-member
    96         return wrapped
    99         return wrapped