From 6b6b294c5f44d962bc639e955a1ccd71263656ab Mon Sep 17 00:00:00 2001 From: gadorlhiac Date: Mon, 16 Sep 2024 13:57:56 -0700 Subject: [PATCH 1/4] SKL Add type-checked template parameters to lute_help --- utilities/src/help/task_parameters.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/utilities/src/help/task_parameters.py b/utilities/src/help/task_parameters.py index 2722095c..6bee0359 100644 --- a/utilities/src/help/task_parameters.py +++ b/utilities/src/help/task_parameters.py @@ -146,7 +146,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) @@ -170,8 +169,23 @@ def _format_parameter_row(param: str, param_description: PropertyDict) -> str: out_msg = f"{out_msg}{_format_parameter_row(param[0], param[1])}" 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])}" + 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: + 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) From cde7359168794837be4cce0f83b36d18371ec301 Mon Sep 17 00:00:00 2001 From: gadorlhiac Date: Mon, 16 Sep 2024 14:32:37 -0700 Subject: [PATCH 2/4] MNT Add the template parameter class as type --- utilities/src/help/task_parameters.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/utilities/src/help/task_parameters.py b/utilities/src/help/task_parameters.py index 6bee0359..eb1451a5 100644 --- a/utilities/src/help/task_parameters.py +++ b/utilities/src/help/task_parameters.py @@ -71,6 +71,9 @@ def _format_parameter_row(param: str, param_description: PropertyDict) -> str: 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] + typeinfo = f"{typeinfo}" else: typeinfo = "No type information" typeinfo = f"({typeinfo})" @@ -182,6 +185,7 @@ def _format_parameter_row(param: str, param_description: PropertyDict) -> str: 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, From ed8dcad11088ec1b30974f0ddf11f864f9535bd9 Mon Sep 17 00:00:00 2001 From: gadorlhiac Date: Mon, 16 Sep 2024 15:31:21 -0700 Subject: [PATCH 3/4] MNT Add a message when default param is empty string. --- utilities/src/help/task_parameters.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/utilities/src/help/task_parameters.py b/utilities/src/help/task_parameters.py index eb1451a5..48c42c66 100644 --- a/utilities/src/help/task_parameters.py +++ b/utilities/src/help/task_parameters.py @@ -82,6 +82,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 From f34c3f7da77e25367988d95d2d7080055e9795b6 Mon Sep 17 00:00:00 2001 From: gadorlhiac Date: Tue, 17 Sep 2024 11:47:26 -0700 Subject: [PATCH 4/4] MNT Add validator list to lute_help task output --- utilities/src/help/task_parameters.py | 33 ++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/utilities/src/help/task_parameters.py b/utilities/src/help/task_parameters.py index 48c42c66..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,7 +64,11 @@ 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: @@ -73,7 +77,6 @@ def _format_parameter_row(param: str, param_description: PropertyDict) -> str: 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] - typeinfo = f"{typeinfo}" else: typeinfo = "No type information" typeinfo = f"({typeinfo})" @@ -92,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 @@ -166,17 +174,30 @@ 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" 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] = [