Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expanding Lark Support #483

Draft
wants to merge 4 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion brewtils/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
GrammarError = ParseError
LexError = ParseError


choices_grammar = r"""
func: CNAME [func_args]
url: ADDRESS [url_args]
Expand All @@ -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
"""

Expand Down
110 changes: 110 additions & 0 deletions test/decorators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""

Expand Down
Loading