Aller au contenu

confit.cli

Cli

Bases: Typer

Custom Typer object that:

  • validates a command parameters before executing it
  • accepts a configuration file describing the parameters
  • automatically instantiates parameters given a dictionary when type hinted
Source code in confit/cli.py
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
class Cli(Typer):
    """
    Custom Typer object that:

    - validates a command parameters before executing it
    - accepts a configuration file describing the parameters
    - automatically instantiates parameters given a dictionary when type hinted
    """

    def command(  # noqa
        self,
        name,
        *,
        cls: Optional[Type[TyperCommand]] = None,
        context_settings: Optional[Dict[Any, Any]] = None,
        help: Optional[str] = None,
        epilog: Optional[str] = None,
        short_help: Optional[str] = None,
        options_metavar: str = "[OPTIONS]",
        add_help_option: bool = True,
        no_args_is_help: bool = False,
        hidden: bool = False,
        deprecated: bool = False,
        # Rich settings
        rich_help_panel: Union[str, None] = Default(None),
        registry: Any = None,
    ) -> Callable[[CommandFunctionType], CommandFunctionType]:
        typer_command = super().command(
            name=name,
            cls=cls,
            help=help,
            epilog=epilog,
            short_help=short_help,
            options_metavar=options_metavar,
            add_help_option=add_help_option,
            no_args_is_help=no_args_is_help,
            hidden=hidden,
            deprecated=deprecated,
            rich_help_panel=rich_help_panel,
            context_settings={
                **(context_settings or {}),
                "ignore_unknown_options": True,
                "allow_extra_args": True,
            },
        )

        def wrapper(fn):
            validated = validate_arguments(fn)

            @typer_command
            def command(ctx: Context, config: Optional[List[Path]] = None):
                config_path = config

                has_meta = _fn_has_meta(fn)
                if config_path:
                    config, name_from_file = merge_from_disk(config_path)
                else:
                    config = Config({name: {}})
                model_fields = (
                    validated.model.model_fields
                    if hasattr(validated.model, "model_fields")
                    else validated.model.__fields__
                )
                for k, v in parse_overrides(ctx.args).items():
                    if "." not in k:
                        parts = (name, k)
                    else:
                        parts = k.split(".")
                        if parts[0] in model_fields and parts[0] not in config:
                            parts = (name, *parts)
                    current = config
                    if parts[0] not in current:
                        raise Exception(
                            f"{k} does not match any existing section in config"
                        )
                    for part in parts[:-1]:
                        current = current.setdefault(part, Config())
                    current[parts[-1]] = v
                try:
                    default_seed = model_fields.get("seed")
                    if default_seed is not None:
                        default_seed = default_seed.get_default()
                    seed = Config.resolve(
                        config.get(name, {}).get("seed", default_seed),
                        registry=registry,
                        root=config,
                    )
                    if seed is not None:
                        set_seed(seed)
                    resolved_config = Config(config[name]).resolve(
                        registry=registry, root=config
                    )
                    if has_meta:
                        config_meta = dict(
                            config_path=config_path,
                            resolved_config=resolved_config,
                            unresolved_config=config,
                        )
                        return validated(
                            **resolved_config,
                            config_meta=config_meta,
                        )
                    else:
                        return validated(**resolved_config)
                except (LegacyValidationError, ConfitValidationError) as e:
                    e.raw_errors = patch_errors(
                        e.raw_errors,
                        (name,),
                    )
                    if is_debug() or e.__cause__ is not None:
                        raise e
                    try:
                        import rich

                        console = rich.console.Console(stderr=True)
                        console.print("Validation error:", style="red", end=" ")
                        console.print(str(e))
                    except ImportError:  # pragma: no cover
                        print("Validation error:", file=sys.stderr, end=" ")
                        print(str(e), file=sys.stderr)
                    sys.exit(1)
                except KeyboardInterrupt as e:  # pragma: no cover
                    raise Exception("Interrupted by user") from e

            return validated

        return wrapper

parse_overrides(args)

Parse the overrides from the command line into a dictionary of key-value pairs.

PARAMETER DESCRIPTION
args

The arguments to parse

TYPE: List[str]

RETURNS DESCRIPTION
Dict[str, Any]

The parsed overrides as a dictionary

Source code in confit/cli.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def parse_overrides(args: List[str]) -> Dict[str, Any]:
    """
    Parse the overrides from the command line into a dictionary
    of key-value pairs.

    Parameters
    ----------
    args: List[str]
        The arguments to parse

    Returns
    -------
    Dict[str, Any]
        The parsed overrides as a dictionary
    """
    result = {}
    while args:
        opt = args.pop(0)
        err = f"Invalid config override '{opt}'"
        if opt.startswith("--"):  # new argument
            opt = opt.replace("--", "")
            if "=" in opt:  # we have --opt=value
                opt, value = opt.split("=", 1)
            else:
                if not args or args[0].startswith("--"):  # flag with no value
                    value = "true"
                else:
                    value = args.pop(0)
            opt = opt.replace("-", "_")
            result[opt] = loads(value)
        else:
            secho(f"{err}: doesn't support shorthands", fg=colors.RED)
            exit(1)
    return result