--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content_es/utility.py Thu Apr 21 18:24:52 2016 +0200
@@ -0,0 +1,81 @@
+#
+# 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
+from pyams_utils.interfaces.zeo import IZEOConnection
+from zope.intid.interfaces import IIntIds
+
+# import packages
+from persistent import Persistent
+from pyams_utils.registry import get_utility
+from pyams_zmq.socket import zmq_socket, zmq_response
+from pyramid.threadlocal import get_current_registry
+from zope.container.contained import Contained
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+
+
+@implementer(IContentIndexerUtility)
+class ContentIndexerUtility(Persistent, Contained):
+ """Content indexer utility"""
+
+ zeo_connection = FieldProperty(IContentIndexerUtility['zeo_connection'])
+
+ def _get_socket(self):
+ registry = get_current_registry()
+ handler = registry.settings.get(INDEXER_HANDLER_KEY, False)
+ if handler:
+ return zmq_socket(handler)
+
+ 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"]
+ if not self.zeo_connection:
+ return [502, "Missing ZEO connection"]
+ zeo = get_utility(IZEOConnection, self.zeo_connection)
+ intids = get_utility(IIntIds)
+ settings = {'zeo': zeo.get_settings(),
+ 'document': intids.register(document)}
+ socket.send_json(['index', settings])
+ return zmq_response(socket)
+
+ 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"]
+ if not self.zeo_connection:
+ return [502, "Missing ZEO connection"]
+ zeo = get_utility(IZEOConnection, self.zeo_connection)
+ intids = get_utility(IIntIds)
+ settings = {'zeo': zeo.get_settings(),
+ 'document': intids.register(document)}
+ socket.send_json(['unindex', settings])
+ return zmq_response(socket)
+
+ 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"]
+ if not self.zeo_connection:
+ return [502, "Missing ZEO connection"]
+ socket.send_json(['test', {}])
+ return zmq_response(socket)