src/pyams_i18n/index.py
changeset 9 017ad36cc2ba
equal deleted inserted replaced
8:44a92dc178ce 9:017ad36cc2ba
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 from pyams_i18n.interfaces import II18n
       
    20 
       
    21 # import packages
       
    22 from hypatia.text import TextIndex
       
    23 from hypatia.text.lexicon import Lexicon
       
    24 from hypatia.util import BaseIndexMixin
       
    25 from persistent import Persistent
       
    26 from pyams_catalog.nltk import NltkFullTextProcessor
       
    27 from ZODB.broken import Broken
       
    28 
       
    29 
       
    30 _marker = object()
       
    31 
       
    32 
       
    33 class I18nTextIndexMixin(BaseIndexMixin):
       
    34     """I18n text index mixin"""
       
    35 
       
    36     def __init__(self, language, interface=None):
       
    37         self.interface = interface
       
    38         self.language = language
       
    39 
       
    40     def discriminate(self, obj, default):
       
    41         if self.interface is not None:
       
    42             obj = self.interface(obj, None)
       
    43             if obj is None:
       
    44                 return default
       
    45 
       
    46         value = II18n(obj).get_attribute(self.discriminator, lang=self.language, default=_marker)
       
    47         if value is _marker:
       
    48             return default
       
    49 
       
    50         if isinstance(value, Persistent):
       
    51             raise ValueError('Catalog cannot index persistent object {0!r}'.format(value))
       
    52 
       
    53         if isinstance(value, Broken):
       
    54             raise ValueError('Catalog cannot index broken object {0!r}'.format(value))
       
    55 
       
    56         return value
       
    57 
       
    58 
       
    59 class I18nTextIndexWithInterface(I18nTextIndexMixin, TextIndex):
       
    60     """I18n text index"""
       
    61 
       
    62     def __init__(self, language, discriminator, interface=None, lexicon=None, index=None, family=None):
       
    63         I18nTextIndexMixin.__init__(self, language, interface)
       
    64         if lexicon is None:
       
    65             lexicon = Lexicon(NltkFullTextProcessor(language))
       
    66         TextIndex.__init__(self, discriminator, lexicon, index, family)