Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #273. Adds schema flattening to clickify_parameters #274

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 11 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 @@ -147,15 +148,20 @@ 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
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 +195,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 +204,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
Loading