Code

More informative error message
[roundup.git] / roundup / roundup_indexer.py
1 #
2 # Copyright (c) 2001 Bizar Software Pty Ltd (http://www.bizarsoftware.com.au/)
3 # This module is free software, and you may redistribute it and/or modify
4 # under the same terms as Python, so long as this copyright message and
5 # disclaimer are retained in their original form.
6 #
7 # IN NO EVENT SHALL BIZAR SOFTWARE PTY LTD BE LIABLE TO ANY PARTY FOR
8 # DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
9 # OUT OF THE USE OF THIS CODE, EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE
10 # POSSIBILITY OF SUCH DAMAGE.
11 #
12 # BIZAR SOFTWARE PTY LTD SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
13 # BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
14 # FOR A PARTICULAR PURPOSE.  THE CODE PROVIDED HEREUNDER IS ON AN "AS IS"
15 # BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
16 # SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
17
18 #$Id: roundup_indexer.py,v 1.2 2002-05-25 07:16:24 rochecompaan Exp $
19 '''
20 This module provides an indexer class, RoundupIndexer, that stores text
21 indices in a roundup instance.  This class makes searching the content of
22 messages and text files possible.
23 '''
24 import os
25 from roundup.indexer import SlicedZPickleIndexer
27 class RoundupIndexer(SlicedZPickleIndexer):
28     ''' Indexes messages and files 
29     '''
31     def __init__(self, db_path):
32         indexdb_path = os.path.join(db_path, 'indexes')
33         index_exists = 0
34         if not os.path.exists(indexdb_path):
35             os.makedirs(indexdb_path)
36             os.chmod(indexdb_path, 0775)
37         else:
38             index_exists = 1
39         index_path = os.path.join(indexdb_path, 'index.db')
40         SlicedZPickleIndexer.__init__(self, 
41             INDEXDB=index_path, QUIET=9)
42         files_path = os.path.join(db_path, 'files')
43         if not index_exists:
44             self.add_files(dir=files_path)
45             self.save_index()
47     def search(self, search_terms, klass):
48         ''' display search results
49         '''
50         hits = self.find(search_terms)
51         links = []
52         nodeids = {}
53         designator_propname = {'msg': 'messages',
54                                'file': 'files'}
55         if hits:
56             hitcount = len(hits)
57             # build a dictionary of nodes and their associated messages
58             # and files
59             for hit in hits.keys():
60                 filename = hits[hit].split('/')[-1]
61                 for designator, propname in designator_propname.items():
62                     if filename.find(designator) == -1: continue
63                     nodeid = filename[len(designator):]
64                     result = apply(klass.find, (), {propname:nodeid})
65                     if not result: continue
67                     id = str(result[0])
68                     if not nodeids.has_key(id):
69                         nodeids[id] = {}
71                     node_dict = nodeids[id]
72                     if not node_dict.has_key(propname):
73                         node_dict[propname] = [nodeid]
74                     elif node_dict.has_key(propname):
75                         node_dict[propname].append(nodeid)
77         return nodeids
80 #
81 #$Log: not supported by cvs2svn $
82 #Revision 1.1.2.3  2002/05/02 11:52:12  rochecompaan
83 #Fixed small bug that prevented indexes from being generated.
84 #
85 #Revision 1.1.2.2  2002/04/19 19:54:42  rochecompaan
86 #cgi_client.py
87 #    removed search link for the time being
88 #    moved rendering of matches to htmltemplate
89 #hyperdb.py
90 #    filtering of nodes on full text search incorporated in filter method
91 #roundupdb.py
92 #    added paramater to call of filter method
93 #roundup_indexer.py
94 #    added search method to RoundupIndexer class
95 #
96 #Revision 1.1.2.1  2002/04/03 11:55:57  rochecompaan
97 # . Added feature #526730 - search for messages capability
98 #