On Fri, Dec 10, 2010 at 5:32 PM, Michael Hanselmann <[email protected]> wrote: > This will be used in clients to build the filters for query2. > --- > lib/qlang.py | 13 +++++++++++++ > test/ganeti.qlang_unittest.py | 18 ++++++++++++++++++ > 2 files changed, 31 insertions(+), 0 deletions(-) > > diff --git a/lib/qlang.py b/lib/qlang.py > index ec4f41b..3d8a806 100644 > --- a/lib/qlang.py > +++ b/lib/qlang.py > @@ -73,3 +73,16 @@ def ReadSimpleFilter(namefield, filter_): > result.append(value) > > return result > + > + > +def MakeSimpleFilter(namefield, values): > + """Builds a filter for use with L{ReadSimpleFilter}. > + > + �...@param namefield: Name of field containing item name > + �...@param values: List of names > + > + """ > + if values: > + return [OP_OR] + [[OP_EQUAL, namefield, i] for i in values] > + > + return None > diff --git a/test/ganeti.qlang_unittest.py b/test/ganeti.qlang_unittest.py > index c0ab03d..72c5558 100755 > --- a/test/ganeti.qlang_unittest.py > +++ b/test/ganeti.qlang_unittest.py > @@ -56,5 +56,23 @@ class TestReadSimpleFilter(unittest.TestCase): > "name", i) > > > +class TestMakeSimpleFilter(unittest.TestCase): > + def _Test(self, field, names, expected, parse_exp=None): > + if parse_exp is None: > + parse_exp = names > + > + filter_ = qlang.MakeSimpleFilter(field, names) > + self.assertEqual(filter_, expected) > + self.assertEqual(qlang.ReadSimpleFilter(field, filter_), parse_exp) > + > + def test(self): > + self._Test("name", None, None, parse_exp=[]) > + self._Test("name", [], None) > + self._Test("name", ["node1.example.com"], > + ["|", ["=", "name", "node1.example.com"]]) > + self._Test("xyz", ["a", "b", "c"], > + ["|", ["=", "xyz", "a"], ["=", "xyz", "b"], ["=", "xyz", > "c"]]) > + > + > if __name__ == "__main__": > testutils.GanetiTestProgram() > -- > 1.7.3.1
LGTM > >
