From 6f26e283f330f8f775440ce4ead084dab6c8330c Mon Sep 17 00:00:00 2001 From: Oleg Smirnov Date: Wed, 10 Apr 2024 04:24:46 +0200 Subject: [PATCH 1/2] fixes #273. Adds schema flattening to clickify_parameters --- scabha/cargo.py | 6 ++++++ scabha/schema_utils.py | 13 ++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/scabha/cargo.py b/scabha/cargo.py index f0157f4d..ba24debe 100644 --- a/scabha/cargo.py +++ b/scabha/cargo.py @@ -88,6 +88,7 @@ class ParameterPolicies(object): pass_missing_as_none: Optional[bool] = None + # This is used to classify parameters, for cosmetic and help purposes. # Usually set automatically based on whether a parameter is required, whether a default is provided, etc. ParameterCategory = IntEnum("ParameterCategory", @@ -161,6 +162,11 @@ class Parameter(object): # arbitrary metadata associated with parameter metadata: Dict[str, Any] = EmptyDictDefault() + + # If True, when constructing a CLI from the schema, omit the default value (if any). + # Useful when the tool constructs itw own default values. + suppress_cli_default: bool = False + def __post_init__(self): diff --git a/scabha/schema_utils.py b/scabha/schema_utils.py index 023a0ed0..47cb8b52 100644 --- a/scabha/schema_utils.py +++ b/scabha/schema_utils.py @@ -2,13 +2,14 @@ import re import click from scabha.exceptions import SchemaError -from .cargo import Parameter, UNSET, _UNSET_DEFAULT +from .cargo import Parameter, UNSET, _UNSET_DEFAULT, Cargo from typing import List, Union, Optional, Callable, Dict, DefaultDict, Any from .basetypes import EmptyDictDefault, File, is_file_type from dataclasses import dataclass, make_dataclass, field from omegaconf import OmegaConf, MISSING from collections.abc import MutableSet, MutableSequence, MutableMapping from scabha import configuratt +from collections import OrderedDict def schema_to_dataclass(io: Dict[str, Parameter], class_name: str, bases=(), post_init: Optional[Callable] =None): """ @@ -149,13 +150,15 @@ def clickify_parameters(schemas: Union[str, Dict[str, Any]]): OmegaConf.load(schemas)) decorator_chain = None - for io in schemas.inputs, schemas.outputs: + inputs = Cargo.flatten_schemas(OrderedDict(), schemas.inputs, "inputs") + outputs = Cargo.flatten_schemas(OrderedDict(), schemas.outputs, "outputs") + for io in inputs, outputs: for name, schema in io.items(): # skip outputs, unless they're named outputs if io is schemas.outputs and not (schema.is_file_type and not schema.implicit): continue - name = name.replace("_", "-") + name = name.replace("_", "-").replace(".", "-") optname = f"--{name}" dtype = schema.dtype validator = None @@ -189,7 +192,7 @@ def clickify_parameters(schemas: Union[str, Dict[str, Any]]): optnames.append(f"-{schema.abbreviation}") if schema.policies.positional: - if schema.default in (UNSET, _UNSET_DEFAULT): + if schema.default in (UNSET, _UNSET_DEFAULT) or schema.suppress_cli_default: deco = click.argument(name, type=dtype, callback=validator, required=schema.required, metavar=schema.metavar) @@ -198,7 +201,7 @@ def clickify_parameters(schemas: Union[str, Dict[str, Any]]): default=schema.default, required=schema.required, metavar=schema.metavar) else: - if schema.default in (UNSET, _UNSET_DEFAULT): + if schema.default in (UNSET, _UNSET_DEFAULT) or schema.suppress_cli_default: deco = click.option(*optnames, type=dtype, callback=validator, required=schema.required, metavar=schema.metavar, help=schema.info) From 866e0183e1d3de94341497737f99d17ee3ba2fac Mon Sep 17 00:00:00 2001 From: Oleg Smirnov Date: Wed, 10 Apr 2024 05:11:17 +0200 Subject: [PATCH 2/2] impose schema structure on clickify_parameters input --- scabha/schema_utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scabha/schema_utils.py b/scabha/schema_utils.py index 47cb8b52..1b792bad 100644 --- a/scabha/schema_utils.py +++ b/scabha/schema_utils.py @@ -148,6 +148,9 @@ def clickify_parameters(schemas: Union[str, Dict[str, Any]]): if type(schemas) is str: schemas = OmegaConf.merge(OmegaConf.structured(Schema), OmegaConf.load(schemas)) + else: + schemas = OmegaConf.merge(OmegaConf.structured(Schema), + dict(inputs=schemas.inputs, outputs=schemas.outputs)) decorator_chain = None inputs = Cargo.flatten_schemas(OrderedDict(), schemas.inputs, "inputs")