Statistics
| Branch: | Tag: | Revision:

root / lib / qlang.py @ 88076fd1

History | View | Annotate | Download (2.7 kB)

1
#
2
#
3

    
4
# Copyright (C) 2010 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
from ganeti import errors
25

    
26

    
27
# Logic operators
28
OP_OR = "|"
29
OP_AND = "&"
30

    
31

    
32
# Unary operators
33
OP_NOT = "!"
34

    
35

    
36
# Binary operators
37
OP_EQUAL = "="
38
OP_NOT_EQUAL = "!="
39
OP_GLOB = "=*"
40
OP_REGEXP = "=~"
41
OP_CONTAINS = "=[]"
42

    
43

    
44
def ReadSimpleFilter(namefield, filter_):
45
  """Function extracting wanted names from restricted filter.
46

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

51
  """
52
  if filter_ is None:
53
    return []
54

    
55
  if not isinstance(filter_, list):
56
    raise errors.ParameterError("Filter should be list")
57

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

    
61
  if len(filter_) < 2:
62
    raise errors.ParameterError("Invalid filter, OR operator should have"
63
                                " operands")
64

    
65
  result = []
66

    
67
  for idx, item in enumerate(filter_[1:]):
68
    if not isinstance(item, list):
69
      raise errors.ParameterError("Invalid OR operator, operand %s not a"
70
                                  " list" % idx)
71

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

    
76
    (_, name, value) = item
77

    
78
    if not isinstance(value, basestring):
79
      raise errors.ParameterError("Operand %s for OR should compare against a"
80
                                  " string" % idx)
81

    
82
    if name != namefield:
83
      raise errors.ParameterError("Operand %s for OR should filter field '%s',"
84
                                  " not '%s'" % (idx, namefield, name))
85

    
86
    result.append(value)
87

    
88
  return result
89

    
90

    
91
def MakeSimpleFilter(namefield, values):
92
  """Builds a filter for use with L{ReadSimpleFilter}.
93

94
  @param namefield: Name of field containing item name
95
  @param values: List of names
96

97
  """
98
  if values:
99
    return [OP_OR] + [[OP_EQUAL, namefield, i] for i in values]
100

    
101
  return None