Skip to content

Commit

Permalink
fix(configure.py): lifecycle/configure.py's defaults/arguments functi…
Browse files Browse the repository at this point in the history
…ons, does not work for @click.argument types

* Remove usage of configure.defaults and modify configure.arguments to consider all cases

* Check for is_flag with @click.options

* Add check if Click parameter is a list to split it into a string
  • Loading branch information
chrisfandrade16 authored Sep 13, 2024
1 parent 5105486 commit 196d8fe
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
41 changes: 32 additions & 9 deletions workflow/lifecycle/configure.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def loki(logger: Logger, config: Dict[str, Any]) -> bool:
return status


def arguments(arguments: Optional[Dict[str, Any]]) -> List[str]:
def arguments(func: Callable[..., Any], work: Work) -> List[str]:
"""Return a list of CLI arguments.
Args:
Expand All @@ -101,14 +101,37 @@ def arguments(arguments: Optional[Dict[str, Any]]) -> List[str]:
List[str]: List of CLI arguments
"""
args: List[str] = []
if arguments:
for key, value in arguments.items():
if not value:
args.append(f"{key}")
elif value and len(key) == 2:
args.append(f"{key} {value}")
elif value and len(key) > 2:
args.append(f"{key}={value}")

for parameter in func.params:
parameter_name_in_cli = parameter.opts[-1]
paraneter_name_in_function = parameter.name
parameter_value_in_work = work.parameters.get(paraneter_name_in_function, None)

if parameter_value_in_work:
if isinstance(parameter_value_in_work, list):
parameter_value_in_work = " ".join(parameter_value_in_work)

if isinstance(parameter, click.Argument):
# If argument, then the parameter is purely positional without a key
args.append(f"{parameter_value_in_work}")
elif isinstance(parameter, click.Option):
if hasattr(parameter, "is_flag") and parameter.is_flag:
# If is_flag=True, then the parameter is a flag and doesn't have a value
args.append(f"{parameter_name_in_cli}")
else:
if len(parameter_name_in_cli) == 2:
# Short options (often denoted by a single hyphen and a single letter like -r)
# are commonly written with a space between the option and its value
args.append(
f"{parameter_name_in_cli} {parameter_value_in_work}"
)
elif len(parameter_name_in_cli) > 2:
# Long options (often denoted by two hyphens and a word like --recursive)
# are commonly written with an equals sign between the option and its value
args.append(
f"{parameter_name_in_cli}={parameter_value_in_work}"
)

return args


Expand Down
5 changes: 1 addition & 4 deletions workflow/lifecycle/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ def function(work: Work) -> Work:
assert isinstance(work.function, str), "missing function to execute"
func: Callable[..., Any] = validate.function(work.function)
arguments: List[str] = []
# Configure default values for the function
work = configure.defaults(func, work)
# Paramters to execute the function with
parameters: Dict[str, Any] = work.parameters or {}

if isinstance(func, click.Command):
arguments = configure.arguments(parameters)
arguments = configure.arguments(func, work)
logger.info(
f"executing: {func.name}.main(args={arguments}, standalone_mode=False)"
)
Expand Down

0 comments on commit 196d8fe

Please sign in to comment.