Skip to content

Commit 820e212

Browse files
committed
Extract code from __main__ into fmix/read_tyro.py
1 parent d8a0979 commit 820e212

3 files changed

Lines changed: 46 additions & 41 deletions

File tree

fmix/__main__.py

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,13 @@
1-
import json
21
import sys
3-
import tomllib
4-
from functools import partial
5-
from pathlib import Path
6-
from typing import Any, TypeIs
72

8-
import tyro
9-
from pydantic import ValidationError
3+
from fmix.read_tyro import read_tyro
104

115
from .fmix import FMix
126

137

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-
368
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())
4711

4812

4913
if __name__ == '__main__':

fmix/read_tyro.py

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

test/test_render_data.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
from pytest import mark
99

1010
from fmix import audio_file, constants
11-
from fmix.__main__ import read_fmix
11+
from fmix.fmix import FMix
12+
from fmix.read_tyro import read_file
1213
from fmix.render import render_samples
1314

1415
from . import REWRITE_TEST_DATA
@@ -21,7 +22,7 @@ def test_render_data(mixfile, monkeypatch):
2122
monkeypatch.setattr(constants, '_dtype', 'float64')
2223
monkeypatch.setattr(constants, '_samplerate', 48_000)
2324
path = Path('test') / mixfile
24-
actual = render_samples(read_fmix(path))
25+
actual = render_samples(FMix(**read_file(path)))
2526

2627
if RESULT_FILE.exists() and not REWRITE_TEST_DATA:
2728
expected, _ = audio_file.read(RESULT_FILE)

0 commit comments

Comments
 (0)