Statistics
| Branch: | Tag: | Revision:

root / lib / qlang.py @ 31554d0a

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
from ganeti import errors
35

    
36

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

    
42

    
43
# Unary operators with exactly one operand
44
OP_NOT = "!"
45
OP_TRUE = "?"
46

    
47

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

    
56

    
57
def MakeSimpleFilter(namefield, values):
58
  """Builds simple a filter.
59

60
  @param namefield: Name of field containing item name
61
  @param values: List of names
62

63
  """
64
  if values:
65
    return [OP_OR] + [[OP_EQUAL, namefield, i] for i in values]
66

    
67
  return None