Statistics
| Branch: | Tag: | Revision:

root / lib / qlang.py @ 341ff8e9

History | View | Annotate | Download (3.4 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 ReadSimpleFilter(namefield, filter_):
58
  """Function extracting wanted names from restricted filter.
59

60
  This should only be used until proper filtering is implemented. The filter
61
  must either be empty or of the format C{["|", ["=", field, "name1"], ["=",
62
  field, "name2"], ...]}.
63

64
  """
65
  if filter_ is None:
66
    return []
67

    
68
  if not isinstance(filter_, list):
69
    raise errors.ParameterError("Filter should be list")
70

    
71
  if not filter_ or filter_[0] != OP_OR:
72
    raise errors.ParameterError("Filter should start with OR operator")
73

    
74
  if len(filter_) < 2:
75
    raise errors.ParameterError("Invalid filter, OR operator should have"
76
                                " operands")
77

    
78
  result = []
79

    
80
  for idx, item in enumerate(filter_[1:]):
81
    if not isinstance(item, list):
82
      raise errors.ParameterError("Invalid OR operator, operand %s not a"
83
                                  " list" % idx)
84

    
85
    if len(item) != 3 or item[0] != OP_EQUAL:
86
      raise errors.ParameterError("Invalid OR operator, operand %s is not an"
87
                                  " equality filter" % idx)
88

    
89
    (_, name, value) = item
90

    
91
    if not isinstance(value, basestring):
92
      raise errors.ParameterError("Operand %s for OR should compare against a"
93
                                  " string" % idx)
94

    
95
    if name != namefield:
96
      raise errors.ParameterError("Operand %s for OR should filter field '%s',"
97
                                  " not '%s'" % (idx, namefield, name))
98

    
99
    result.append(value)
100

    
101
  return result
102

    
103

    
104
def MakeSimpleFilter(namefield, values):
105
  """Builds a filter for use with L{ReadSimpleFilter}.
106

107
  @param namefield: Name of field containing item name
108
  @param values: List of names
109

110
  """
111
  if values:
112
    return [OP_OR] + [[OP_EQUAL, namefield, i] for i in values]
113

    
114
  return None