Skip to content

Commit

Permalink
Fix Broker's handling of argument assignment
Browse files Browse the repository at this point in the history
Ian noticed an issue had when specifying argument values in broker's cli,
specifically when using an {{=}} assignment instead of space-delimited.
This is likely an issue with the way click bundles arguments in its ctx.args.
  • Loading branch information
JacobCallahan committed Aug 6, 2024
1 parent 484dc0a commit 35ad5c6
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
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"}

0 comments on commit 35ad5c6

Please sign in to comment.