Exemplo n.º 1
0
class Catalog(object):

    def __init__(self):

        self._catalog = RepozeCatalog()
        self._document_map = DocumentMap()

    @property
    def catalog(self):
        """ convenient proxy to real catalog """

        return self._catalog

    def query(self, qry, as_summary=False, as_object=False, **kwargs):

        """ Query the catalog. If as_summary is set, return object summaries,
        as fetched from info from the indexes"""

        res = self._catalog.query(qry, **kwargs)

        if as_summary:
            return [self.get_object_summary(uuid) for uuid in res[1]]
        elif as_object:
            return [self.get_object(uuid) for uuid in res[1]]
        else:
            return res

    def index_object(self, object):

        path = object_to_path(object)
        uuid = object.uuid

        docid = self._document_map.add(uuid)
        self._document_map.add_metadata(docid, {'path': path})

        try:
            self.catalog.index_doc(docid, object)
            self._p_changed = 1
            self.catalog._p_changed = 1
            self._document_map._p_changed = 1
            self.__parent__._p_changed = 1
        except:
            LOGGER.exception("Could not index object!")

    def reindex_object(self, object):

        uuid = object.uuid

        docid = self._document_map.docid_for_address(uuid)
        if not docid:
            self.index_object(object)
            docid = self._document_map.docid_for_address(uuid)

        # update the path of the object in the documentmap since the
        # object might have been renamed / moved
        path = object_to_path(object)
        self._document_map.add_metadata(docid, {'path': path})

        try:
            self.catalog.reindex_doc(docid, object)
            self._p_changed = 1
            self.catalog._p_changed = 1
            self._document_map._p_changed = 1
            self.__parent__._p_changed = 1
        except:
            LOGGER.exception("Could not index object!")

    def unindex_object(self, object):

        uuid = object.uuid

        docid = self._document_map.docid_for_address(uuid)
        if docid:
            self.catalog.unindex_doc(docid)
            self._document_map.remove_docid(docid)
            self._p_changed = 1
            self.catalog._p_changed = 1
            self._document_map._p_changed = 1
            self.__parent__._p_changed = 1

    def clear(self):

        self._catalog.clear()
        self._document_map = DocumentMap()
        self._p_changed = 1
        self.catalog._p_changed = 1
        self._document_map._p_changed = 1
        self.__parent__._p_changed = 1

    def get_object(self, docid):

        metadata = self._document_map.get_metadata(docid)
        path = metadata['path']
        return path_to_object(path, self.__parent__)

    def get_object_summary(self, uuid):

        """ Return a summary of the found object, based on the values that
        the indexes hold on the given uuid"""

        summ = {}

        for key in self.catalog.keys():
            idx = self.catalog[key]
            if hasattr(idx, "_rev_index"):
                summ[key] = idx._rev_index.get(uuid, '')

        summ['key'] = uuid

        return ObjectSummary(summ)

    def list_objects(self):

        docids = self.list_object_ids()
        for docid in docids:
            metadata = self._document_map.get_metadata(docid)
            yield (docid, metadata['path'])

    def list_object_ids(self):

        return self._document_map.docid_to_address.keys()