Mei Liu has uploaded a new change for review. Change subject: mom: add basic operations in policy parser ......................................................................
mom: add basic operations in policy parser This patch adds getItem and setItem for list, and in operator. Change-Id: I18c8448b4ebb5b53c70bf16a9fb8bee8337f0801 Signed-off-by: MeiLiu <[email protected]> --- M mom/Policy/Parser.py M tests/ParserTests.py 2 files changed, 52 insertions(+), 3 deletions(-) git pull ssh://gerrit.ovirt.org:29418/mom refs/changes/12/17512/1 diff --git a/mom/Policy/Parser.py b/mom/Policy/Parser.py index 6d5599f..6704147 100644 --- a/mom/Policy/Parser.py +++ b/mom/Policy/Parser.py @@ -15,7 +15,18 @@ # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA import re +from functools import wraps from spark import GenericScanner, GenericParser + +def require_list(func): + @wraps(func) + def wrapper(self, iterable, *args): + if not isinstance(iterable, list): + raise PolicyError("Expecting list iterable in %s statement" % + func.__name__[2:]) + else: + return func(self, iterable, *args) + return wrapper class PolicyError(Exception): pass @@ -390,10 +401,22 @@ def c_list(self, *items): return list(items) + @require_list def c_append(self, iterable, item): - if isinstance(iterable, list): - iterable.append(item) - return iterable + iterable.append(item) + return iterable + + @require_list + def c_getItem(self, iterable, index): + return iterable[index] + + @require_list + def c_setItem(self, iterable, index, value): + iterable[index] = value + return iterable[index] + + def c_in(self, iterable, value): + return value in iterable def c_if(self, cond, yes, no): 'value code code' diff --git a/tests/ParserTests.py b/tests/ParserTests.py index 32a944c..59a2d39 100644 --- a/tests/ParserTests.py +++ b/tests/ParserTests.py @@ -177,6 +177,32 @@ """ self.verify(pol, [[6, 3, 'a'], [6, 3, 'a'], [7, 8, 'b']]) + def test_getItem(self): + pol = """ + (defvar a (list (+ 1 5) 3 (list))) + (getItem a 0) + (getItem a (+ -2 3)) + (getItem a (/ 4 2)) + """ + self.verify(pol, [[6, 3, []], 6, 3, []]) + + def test_setItem(self): + pol = """ + (defvar a (list (+ 1 5) 3 (list))) + (setItem a 0 (list)) + (setItem a (/ 2 2) 'a') + (setItem a (/ 4 2) (list 'b')) + """ + self.verify(pol, [[[], 'a', ['b']], [], 'a', ['b']]) + + def test_in(self): + pol = """ + (defvar a (list (+ 1 5) 3 (list))) + (in a 0) + (in a (* 3 2)) + """ + self.verify(pol, [[6, 3, []], False, True]) + def test_scope(self): pol = """ (defvar a 10) -- To view, visit http://gerrit.ovirt.org/17512 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I18c8448b4ebb5b53c70bf16a9fb8bee8337f0801 Gerrit-PatchSet: 1 Gerrit-Project: mom Gerrit-Branch: master Gerrit-Owner: Mei Liu <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
