query_parser.py revision 2961
bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch#!/usr/bin/python
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen#
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen# CDDL HEADER START
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen#
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen# The contents of this file are subject to the terms of the
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen# Common Development and Distribution License (the "License").
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen# You may not use this file except in compliance with the License.
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen#
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen# or http://www.opensolaris.org/os/licensing.
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen# See the License for the specific language governing permissions
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen# and limitations under the License.
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen#
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen# When distributing Covered Code, include this CDDL HEADER in each
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen# If applicable, add the following below this CDDL HEADER, with the
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek# fields enclosed by brackets "[]" replaced with your own identifying
9384ef699a57687ce6dbdae7d686181f4791b1e5Aki Tuomi# information: Portions Copyright [yyyy] [name of copyright owner]
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen#
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen# CDDL HEADER END
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen#
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek#
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek# Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek#
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainenimport sys
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipekimport pkg.query_parser as qp
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainenfrom pkg.query_parser import BooleanQueryException, ParseError, QueryException, QueryLengthExceeded
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek
9384ef699a57687ce6dbdae7d686181f4791b1e5Aki Tuomiclass QueryLexer(qp.QueryLexer):
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek pass
26c41874cf6019c3e39f0ed630b2a07a92b2635fJosef 'Jeff' Sipek
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipekclass QueryParser(qp.QueryParser):
26c41874cf6019c3e39f0ed630b2a07a92b2635fJosef 'Jeff' Sipek """This class exists so that the classes the parent class query parser
26c41874cf6019c3e39f0ed630b2a07a92b2635fJosef 'Jeff' Sipek uses to build the AST are the ones defined in this module and not the
26c41874cf6019c3e39f0ed630b2a07a92b2635fJosef 'Jeff' Sipek parent class's module. This is done so that a single query parser can
26c41874cf6019c3e39f0ed630b2a07a92b2635fJosef 'Jeff' Sipek be shared between the client and server modules but will construct an
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek AST using the appropriate classes."""
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek
26c41874cf6019c3e39f0ed630b2a07a92b2635fJosef 'Jeff' Sipek def __init__(self, lexer):
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek qp.QueryParser.__init__(self, lexer)
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek mod = sys.modules[QueryParser.__module__]
26c41874cf6019c3e39f0ed630b2a07a92b2635fJosef 'Jeff' Sipek tmp = {}
26c41874cf6019c3e39f0ed630b2a07a92b2635fJosef 'Jeff' Sipek for class_name in self.query_objs.keys():
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek assert hasattr(mod, class_name)
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek tmp[class_name] = getattr(mod, class_name)
26c41874cf6019c3e39f0ed630b2a07a92b2635fJosef 'Jeff' Sipek self.query_objs = tmp
61cf001f1944d92eb25f113ba4c08985d6e30d53Timo Sirainen
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen# Because many classes do not have client specific modifications, they
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek# simply subclass the parent module's classes.
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainenclass Query(qp.Query):
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi pass
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipekclass AndQuery(qp.AndQuery):
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen pass
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipekclass OrQuery(qp.OrQuery):
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek pass
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipekclass PkgConversion(qp.PkgConversion):
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek pass
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipekclass PhraseQuery(qp.PhraseQuery):
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek pass
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipekclass FieldQuery(qp.FieldQuery):
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek pass
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipekclass TopQuery(qp.TopQuery):
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek pass
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipekclass TermQuery(qp.TermQuery):
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek """This class handles the client specific search logic for searching
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek for a specific query term."""
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek _global_data_dict = {}
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi def search(self, restriction, fmris):
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen """This function performs the specific steps needed to do
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen search on a server.
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek
70e0c96e14bb7cf6601645b3a858ee0261230031Josef 'Jeff' Sipek The "restriction" parameter is a generator over results that
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen another branch of the AST has already found. If it's not None,
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen then it's treated as the domain for search. If it is None then
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi the actions of all known packages is the domain for search.
bb25bed75eefd011138ebf1b8e033fc8ef55ca74Timo Sirainen
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen The "fmris" parameter is a function which produces an object
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen which iterates over all known fmris."""
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi if restriction:
bb25bed75eefd011138ebf1b8e033fc8ef55ca74Timo Sirainen return self._restricted_search_internal(restriction)
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen base_res = self._search_internal(fmris)
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen it = self._get_results(base_res)
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen return it
2c57ebc900742bd1119ef011b77b4910c4660cfaTimo Sirainen