Skip to content

Commit

Permalink
Add requires to decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
1maple1 committed Aug 20, 2024
1 parent 81f006f commit e1c1053
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
10 changes: 10 additions & 0 deletions brewtils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def client(
group=None, # type: str
groups=[], # type: Optional[List[str]]
prefix_topic=None, # type: Optional[str]
require=None, # type: str
requires=[], # type: Optional[List[str]]
):
# type: (...) -> Type
"""Class decorator that marks a class as a beer-garden Client
Expand Down Expand Up @@ -70,6 +72,8 @@ def client(
group: Optional plugin group
groups: Optional plugin groups
prefix_topic: Optional prefix for Generated Command to Topic mappings
require: Optional system dependency
requires: Optional system dependencies
Returns:
The decorated class
Expand All @@ -83,6 +87,8 @@ def client(
groups=groups,
group=group,
prefix_topic=prefix_topic,
require=require,
requires=requires,
) # noqa

# Assign these here so linters don't complain
Expand All @@ -92,10 +98,14 @@ def client(
_wrapped._current_request = None
_wrapped._groups = groups
_wrapped._prefix_topic = prefix_topic
_wrapped._requires = requires

if group:
_wrapped._groups.append(group)

if require:
_wrapped._requires.append(require)

return _wrapped


Expand Down
3 changes: 3 additions & 0 deletions brewtils/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ def _set_client(self, new_client):
self._system.prefix_topic = getattr(
new_client, "_prefix_topic", None
) # noqa
if not self._system.requires:
self._system.requires = getattr(new_client, "_requires", []) # noqa
# Now roll up / interpret all metadata to get the Commands
self._system.commands = _parse_client(new_client)

Expand All @@ -326,6 +328,7 @@ def _set_client(self, new_client):
client_clazz._bg_commands = self._system.commands
client_clazz._groups = self._system.groups
client_clazz._prefix_topic = self._system.prefix_topic
client_clazz._requires = self._system.requires
client_clazz._current_request = client_clazz.current_request
except TypeError:
if sys.version_info.major != 2:
Expand Down
40 changes: 40 additions & 0 deletions test/decorators_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,13 +512,15 @@ def foo(self):
assert hasattr(ClientClass, "_current_request")
assert hasattr(ClientClass, "_groups")
assert hasattr(ClientClass, "_prefix_topic")
assert hasattr(ClientClass, "_requires")

def test_with_args(self):
@client(
bg_name="sys",
bg_version="1.0.0",
groups=["GroupA"],
prefix_topic="custom_topic",
requires=["SystemA"],
)
class ClientClass(object):
@command
Expand All @@ -531,11 +533,13 @@ def foo(self):
assert hasattr(ClientClass, "_current_request")
assert hasattr(ClientClass, "_groups")
assert hasattr(ClientClass, "_prefix_topic")
assert hasattr(ClientClass, "_requires")

assert ClientClass._bg_name == "sys"
assert ClientClass._bg_version == "1.0.0"
assert ClientClass._groups == ["GroupA"]
assert ClientClass._prefix_topic == "custom_topic"
assert ClientClass._requires == ["SystemA"]

def test_group(self):
@client(bg_name="sys", bg_version="1.0.0", group="GroupB")
Expand Down Expand Up @@ -571,6 +575,42 @@ def foo(self):
assert ClientClass._bg_version == "1.0.0"
assert ClientClass._groups == ["GroupA", "GroupB"]

def test_require(self):
@client(bg_name="sys", bg_version="1.0.0", require="SystemB")
class ClientClass(object):
@command
def foo(self):
pass

assert hasattr(ClientClass, "_bg_name")
assert hasattr(ClientClass, "_bg_version")
assert hasattr(ClientClass, "_bg_commands")
assert hasattr(ClientClass, "_current_request")
assert hasattr(ClientClass, "_requires")

assert ClientClass._bg_name == "sys"
assert ClientClass._bg_version == "1.0.0"
assert ClientClass._requires == ["SystemB"]

def test_requires_combine(self):
@client(
bg_name="sys", bg_version="1.0.0", requires=["SystemA"], require="SystemB"
)
class ClientClass(object):
@command
def foo(self):
pass

assert hasattr(ClientClass, "_bg_name")
assert hasattr(ClientClass, "_bg_version")
assert hasattr(ClientClass, "_bg_commands")
assert hasattr(ClientClass, "_current_request")
assert hasattr(ClientClass, "_requires")

assert ClientClass._bg_name == "sys"
assert ClientClass._bg_version == "1.0.0"
assert ClientClass._requires == ["SystemA", "SystemB"]


class TestCommand(object):
"""Test command decorator"""
Expand Down

0 comments on commit e1c1053

Please sign in to comment.