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

Fix Broker's handling of argument assignment #303

Merged
merged 1 commit into from
Aug 6, 2024
Merged
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
20 changes: 4 additions & 16 deletions broker/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,7 @@ def checkout(ctx, background, nick, count, args_file, provider_labels, **kwargs)
if provider_labels:
broker_args["provider_labels"] = parse_labels(provider_labels)

# if additional arguments were passed, include them in the broker args
# strip leading -- characters
broker_args.update(
{
(key[2:] if key.startswith("--") else key): val
for key, val in zip(ctx.args[::2], ctx.args[1::2])
}
)
broker_args.update(helpers.kwargs_from_click_ctx(ctx))
if background:
helpers.fork_broker()
Broker(**broker_args).checkout()
Expand Down Expand Up @@ -375,14 +368,9 @@ def execute(ctx, background, nick, output_format, artifacts, args_file, provider
broker_args["args_file"] = args_file
if provider_labels:
broker_args["provider_labels"] = parse_labels(provider_labels)
# if additional arguments were passed, include them in the broker args
# strip leading -- characters
broker_args.update(
{
(key[2:] if key.startswith("--") else key): val
for key, val in zip(ctx.args[::2], ctx.args[1::2])
}
)

broker_args.update(helpers.kwargs_from_click_ctx(ctx))

if background:
helpers.fork_broker()
result = Broker(**broker_args).execute()
Expand Down
19 changes: 19 additions & 0 deletions broker/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Miscellaneous helpers live here."""

import collections
from collections import UserDict, namedtuple
from collections.abc import MutableMapping
Expand Down Expand Up @@ -267,6 +268,24 @@ def yaml_format(in_struct):
return yaml.dump(in_struct, default_flow_style=False, sort_keys=False)


def kwargs_from_click_ctx(ctx):
"""Convert a Click context object to a dictionary of keyword arguments."""
# if users use `=` to note arg=value assignment, then we need to split it
_args = []
for arg in ctx.args:
if "=" in arg:
_args.extend(arg.split("="))
else:
_args.append(arg)
ctx.args = _args
# if additional arguments were passed, include them in the broker args
# strip leading -- characters
return {
(key[2:] if key.startswith("--") else key): val
for key, val in zip(ctx.args[::2], ctx.args[1::2])
}


class Emitter:
"""Class that provides a simple interface to emit messages to a json-formatted file.

Expand Down
9 changes: 9 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,12 @@ def test_dict_from_paths_nested():
paths = {"person_name": "person/name", "person_zip": "person/address/zip"}
result = helpers.dict_from_paths(source_dict, paths)
assert result == {"person_name": "John", "person_zip": "12345"}


def test_kwargs_from_click_ctx():
"""Test that we can extract kwargs from a mixed-style click context object"""
class ctx:
args = ["--arg1", "value1", "--arg2=value2", "--some-flag"]

kwargs = helpers.kwargs_from_click_ctx(ctx)
assert kwargs == {"arg1": "value1", "arg2": "value2"}