[Zope3-checkins] CVS: Zope3/lib/python/Zope/App/index - processors.py:1.1 queries.py:1.1

Christian Theune ct@gocept.com
Wed, 4 Dec 2002 11:10:58 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/App/index
In directory cvs.zope.org:/tmp/cvs-serv2467

Added Files:
	processors.py queries.py 
Log Message:
- Some refactoring from Zope.App.index.text 
- Introducing the ObjectRetrievingProcessor


=== Added File Zope3/lib/python/Zope/App/index/processors.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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.
#
##############################################################################
"""Generic query processors for use with multiple indexes..

$Id: processors.py,v 1.1 2002/12/04 16:10:57 ctheune Exp $
"""

from __future__ import generators

from interfaces import IRankedObjectIterator, IRankedObjectRecord, \
    IRankedHubIdList, IBatchedResult
from Zope.App.OFS.Services.QueryService.IQueryProcessor import IQueryProcessor

from Zope.ComponentArchitecture import getAdapter, getService
from Zope.ContextWrapper import ContextMethod



class ObjectRetrievingProcessor:
    """Converts a RankedHubIdList into an iteratable
       list of ranked objects by retrieving the objects
       from the ObjectHub.
    """

    __implements__ = IQueryProcessor
    
    input_interface = IRankedHubIdList, IBatchedResult
    output_interface = IRankedObjectIterator

    def __call__(wrapped_self, query):
        list = getAdapter(query, IRankedHubIdList)
        batch = getAdapter(query, IBatchedResult)

        objectHub = getService(wrapped_self, "ObjectHub")
        
        # XXX do we need wrapping for the objects returned by the hub?
        iterator = RankedObjectIterator(list, objectHub.getObject, batch.startPosition, 
                                        batch.batchSize, batch.totalSize)

        return iterator
    __call__ = ContextMethod(__call__)
            
class RankedObjectIterator:
    """Iterates over a given list of IRankedObjectRecord."""

    __implements__ = IRankedObjectIterator, IBatchedResult

    def __init__(self, recordlist, objectfetcher, startposition, batchsize, totalsize):
        self._records = recordlist
        self.startPosition = startposition
        self.batchSize = batchsize
        self.totalSize = totalsize
        self.__objectfetcher = objectfetcher

    def __iter__(self):
        objectfetcher = self.__objectfetcher
        
        for hubid, rank in self._records:
            # XXX maybe we should catch some exceptions like security related
            # ones or NotFoundError, to avoid breaking the iteration. Think
            # about yielding an NotFound-Indicator in such a case.
            yield RankedObjectRecord(objectfetcher(hubid), rank)
        raise StopIteration

class RankedObjectRecord:
    """Contains a reference to a ranked object."""

    __slots__ = ["rank", "object"]

    __implements__ = IRankedObjectRecord

    def __init__(self, object, rank):
        self.rank = rank
        self.object = object



=== Added File Zope3/lib/python/Zope/App/index/queries.py ===
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (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.
#
##############################################################################
"""Generic queries for indexes.

$Id: queries.py,v 1.1 2002/12/04 16:10:57 ctheune Exp $
"""

from Zope.ComponentArchitecture import getAdapter
from Zope.App.index.interfaces import IBatchedResult, IRankedHubIdList

class BatchedRankedResult:

    __implements__ = IBatchedResult, IRankedHubIdList

    def __init__(self, hubidlist, startposition, batchsize, totalsize):
        self.__hubidlist = hubidlist
        self.startPosition = startposition
        self.batchSize = batchsize
        self.totalSize = totalsize

    def __getitem__(self, index):
        return self.__hubidlist[index]