Statistics
| Branch: | Tag: | Revision:

root / lib / qlang.py @ e5395072

History | View | Annotate | Download (1.9 kB)

1
#
2
#
3

    
4
# Copyright (C) 2010, 2011 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Module for a simple query language
23

24
A query filter is always a list. The first item in the list is the operator
25
(e.g. C{[OP_AND, ...]}), while the other items depend on the operator. For
26
logic operators (e.g. L{OP_AND}, L{OP_OR}), they are subfilters whose results
27
are combined. Unary operators take exactly one other item (e.g. a subfilter for
28
L{OP_NOT} and a field name for L{OP_TRUE}). Binary operators take exactly two
29
operands, usually a field name and a value to compare against. Filters are
30
converted to callable functions by L{query._CompileFilter}.
31

32
"""
33

    
34
# Logic operators with one or more operands, each of which is a filter on its
35
# own
36
OP_OR = "|"
37
OP_AND = "&"
38

    
39

    
40
# Unary operators with exactly one operand
41
OP_NOT = "!"
42
OP_TRUE = "?"
43

    
44

    
45
# Binary operators with exactly two operands, the field name and an
46
# operator-specific value
47
OP_EQUAL = "="
48
OP_NOT_EQUAL = "!="
49
OP_GLOB = "=*"
50
OP_REGEXP = "=~"
51
OP_CONTAINS = "=[]"
52

    
53

    
54
def MakeSimpleFilter(namefield, values):
55
  """Builds simple a filter.
56

57
  @param namefield: Name of field containing item name
58
  @param values: List of names
59

60
  """
61
  if values:
62
    return [OP_OR] + [[OP_EQUAL, namefield, i] for i in values]
63

    
64
  return None