Source code for pyams_content_es.utility

#
# 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 interfaces
from pyams_content_es.interfaces import IContentIndexerUtility, INDEXER_HANDLER_KEY, INDEXER_AUTH_KEY
from zope.intid.interfaces import IIntIds

# import packages
from persistent import Persistent
from pyams_utils.registry import get_utility, get_global_registry
from pyams_zmq.socket import zmq_socket, zmq_response
from zope.container.contained import Contained
from zope.interface import implementer
from zope.schema.fieldproperty import FieldProperty


[docs]@implementer(IContentIndexerUtility) class ContentIndexerUtility(Persistent, Contained): """Content indexer utility""" zodb_name = FieldProperty(IContentIndexerUtility['zodb_name'])
[docs] def get_socket(self): registry = get_global_registry() handler = registry.settings.get(INDEXER_HANDLER_KEY, False) if handler: return zmq_socket(handler, auth=registry.settings.get(INDEXER_AUTH_KEY))
[docs] def index_document(self, document): """Send index request for given document""" socket = self.get_socket() if socket is None: return [501, "No socket handler defined in configuration file"] intids = get_utility(IIntIds) settings = {'zodb_name': self.zodb_name, 'document': intids.register(document)} socket.send_json(['index', settings]) return zmq_response(socket)
[docs] def unindex_document(self, document): """Send unindex request for given document""" socket = self.get_socket() if socket is None: return [501, "No socket handler defined in configuration file"] intids = get_utility(IIntIds) settings = {'zodb_name': self.zodb_name, 'document': intids.register(document)} socket.send_json(['unindex', settings]) return zmq_response(socket)
[docs] def test_process(self): """Send test request to indexer process""" socket = self.get_socket() if socket is None: return [501, "No socket handler defined in configuration file"] socket.send_json(['test', {}]) return zmq_response(socket)