4 # Copyright (C) 2010 Google Inc.
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.
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.
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
22 """Module for a simple query language"""
24 from ganeti import errors
31 def ReadSimpleFilter(namefield, filter_):
32 """Function extracting wanted names from restricted filter.
34 This should only be used until proper filtering is implemented. The filter
35 must either be empty or of the format C{["|", ["=", field, "name1"], ["=",
36 field, "name2"], ...]}.
42 if not isinstance(filter_, list):
43 raise errors.ParameterError("Filter should be list")
45 if not filter_ or filter_[0] != OP_OR:
46 raise errors.ParameterError("Filter should start with OR operator")
49 raise errors.ParameterError("Invalid filter, OR operator should have"
54 for idx, item in enumerate(filter_[1:]):
55 if not isinstance(item, list):
56 raise errors.ParameterError("Invalid OR operator, operand %s not a"
59 if len(item) != 3 or item[0] != OP_EQUAL:
60 raise errors.ParameterError("Invalid OR operator, operand %s is not an"
61 " equality filter" % idx)
63 (_, name, value) = item
65 if not isinstance(value, basestring):
66 raise errors.ParameterError("Operand %s for OR should compare against a"
70 raise errors.ParameterError("Operand %s for OR should filter field '%s',"
71 " not '%s'" % (idx, namefield, name))
78 def MakeSimpleFilter(namefield, values):
79 """Builds a filter for use with L{ReadSimpleFilter}.
81 @param namefield: Name of field containing item name
82 @param values: List of names
86 return [OP_OR] + [[OP_EQUAL, namefield, i] for i in values]