|
1 | | -import json |
2 | 1 | import sys |
3 | | -import tomllib |
4 | | -from functools import partial |
5 | | -from pathlib import Path |
6 | | -from typing import Any, TypeIs |
7 | 2 |
|
8 | | -import tyro |
9 | | -from pydantic import ValidationError |
| 3 | +from fmix.read_tyro import read_tyro |
10 | 4 |
|
11 | 5 | from .fmix import FMix |
12 | 6 |
|
13 | 7 |
|
14 | | -def is_str_dict(x: Any) -> TypeIs[dict[str, Any]]: |
15 | | - return isinstance(x, dict) and all(isinstance(k, str) for k in x.keys()) |
16 | | - |
17 | | - |
18 | | -def read_file(path: Path) -> dict[str, Any]: |
19 | | - data = path.read_text() |
20 | | - match path.suffix: |
21 | | - case '.toml': |
22 | | - result = tomllib.loads(data) |
23 | | - case '.json': |
24 | | - result = json.loads(data) |
25 | | - case _: |
26 | | - raise ValueError(f'Do not understand file {path}') |
27 | | - if not is_str_dict(result): |
28 | | - raise ValueError(f'File {path} does not contain a string dictionary') |
29 | | - return result |
30 | | - |
31 | | - |
32 | | -def read_fmix(path: Path) -> FMix: |
33 | | - return FMix(**read_file(path)) |
34 | | - |
35 | | - |
36 | 8 | def main(): |
37 | | - try: |
38 | | - cli = partial(tyro.cli, FMix, prog='fmix') |
39 | | - if (f := cli()).config_file: |
40 | | - f = cli(default=read_fmix(f.config_file)) |
41 | | - result = f() |
42 | | - except (ValidationError, FileExistsError) as e: |
43 | | - if getattr(locals().get('f'), 'verbose', False): |
44 | | - raise |
45 | | - result = str(e) |
46 | | - sys.exit(result) |
| 9 | + fmix = read_tyro(cls=FMix, prog='fmix') |
| 10 | + sys.exit(fmix()) |
47 | 11 |
|
48 | 12 |
|
49 | 13 | if __name__ == '__main__': |
|
0 commit comments