diff --git a/utilities/src/help/task_parameters.py b/utilities/src/help/task_parameters.py index 2722095c..2186cac8 100644 --- a/utilities/src/help/task_parameters.py +++ b/utilities/src/help/task_parameters.py @@ -1,7 +1,7 @@ import sys import argparse import logging -from typing import Dict, Optional, List, Set, Tuple, Any +from typing import Dict, Optional, List, Set, Tuple, Any, Callable from typing_extensions import TypedDict import pprint @@ -64,13 +64,19 @@ class ModelSchema(TypedDict): ) -def _format_parameter_row(param: str, param_description: PropertyDict) -> str: +def _format_parameter_row( + param: str, + param_description: PropertyDict, + validators: Optional[List[Callable]] = None, +) -> str: """Take a property dictionary for a parameter and format it for printing.""" typeinfo: str if "type" in param_description: typeinfo = param_description["type"] elif "anyOf" in param_description: # anyOf is present instead typeinfo = " | ".join(_["type"] for _ in param_description["anyOf"]) + elif "allOf" in param_description and "$ref" in param_description["allOf"][0]: + typeinfo = param_description["allOf"][0]["$ref"].split("/")[-1] else: typeinfo = "No type information" typeinfo = f"({typeinfo})" @@ -79,6 +85,8 @@ def _format_parameter_row(param: str, param_description: PropertyDict) -> str: default: str if "default" in param_description: default = param_description["default"] + if default == "": + default = " - May be populated by validator" msg = f"{msg} - Default: {default}" description: str @@ -87,7 +95,12 @@ def _format_parameter_row(param: str, param_description: PropertyDict) -> str: else: description = "Unknown description." - msg = f"{msg}\n\t{description}\n\n" + msg = f"{msg}\n\t{description}" + if validators is not None: + msg = f"{msg}\n\tValidators:" + for validator in validators: + msg = f"{msg}\n\t\t- {validator.func.__name__}" + msg = f"{msg}\n\n" return msg @@ -146,7 +159,6 @@ def _format_parameter_row(param: str, param_description: PropertyDict) -> str: # For types need to check for key `type` or a list of dicts `anyOf=[{'type': ...}, {'type': ...}]` parameter_schema: ModelSchema = parameter_model.schema() - if args.full_schema: pprint.pprint(parameter_schema) sys.exit(0) @@ -162,16 +174,45 @@ def _format_parameter_row(param: str, param_description: PropertyDict) -> str: for param in parameter_schema["required"] ] + validators: Optional[List[Callable]] = None out_msg: str = f"{task_name}\n{'-'*len(task_name)}\n" out_msg = f"{out_msg}{task_description}\n\n\n" if required_parameters is not None: out_msg = f"{out_msg}Required Parameters:\n--------------------\n" for param in required_parameters: - out_msg = f"{out_msg}{_format_parameter_row(param[0], param[1])}" + validators = ( + parameter_model.__validators__[param[0]] + if param[0] in parameter_model.__validators__ + else None + ) + out_msg = ( + f"{out_msg}{_format_parameter_row(param[0], param[1], validators)}" + ) out_msg = f"{out_msg}\n\n" - out_msg = f"{out_msg}All Parameters:\n-------------\n" + out_msg = f"{out_msg}All Parameters:\n---------------\n" for param in parameter_schema["properties"]: - out_msg = f"{out_msg}{_format_parameter_row(param, parameter_schema['properties'][param])}" - + validators = ( + parameter_model.__validators__[param] + if param in parameter_model.__validators__ + else None + ) + out_msg = f"{out_msg}{_format_parameter_row(param, parameter_schema['properties'][param], validators)}" + + if "definitions" in parameter_schema and parameter_schema["definitions"]: + definitions: List[str] = [ + defn + for defn in parameter_schema["definitions"] + if defn not in ("AnalysisHeader", "TemplateConfig") + ] + if len(definitions) > 0: + out_msg = f"{out_msg}Template Parameters:\n--------------------\n" + for defn in definitions: + out_msg = f"{out_msg}{defn}:\n" + for param in parameter_schema["definitions"][defn]["properties"]: + row: str = _format_parameter_row( + param, + parameter_schema["definitions"][defn]["properties"][param], + ) + out_msg = f"{out_msg}{row}" print(out_msg)