"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/extent.py $
$Id: extent.py 25070 2004-09-10 18:02:07Z dbinger $
"""

from durus.storage import gen_oid_class
from dulcinea import local

class Extent:
    def __init__(self, *class_names):
        self.objects = {}
        if class_names:
            self.build(*class_names)

    def build(self, *class_names):
        for class_name in class_names:
            self.objects[class_name] = []
        connection = local.get_connection()
        storage = connection.get_storage()
        for oid, class_name in gen_oid_class(storage, *class_names):
            self.objects[class_name].append(connection.get(oid))

    def all(self, *class_names):
        for class_name in class_names:
            if class_name not in self.objects:
                raise RuntimeError, 'extent was not built for %r' % class_name
        for class_name in class_names:
            for obj in self.objects[class_name]:
                yield obj
