Skip to content

Commit

Permalink
fixed clickify_parameters to load a file on the fly
Browse files Browse the repository at this point in the history
  • Loading branch information
o-smirnov committed Sep 13, 2023
1 parent 72aab9e commit 7f8bcb1
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions scabha/schema_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import re
import click
from scabha.exceptions import SchemaError
from .cargo import Parameter
from .cargo import Parameter, UNSET, _UNSET_DEFAULT
from .basetypes import EmptyDictDefault
from typing import *
from .cargo import UNSET
from dataclasses import make_dataclass, field
from dataclasses import dataclass, make_dataclass, field
from omegaconf import OmegaConf, MISSING
from collections.abc import MutableSet, MutableSequence, MutableMapping

Expand Down Expand Up @@ -129,7 +129,17 @@ def _validate_list(text: str, element_type, schema):
except ValueError:
raise click.BadParameter(f"can't convert to '{schema.dtype}'")

def clickify_parameters(schemas: Dict[str, Any]):
@dataclass
class Schema(object):
inputs: Dict[str, Parameter] = EmptyDictDefault()
outputs: Dict[str, Parameter] = EmptyDictDefault()


def clickify_parameters(schemas: Union[str, Dict[str, Any]]):

if type(schemas) is str:
schemas = OmegaConf.merge(OmegaConf.structured(Schema),
OmegaConf.load(schemas))

decorator_chain = None
for io in schemas.inputs, schemas.outputs:
Expand All @@ -139,7 +149,6 @@ def clickify_parameters(schemas: Dict[str, Any]):
optname = f"--{name}"
dtype = schema.dtype
validator = None
default_value = schema.default

# sort out option type. Atomic type?
if dtype in _atomic_types:
Expand Down Expand Up @@ -169,14 +178,24 @@ def clickify_parameters(schemas: Dict[str, Any]):
if schema.abbreviation:
optnames.append(f"-{schema.abbreviation}")

if schema.default is UNSET:
deco = click.option(*optnames, type=dtype, callback=validator,
required=schema.required,
metavar=schema.metavar, help=schema.info)
if schema.policies.positional:
if schema.default in (UNSET, _UNSET_DEFAULT):
deco = click.argument(name, type=dtype, callback=validator,
required=schema.required,
metavar=schema.metavar)
else:
deco = click.argument(name, type=dtype, callback=validator,
default=schema.default, required=schema.required,
metavar=schema.metavar)
else:
deco = click.option(*optnames, type=dtype, callback=validator,
default=schema.default, required=schema.required,
metavar=schema.metavar, help=schema.info)
if schema.default in (UNSET, _UNSET_DEFAULT):
deco = click.option(*optnames, type=dtype, callback=validator,
required=schema.required,
metavar=schema.metavar, help=schema.info)
else:
deco = click.option(*optnames, type=dtype, callback=validator,
default=schema.default, required=schema.required,
metavar=schema.metavar, help=schema.info)

if decorator_chain is None:
decorator_chain = deco
Expand Down

0 comments on commit 7f8bcb1

Please sign in to comment.