Skip to content

Commit

Permalink
fixes #273. Adds schema flattening to clickify_parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
o-smirnov committed Apr 10, 2024
1 parent 7a28ba5 commit 6f26e28
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 6 additions & 0 deletions scabha/cargo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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):
Expand Down
13 changes: 8 additions & 5 deletions scabha/schema_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 6f26e28

Please sign in to comment.