From fe6d140952dc48af5660dfc3ccc75205a3de20a7 Mon Sep 17 00:00:00 2001 From: 1maple1 <160027655+1maple1@users.noreply.github.com> Date: Wed, 27 Nov 2024 13:21:15 +0000 Subject: [PATCH 1/3] Command display name --- brewtils/decorators.py | 5 +++++ brewtils/models.py | 2 ++ brewtils/schemas.py | 1 + brewtils/test/fixtures.py | 1 + test/decorators_test.py | 21 +++++++++++++++++++++ 5 files changed, 30 insertions(+) diff --git a/brewtils/decorators.py b/brewtils/decorators.py index dde4da9a..a395893f 100644 --- a/brewtils/decorators.py +++ b/brewtils/decorators.py @@ -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 @@ -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 @@ -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, @@ -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, @@ -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: diff --git a/brewtils/models.py b/brewtils/models.py index d0b967fa..8f307bfc 100644 --- a/brewtils/models.py +++ b/brewtils/models.py @@ -129,6 +129,7 @@ class Command(BaseModel): def __init__( self, name=None, + display_name=None, description=None, parameters=None, command_type=None, @@ -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 diff --git a/brewtils/schemas.py b/brewtils/schemas.py index ccaeee87..b62c4cae 100644 --- a/brewtils/schemas.py +++ b/brewtils/schemas.py @@ -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) diff --git a/brewtils/test/fixtures.py b/brewtils/test/fixtures.py index 645fc4fe..c48b990e 100644 --- a/brewtils/test/fixtures.py +++ b/brewtils/test/fixtures.py @@ -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", diff --git a/test/decorators_test.py b/test/decorators_test.py index 33c4a7b3..69d6c2f5 100644 --- a/test/decorators_test.py +++ b/test/decorators_test.py @@ -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"] @@ -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 @@ -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): From b137dccaf64017598330ecd32631288518ea7fef Mon Sep 17 00:00:00 2001 From: 1maple1 <160027655+1maple1@users.noreply.github.com> Date: Wed, 27 Nov 2024 13:25:11 +0000 Subject: [PATCH 2/3] Changelog --- CHANGELOG.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 933d4d88..c5b28d18 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,12 @@ Brewtils Changelog ================== +TBD +------ +TBD + +- Added support for display name to command decorator + 3.29.0 ------ 11/25/24 From 31633d6b9ccdf040768b50f1508ff1325f4619eb Mon Sep 17 00:00:00 2001 From: 1maple1 <160027655+1maple1@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:32:40 +0000 Subject: [PATCH 3/3] Adding command_display_name to Request and RequestTemplate --- brewtils/models.py | 5 +++++ brewtils/schemas.py | 1 + brewtils/test/fixtures.py | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/brewtils/models.py b/brewtils/models.py index 8f307bfc..635634e4 100644 --- a/brewtils/models.py +++ b/brewtils/models.py @@ -640,6 +640,7 @@ class RequestTemplate(BaseModel): "instance_name", "namespace", "command", + "command_display_name", "command_type", "parameters", "comment", @@ -654,6 +655,7 @@ def __init__( instance_name=None, namespace=None, command=None, + command_display_name=None, command_type=None, parameters=None, comment=None, @@ -665,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 @@ -711,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, @@ -738,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, diff --git a/brewtils/schemas.py b/brewtils/schemas.py index b62c4cae..fe3828f5 100644 --- a/brewtils/schemas.py +++ b/brewtils/schemas.py @@ -308,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) diff --git a/brewtils/test/fixtures.py b/brewtils/test/fixtures.py index c48b990e..321e393a 100644 --- a/brewtils/test/fixtures.py +++ b/brewtils/test/fixtures.py @@ -344,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": {}, @@ -384,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, @@ -425,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!", @@ -448,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,