Esempio n. 1
0
def search_for_objects(searchFile, query):
    # returns a list of matching metas

    # TODO: Add locking to searchFile, I am not comfortable dealing with your log files..
    # For my purposes, I'll do my own logging. So let this thing raise an exception if
    # there is an error.

    metas, error_message = read_index_file_to_get_metas(searchFile)
    if metas is None:
        raise Exception('Error reading searchFile %s: %s' % (searchFile, error_message))

    ## build a list of metas that satisfy the given query
    matches = []
    for meta in metas:
	if cnf_match.matches_cnf(meta, query):
		matches.append(meta)

    return matches
Esempio n. 2
0
def main(logDir, logFile, searchFile, scriptName, resources = None):
    """main function processing search request."""

    # initialization.
    name = print_header()
    form = cgi.FieldStorage()
    setup_logging(logDir, logFile)

    ## cgi.print_form(form)

    ## parse the form to get: query, sorting information, start_index
    ## to know which subset of matches/results to return,
    ## search_attributes for the next_page_button
    query, sort_tuple, start_index, search_attributes, error_message = \
           parse_form(form)
    if query is None:
	print '<P> ERROR while parsing form and constructing ' + \
              'search query:', error_message
	sys.exit(1)

    ## print '<HR>Query:', query

    ## read index file to get the list of dictionaries:
    ## one dictionary for each version of each object, contains its meta-data

    #
    # used to open search file without read.
    # because Sobti is doing the read by himself below.
    # I have to change this because the SearchFile will be passed
    # onto the ReverseLists module for more later lookups.
    # this makes it necessary to do a real SearchFile open.
    #
    #success,searchFileLock = ryw.open_search_file(
    #    'Search:', logDir, logFile, searchFile, False, skipRead = True)
    success,searchFileLock = ryw.open_search_file(
        'Search:', logDir, logFile, searchFile, False, skipRead = False)
    if not success:
        ryw.give_bad_news('Search: failed to acquire search file lock: ' +
                          searchFile, logging.critical)
        ryw_upload.quick_exit(1)
    #else:
        #
        # mis-named: it's really not a searchFileLock, but searchFile itself.
        #
        #displayObject.set_search_file(searchFileLock)
        

    #
    # this is when Sobti used to do his own read, now replaced by mine.
    #
    #metas, error_message = read_index_file_to_get_metas(searchFile)
    metas = searchFileLock.convert_to_sobti_list()
    if metas is None:
	print '<P> ERROR while reading the index file to get ' + \
              'the meta-data dictionaries:', error_message
        searchFileLock.done()
        ryw_upload.quick_exit(1)

    ## build a list of metas that satisfy the given query
    matches = []
    for meta in metas:
        if not ryw_view.should_show_object(meta, resources):
            continue
	if cnf_match.matches_cnf(meta, query):
            matches.append(meta)
            ryw.db_print2('Search:main() ' + repr(meta), 53)

    num_matches = len(matches)

    ## sort the matches by the given sort tuple
    matches, error_message = sort_matches(matches, sort_tuple)
    if matches is None:
	print '<P> ERROR while sorting matches:', error_message
        searchFileLock.done()
        ryw_upload.quick_exit(1)


    #
    # save all current search results in a file for
    # possible inclusion in the current selection.
    #
    matchAllName = save_matches(matches)
    

    ## Return the start_index..(start_index + N) entries from the top
    if num_matches == 0 or start_index >= num_matches:
	num_items = 0
    else:
	start_index = max(0, start_index)
        assert start_index < num_matches

	end_index = start_index + NUM_OBJECTS_PER_PAGE - 1
	end_index = max(0, end_index)
	end_index = min(num_matches - 1, end_index)

	if not (0 <= start_index <= end_index < num_matches):
            print '<P> ASSERT ERROR: start_index, end_index, ' + \
                  'num_matches', start_index, end_index, num_matches
            searchFileLock.done()
            ryw_upload.quick_exit(1)

	num_items = end_index - start_index + 1

    ## num_items is the number of items that will actually be displayed
    ## num_items <= num_matches
    if num_items == 0:
	print '<P><H3>No objects to display</H3>'
        searchFileLock.done()
        ryw_upload.quick_exit(1)
    else:
	#print '<P><H3>%d objects satisfy the search criteria, ' + \
        #      'displaying %d of them</H3>' % (num_matches, num_items)
        print """
<BR><B><FONT SIZE=2>%d
object(s) satisfy the search criteria.</FONT></B>""" % \
              (num_matches)
        print """
<BR><B><FONT SIZE=2>displaying matches %d - %d.</FONT></B><BR>""" % \
              (start_index + 1, start_index + num_items)

        print_next_page_button1(search_attributes, end_index + 1, scriptName,
                                num_matches)

        shownMatches = []
        
        success,reverseLists = ReverseLists.open_reverse_lists(
            'Search:', '', '',
            os.path.join(RepositoryRoot, 'ReverseLists'), True,
            searchFile = searchFileLock,
            repositoryRoot = RepositoryRoot)
        if not (success and reverseLists):
            ryw.give_bad_news('Search.main: failed to open ReverseLists.',
                              logging.critical)
            if reverseLists:
                reverseLists.done()
            return False

        displayObject = ryw_view.DisplayObject(
            RepositoryRoot, calledByVillageSide = False,
            missingFileFunc=Browse.reqDownloadFunc,
            searchFile = searchFileLock,
            reverseLists = reverseLists)
        
        displayObject.begin_print()
	for i in range(start_index, end_index + 1):
            #display_object(matches[i])
            displayObject.show_an_object_compact(matches[i])
            shownMatches.append(matches[i])
        displayObject.end_print()
        reverseLists.done()

        #
        # save search results on this page in a file for
        # possible inclusion in the current selection.
        #
        matchThisPageName = save_matches(shownMatches)

        print_selection_links(matchAllName, matchThisPageName)
        
    	## Include a next-page button
	print_next_page_button2(search_attributes, end_index + 1, scriptName,
                                num_matches)

    searchFileLock.done()
    ryw_view.print_footer()