diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e2dcc9f5..b328d896 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,12 @@ Brewtils Changelog ================== +3.26.2 +------ +TBD + +- Expanded Lark parsing to support static String values provided for command choices + 3.26.1 ------ 5/24/2024 diff --git a/brewtils/choices.py b/brewtils/choices.py index de297692..9a954cd7 100644 --- a/brewtils/choices.py +++ b/brewtils/choices.py @@ -20,7 +20,6 @@ GrammarError = ParseError LexError = ParseError - choices_grammar = r""" func: CNAME [func_args] url: ADDRESS [url_args] @@ -31,11 +30,13 @@ arg_pair: CNAME "=" ref ?ref: "${" CNAME "}" + | ESCAPED_STRING ADDRESS: /^http[^\?]*/ %import common.CNAME %import common.WS + %import common.ESCAPED_STRING %ignore WS """ diff --git a/test/decorators_test.py b/test/decorators_test.py index be417781..2841344c 100644 --- a/test/decorators_test.py +++ b/test/decorators_test.py @@ -762,6 +762,116 @@ def func(foo): return foo +class TestDynamicParameters(object): + + def test_command_static_choices(self): + + STATIC_CHOICES = ["a", "b", "c"] + + @parameter(key="key", type="String", choices=STATIC_CHOICES) + def func1(key): + return key + + cmd1 = _parse_method(func1) + + assert cmd1.parameters[0].choices.value == STATIC_CHOICES + assert cmd1.parameters[0].choices.type == "static" + + def test_command_url_choices(self): + + CHOICES_URL = "http://example.com/api/choices.json" + + @parameter( + key="key", + type="String", + choices={"type": "url", "value": CHOICES_URL}, + ) + def func1(key): + return key + + cmd1 = _parse_method(func1) + + assert cmd1.parameters[0].choices.value == CHOICES_URL + assert cmd1.parameters[0].choices.type == "url" + + def test_command_command_choices(self): + + @parameter( + key="key", + type="String", + choices={"type": "command", "value": "get_choices"}, + ) + def func1(key): + return key + + cmd1 = _parse_method(func1) + + assert cmd1.parameters[0].choices.value == "get_choices" + assert cmd1.parameters[0].choices.type == "command" + + def test_command_reference_input(self): + + STATIC_CHOICES = ["a", "b", "c"] + + @parameter(key="index", type="String", choices=STATIC_CHOICES, default="a") + @parameter( + key="key", + type="String", + optional=False, + choices={ + "type": "command", + "display": "select", + "strict": True, + "value": "get_choices_with_argument(key=${index})", + }, + ) + def func1(index, key): + return {index: key} + + cmd1 = _parse_method(func1) + + assert cmd1.parameters[0].choices.value == STATIC_CHOICES + assert cmd1.parameters[0].choices.type == "static" + + assert ( + cmd1.parameters[1].choices.value + == "get_choices_with_argument(key=${index})" + ) + assert cmd1.parameters[1].choices.type == "command" + + def test_command_reference_and_static_inputs(self): + + STATIC_CHOICES = ["a", "b", "c"] + + @parameter(key="index", type="String", choices=STATIC_CHOICES, default="a") + @parameter( + key="key", + type="String", + optional=False, + choices={ + "type": "command", + "display": "select", + "strict": True, + "value": 'get_choices_with_argument(key=${index}, value="foo")', + }, + ) + def func1(index, key): + return {index: key} + + cmd1 = _parse_method(func1) + + assert cmd1.parameters[0].choices.value == STATIC_CHOICES + assert cmd1.parameters[0].choices.type == "static" + + assert ( + cmd1.parameters[1].choices.value + == 'get_choices_with_argument(key=${index}, value="foo")' + ) + assert cmd1.parameters[1].choices.type == "command" + assert cmd1.parameters[1].choices.details["args"][1] == ("value", '"foo"') + assert cmd1.parameters[1].choices.details["args"][0] == ("key", "index") + + class TestParseMethod(object): """Test the various ways of marking a method as a Command"""