Skip to content

Commit

Permalink
Merge pull request #52 from gadorlhiac/ENH/template_param_help
Browse files Browse the repository at this point in the history
ENH Template parameter help
  • Loading branch information
valmar authored Oct 23, 2024
2 parents d0c7aa7 + f34c3f7 commit 5d275f7
Showing 1 changed file with 49 additions and 8 deletions.
57 changes: 49 additions & 8 deletions utilities/src/help/task_parameters.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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})"
Expand All @@ -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 = "<Empty String> - May be populated by validator"
msg = f"{msg} - Default: {default}"

description: str
Expand All @@ -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


Expand Down Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit 5d275f7

Please sign in to comment.