Commit 2e5c33db authored by Iustin Pop's avatar Iustin Pop

Rename filter and filter_ to qfilter

We currently use 'filter' as the OpCode, QueryRequest and RAPI field
name for representing a query filter. However, since 'filter' is a
built-in function, we actually have to use filter_ throughout the code
in order to not override the built-in function.

This patch simply goes and does a global sed over the code. Due to the
fact that the RAPI interface already exposed this field, we add
compatibility code for now which handles both forms.
Signed-off-by: default avatarIustin Pop <iustin@google.com>
Reviewed-by: default avatarMichael Hanselmann <hansmi@google.com>
parent 92678b3c
......@@ -78,7 +78,7 @@ $(genLuxiOp "LuxiOp"
[("Query" ,
[ ("what", [t| QrViaLuxi |], [| id |])
, ("fields", [t| [String] |], [| id |])
, ("filter", [t| Maybe (String, [[String]]) |], [| id |])
, ("qfilter", [t| Maybe (String, [[String]]) |], [| id |])
], SDict)
, ("QueryNodes",
[ ("names", [t| [String] |], [| id |])
......
......@@ -2694,9 +2694,9 @@ def GenericList(resource, fields, names, unit, separator, header, cl=None,
if not names:
names = None
filter_ = qlang.MakeFilter(names, force_filter)
qfilter = qlang.MakeFilter(names, force_filter)
response = cl.Query(resource, fields, filter_)
response = cl.Query(resource, fields, qfilter)
found_unknown = _WarnUnknownFields(response.fields)
......@@ -2925,24 +2925,24 @@ def GetOnlineNodes(nodes, cl=None, nowarn=False, secondary_ips=False,
if cl is None:
cl = GetClient()
filter_ = []
qfilter = []
if nodes:
filter_.append(qlang.MakeSimpleFilter("name", nodes))
qfilter.append(qlang.MakeSimpleFilter("name", nodes))
if nodegroup is not None:
filter_.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup],
qfilter.append([qlang.OP_OR, [qlang.OP_EQUAL, "group", nodegroup],
[qlang.OP_EQUAL, "group.uuid", nodegroup]])
if filter_master:
filter_.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
qfilter.append([qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
if filter_:
if len(filter_) > 1:
final_filter = [qlang.OP_AND] + filter_
if qfilter:
if len(qfilter) > 1:
final_filter = [qlang.OP_AND] + qfilter
else:
assert len(filter_) == 1
final_filter = filter_[0]
assert len(qfilter) == 1
final_filter = qfilter[0]
else:
final_filter = None
......
......@@ -468,13 +468,13 @@ class _QueryBase:
#: Attribute holding field definitions
FIELDS = None
def __init__(self, filter_, fields, use_locking):
def __init__(self, qfilter, fields, use_locking):
"""Initializes this class.
"""
self.use_locking = use_locking
self.query = query.Query(self.FIELDS, fields, filter_=filter_,
self.query = query.Query(self.FIELDS, fields, qfilter=qfilter,
namefield="name")
self.requested_data = self.query.RequestedData()
self.names = self.query.RequestedNames()
......@@ -4820,7 +4820,7 @@ class LUQuery(NoHooksLU):
def CheckArguments(self):
qcls = _GetQueryImplementation(self.op.what)
self.impl = qcls(self.op.filter, self.op.fields, self.op.use_locking)
self.impl = qcls(self.op.qfilter, self.op.fields, self.op.use_locking)
def ExpandNames(self):
self.impl.ExpandNames(self)
......
......@@ -499,18 +499,18 @@ class Client(object):
break
return result
def Query(self, what, fields, filter_):
def Query(self, what, fields, qfilter):
"""Query for resources/items.
@param what: One of L{constants.QR_VIA_LUXI}
@type fields: List of strings
@param fields: List of requested fields
@type filter_: None or list
@param filter_: Query filter
@type qfilter: None or list
@param qfilter: Query filter
@rtype: L{objects.QueryResponse}
"""
req = objects.QueryRequest(what=what, fields=fields, filter=filter_)
req = objects.QueryRequest(what=what, fields=fields, qfilter=qfilter)
result = self.CallMethod(REQ_QUERY, req.ToDict())
return objects.QueryResponse.FromDict(result)
......
......@@ -1468,7 +1468,7 @@ class QueryRequest(ConfigObject):
__slots__ = [
"what",
"fields",
"filter",
"qfilter",
]
......
......@@ -821,7 +821,7 @@ class OpQuery(OpCode):
@ivar what: Resources to query for, must be one of L{constants.QR_VIA_OP}
@ivar fields: List of fields to retrieve
@ivar filter: Query filter
@ivar qfilter: Query filter
"""
OP_DSC_FIELD = "what"
......@@ -830,7 +830,7 @@ class OpQuery(OpCode):
_PUseLocking,
("fields", ht.NoDefault, ht.TListOf(ht.TNonEmptyString),
"Requested fields"),
("filter", None, ht.TOr(ht.TNone, ht.TListOf),
("qfilter", None, ht.TOr(ht.TNone, ht.TListOf),
"Query filter"),
]
......
......@@ -409,13 +409,13 @@ class _FilterCompilerHelper:
self._hints = None
self._op_handler = None
def __call__(self, hints, filter_):
def __call__(self, hints, qfilter):
"""Converts a query filter into a callable function.
@type hints: L{_FilterHints} or None
@param hints: Callbacks doing analysis on filter
@type filter_: list
@param filter_: Filter structure
@type qfilter: list
@param qfilter: Filter structure
@rtype: callable
@return: Function receiving context and item as parameters, returning
boolean as to whether item matches filter
......@@ -431,20 +431,20 @@ class _FilterCompilerHelper:
}
try:
filter_fn = self._Compile(filter_, 0)
filter_fn = self._Compile(qfilter, 0)
finally:
self._op_handler = None
return filter_fn
def _Compile(self, filter_, level):
def _Compile(self, qfilter, level):
"""Inner function for converting filters.
Calls the correct handler functions for the top-level operator. This
function is called recursively (e.g. for logic operators).
"""
if not (isinstance(filter_, (list, tuple)) and filter_):
if not (isinstance(qfilter, (list, tuple)) and qfilter):
raise errors.ParameterError("Invalid filter on level %s" % level)
# Limit recursion
......@@ -453,7 +453,7 @@ class _FilterCompilerHelper:
" nested too deep)" % self._LEVELS_MAX)
# Create copy to be modified
operands = filter_[:]
operands = qfilter[:]
op = operands.pop(0)
try:
......@@ -581,7 +581,7 @@ class _FilterCompilerHelper:
" (op '%s', flags %s)" % (op, field_flags))
def _CompileFilter(fields, hints, filter_):
def _CompileFilter(fields, hints, qfilter):
"""Converts a query filter into a callable function.
See L{_FilterCompilerHelper} for details.
......@@ -589,11 +589,11 @@ def _CompileFilter(fields, hints, filter_):
@rtype: callable
"""
return _FilterCompilerHelper(fields)(hints, filter_)
return _FilterCompilerHelper(fields)(hints, qfilter)
class Query:
def __init__(self, fieldlist, selected, filter_=None, namefield=None):
def __init__(self, fieldlist, selected, qfilter=None, namefield=None):
"""Initializes this class.
The field definition is a dictionary with the field's name as a key and a
......@@ -620,7 +620,7 @@ class Query:
self._requested_names = None
self._filter_datakinds = frozenset()
if filter_ is not None:
if qfilter is not None:
# Collect requested names if wanted
if namefield:
hints = _FilterHints(namefield)
......@@ -628,7 +628,7 @@ class Query:
hints = None
# Build filter function
self._filter_fn = _CompileFilter(fieldlist, hints, filter_)
self._filter_fn = _CompileFilter(fieldlist, hints, qfilter)
if hints:
self._requested_names = hints.RequestedNames()
self._filter_datakinds = hints.ReferencedData()
......
......@@ -1829,15 +1829,15 @@ class GanetiRapiClient(object): # pylint: disable=R0904
("/%s/groups/%s/tags" %
(GANETI_RAPI_VERSION, group)), query, None)
def Query(self, what, fields, filter_=None):
def Query(self, what, fields, qfilter=None):
"""Retrieves information about resources.
@type what: string
@param what: Resource name, one of L{constants.QR_VIA_RAPI}
@type fields: list of string
@param fields: Requested fields
@type filter_: None or list
@param filter_: Query filter
@type qfilter: None or list
@param qfilter: Query filter
@rtype: string
@return: job id
......@@ -1847,8 +1847,10 @@ class GanetiRapiClient(object): # pylint: disable=R0904
"fields": fields,
}
if filter_ is not None:
body["filter"] = filter_
if qfilter is not None:
body["qfilter"] = qfilter
# TODO: remove this after 2.7
body["filter"] = qfilter
return self._SendRequest(HTTP_PUT,
("/%s/query/%s" %
......
......@@ -1189,8 +1189,8 @@ class R_2_query(baserlib.ResourceBase):
GET_OPCODE = opcodes.OpQuery
PUT_OPCODE = opcodes.OpQuery
def _Query(self, fields, filter_):
return self.GetClient().Query(self.items[0], fields, filter_).ToDict()
def _Query(self, fields, qfilter):
return self.GetClient().Query(self.items[0], fields, qfilter).ToDict()
def GET(self):
"""Returns resource information.
......@@ -1215,7 +1215,12 @@ class R_2_query(baserlib.ResourceBase):
except KeyError:
fields = _GetQueryFields(self.queryargs)
return self._Query(fields, self.request_body.get("filter", None))
qfilter = body.get("qfilter", None)
# TODO: remove this after 2.7
if qfilter is None:
qfilter = body.get("filter", None)
return self._Query(fields, qfilter)
class R_2_query_fields(baserlib.ResourceBase):
......
......@@ -235,9 +235,9 @@ class ClientOps:
if req.what in constants.QR_VIA_OP:
result = self._Query(opcodes.OpQuery(what=req.what, fields=req.fields,
filter=req.filter))
qfilter=req.qfilter))
elif req.what == constants.QR_LOCK:
if req.filter is not None:
if req.qfilter is not None:
raise errors.OpPrereqError("Lock queries can't be filtered")
return self.server.context.glm.QueryLocks(req.fields)
elif req.what in constants.QR_VIA_LUXI:
......
......@@ -638,13 +638,13 @@ def _GetGroupData(cl, uuid):
opcodes.OpQuery(what=constants.QR_INSTANCE,
fields=["name", "status", "admin_state", "snodes",
"pnode.group.uuid", "snodes.group.uuid"],
filter=[qlang.OP_EQUAL, "pnode.group.uuid", uuid],
qfilter=[qlang.OP_EQUAL, "pnode.group.uuid", uuid],
use_locking=True),
# Get all nodes in group
opcodes.OpQuery(what=constants.QR_NODE,
fields=["name", "bootid", "offline"],
filter=[qlang.OP_EQUAL, "group.uuid", uuid],
qfilter=[qlang.OP_EQUAL, "group.uuid", uuid],
use_locking=True),
]
......
......@@ -765,7 +765,7 @@ class TestGetOnlineNodes(unittest.TestCase):
def CountPending(self):
return len(self._query)
def Query(self, res, fields, filter_):
def Query(self, res, fields, qfilter):
if res != constants.QR_NODE:
raise Exception("Querying wrong resource")
......@@ -774,7 +774,7 @@ class TestGetOnlineNodes(unittest.TestCase):
if exp_fields != fields:
raise Exception("Expected fields %s, got %s" % (exp_fields, fields))
if not (filter_ is None or check_filter(filter_)):
if not (qfilter is None or check_filter(qfilter)):
raise Exception("Filter doesn't match expectations")
return objects.QueryResponse(fields=None, data=result)
......@@ -804,8 +804,8 @@ class TestGetOnlineNodes(unittest.TestCase):
def testNoMaster(self):
cl = self._FakeClient()
def _CheckFilter(filter_):
self.assertEqual(filter_, [qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
def _CheckFilter(qfilter):
self.assertEqual(qfilter, [qlang.OP_NOT, [qlang.OP_TRUE, "master"]])
return True
cl.AddQueryResult(["name", "offline", "sip"], _CheckFilter, [
......@@ -835,8 +835,8 @@ class TestGetOnlineNodes(unittest.TestCase):
def testNoMasterFilterNodeName(self):
cl = self._FakeClient()
def _CheckFilter(filter_):
self.assertEqual(filter_,
def _CheckFilter(qfilter):
self.assertEqual(qfilter,
[qlang.OP_AND,
[qlang.OP_OR] + [[qlang.OP_EQUAL, "name", name]
for name in ["node2", "node3"]],
......@@ -877,8 +877,8 @@ class TestGetOnlineNodes(unittest.TestCase):
def testNodeGroup(self):
cl = self._FakeClient()
def _CheckFilter(filter_):
self.assertEqual(filter_,
def _CheckFilter(qfilter):
self.assertEqual(qfilter,
[qlang.OP_OR, [qlang.OP_EQUAL, "group", "foobar"],
[qlang.OP_EQUAL, "group.uuid", "foobar"]])
return True
......
#!/usr/bin/python
#
# Copyright (C) 2010 Google Inc.
# Copyright (C) 2010, 2011 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -37,8 +37,8 @@ class TestMakeSimpleFilter(unittest.TestCase):
if parse_exp is None:
parse_exp = names
filter_ = qlang.MakeSimpleFilter(field, names)
self.assertEqual(filter_, expected)
qfilter = qlang.MakeSimpleFilter(field, names)
self.assertEqual(qfilter, expected)
def test(self):
self._Test("name", None, None, parse_exp=[])
......@@ -53,9 +53,9 @@ class TestParseFilter(unittest.TestCase):
def setUp(self):
self.parser = qlang.BuildFilterParser()
def _Test(self, filter_, expected, expect_filter=True):
self.assertEqual(qlang.MakeFilter([filter_], not expect_filter), expected)
self.assertEqual(qlang.ParseFilter(filter_, parser=self.parser), expected)
def _Test(self, qfilter, expected, expect_filter=True):
self.assertEqual(qlang.MakeFilter([qfilter], not expect_filter), expected)
self.assertEqual(qlang.ParseFilter(qfilter, parser=self.parser), expected)
def test(self):
self._Test("name==\"foobar\"", [qlang.OP_EQUAL, "name", "foobar"])
......@@ -167,13 +167,13 @@ class TestParseFilter(unittest.TestCase):
# Non-matching regexp delimiters
tests.append("name =~ /foobarbaz#")
for filter_ in tests:
for qfilter in tests:
try:
qlang.ParseFilter(filter_, parser=self.parser)
qlang.ParseFilter(qfilter, parser=self.parser)
except errors.QueryFilterParseError, err:
self.assertEqual(len(err.GetDetails()), 3)
else:
self.fail("Invalid filter '%s' did not raise exception" % filter_)
self.fail("Invalid filter '%s' did not raise exception" % qfilter)
class TestMakeFilter(unittest.TestCase):
......
......@@ -1060,32 +1060,32 @@ class TestQueryFilter(unittest.TestCase):
assert "name" in fielddefs
# No name field
q = query.Query(fielddefs, ["name"], filter_=["=", "name", "abc"],
q = query.Query(fielddefs, ["name"], qfilter=["=", "name", "abc"],
namefield=None)
self.assertEqual(q.RequestedNames(), None)
# No filter
q = query.Query(fielddefs, ["name"], filter_=None, namefield="name")
q = query.Query(fielddefs, ["name"], qfilter=None, namefield="name")
self.assertEqual(q.RequestedNames(), None)
# Check empty query
q = query.Query(fielddefs, ["name"], filter_=["|"], namefield="name")
q = query.Query(fielddefs, ["name"], qfilter=["|"], namefield="name")
self.assertEqual(q.RequestedNames(), None)
# Check order
q = query.Query(fielddefs, ["name"], filter_=["|"] + innerfilter,
q = query.Query(fielddefs, ["name"], qfilter=["|"] + innerfilter,
namefield="name")
self.assertEqual(q.RequestedNames(), ["x0", "x1", "x2", "x3"])
# Check reverse order
q = query.Query(fielddefs, ["name"],
filter_=["|"] + list(reversed(innerfilter)),
qfilter=["|"] + list(reversed(innerfilter)),
namefield="name")
self.assertEqual(q.RequestedNames(), ["x3", "x2", "x1", "x0"])
# Duplicates
q = query.Query(fielddefs, ["name"],
filter_=["|"] + innerfilter + list(reversed(innerfilter)),
qfilter=["|"] + innerfilter + list(reversed(innerfilter)),
namefield="name")
self.assertEqual(q.RequestedNames(), ["x0", "x1", "x2", "x3"])
......@@ -1095,21 +1095,21 @@ class TestQueryFilter(unittest.TestCase):
# Filter with AND
q = query.Query(fielddefs, ["name"],
filter_=["|", ["=", "name", "foo"],
qfilter=["|", ["=", "name", "foo"],
["&", ["=", "name", ""]]],
namefield="name")
self.assertTrue(q.RequestedNames() is None)
# Filter with NOT
q = query.Query(fielddefs, ["name"],
filter_=["|", ["=", "name", "foo"],
qfilter=["|", ["=", "name", "foo"],
["!", ["=", "name", ""]]],
namefield="name")
self.assertTrue(q.RequestedNames() is None)
# Filter with only OR (names must be in correct order)
q = query.Query(fielddefs, ["name"],
filter_=["|", ["=", "name", "x17361"],
qfilter=["|", ["=", "name", "x17361"],
["|", ["=", "name", "x22015"]],
["|", ["|", ["=", "name", "x13193"]]],
["=", "name", "x15215"]],
......@@ -1136,14 +1136,14 @@ class TestQueryFilter(unittest.TestCase):
]
for fielddefs in query.ALL_FIELD_LISTS:
for filter_ in checks:
for qfilter in checks:
self.assertRaises(errors.ParameterError, query._CompileFilter,
fielddefs, None, filter_)
fielddefs, None, qfilter)
for op in ["|", "!"]:
filter_ = self._GenNestedFilter(op, levels_max - 1)
qfilter = self._GenNestedFilter(op, levels_max - 1)
self.assertTrue(callable(query._CompileFilter(fielddefs, None,
filter_)))
qfilter)))
def testQueryInputOrder(self):
fielddefs = query._PrepareFieldList([
......@@ -1160,10 +1160,10 @@ class TestQueryFilter(unittest.TestCase):
{ "pnode": "node20", "snode": "node1", },
]
filter_ = ["|", ["=", "pnode", "node1"], ["=", "snode", "node1"]]
qfilter = ["|", ["=", "pnode", "node1"], ["=", "snode", "node1"]]
q = query.Query(fielddefs, ["pnode", "snode"], namefield="pnode",
filter_=filter_)
qfilter=qfilter)
self.assertTrue(q.RequestedNames() is None)
self.assertFalse(q.RequestedData())
self.assertEqual(q.Query(data),
......@@ -1179,7 +1179,7 @@ class TestQueryFilter(unittest.TestCase):
# No name field, result must be in incoming order
q = query.Query(fielddefs, ["pnode", "snode"], namefield=None,
filter_=filter_)
qfilter=qfilter)
self.assertFalse(q.RequestedData())
self.assertEqual(q.Query(data),
[[(constants.RS_NORMAL, "node1"), (constants.RS_NORMAL, "node44")],
......@@ -1242,7 +1242,7 @@ class TestQueryFilter(unittest.TestCase):
]
q = query.Query(fielddefs, ["pnode", "num"], namefield="pnode",
filter_=["|", ["=", "pnode", "node1"],
qfilter=["|", ["=", "pnode", "node1"],
["=", "pnode", "node2"],
["=", "pnode", "node1"]])
self.assertEqual(q.RequestedNames(), ["node1", "node2"],
......@@ -1268,7 +1268,7 @@ class TestQueryFilter(unittest.TestCase):
]
q = query.Query(fielddefs, ["pnode", "num"], namefield="pnode",
filter_=["|", ["=", "pnode", "nodeX"],
qfilter=["|", ["=", "pnode", "nodeX"],
["=", "pnode", "nodeY"],
["=", "pnode", "nodeY"],
["=", "pnode", "nodeY"],
......@@ -1311,20 +1311,20 @@ class TestQueryFilter(unittest.TestCase):
# Empty filter
q = query.Query(fielddefs, ["name", "other"], namefield="name",
filter_=["|"])
qfilter=["|"])
self.assertTrue(q.RequestedNames() is None)
self.assertEqual(q.RequestedData(), set([DK_A, DK_B]))
self.assertEqual(q.Query(data), [])
# Normal filter
q = query.Query(fielddefs, ["name", "other"], namefield="name",
filter_=["=", "name", "node1"])
qfilter=["=", "name", "node1"])
self.assertEqual(q.RequestedNames(), ["node1"])
self.assertEqual(q.Query(data),
[[(constants.RS_NORMAL, "node1"), (constants.RS_NORMAL, "foo")]])
q = query.Query(fielddefs, ["name", "other"], namefield="name",
filter_=(["|", ["=", "name", "node1"],
qfilter=(["|", ["=", "name", "node1"],
["=", "name", "node3"]]))
self.assertEqual(q.RequestedNames(), ["node1", "node3"])
self.assertEqual(q.Query(data),
......@@ -1333,7 +1333,7 @@ class TestQueryFilter(unittest.TestCase):
# Complex filter
q = query.Query(fielddefs, ["name", "other"], namefield="name",
filter_=(["|", ["=", "name", "node1"],
qfilter=(["|", ["=", "name", "node1"],
["|", ["=", "name", "node3"],
["=", "name", "node2"]],
["=", "name", "node3"]]))
......@@ -1348,11 +1348,11 @@ class TestQueryFilter(unittest.TestCase):
for i in [-1, 0, 1, 123, [], None, True, False]:
self.assertRaises(errors.ParameterError, query.Query,
fielddefs, ["name", "other"], namefield="name",
filter_=["=", "name", i])
qfilter=["=", "name", i])
# Negative filter
q = query.Query(fielddefs, ["name", "other"], namefield="name",
filter_=["!", ["|", ["=", "name", "node1"],
qfilter=["!", ["|", ["=", "name", "node1"],
["=", "name", "node3"]]])
self.assertTrue(q.RequestedNames() is None)
self.assertEqual(q.Query(data),
......@@ -1360,7 +1360,7 @@ class TestQueryFilter(unittest.TestCase):
# Not equal
q = query.Query(fielddefs, ["name", "other"], namefield="name",
filter_=["!=", "name", "node3"])
qfilter=["!=", "name", "node3"])
self.assertTrue(q.RequestedNames() is None)
self.assertEqual(q.Query(data),
[[(constants.RS_NORMAL, "node1"), (constants.RS_NORMAL, "foo")],
......@@ -1368,7 +1368,7 @@ class TestQueryFilter(unittest.TestCase):
# Data type
q = query.Query(fielddefs, [], namefield="name",
filter_=["|", ["=", "other", "bar"],
qfilter=["|", ["=", "other", "bar"],
["=", "name", "foo"]])
self.assertTrue(q.RequestedNames() is None)
self.assertEqual(q.RequestedData(), set([DK_A, DK_B]))
......@@ -1376,13 +1376,13 @@ class TestQueryFilter(unittest.TestCase):
# Only one data type
q = query.Query(fielddefs, ["other"], namefield="name",
filter_=["=", "other", "bar"])
qfilter=["=", "other", "bar"])
self.assertTrue(q.RequestedNames() is None)
self.assertEqual(q.RequestedData(), set([DK_B]))
self.assertEqual(q.Query(data), [[(constants.RS_NORMAL, "bar")]])
q = query.Query(fielddefs, [], namefield="name",