src/ztfy/utils/catalog/__init__.py
branchZTK-1.1
changeset 148 d3668ecd9137
parent 91 9bd1b95afc93
child 168 c02d355d3ffd
equal deleted inserted replaced
147:044dc196ec8a 148:d3668ecd9137
       
     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.file.interfaces import IFile
       
    23 from zope.catalog.interfaces import ICatalog
       
    24 from zope.container.interfaces import IContainer
       
    25 from zope.intid.interfaces import IIntIds
       
    26 
       
    27 # import local interfaces
       
    28 
       
    29 # import Zope3 packages
       
    30 from zope.component import queryUtility, getAllUtilitiesRegisteredFor
       
    31 
       
    32 # import local packages
       
    33 from ztfy.utils 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.queryRequest()
       
    46     intids = request_utils.getRequestData('IntIdsUtility::' + name, request)
       
    47     if intids is None:
       
    48         intids = queryUtility(IIntIds, name, context=context)
       
    49         if (request is not None) and (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 object is None:
       
    57         return None
       
    58     if request is None:
       
    59         request = request_utils.queryRequest()
       
    60     intids = getIntIdUtility(intids_name, request, context)
       
    61     if intids is not None:
       
    62         return intids.queryId(object)
       
    63     return None
       
    64 
       
    65 
       
    66 def getObject(id, intids_name='', request=None, context=None):
       
    67     """Look for an object recorded by given IIntIds utility and id"""
       
    68     if id is None:
       
    69         return None
       
    70     if request is None:
       
    71         request = request_utils.getRequest()
       
    72     intids = getIntIdUtility(intids_name, request, context)
       
    73     if intids is not None:
       
    74         return intids.queryObject(id)
       
    75     return None
       
    76 
       
    77 
       
    78 #
       
    79 # Catalog utility functions
       
    80 #
       
    81 
       
    82 def queryCatalog(name='', context=None):
       
    83     """Look for a registered catalog"""
       
    84     return queryUtility(ICatalog, name, context=context)
       
    85 
       
    86 
       
    87 def indexObject(object, catalog_name='', index_name='', request=None, context=None):
       
    88     """Index object into a registered catalog"""
       
    89     if request is None:
       
    90         request = request_utils.getRequest()
       
    91     intids = getIntIdUtility('', request, context)
       
    92     if intids is not None:
       
    93         if ICatalog.providedBy(catalog_name):
       
    94             catalog = catalog_name
       
    95         else:
       
    96             catalog = queryCatalog(catalog_name, context)
       
    97         if catalog is not None:
       
    98             id = intids.register(object)
       
    99             if index_name:
       
   100                 catalog[index_name].index_doc(id, object)
       
   101             else:
       
   102                 catalog.index_doc(id, object)
       
   103             return True
       
   104     return False
       
   105 
       
   106 
       
   107 def unindexObject(object, catalog_name='', index_name='', request=None, context=None):
       
   108     """Remove object from a registered catalog"""
       
   109     if request is None:
       
   110         request = request_utils.getRequest()
       
   111     id = getObjectId(object, '', request, context)
       
   112     if id is not None:
       
   113         if ICatalog.providedBy(catalog_name):
       
   114             catalog = catalog_name
       
   115         else:
       
   116             catalog = queryCatalog(catalog_name, context)
       
   117         if catalog is not None:
       
   118             if index_name:
       
   119                 catalog[index_name].unindex_doc(id)
       
   120             else:
       
   121                 catalog.unindex_doc(id)
       
   122             return True
       
   123     return False
       
   124 
       
   125 
       
   126 def _indexObject(object, intids, catalogs):
       
   127     """Index object data into given set of catalogs"""
       
   128     id = intids.register(object)
       
   129     for catalog in catalogs:
       
   130         catalog.index_doc(id, object)
       
   131 
       
   132 def _indexObjectValues(object, intids, catalogs):
       
   133     """Index object values into given set of catalogs"""
       
   134     container = IContainer(object, None)
       
   135     if container is not None:
       
   136         for subobject in container.values():
       
   137             _indexAllObject(subobject, intids, catalogs)
       
   138 
       
   139 def _indexObjectAnnotations(object, intids, catalogs):
       
   140     """Index object annotations into given set of catalogs"""
       
   141     annotations = IAnnotations(object, None)
       
   142     if annotations is not None:
       
   143         keys = annotations.keys()
       
   144         for key in keys:
       
   145             _indexAllObject(annotations[key], intids, catalogs)
       
   146             file = IFile(annotations[key], None)
       
   147             if file is not None:
       
   148                 _indexObject(file, intids, catalogs)
       
   149 
       
   150 def _indexAllObject(object, intids, catalogs):
       
   151     """Index object, object values and annotations into given set of catalogs"""
       
   152     _indexObject(object, intids, catalogs)
       
   153     _indexObjectValues(object, intids, catalogs)
       
   154     _indexObjectAnnotations(object, intids, catalogs)
       
   155 
       
   156 def indexAllObjectValues(object, context=None):
       
   157     """Reindex a whole container properties and contents (including annotations) into site's catalogs"""
       
   158     if context is None:
       
   159         context = object
       
   160     intids = queryUtility(IIntIds, context=context)
       
   161     if intids is not None:
       
   162         catalogs = getAllUtilitiesRegisteredFor(ICatalog, context)
       
   163         if catalogs:
       
   164             _indexAllObject(object, intids, catalogs)