Source code for pyams_catalog.utils

#
# 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 (catalog)')

# import interfaces
from hypatia.interfaces import ICatalog
from pyams_utils.interfaces import ICacheKeyValue
from zope.intid.interfaces import IIntIds

# import packages
from pyams_utils.adapter import adapter_config
from pyams_utils.registry import query_utility
from zope.keyreference.interfaces import NotYet


[docs]@adapter_config(context=ICatalog, provides=ICacheKeyValue) def catalog_key_adapter(obj): """Catalog key value adapter""" return str(frozenset(obj))
[docs]def index_object(obj, catalog='', ignore_notyet=False): """Index given object into catalog""" logger.debug("Indexing object {0!r}".format(obj)) intids = query_utility(IIntIds) if intids is not None: try: object_id = intids.register(obj) except NotYet: if not ignore_notyet: raise else: if isinstance(catalog, str): catalog = query_utility(ICatalog, name=catalog) if catalog is not None: catalog.index_doc(object_id, obj)
[docs]def reindex_object(obj, catalog=''): """Reindex given object into catalog""" logger.debug("Re-indexing object {0!r}".format(obj)) intids = query_utility(IIntIds) if intids is not None: object_id = intids.queryId(obj) if object_id is not None: if isinstance(catalog, str): catalog = query_utility(ICatalog, name=catalog) if catalog is not None: catalog.reindex_doc(object_id, obj)
[docs]def unindex_object(obj, catalog=''): """Unindex given object from catalog""" logger.debug("Un-indexing object {0!r}".format(obj)) intids = query_utility(IIntIds) if intids is not None: object_id = intids.queryId(obj) if object_id is not None: if isinstance(catalog, str): catalog = query_utility(ICatalog, name=catalog) if catalog is not None: catalog.unindex_doc(object_id)