Changeset 8718 for main/waeup.kofa


Ignore:
Timestamp:
14 Jun 2012, 06:58:14 (12 years ago)
Author:
uli
Message:

Add maintenance function to add objects in catalogs.

Location:
main/waeup.kofa/trunk/src/waeup/kofa
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/maintenance.py

    r8417 r8718  
    66import sys
    77from ZODB.scripts.analyze import report, analyze
     8from zope.component.hooks import setSite
     9from zope.component import getUtility
     10from zope.catalog.interfaces import ICatalog
     11from zope.intid.interfaces import IIntIds
     12from zope.location.interfaces import ILocation
    813
    914def db_analyze(args=None):
     
    2631
    2732    report(analyze(path))
     33
     34def update_catalog(site, cat_name, objects=[], func=None):
     35    """Update a catalog.
     36
     37    Put `objects` or objects delivered by `func()` into the catalog
     38    registered under `cat_name` in `site`.
     39
     40    Objects to be catalogued must be 'located', i.e. they must have a
     41    __name__ and __parent__ (because they are adapted to
     42    IKeyReference).
     43
     44    You can pass in objects as some iterable or as a function that is
     45    called to deliver the set of objects to be catalogued.
     46
     47    A function takes precedence over object lists.
     48    """
     49    setSite(site)
     50    cat = getUtility(ICatalog, name=cat_name)
     51    intids = getUtility(IIntIds, context=cat)
     52    if func is not None:
     53        objects = func()
     54    for ob in objects:
     55        doc_id = intids.queryId(ob, None)
     56        if doc_id is None:
     57            doc_id = intids.register(ob)
     58        cat.index_doc(doc_id, ob)
     59    return cat
Note: See TracChangeset for help on using the changeset viewer.