ztfy/utils/catalog.py
changeset 42 cb2a0e2d3bbf
parent 41 da1558b96f0a
child 43 beb799c08f57
equal deleted inserted replaced
41:da1558b96f0a 42:cb2a0e2d3bbf
     1 ### -*- coding: utf-8 -*- ####################################################
       
     2 ##############################################################################
       
     3 #
       
     4 # Copyright (c) 2008-2009 Thierry Florac <tflorac AT ulthar.net>
       
     5 # All Rights Reserved.
       
     6 #
       
     7 # This software is subject to the provisions of the Zope Public License,
       
     8 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     9 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
    10 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
    11 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    12 # FOR A PARTICULAR PURPOSE.
       
    13 #
       
    14 ##############################################################################
       
    15 
       
    16 __docformat__ = "restructuredtext"
       
    17 
       
    18 # import standard packages
       
    19 
       
    20 # import Zope3 interfaces
       
    21 from zope.annotation.interfaces import IAnnotations
       
    22 from zope.app.catalog.interfaces import ICatalog
       
    23 from zope.app.container.interfaces import IContainer
       
    24 from zope.app.file.interfaces import IFile
       
    25 from zope.app.intid.interfaces import IIntIds
       
    26 
       
    27 # import local interfaces
       
    28 
       
    29 # import Zope3 packages
       
    30 from zope.app import zapi
       
    31 
       
    32 # import local packages
       
    33 import request as request_utils
       
    34 
       
    35 from ztfy.utils import _
       
    36 
       
    37 
       
    38 #
       
    39 # IntIds utility functions
       
    40 #
       
    41 
       
    42 def getIntIdUtility(name='', request=None, context=None):
       
    43     """Look for a named IIntIds utility"""
       
    44     if request is None:
       
    45         request = request_utils.getRequest()
       
    46     intids = request_utils.getRequestData('IntIdsUtility::' + name, request)
       
    47     if intids is None:
       
    48         intids = zapi.queryUtility(IIntIds, name, context=context)
       
    49         if intids is not None:
       
    50             request_utils.setRequestData('IntIdsUtility::' + name, intids, request)
       
    51     return intids
       
    52 
       
    53 
       
    54 def getObjectId(object, intids_name='', request=None, context=None):
       
    55     """Look for an object Id as recorded by given IIntIds utility"""
       
    56     if request is None:
       
    57         request = request_utils.getRequest()
       
    58     intids = getIntIdUtility(intids_name, request, context)
       
    59     if intids is not None:
       
    60         return intids.queryId(object)
       
    61     return None
       
    62 
       
    63 
       
    64 def getObject(id, intids_name='', request=None, context=None):
       
    65     """Look for an object recorded by given IIntIds utility and id"""
       
    66     if request is None:
       
    67         request = request_utils.getRequest()
       
    68     intids = getIntIdUtility(intids_name, request, context)
       
    69     if intids is not None:
       
    70         return intids.queryObject(id)
       
    71     return None
       
    72 
       
    73 
       
    74 #
       
    75 # Catalog utility functions
       
    76 #
       
    77 
       
    78 def queryCatalog(name='', context=None):
       
    79     """Look for a registered catalog"""
       
    80     return zapi.queryUtility(ICatalog, name, context=context)
       
    81 
       
    82 
       
    83 def indexObject(object, catalog_name='', index_name='', request=None, context=None):
       
    84     """Index object into a registered catalog"""
       
    85     if request is None:
       
    86         request = request_utils.getRequest()
       
    87     intids = getIntIdUtility('', request, context)
       
    88     if intids is not None:
       
    89         if ICatalog.providedBy(catalog_name):
       
    90             catalog = catalog_name
       
    91         else:
       
    92             catalog = queryCatalog(catalog_name, context)
       
    93         if catalog is not None:
       
    94             id = intids.register(object)
       
    95             if index_name:
       
    96                 catalog[index_name].index_doc(id, object)
       
    97             else:
       
    98                 catalog.index_doc(id, object)
       
    99             return True
       
   100     return False
       
   101 
       
   102 
       
   103 def unindexObject(object, catalog_name='', index_name='', request=None, context=None):
       
   104     """Remove object from a registered catalog"""
       
   105     if request is None:
       
   106         request = request_utils.getRequest()
       
   107     id = getObjectId(object, '', request, context)
       
   108     if id is not None:
       
   109         if ICatalog.providedBy(catalog_name):
       
   110             catalog = catalog_name
       
   111         else:
       
   112             catalog = queryCatalog(catalog_name, context)
       
   113         if catalog is not None:
       
   114             if index_name:
       
   115                 catalog[index_name].unindex_doc(id)
       
   116             else:
       
   117                 catalog.unindex_doc(id)
       
   118             return True
       
   119     return False
       
   120 
       
   121 
       
   122 def _indexObject(object, intids, catalogs):
       
   123     """Index object data into given set of catalogs"""
       
   124     id = intids.register(object)
       
   125     for catalog in catalogs:
       
   126         catalog.index_doc(id, object)
       
   127 
       
   128 def _indexObjectValues(object, intids, catalogs):
       
   129     """Index object values into given set of catalogs"""
       
   130     container = IContainer(object, None)
       
   131     if container is not None:
       
   132         for subobject in container.values():
       
   133             _indexAllObject(subobject, intids, catalogs)
       
   134 
       
   135 def _indexObjectAnnotations(object, intids, catalogs):
       
   136     """Index object annotations into given set of catalogs"""
       
   137     annotations = IAnnotations(object, None)
       
   138     if annotations is not None:
       
   139         keys = annotations.keys()
       
   140         for key in keys:
       
   141             _indexAllObject(annotations[key], intids, catalogs)
       
   142             file = IFile(annotations[key], None)
       
   143             if file is not None:
       
   144                 _indexObject(file, intids, catalogs)
       
   145 
       
   146 def _indexAllObject(object, intids, catalogs):
       
   147     """Index object, object values and annotations into given set of catalogs"""
       
   148     _indexObject(object, intids, catalogs)
       
   149     _indexObjectValues(object, intids, catalogs)
       
   150     _indexObjectAnnotations(object, intids, catalogs)
       
   151 
       
   152 def indexAllObjectValues(object, context=None):
       
   153     """Reindex a whole container properties and contents (including annotations) into site's catalogs"""
       
   154     if context is None:
       
   155         context = object
       
   156     intids = zapi.queryUtility(IIntIds, context=context)
       
   157     if intids is not None:
       
   158         catalogs = zapi.getAllUtilitiesRegisteredFor(ICatalog, context)
       
   159         if catalogs:
       
   160             _indexAllObject(object, intids, catalogs)