Martin Sivák has uploaded a new change for review. Change subject: Add valid function to the policy parser ......................................................................
Add valid function to the policy parser This new valid function accepts any number of arguments and returns True when none of the arguments evaluates to nil (None). It returns False in all other cases. The Common Lisp way of doing this is by using higher order functions like this: (every #'null my-list) Our parser lacks support for such constructs so I decided to add the new function. This will allow us to escape parts of the policy with checks for the presence of optional collector fields. For example: (if (valid guest.balloon_min guest.balloon_cur) (process_ballooning)) Change-Id: I5aa31886811593860c75cf13bf7743e85fde7358 Related-To: https://bugzilla.redhat.com/1143992 Signed-off-by: Martin Sivak <msi...@redhat.com> --- M mom/Policy/Parser.py M tests/ParserTests.py 2 files changed, 17 insertions(+), 1 deletion(-) git pull ssh://gerrit.ovirt.org:29418/mom refs/changes/91/34791/1 diff --git a/mom/Policy/Parser.py b/mom/Policy/Parser.py index 9ea9f51..c14d10b 100644 --- a/mom/Policy/Parser.py +++ b/mom/Policy/Parser.py @@ -327,7 +327,8 @@ '<<': 'shl', '>>': 'shr', '==': 'eq', '!=': 'neq', 'and': 'and', 'or': 'or', 'not': 'not', - 'min': 'min', 'max': 'max', "null": "null"} + 'min': 'min', 'max': 'max', "null": "null", + "valid": "valid"} def __init__(self): GenericEvaluator.__init__(self) @@ -485,6 +486,12 @@ # some value is not null and not iterable return False + def c_valid(self, *args): + try: + return not any(v is None for v in args) + except TypeError: + return False + def get_code(e, string): try: scanner = Scanner(e.get_operators()) diff --git a/tests/ParserTests.py b/tests/ParserTests.py index 012c015..4c8b9bd 100644 --- a/tests/ParserTests.py +++ b/tests/ParserTests.py @@ -334,5 +334,14 @@ """ self.verify(pol, ["lala"]) + def test_valid(self): + pol = """ + (valid "test" 1 nil "lala") + (valid "test" 1 "lala") + (valid) + (valid nil) + """ + self.verify(pol, [False, True, True, False]) + if __name__ == '__main__': unittest.main() -- To view, visit http://gerrit.ovirt.org/34791 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5aa31886811593860c75cf13bf7743e85fde7358 Gerrit-PatchSet: 1 Gerrit-Project: mom Gerrit-Branch: master Gerrit-Owner: Martin Sivák <msi...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches