Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Private Member Functions | Private Attributes
MoinMoin.search.queryparser.QueryParser Class Reference

List of all members.

Public Member Functions

def __init__
def parse_query

Public Attributes

 titlesearch
 case
 regex

Private Member Functions

def _analyse_items

Private Attributes

 _M

Detailed Description

Converts a String into a tree of Query objects.

Definition at line 27 of file __init__.py.


Constructor & Destructor Documentation

@keyword titlesearch: treat all terms as title searches
@keyword case: do case sensitive search
@keyword regex: treat all terms as regular expressions

Definition at line 32 of file __init__.py.

00032 
00033     def __init__(self, **kw):
00034         """
00035         @keyword titlesearch: treat all terms as title searches
00036         @keyword case: do case sensitive search
00037         @keyword regex: treat all terms as regular expressions
00038         """
00039         self.titlesearch = kw.get('titlesearch', 0)
00040         self.case = kw.get('case', 0)
00041         self.regex = kw.get('regex', 0)
00042         self._M = wikiutil.ParserPrefix('-')


Member Function Documentation

def MoinMoin.search.queryparser.QueryParser._analyse_items (   self,
  items 
) [private]

Definition at line 43 of file __init__.py.

00043 
00044     def _analyse_items(self, items):
00045         terms = AndExpression()
00046         M = self._M
00047         while items:
00048             item = items[0]
00049             items = items[1:]
00050 
00051             if isinstance(item, unicode):
00052                 if item.lower() == 'or':
00053                     sub = terms.subterms()
00054                     if len(sub) >= 1:
00055                         last = sub[-1]
00056                         if last.__class__ == OrExpression:
00057                             orexpr = last
00058                         else:
00059                             # Note: do NOT reduce "terms" when it has a single subterm only!
00060                             # Doing that would break "-someterm" searches as we rely on AndExpression
00061                             # doing a "MatchAll AND_NOT someterm" for that case!
00062                             orexpr = OrExpression(terms)
00063                         terms = AndExpression(orexpr)
00064                     else:
00065                         raise QueryError('Nothing to OR')
00066                     remaining = self._analyse_items(items)
00067                     if remaining.__class__ == OrExpression:
00068                         for sub in remaining.subterms():
00069                             orexpr.append(sub)
00070                     else:
00071                         orexpr.append(remaining)
00072                     break
00073                 elif item.lower() == 'and':
00074                     pass
00075                 else:
00076                     # odd workaround; we should instead ignore this term
00077                     # and reject expressions that contain nothing after
00078                     # being parsed rather than rejecting an empty string
00079                     # before parsing...
00080                     if not item:
00081                         raise QueryError("Term too short")
00082                     regex = self.regex
00083                     case = self.case
00084                     if self.titlesearch:
00085                         terms.append(TitleSearch(item, use_re=regex, case=case))
00086                     else:
00087                         terms.append(TextSearch(item, use_re=regex, case=case))
00088             elif isinstance(item, tuple):
00089                 negate = item[0] == M
00090                 title_search = self.titlesearch
00091                 regex = self.regex
00092                 case = self.case
00093                 linkto = False
00094                 lang = False
00095                 category = False
00096                 mimetype = False
00097                 domain = False
00098                 while len(item) > 1:
00099                     m = item[0]
00100                     if m is None:
00101                         raise QueryError("Invalid search prefix")
00102                     elif m == M:
00103                         negate = True
00104                     elif "title".startswith(m):
00105                         title_search = True
00106                     elif "regex".startswith(m):
00107                         regex = True
00108                     elif "case".startswith(m):
00109                         case = True
00110                     elif "linkto".startswith(m):
00111                         linkto = True
00112                     elif "language".startswith(m):
00113                         lang = True
00114                     elif "category".startswith(m):
00115                         category = True
00116                     elif "mimetype".startswith(m):
00117                         mimetype = True
00118                     elif "domain".startswith(m):
00119                         domain = True
00120                     else:
00121                         raise QueryError("Invalid search prefix")
00122                     item = item[1:]
00123 
00124                 text = item[0]
00125                 if category:
00126                     obj = CategorySearch(text, use_re=regex, case=case)
00127                 elif mimetype:
00128                     obj = MimetypeSearch(text, use_re=regex, case=False)
00129                 elif lang:
00130                     obj = LanguageSearch(text, use_re=regex, case=False)
00131                 elif linkto:
00132                     obj = LinkSearch(text, use_re=regex, case=case)
00133                 elif domain:
00134                     obj = DomainSearch(text, use_re=regex, case=False)
00135                 elif title_search:
00136                     obj = TitleSearch(text, use_re=regex, case=case)
00137                 else:
00138                     obj = TextSearch(text, use_re=regex, case=case)
00139                 obj.negated = negate
00140                 terms.append(obj)
00141             elif isinstance(item, list):
00142                 # strip off the opening parenthesis
00143                 terms.append(self._analyse_items(item[1:]))
00144 
00145         # Note: do NOT reduce "terms" when it has a single subterm only!
00146         # Doing that would break "-someterm" searches as we rely on AndExpression
00147         # doing a "MatchAll AND_NOT someterm" for that case!
00148         return terms

Here is the call graph for this function:

Here is the caller graph for this function:

transform an string into a tree of Query objects 

Definition at line 149 of file __init__.py.

00149 
00150     def parse_query(self, query):
00151         """ transform an string into a tree of Query objects """
00152         if isinstance(query, str):
00153             query = query.decode(config.charset)
00154         try:
00155             items = wikiutil.parse_quoted_separated_ext(query,
00156                                                         name_value_separator=':',
00157                                                         prefixes='-',
00158                                                         multikey=True,
00159                                                         brackets=('()', ),
00160                                                         quotes='\'"')
00161         except wikiutil.BracketError, err:
00162             raise QueryError(str(err))
00163         logging.debug("parse_quoted_separated items: %r" % items)
00164         query = self._analyse_items(items)
00165         logging.debug("analyse_items query: %r" % query)
00166         return query

Here is the call graph for this function:


Member Data Documentation

Definition at line 41 of file __init__.py.

Definition at line 39 of file __init__.py.

Definition at line 40 of file __init__.py.

Definition at line 38 of file __init__.py.


The documentation for this class was generated from the following file: