Skip to content

Commit

Permalink
Merge pull request #521 from beer-garden/command_display_name
Browse files Browse the repository at this point in the history
Command display name
  • Loading branch information
1maple1 authored Dec 16, 2024
2 parents aff3105 + 86ac725 commit bec09cd
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Brewtils Changelog
------
TBD

- Added support for display name to command decorator
- Updated Wait Timeout Exception expected HTTP code from 408 to 504
- Dropping Official Python 2.7 Support

Expand Down
5 changes: 5 additions & 0 deletions brewtils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def client(

def command(
_wrapped=None, # type: Union[Callable, MethodType]
display_name=None, # type: Optional[str]
description=None, # type: Optional[str]
parameters=None, # type: Optional[List[Parameter]]
command_type="ACTION", # type: str
Expand Down Expand Up @@ -138,6 +139,7 @@ def echo_json(self, message):
Args:
_wrapped: The function to decorate. This is handled as a positional argument and
shouldn't be explicitly set.
display_name: Command display name to use instead of function name.
description: The command description. If not given the first line of the method
docstring will be used.
parameters: A list of Command parameters. It's recommended to use @parameter
Expand Down Expand Up @@ -192,6 +194,7 @@ def echo_json(self, message):

return functools.partial(
command,
display_name=display_name,
description=description,
parameters=parameters,
command_type=command_type,
Expand All @@ -215,6 +218,7 @@ def echo_json(self, message):
else:
output_type = "STRING"
new_command = Command(
display_name=display_name,
description=description,
parameters=parameters,
command_type=command_type,
Expand Down Expand Up @@ -673,6 +677,7 @@ def _initialize_command(method):
break

cmd.name = _method_name(method)
cmd.display_name = cmd.display_name or _method_name(method)
cmd.description = cmd.description or _method_docstring(method)

try:
Expand Down
7 changes: 7 additions & 0 deletions brewtils/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class Command(BaseModel):
def __init__(
self,
name=None,
display_name=None,
description=None,
parameters=None,
command_type=None,
Expand All @@ -144,6 +145,7 @@ def __init__(
allow_any_kwargs=None,
):
self.name = name
self.display_name = display_name or name
self.description = description
self.parameters = parameters or []
self.command_type = command_type
Expand Down Expand Up @@ -638,6 +640,7 @@ class RequestTemplate(BaseModel):
"instance_name",
"namespace",
"command",
"command_display_name",
"command_type",
"parameters",
"comment",
Expand All @@ -652,6 +655,7 @@ def __init__(
instance_name=None,
namespace=None,
command=None,
command_display_name=None,
command_type=None,
parameters=None,
comment=None,
Expand All @@ -663,6 +667,7 @@ def __init__(
self.instance_name = instance_name
self.namespace = namespace
self.command = command
self.command_display_name = command_display_name or command
self.command_type = command_type
self.parameters = parameters
self.comment = comment
Expand Down Expand Up @@ -709,6 +714,7 @@ def __init__(
instance_name=None,
namespace=None,
command=None,
command_display_name=None,
id=None, # noqa # shadows built-in
is_event=None,
parent=None,
Expand Down Expand Up @@ -736,6 +742,7 @@ def __init__(
instance_name=instance_name,
namespace=namespace,
command=command,
command_display_name=command_display_name,
command_type=command_type,
parameters=parameters,
comment=comment,
Expand Down
2 changes: 2 additions & 0 deletions brewtils/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ class ParameterSchema(BaseSchema):

class CommandSchema(BaseSchema):
name = fields.Str(allow_none=True)
display_name = fields.Str(allow_none=True)
description = fields.Str(allow_none=True)
parameters = fields.Nested("ParameterSchema", many=True)
command_type = fields.Str(allow_none=True)
Expand Down Expand Up @@ -307,6 +308,7 @@ class RequestTemplateSchema(BaseSchema):
instance_name = fields.Str(allow_none=True)
namespace = fields.Str(allow_none=True)
command = fields.Str(allow_none=True)
command_display_name = fields.Str(allow_none=True)
command_type = fields.Str(allow_none=True)
parameters = fields.Dict(allow_none=True)
comment = fields.Str(allow_none=True)
Expand Down
5 changes: 5 additions & 0 deletions brewtils/test/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def command_dict(parameter_dict, system_id):
"""A command represented as a dictionary."""
return {
"name": "speak",
"display_name": "speak_display",
"description": "desc",
"parameters": [parameter_dict],
"command_type": "ACTION",
Expand Down Expand Up @@ -343,6 +344,7 @@ def child_request_dict(ts_epoch):
"instance_name": "default",
"namespace": "ns",
"command": "say",
"command_display_name": "say",
"id": "58542eb571afd47ead90d25f",
"is_event": False,
"parameters": {},
Expand Down Expand Up @@ -383,6 +385,7 @@ def parent_request_dict(ts_epoch):
"instance_name": "default",
"namespace": "ns",
"command": "say",
"command_display_name": "speak",
"id": "58542eb571afd47ead90d25d",
"is_event": False,
"parent": None,
Expand Down Expand Up @@ -424,6 +427,7 @@ def request_template_dict():
"instance_name": "default",
"namespace": "ns",
"command": "speak",
"command_display_name": "speak",
"command_type": "ACTION",
"parameters": {"message": "hey!"},
"comment": "hi!",
Expand All @@ -447,6 +451,7 @@ def request_dict(parent_request_dict, child_request_dict, ts_epoch):
"instance_name": "default",
"namespace": "ns",
"command": "speak",
"command_display_name": "speak",
"id": "58542eb571afd47ead90d25e",
"is_event": False,
"parent": parent_request_dict,
Expand Down
21 changes: 21 additions & 0 deletions test/decorators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,10 @@ class TestCommand(object):
def test_basic(self, command_dict, bg_command):
# Removing things that need to be initialized
bg_command.name = None
bg_command.display_name = None
bg_command.parameters = []
del command_dict["name"]
del command_dict["display_name"]
del command_dict["parameters"]
del command_dict["topics"]

Expand Down Expand Up @@ -688,6 +690,13 @@ def cmd3(foo):
assert cmd2._command.output_type == "STRING"
assert cmd3._command.output_type == "STRING"

def test_display_name(self):
@command(display_name="foo_test")
def cmd1(foo):
return foo

assert cmd1._command.display_name == "foo_test"


class TestParameter(object):
"""Test parameter decorator
Expand Down Expand Up @@ -974,6 +983,18 @@ def _cmd(_):

assert _initialize_command(_cmd).description == new_description

def test_display_name(self):
@command(display_name="foo_test")
def _cmd(_):
pass

assert hasattr(_cmd, "_command")

_cmd = _initialize_command(_cmd)

assert _cmd.name == "_cmd"
assert _cmd.display_name == "foo_test"


class TestMethodName(object):
def test_name(self, cmd):
Expand Down

0 comments on commit bec09cd

Please sign in to comment.