Statistics
| Branch: | Tag: | Revision:

root / lib / qlang.py @ 3b877f08

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
OP_TRUE = "?"
35

    
36

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

    
44

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

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

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

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

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

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

    
66
  result = []
67

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

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

    
77
    (_, name, value) = item
78

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

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

    
87
    result.append(value)
88

    
89
  return result
90

    
91

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

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

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

    
102
  return None