|
1 | 1 | """Convert DFNs to TOML.""" |
2 | 2 |
|
3 | 3 | import argparse |
4 | | -import sys |
5 | | -import textwrap |
6 | | -from dataclasses import asdict |
7 | 4 | from os import PathLike |
8 | 5 | from pathlib import Path |
9 | 6 |
|
10 | 7 | import tomli_w as tomli |
11 | 8 | from boltons.iterutils import remap |
12 | 9 |
|
13 | | -from modflow_devtools.dfns import Dfn, is_valid, load, load_flat, map, to_flat, to_tree |
14 | | -from modflow_devtools.dfns.schema.block import block_sort_key |
15 | | -from modflow_devtools.misc import drop_none_or_empty |
| 10 | +from modflow_devtools.dfn import Dfn |
16 | 11 |
|
17 | 12 | # mypy: ignore-errors |
18 | 13 |
|
19 | 14 |
|
20 | | -def convert(inpath: PathLike, outdir: PathLike, schema_version: str = "2") -> None: |
21 | | - """ |
22 | | - Convert DFN files in `inpath` to TOML files in `outdir`. |
23 | | - By default, convert the definitions to schema version 2. |
24 | | - """ |
25 | | - inpath = Path(inpath).expanduser().absolute() |
| 15 | +def convert(indir: PathLike, outdir: PathLike): |
| 16 | + indir = Path(indir).expanduser().absolute() |
26 | 17 | outdir = Path(outdir).expanduser().absolute() |
27 | 18 | outdir.mkdir(exist_ok=True, parents=True) |
| 19 | + for dfn in Dfn.load_all(indir).values(): |
| 20 | + with Path.open(outdir / f"{dfn['name']}.toml", "wb") as f: |
28 | 21 |
|
29 | | - if inpath.is_file(): |
30 | | - if inpath.name == "common.dfn": |
31 | | - raise ValueError("Cannot convert common.dfn as a standalone file") |
| 22 | + def drop_none_or_empty(path, key, value): |
| 23 | + if value is None or value == "" or value == [] or value == {}: |
| 24 | + return False |
| 25 | + return True |
32 | 26 |
|
33 | | - common_path = inpath.parent / "common.dfn" |
34 | | - if common_path.exists(): |
35 | | - with common_path.open() as f: |
36 | | - from modflow_devtools.dfn import parse_dfn |
37 | | - |
38 | | - common, _ = parse_dfn(f) |
39 | | - else: |
40 | | - common = {} |
41 | | - |
42 | | - with inpath.open() as f: |
43 | | - dfn = load(f, name=inpath.stem, common=common, format="dfn") |
44 | | - |
45 | | - dfn = map(dfn, schema_version=schema_version) |
46 | | - _convert(dfn, outdir / f"{inpath.stem}.toml") |
47 | | - else: |
48 | | - dfns = { |
49 | | - name: map(dfn, schema_version=schema_version) for name, dfn in load_flat(inpath).items() |
50 | | - } |
51 | | - tree = to_tree(dfns) |
52 | | - flat = to_flat(tree) |
53 | | - for dfn_name, dfn in flat.items(): |
54 | | - _convert(dfn, outdir / f"{dfn_name}.toml") |
55 | | - |
56 | | - |
57 | | -def _convert(dfn: Dfn, outpath: Path) -> None: |
58 | | - with Path.open(outpath, "wb") as f: |
59 | | - # TODO if we start using c/attrs, swap out |
60 | | - # all this for a custom unstructuring hook |
61 | | - dfn_dict = asdict(dfn) |
62 | | - dfn_dict["schema_version"] = str(dfn_dict["schema_version"]) |
63 | | - if blocks := dfn_dict.pop("blocks", None): |
64 | | - for block_name, block_fields in blocks.items(): |
65 | | - if block_name not in dfn_dict: |
66 | | - dfn_dict[block_name] = {} |
67 | | - for field_name, field_data in block_fields.items(): |
68 | | - dfn_dict[block_name][field_name] = field_data |
69 | | - |
70 | | - tomli.dump( |
71 | | - dict( |
72 | | - sorted( |
73 | | - remap(dfn_dict, visit=drop_none_or_empty).items(), |
74 | | - key=block_sort_key, |
75 | | - ) |
76 | | - ), |
77 | | - f, |
78 | | - ) |
| 27 | + tomli.dump(remap(dfn, visit=drop_none_or_empty), f) |
79 | 28 |
|
80 | 29 |
|
81 | 30 | if __name__ == "__main__": |
82 | | - """ |
83 | | - Convert DFN files in the original format and schema version 1 |
84 | | - to TOML files, by default also converting to schema version 2. |
85 | | - """ |
| 31 | + """Convert DFN files to TOML.""" |
86 | 32 |
|
87 | | - parser = argparse.ArgumentParser( |
88 | | - description="Convert DFN files to TOML.", |
89 | | - epilog=textwrap.dedent( |
90 | | - """\ |
91 | | -Convert DFN files in the original format and schema version 1 |
92 | | -to TOML files, by default also converting to schema version 2. |
93 | | -""" |
94 | | - ), |
95 | | - ) |
| 33 | + parser = argparse.ArgumentParser(description="Convert DFN files to TOML.") |
96 | 34 | parser.add_argument( |
97 | 35 | "--indir", |
98 | 36 | "-i", |
99 | 37 | type=str, |
100 | | - help="Directory containing DFN files, or a single DFN file.", |
| 38 | + help="Directory containing DFN files.", |
101 | 39 | ) |
102 | 40 | parser.add_argument( |
103 | 41 | "--outdir", |
104 | 42 | "-o", |
105 | 43 | help="Output directory.", |
106 | 44 | ) |
107 | | - parser.add_argument( |
108 | | - "--schema-version", |
109 | | - "-s", |
110 | | - type=str, |
111 | | - default="2", |
112 | | - help="Schema version to convert to.", |
113 | | - ) |
114 | | - parser.add_argument( |
115 | | - "--validate", |
116 | | - "-v", |
117 | | - action="store_true", |
118 | | - help="Validate DFN files without converting them.", |
119 | | - ) |
120 | 45 | args = parser.parse_args() |
121 | | - |
122 | | - if args.validate: |
123 | | - if not is_valid(args.indir): |
124 | | - sys.exit(1) |
125 | | - else: |
126 | | - convert(args.indir, args.outdir, args.schema_version) |
| 46 | + convert(args.indir, args.outdir) |
0 commit comments