Skip to content

Commit

Permalink
helpers: Allow returning of multiple messages to spread among publishers
Browse files Browse the repository at this point in the history
  • Loading branch information
jara001 committed Jul 10, 2024
1 parent e607295 commit 837e7c7
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions autopsy/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,29 @@ def publish(cls, *args, **kwargs):

msg = func(cls, *args, **kwargs)

for pub in pubs:
pub.publish(
msg
# Only one message is returned; send it to every publisher.
if not isinstance(msg, list):
for pub in pubs:
pub.publish(msg)

# The returned structure is a list.
# But raise an exception if the number of returned messages
# does not correspond to the number of the publishers.
# Note: List with one message is allowed everytime.
elif len(msg) != len(pubs) and len(msg) > 1:
raise ValueError(
"Unable to publish messages using '%s()' as the number "
"or returned messages is '%d' while number of registered "
"publishers is '%d'."
% (func.__name__, len(msg), len(pubs))
)

else:
for i, pub in enumerate(pubs):
pub.publish(
msg[i % len(msg)]
)

# Initialize decorated function, prepare it for being a publisher.
if not getattr(func, "_is_publisher", False):
publish._is_publisher = True
Expand Down

0 comments on commit 837e7c7

Please sign in to comment.