Skip to content
This repository has been archived by the owner on May 13, 2020. It is now read-only.

Commit

Permalink
Move IQueryParseTree to a separate file, to conform to style
Browse files Browse the repository at this point in the history
guidelines.  Added some conformance tests.
  • Loading branch information
gvanrossum committed May 24, 2002
1 parent a1ffe03 commit ae87725
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 37 deletions.
52 changes: 52 additions & 0 deletions IQueryParseTree.py
@@ -0,0 +1,52 @@
##############################################################################
#
# Copyright (c) 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################

"""Query Parser Tree Interface."""

import Interface

class IQueryParseTree(Interface.Base):
"""Interface for parse trees returned by parseQuery()."""

def nodeType():
"""Return the node type.
This is one of 'AND', 'OR', 'NOT', 'ATOM', 'PHRASE' or 'GLOB'.
"""

def getValue():
"""Return a node-type specific value.
For node type: Return:
'AND' a list of parse trees
'OR' a list of parse trees
'NOT' a parse tree
'ATOM' a string (representing a single search term)
'PHRASE' a string (representing a search phrase)
'GLOB' a string (representing a pattern, e.g. "foo*")
"""

def terms():
"""Return a list of all terms in this node, excluding NOT subtrees."""

def executeQuery(index):
"""Execute the query represented by this node against the index.
The index argument must implement the IIndex interface.
Return an IIBucket or IIBTree mapping document ids to scores
(higher scores mean better results).
May raise ParseTree.QueryError.
"""
35 changes: 0 additions & 35 deletions IQueryParser.py
Expand Up @@ -51,38 +51,3 @@ def parseQueryEx(query):
May raise ParseTree.ParseError.
"""

class IQueryParseTree(Interface.Base):
"""Interface for parse trees returned by parseQuery()."""

def nodeType():
"""Return the node type.
This is one of 'AND', 'OR', 'NOT', 'ATOM', 'PHRASE' or 'GLOB'.
"""

def getValue():
"""Return a node-type specific value.
For node type: Return:
'AND' a list of parse trees
'OR' a list of parse trees
'NOT' a parse tree
'ATOM' a string (representing a single search term)
'PHRASE' a string (representing a search phrase)
'GLOB' a string (representing a pattern, e.g. "foo*")
"""

def terms():
"""Return a list of all terms in this node, excluding NOT subtrees."""

def executeQuery(index):
"""Execute the query represented by this node against the index.
The index argument must implement the IIndex interface.
Return an IIBucket or IIBTree mapping document ids to scores
(higher scores mean better results).
May raise ParseTree.QueryError.
"""
3 changes: 3 additions & 0 deletions ParseTree.py
Expand Up @@ -16,6 +16,7 @@

from BTrees.IIBTree import difference

from Products.ZCTextIndex.IQueryParseTree import IQueryParseTree
from Products.ZCTextIndex.SetOps import mass_weightedIntersection, \
mass_weightedUnion

Expand All @@ -27,6 +28,8 @@ class ParseError(Exception):

class ParseTreeNode:

__implements__ = IQueryParseTree

_nodeType = None

def __init__(self, value):
Expand Down
5 changes: 4 additions & 1 deletion QueryParser.py
Expand Up @@ -57,7 +57,8 @@

import re

import ParseTree # relative import
from Products.ZCTextIndex.IQueryParser import IQueryParser
from Products.ZCTextIndex import ParseTree

# Create unique symbols for token types.
_AND = intern("AND")
Expand Down Expand Up @@ -94,6 +95,8 @@

class QueryParser:

__implements__ = IQueryParser

# This class is not thread-safe;
# each thread should have its own instance

Expand Down
25 changes: 24 additions & 1 deletion tests/testQueryParser.py
Expand Up @@ -14,13 +14,31 @@

from unittest import TestCase, TestSuite, main, makeSuite

from Products.ZCTextIndex.QueryParser import QueryParser
from Interface import verify_class_implementation

from Products.ZCTextIndex.IQueryParser import IQueryParser
from Products.ZCTextIndex.IQueryParseTree import IQueryParseTree

from Products.ZCTextIndex.QueryParser import QueryParser
from Products.ZCTextIndex.ParseTree import ParseError, ParseTreeNode
from Products.ZCTextIndex.ParseTree import OrNode, AndNode, NotNode
from Products.ZCTextIndex.ParseTree import AtomNode, PhraseNode, GlobNode
from Products.ZCTextIndex.Lexicon import Lexicon, Splitter


class TestInterfaces(TestCase):

def testInterfaces(self):
verify_class_implementation(IQueryParser, QueryParser)
verify_class_implementation(IQueryParseTree, ParseTreeNode)
verify_class_implementation(IQueryParseTree, OrNode)
verify_class_implementation(IQueryParseTree, AndNode)
verify_class_implementation(IQueryParseTree, NotNode)
verify_class_implementation(IQueryParseTree, AtomNode)
verify_class_implementation(IQueryParseTree, PhraseNode)
verify_class_implementation(IQueryParseTree, GlobNode)


class TestQueryParserBase(TestCase):

def setUp(self):
Expand Down Expand Up @@ -67,6 +85,7 @@ def compareParseTrees(self, got, expected, msg=None):
for i in range(len(list1)):
self.compareParseTrees(list1[i], list2[i], msg)


class TestQueryParser(TestQueryParserBase):

def test001(self):
Expand Down Expand Up @@ -216,6 +235,7 @@ def test121(self):
def test122(self):
self.failure("foo AND -bar")


class StopWordTestQueryParser(TestQueryParserBase):

def setUp(self):
Expand Down Expand Up @@ -259,6 +279,7 @@ def test305(self):
def test306(self):
self.failure('stop AND NOT foo')


class FakeStopWordRemover:

def process(self, list):
Expand All @@ -268,7 +289,9 @@ def process(self, list):
def test_suite():
return TestSuite((makeSuite(TestQueryParser),
makeSuite(StopWordTestQueryParser),
makeSuite(TestInterfaces),
))


if __name__=="__main__":
main(defaultTest='test_suite')

0 comments on commit ae87725

Please sign in to comment.