• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MuellerSeb / nml-tools / 26478216192

26 May 2026 10:14PM UTC coverage: 83.041% (+2.0%) from 81.064%
26478216192

Pull #32

github

MuellerSeb
fix mypy
Pull Request #32: Add One-Level Derived-Type Support For Generated Namelists

642 of 729 new or added lines in 7 files covered. (88.07%)

1 existing line in 1 file now uncovered.

3697 of 4452 relevant lines covered (83.04%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.71
/src/nml_tools/cli.py
1
"""Command line interface for nml-tools."""
2

3
from __future__ import annotations
1✔
4

5
import logging
1✔
6
import sys
1✔
7
from collections.abc import Mapping
1✔
8
from dataclasses import dataclass
1✔
9
from difflib import unified_diff
1✔
10
from pathlib import Path
1✔
11
from typing import Any, cast
1✔
12

13
import click
1✔
14
import f90nml  # type: ignore
1✔
15
from click.exceptions import Exit
1✔
16
from packaging.version import InvalidVersion, Version
1✔
17

18
from ._utils import constant_dimension_overlap, is_fortran_identifier
1✔
19
from ._version import __version__
1✔
20
from .codegen_f2py import (
1✔
21
    F2pyCTypeMap,
22
    build_f2py_namelist_spec,
23
    collect_f2py_kind_usage,
24
    merge_f2py_kind_usage,
25
    render_f2cmap,
26
    render_f2py_wrappers,
27
    render_python_wrappers,
28
)
29
from .codegen_fortran import (
1✔
30
    ConstantSpec,
31
    collect_local_derived_types,
32
    generate_fortran,
33
    generate_helper,
34
    render_fortran,
35
    render_helper,
36
)
37
from .codegen_markdown import generate_docs, render_docs
1✔
38
from .codegen_template import generate_template, render_template
1✔
39
from .schema import SchemaResolver, load_schema
1✔
40
from .validate import validate_namelist
1✔
41

42
if sys.version_info >= (3, 11):
1✔
43
    import tomllib
1✔
44
else:  # pragma: no cover - python<3.11
45
    import tomli as tomllib
46

47
logger = logging.getLogger(__name__)
1✔
48

49

50
def _normalize_f90nml_values(value: Any) -> Any:
1✔
51
    """Remove parser bookkeeping while preserving nested derived values."""
52
    if isinstance(value, Mapping):
1✔
53
        return {
1✔
54
            key: _normalize_f90nml_values(item)
55
            for key, item in value.items()
56
            if not (isinstance(key, str) and key.lower() == "_start_index")
57
        }
58
    if isinstance(value, list):
1✔
59
        return [_normalize_f90nml_values(item) for item in value]
1✔
60
    return value
1✔
61
_CONTEXT_SETTINGS = {"help_option_names": ["-h", "--help"]}
1✔
62
_DEFAULT_CONFIG = Path("nml-config.toml")
1✔
63
_PYPROJECT_CONFIG = Path("pyproject.toml")
1✔
64

65
_NamedIntegerTypeBase = cast(Any, click.ParamType)
1✔
66

67

68
class NamedIntegerType(_NamedIntegerTypeBase):  # type: ignore[valid-type, misc]
1✔
69
    """Parse NAME=INT values for CLI options."""
70

71
    name = "NAME=INT"
1✔
72

73
    def __init__(self, *, label: str, positive: bool = False) -> None:
1✔
74
        self._label = label
1✔
75
        self._positive = positive
1✔
76

77
    def convert(
1✔
78
        self,
79
        value: Any,
80
        param: click.Parameter | None,
81
        ctx: click.Context | None,
82
    ) -> tuple[str, int]:
83
        if not isinstance(value, str) or "=" not in value:
1✔
84
            self.fail("must be NAME=INT", param, ctx)
1✔
85

86
        raw_name, raw_value = value.split("=", 1)
1✔
87
        name = raw_name.strip()
1✔
88
        if not name:
1✔
89
            self.fail("must use non-empty names", param, ctx)
×
90
        if not is_fortran_identifier(name):
1✔
91
            self.fail(f"{self._label} '{name}' must be a valid identifier", param, ctx)
1✔
92

93
        value_text = raw_value.strip()
1✔
94
        if not value_text:
1✔
95
            self.fail(f"{self._label} '{name}' must define a value", param, ctx)
×
96
        value_digits = value_text[1:] if value_text[:1] in {"+", "-"} else value_text
1✔
97
        if not value_digits.isdigit():
1✔
98
            self.fail(f"{self._label} '{name}' value must be an integer", param, ctx)
1✔
99

100
        parsed_value = int(value_text)
1✔
101
        if self._positive and parsed_value <= 0:
1✔
102
            self.fail(f"{self._label} '{name}' value must be positive", param, ctx)
1✔
103
        return name.lower(), parsed_value
1✔
104

105

106
_CONSTANT_TYPE = NamedIntegerType(label="constant")
1✔
107
_DIMENSION_TYPE = NamedIntegerType(label="dimension", positive=True)
1✔
108

109

110
@dataclass(frozen=True)
1✔
111
class GeneratedOutput:
1✔
112
    """Generated file content for write/check commands."""
113

114
    path: Path
1✔
115
    content: str
1✔
116

117

118
def _configure_logging(verbose: int, quiet: int) -> None:
1✔
119
    base_level = logging.INFO
1✔
120
    level = base_level - (10 * verbose) + (10 * quiet)
1✔
121
    level = max(logging.DEBUG, min(logging.CRITICAL, level))
1✔
122
    logging.basicConfig(level=level, format="%(levelname)s: %(message)s")
1✔
123

124

125
def _load_toml(path: Path) -> dict[str, Any]:
1✔
126
    with path.open("rb") as handle:
1✔
127
        data = tomllib.load(handle)
1✔
128
    if not isinstance(data, dict):
1✔
129
        raise click.ClickException("config must be a table")
×
130
    return data
1✔
131

132

133
def _load_toml_checked(path: Path) -> dict[str, Any]:
1✔
134
    try:
1✔
135
        return _load_toml(path)
1✔
136
    except FileNotFoundError as exc:
1✔
137
        raise click.ClickException(str(exc)) from exc
1✔
138
    except Exception as exc:  # pragma: no cover - tomllib may raise ValueError
139
        raise click.ClickException(f"failed to read config: {exc}") from exc
140

141

142
def _load_config_checked(path: Path | None) -> tuple[dict[str, Any], Path]:
1✔
143
    config_path = _resolve_config_path(path)
1✔
144
    raw_config = _load_toml_checked(config_path)
1✔
145
    if config_path.name == _PYPROJECT_CONFIG.name:
1✔
146
        config = _extract_pyproject_config(raw_config)
1✔
147
    else:
148
        config = raw_config
1✔
149
    _check_minimum_version(config)
1✔
150
    return config, config_path
1✔
151

152

153
def _resolve_config_path(path: Path | None) -> Path:
1✔
154
    if path is not None:
1✔
155
        return path
1✔
156
    if _DEFAULT_CONFIG.is_file():
1✔
157
        return _DEFAULT_CONFIG
1✔
158
    if _PYPROJECT_CONFIG.is_file():
1✔
159
        raw_config = _load_toml_checked(_PYPROJECT_CONFIG)
1✔
160
        if _has_pyproject_config(raw_config):
1✔
161
            return _PYPROJECT_CONFIG
1✔
162
    raise click.ClickException(
1✔
163
        "no config found; create nml-config.toml or add [tool.nml-tools] to pyproject.toml"
164
    )
165

166

167
def _has_pyproject_config(config: dict[str, Any]) -> bool:
1✔
168
    tool_raw = config.get("tool")
1✔
169
    return isinstance(tool_raw, dict) and isinstance(tool_raw.get("nml-tools"), dict)
1✔
170

171

172
def _extract_pyproject_config(config: dict[str, Any]) -> dict[str, Any]:
1✔
173
    tool_raw = config.get("tool")
1✔
174
    if not isinstance(tool_raw, dict):
1✔
175
        raise click.ClickException("pyproject.toml must define [tool.nml-tools]")
1✔
176
    nml_tools_raw = tool_raw.get("nml-tools")
1✔
177
    if not isinstance(nml_tools_raw, dict):
1✔
178
        raise click.ClickException("pyproject.toml must define [tool.nml-tools]")
1✔
179
    return nml_tools_raw
1✔
180

181

182
def _check_minimum_version(config: dict[str, Any]) -> None:
1✔
183
    minimum_raw = config.get("minimum-version")
1✔
184
    if minimum_raw is None:
1✔
185
        return
1✔
186
    if not isinstance(minimum_raw, str) or not minimum_raw.strip():
1✔
187
        raise click.ClickException("config 'minimum-version' must be a non-empty string")
1✔
188
    try:
1✔
189
        minimum = Version(minimum_raw.strip())
1✔
190
    except InvalidVersion as exc:
1✔
191
        raise click.ClickException(
1✔
192
            f"config 'minimum-version' is not a valid version: {minimum_raw}"
193
        ) from exc
194
    try:
1✔
195
        current = Version(__version__)
1✔
196
    except InvalidVersion as exc:  # pragma: no cover - package version should be valid
197
        raise click.ClickException(f"nml-tools version is not valid: {__version__}") from exc
198
    if current < minimum:
1✔
199
        raise click.ClickException(
1✔
200
            f"config requires nml-tools >= {minimum}, current version is {current}"
201
        )
202

203

204
def _resolve_optional_path(
1✔
205
    value: Any,
206
    *,
207
    base_dir: Path,
208
    key: str,
209
) -> Path | None:
210
    if value is None:
1✔
211
        return None
1✔
212
    if not isinstance(value, str):
1✔
213
        raise click.ClickException(f"config '{key}' must be a string")
×
214
    return base_dir / value
1✔
215

216

217
def _load_helper_settings(
1✔
218
    config: dict[str, Any],
219
    base_dir: Path,
220
) -> tuple[Path | None, str, int, str | None]:
221
    default_buffer = 1024
1✔
222
    helper_raw = config.get("helper")
1✔
223
    if helper_raw is None:
1✔
224
        helper_path = _resolve_optional_path(
1✔
225
            config.get("helper_path"),
226
            base_dir=base_dir,
227
            key="helper_path",
228
        )
229
        helper_module_raw = config.get("helper_module", "nml_helper")
1✔
230
        if not isinstance(helper_module_raw, str):
1✔
231
            raise click.ClickException("config 'helper_module' must be a string")
×
232
        helper_module = helper_module_raw.strip()
1✔
233
        if not helper_module:
1✔
234
            raise click.ClickException("config 'helper_module' must be a non-empty string")
×
235
        return helper_path, helper_module, default_buffer, None
1✔
236

237
    if not isinstance(helper_raw, dict):
1✔
238
        raise click.ClickException("config 'helper' must be a table")
×
239
    if "helper_path" in config or "helper_module" in config:
1✔
240
        logger.warning("config uses [helper]; ignoring legacy helper_path/helper_module")
×
241
    helper_path = _resolve_optional_path(
1✔
242
        helper_raw.get("path"),
243
        base_dir=base_dir,
244
        key="helper.path",
245
    )
246
    helper_module_raw = helper_raw.get("module", "nml_helper")
1✔
247
    if not isinstance(helper_module_raw, str):
1✔
248
        raise click.ClickException("config 'helper.module' must be a string")
×
249
    helper_module = helper_module_raw.strip()
1✔
250
    if not helper_module:
1✔
251
        raise click.ClickException("config 'helper.module' must be a non-empty string")
×
252
    header_raw = helper_raw.get("header")
1✔
253
    if header_raw is None:
1✔
254
        header = None
1✔
255
    else:
256
        if not isinstance(header_raw, str):
×
257
            raise click.ClickException("config 'helper.header' must be a string")
×
258
        header = header_raw.rstrip()
×
259
        if not header:
×
260
            header = None
×
261
    buffer_raw = helper_raw.get("buffer", default_buffer)
1✔
262
    if isinstance(buffer_raw, bool) or not isinstance(buffer_raw, int):
1✔
263
        raise click.ClickException("config 'helper.buffer' must be an integer")
×
264
    if buffer_raw <= 0:
1✔
265
        raise click.ClickException("config 'helper.buffer' must be positive")
×
266
    return helper_path, helper_module, buffer_raw, header
1✔
267

268

269
def _load_kind_settings(config: dict[str, Any]) -> tuple[str, dict[str, str], set[str]]:
1✔
270
    kinds_raw = config.get("kinds")
1✔
271
    if not isinstance(kinds_raw, dict):
1✔
272
        raise click.ClickException("config must define a [kinds] table")
×
273
    module_raw = kinds_raw.get("module")
1✔
274
    if not isinstance(module_raw, str) or not module_raw.strip():
1✔
275
        raise click.ClickException("config 'kinds.module' must be a non-empty string")
×
276
    module = module_raw.strip()
1✔
277

278
    map_raw = kinds_raw.get("map", {})
1✔
279
    if map_raw is None:
1✔
280
        map_raw = {}
×
281
    if not isinstance(map_raw, dict):
1✔
282
        raise click.ClickException("config 'kinds.map' must be a table")
×
283
    kind_map: dict[str, str] = {}
1✔
284
    for alias, target in map_raw.items():
1✔
285
        if not isinstance(alias, str) or not isinstance(target, str):
1✔
286
            raise click.ClickException("config 'kinds.map' keys and values must be strings")
×
287
        kind_map[alias] = target
1✔
288

289
    real_raw = kinds_raw.get("real", [])
1✔
290
    integer_raw = kinds_raw.get("integer", [])
1✔
291
    if not isinstance(real_raw, list) or not all(isinstance(item, str) for item in real_raw):
1✔
292
        raise click.ClickException("config 'kinds.real' must be a list of strings")
×
293
    if not isinstance(integer_raw, list) or not all(isinstance(item, str) for item in integer_raw):
1✔
294
        raise click.ClickException("config 'kinds.integer' must be a list of strings")
×
295
    allowlist = set(real_raw) | set(integer_raw)
1✔
296

297
    return module, kind_map, allowlist
1✔
298

299

300
def _load_f2py_settings(
1✔
301
    config: dict[str, Any],
302
    base_dir: Path,
303
) -> tuple[Path | None, F2pyCTypeMap]:
304
    f2py_raw = config.get("f2py")
1✔
305
    if f2py_raw is None:
1✔
306
        return None, F2pyCTypeMap(real={}, integer={})
1✔
307
    if not isinstance(f2py_raw, dict):
1✔
308
        raise click.ClickException("config 'f2py' must be a table")
×
309
    if "python_package" in f2py_raw:
1✔
310
        raise click.ClickException(
×
311
            "config 'f2py.python_package' is no longer supported; "
312
            "place the f2py extension next to the generated Python wrapper"
313
        )
314

315
    f2cmap_path = _resolve_optional_path(
1✔
316
        f2py_raw.get("f2cmap_path"),
317
        base_dir=base_dir,
318
        key="f2py.f2cmap_path",
319
    )
320

321
    c_types_raw = f2py_raw.get("c_types", {})
1✔
322
    if c_types_raw is None:
1✔
323
        c_types_raw = {}
×
324
    if not isinstance(c_types_raw, dict):
1✔
325
        raise click.ClickException("config 'f2py.c_types' must be a table")
×
326

327
    real = _load_f2py_ctype_table(c_types_raw, "real")
1✔
328
    integer = _load_f2py_ctype_table(c_types_raw, "integer")
1✔
329
    return f2cmap_path, F2pyCTypeMap(real=real, integer=integer)
1✔
330

331

332
def _load_f2py_ctype_table(c_types: dict[str, Any], key: str) -> dict[str, str]:
1✔
333
    raw = c_types.get(key, {})
1✔
334
    if raw is None:
1✔
335
        raw = {}
×
336
    if not isinstance(raw, dict):
1✔
337
        raise click.ClickException(f"config 'f2py.c_types.{key}' must be a table")
×
338
    values: dict[str, str] = {}
1✔
339
    for kind, c_type in raw.items():
1✔
340
        if not isinstance(kind, str) or not kind.strip():
1✔
341
            raise click.ClickException(
×
342
                f"config 'f2py.c_types.{key}' keys must be non-empty strings"
343
            )
344
        if not isinstance(c_type, str) or not c_type.strip():
1✔
345
            raise click.ClickException(
×
346
                f"config 'f2py.c_types.{key}.{kind}' must be a non-empty string"
347
            )
348
        values[kind.strip()] = c_type.strip()
1✔
349
    return values
1✔
350

351

352
def _format_constant_literal(value: int) -> tuple[str, str]:
1✔
353
    if isinstance(value, bool):
1✔
354
        raise click.ClickException("config constants must not be boolean")
×
355
    if isinstance(value, int):
1✔
356
        return "integer", str(value)
1✔
357
    raise click.ClickException("config constants must be integers")
×
358

359

360
def _load_constants(config: dict[str, Any]) -> tuple[dict[str, int], list[ConstantSpec]]:
1✔
361
    constants_raw = config.get("constants", {})
1✔
362
    if constants_raw is None:
1✔
363
        constants_raw = {}
×
364
    if not isinstance(constants_raw, dict):
1✔
365
        raise click.ClickException("config 'constants' must be a table")
×
366

367
    constants: dict[str, int] = {}
1✔
368
    specs: list[ConstantSpec] = []
1✔
369
    for name_raw, entry in constants_raw.items():
1✔
370
        if not isinstance(name_raw, str):
1✔
371
            raise click.ClickException("config constants must use string keys")
×
372
        name = name_raw.strip()
1✔
373
        if not name:
1✔
374
            raise click.ClickException("config constants must have non-empty names")
×
375
        if not is_fortran_identifier(name):
1✔
376
            raise click.ClickException(
×
377
                f"config constant '{name}' must be a valid Fortran identifier"
378
            )
379
        canonical_name = name.lower()
1✔
380
        if canonical_name in constants:
1✔
381
            raise click.ClickException(
1✔
382
                f"config constant '{name}' duplicates another constant name"
383
            )
384
        if not isinstance(entry, dict):
1✔
385
            raise click.ClickException(f"config constant '{name}' must be a table with 'value'")
×
386
        if "value" not in entry:
1✔
387
            raise click.ClickException(f"config constant '{name}' must define 'value'")
×
388
        value = entry.get("value")
1✔
389
        if isinstance(value, bool) or not isinstance(value, int):
1✔
390
            raise click.ClickException("config constants must be integers")
1✔
391
        type_spec, literal = _format_constant_literal(value)
1✔
392
        doc = entry.get("doc")
1✔
393
        if doc is not None:
1✔
394
            if not isinstance(doc, str):
1✔
395
                raise click.ClickException(f"config constant '{name}' doc must be a string")
×
396
            doc = " ".join(doc.splitlines()).strip() or None
1✔
397
        specs.append(
1✔
398
            ConstantSpec(
399
                name=canonical_name,
400
                type_spec=type_spec,
401
                value=literal,
402
                doc=doc,
403
            )
404
        )
405
        constants[canonical_name] = value
1✔
406
    return constants, specs
1✔
407

408

409
def _load_dimensions(
1✔
410
    config: dict[str, Any],
411
    constants: dict[str, int],
412
) -> tuple[dict[str, int], list[ConstantSpec]]:
413
    dimensions_raw = config.get("dimensions", {})
1✔
414
    if dimensions_raw is None:
1✔
415
        dimensions_raw = {}
×
416
    if not isinstance(dimensions_raw, dict):
1✔
417
        raise click.ClickException("config 'dimensions' must be a table")
×
418

419
    dimensions: dict[str, int] = {}
1✔
420
    specs: list[ConstantSpec] = []
1✔
421
    constant_names = {name.lower() for name in constants}
1✔
422
    for name_raw, entry in dimensions_raw.items():
1✔
423
        if not isinstance(name_raw, str):
1✔
424
            raise click.ClickException("config dimensions must use string keys")
×
425
        name = name_raw.strip()
1✔
426
        if not name:
1✔
427
            raise click.ClickException("config dimensions must have non-empty names")
×
428
        if not is_fortran_identifier(name):
1✔
429
            raise click.ClickException(
×
430
                f"config dimension '{name}' must be a valid Fortran identifier"
431
            )
432
        canonical_name = name.lower()
1✔
433
        if canonical_name in constant_names:
1✔
434
            raise click.ClickException(
1✔
435
                f"config dimension '{name}' duplicates a constant name"
436
            )
437
        if canonical_name in dimensions:
1✔
438
            raise click.ClickException(
1✔
439
                f"config dimension '{name}' duplicates another dimension name"
440
            )
441
        if not isinstance(entry, dict):
1✔
442
            raise click.ClickException(f"config dimension '{name}' must be a table with 'value'")
×
443
        if "value" not in entry:
1✔
444
            raise click.ClickException(f"config dimension '{name}' must define 'value'")
×
445
        value = entry.get("value")
1✔
446
        if isinstance(value, bool) or not isinstance(value, int):
1✔
447
            raise click.ClickException(f"config dimension '{name}' value must be an integer")
×
448
        if value <= 0:
1✔
449
            raise click.ClickException(f"config dimension '{name}' value must be positive")
1✔
450
        doc = entry.get("doc")
1✔
451
        if doc is not None:
1✔
452
            if not isinstance(doc, str):
1✔
453
                raise click.ClickException(f"config dimension '{name}' doc must be a string")
×
454
            doc = " ".join(doc.splitlines()).strip() or None
1✔
455
        specs.append(
1✔
456
            ConstantSpec(
457
                name=canonical_name,
458
                type_spec="integer",
459
                value=str(value),
460
                doc=doc,
461
            )
462
        )
463
        dimensions[canonical_name] = value
1✔
464
    return dimensions, specs
1✔
465

466

467
def _load_bool_field(section: dict[str, Any], key: str, *, label: str) -> bool:
1✔
468
    value = section.get(key, False)
×
469
    if isinstance(value, bool):
×
470
        return value
×
471
    raise click.ClickException(f"config '{label}' must be a boolean")
×
472

473

474
def _load_documentation_settings(
1✔
475
    config: dict[str, Any],
476
) -> tuple[str | None, bool, bool, str]:
477
    doc_raw = config.get("documentation")
1✔
478
    if doc_raw is None:
1✔
479
        return None, False, False, "numpy"
1✔
480
    if not isinstance(doc_raw, dict):
×
481
        raise click.ClickException("config 'documentation' must be a table")
×
482
    module_doc = None
×
483
    if "module" in doc_raw:
×
484
        module_raw = doc_raw.get("module")
×
485
        if not isinstance(module_raw, str):
×
486
            raise click.ClickException("config 'documentation.module' must be a string")
×
487
        module_doc = module_raw.strip() or None
×
488
    md_doxygen_id_from_name = _load_bool_field(
×
489
        doc_raw,
490
        "md_doxygen_id_from_name",
491
        label="documentation.md_doxygen_id_from_name",
492
    )
493
    md_add_toc_statement = _load_bool_field(
×
494
        doc_raw,
495
        "md_add_toc_statement",
496
        label="documentation.md_add_toc_statement",
497
    )
498
    py_style_raw = doc_raw.get("py-style", "numpy")
×
499
    if not isinstance(py_style_raw, str):
×
500
        raise click.ClickException("config 'documentation.py-style' must be a string")
×
501
    py_style = py_style_raw.strip().lower()
×
502
    if py_style not in {"numpy", "doxygen"}:
×
503
        raise click.ClickException(
×
504
            "config 'documentation.py-style' must be 'numpy' or 'doxygen'"
505
        )
506
    return module_doc, md_doxygen_id_from_name, md_add_toc_statement, py_style
×
507

508

509
def _parse_cli_constants(values: tuple[tuple[str, int], ...]) -> dict[str, int]:
1✔
510
    constants: dict[str, int] = {}
1✔
511
    for name, value in values:
1✔
512
        if name in constants:
1✔
513
            raise click.ClickException(f"constant '{name}' duplicates another constant name")
1✔
514
        constants[name] = value
1✔
515
    return constants
1✔
516

517

518
def _parse_cli_dimensions(values: tuple[tuple[str, int], ...]) -> dict[str, int]:
1✔
519
    dimensions: dict[str, int] = {}
1✔
520
    for name, value in values:
1✔
521
        if name in dimensions:
1✔
522
            raise click.ClickException(f"dimension '{name}' duplicates another dimension name")
1✔
523
        if value <= 0:
1✔
524
            raise click.ClickException(f"dimension '{name}' value must be positive")
×
525
        dimensions[name] = value
1✔
526
    return dimensions
1✔
527

528

529
def _reject_constant_dimension_overlap(
1✔
530
    constants: dict[str, int],
531
    dimensions: dict[str, int],
532
) -> None:
533
    duplicate_names = constant_dimension_overlap(constants, dimensions)
1✔
534
    if duplicate_names:
1✔
535
        raise click.ClickException(
×
536
            "constants and dimensions must not share names: " + ", ".join(duplicate_names)
537
        )
538

539

540
def _iter_namelists(config: dict[str, Any], base_dir: Path) -> list[dict[str, Any]]:
1✔
541
    raw_entries = config.get("namelists")
1✔
542
    if raw_entries is None and "nml-files" in config:
1✔
543
        raise click.ClickException("config uses deprecated 'nml-files'; rename to 'namelists'")
×
544
    if not isinstance(raw_entries, list) or not raw_entries:
1✔
545
        raise click.ClickException("config must define non-empty 'namelists'")
×
546

547
    entries: list[dict[str, Path | None]] = []
1✔
548
    for entry in raw_entries:
1✔
549
        if not isinstance(entry, dict):
1✔
550
            raise click.ClickException("each namelists entry must be a table")
×
551
        schema_raw = entry.get("schema")
1✔
552
        if not isinstance(schema_raw, str):
1✔
553
            raise click.ClickException("namelists entry must define string 'schema'")
×
554
        schema_path = base_dir / schema_raw
1✔
555
        f2py_path = _resolve_optional_path(
1✔
556
            entry.get("f2py_path"),
557
            base_dir=base_dir,
558
            key="f2py_path",
559
        )
560
        py_path = _resolve_optional_path(
1✔
561
            entry.get("py_path"),
562
            base_dir=base_dir,
563
            key="py_path",
564
        )
565
        mod_path = _resolve_optional_path(
1✔
566
            entry.get("mod_path"),
567
            base_dir=base_dir,
568
            key="mod_path",
569
        )
570
        if f2py_path is not None and mod_path is None:
1✔
571
            raise click.ClickException(
×
572
                "namelists entry with 'f2py_path' must define 'mod_path'"
573
            )
574
        if py_path is not None and f2py_path is None:
1✔
575
            raise click.ClickException("namelists entry with 'py_path' must define 'f2py_path'")
×
576
        entries.append(
1✔
577
            {
578
                "schema": schema_path,
579
                "mod_path": mod_path,
580
                "doc_path": _resolve_optional_path(
581
                    entry.get("doc_path"),
582
                    base_dir=base_dir,
583
                    key="doc_path",
584
                ),
585
                "temp_path": _resolve_optional_path(
586
                    entry.get("temp_path"),
587
                    base_dir=base_dir,
588
                    key="temp_path",
589
                ),
590
                "f2py_path": f2py_path,
591
                "py_path": py_path,
592
            }
593
        )
594
    return entries
1✔
595

596

597
def _iter_templates(config: dict[str, Any], base_dir: Path) -> list[dict[str, Any]]:
1✔
598
    raw_entries = config.get("templates")
1✔
599
    if raw_entries is None:
1✔
600
        return []
1✔
601
    if not isinstance(raw_entries, list) or not raw_entries:
1✔
602
        raise click.ClickException("config 'templates' must be a non-empty list")
×
603

604
    entries: list[dict[str, Any]] = []
1✔
605
    for entry in raw_entries:
1✔
606
        if not isinstance(entry, dict):
1✔
607
            raise click.ClickException("each templates entry must be a table")
×
608
        output_raw = entry.get("output")
1✔
609
        if not isinstance(output_raw, str):
1✔
610
            raise click.ClickException("templates entry must define string 'output'")
×
611
        output_path = base_dir / output_raw
1✔
612

613
        schemas_raw = entry.get("schemas")
1✔
614
        if not isinstance(schemas_raw, list) or not schemas_raw:
1✔
615
            raise click.ClickException("templates entry must define non-empty 'schemas'")
×
616
        if not all(isinstance(item, str) for item in schemas_raw):
1✔
617
            raise click.ClickException("templates 'schemas' entries must be strings")
×
618
        schema_paths = [base_dir / item for item in schemas_raw]
1✔
619

620
        doc_mode = entry.get("doc_mode", "plain")
1✔
621
        if not isinstance(doc_mode, str):
1✔
622
            raise click.ClickException("templates 'doc_mode' must be a string")
×
623
        value_mode = entry.get("value_mode", "empty")
1✔
624
        if not isinstance(value_mode, str):
1✔
625
            raise click.ClickException("templates 'value_mode' must be a string")
×
626
        values_raw = entry.get("values", {})
1✔
627
        if values_raw is None:
1✔
628
            values_raw = {}
×
629
        if not isinstance(values_raw, dict):
1✔
630
            raise click.ClickException("templates 'values' must be a table")
×
631

632
        entries.append(
1✔
633
            {
634
                "output": output_path,
635
                "schemas": schema_paths,
636
                "doc_mode": doc_mode,
637
                "value_mode": value_mode,
638
                "values": values_raw,
639
            }
640
        )
641
    return entries
1✔
642

643

644
def _collect_generated_outputs(
1✔
645
    config: dict[str, Any],
646
    config_path: Path,
647
) -> list[GeneratedOutput]:
648
    base_dir = config_path.parent
1✔
649
    logger.debug("Base directory: %s", base_dir)
1✔
650
    helper_path, helper_module, helper_buffer, helper_header = _load_helper_settings(
1✔
651
        config,
652
        base_dir,
653
    )
654
    (
1✔
655
        module_doc,
656
        md_doxygen_id_from_name,
657
        md_add_toc_statement,
658
        py_style,
659
    ) = _load_documentation_settings(config)
660
    constants, constant_specs = _load_constants(config)
1✔
661
    dimensions, dimension_specs = _load_dimensions(config, constants)
1✔
662
    kind_module, kind_map, kind_allowlist = _load_kind_settings(config)
1✔
663
    f2cmap_path, f2py_c_types = _load_f2py_settings(config, base_dir)
1✔
664
    resolver = SchemaResolver()
1✔
665
    outputs: list[GeneratedOutput] = []
1✔
666

667
    entries = _iter_namelists(config, base_dir)
1✔
668
    logger.debug("Found %d schema entries", len(entries))
1✔
669
    loaded_entries: list[dict[str, Any]] = []
1✔
670
    for namelist_entry in entries:
1✔
671
        schema_path = namelist_entry["schema"]
1✔
672
        if schema_path is None:
1✔
673
            raise click.ClickException("namelists entry missing schema path")
×
674
        try:
1✔
675
            logger.debug("Loading schema %s", schema_path)
1✔
676
            schema = load_schema(schema_path, resolver=resolver)
1✔
677
        except (FileNotFoundError, ValueError) as exc:
×
678
            raise click.ClickException(str(exc)) from exc
×
679
        loaded_entries.append({"entry": namelist_entry, "schema": schema})
1✔
680

681
        mod_path = namelist_entry["mod_path"]
1✔
682
        if mod_path is not None:
1✔
683
            try:
1✔
684
                logger.debug("Rendering Fortran module at %s", mod_path)
1✔
685
                outputs.append(
1✔
686
                    GeneratedOutput(
687
                        mod_path,
688
                        render_fortran(
689
                            schema,
690
                            file_name=mod_path.name,
691
                            helper_module=helper_module,
692
                            kind_module=kind_module,
693
                            kind_map=kind_map,
694
                            kind_allowlist=kind_allowlist,
695
                            constants=constants,
696
                            dimensions=dimensions,
697
                            module_doc=module_doc,
698
                            f2py_handle_helpers=namelist_entry["f2py_path"] is not None,
699
                        ),
700
                    )
701
                )
702
            except ValueError as exc:
×
703
                raise click.ClickException(str(exc)) from exc
×
704

705
        doc_path = namelist_entry["doc_path"]
1✔
706
        if doc_path is not None:
1✔
707
            try:
1✔
708
                logger.debug("Rendering Markdown docs at %s", doc_path)
1✔
709
                outputs.append(
1✔
710
                    GeneratedOutput(
711
                        doc_path,
712
                        render_docs(
713
                            schema,
714
                            constants=constants,
715
                            dimensions=dimensions,
716
                            md_doxygen_id_from_name=md_doxygen_id_from_name,
717
                            md_add_toc_statement=md_add_toc_statement,
718
                        ),
719
                    )
720
                )
721
            except ValueError as exc:
×
722
                raise click.ClickException(str(exc)) from exc
×
723

724
    try:
1✔
725
        local_derived_types = collect_local_derived_types(
1✔
726
            [loaded["schema"] for loaded in loaded_entries],
727
            constants=constants,
728
        )
729
        if local_derived_types and helper_path is None:
1✔
NEW
730
            raise ValueError("locally generated derived types require a configured helper output")
×
731
        if helper_path is not None:
1✔
732
            logger.debug("Rendering helper module at %s", helper_path)
1✔
733
            outputs.append(
1✔
734
                GeneratedOutput(
735
                    helper_path,
736
                    render_helper(
737
                        file_name=helper_path.name,
738
                        module_name=helper_module,
739
                        len_buf=helper_buffer,
740
                        constants=constant_specs + dimension_specs,
741
                        local_derived_types=local_derived_types,
742
                        kind_module=kind_module,
743
                        kind_map=kind_map,
744
                        kind_allowlist=kind_allowlist,
745
                        module_doc=module_doc,
746
                        helper_header=helper_header,
747
                    ),
748
                )
749
            )
NEW
750
    except ValueError as exc:
×
NEW
751
        raise click.ClickException(str(exc)) from exc
×
752

753
    outputs.extend(
1✔
754
        _collect_f2py_outputs(
755
            loaded_entries,
756
            helper_module=helper_module,
757
            helper_buffer=helper_buffer,
758
            kind_module=kind_module,
759
            kind_map=kind_map,
760
            kind_allowlist=kind_allowlist,
761
            constants=constants,
762
            dimensions=dimensions,
763
            f2cmap_path=f2cmap_path,
764
            f2py_c_types=f2py_c_types,
765
            py_style=py_style,
766
            include_python=True,
767
        )
768
    )
769

770
    template_entries = _iter_templates(config, base_dir)
1✔
771
    if template_entries:
1✔
772
        logger.debug("Found %d template entries", len(template_entries))
1✔
773
    for template_entry in template_entries:
1✔
774
        try:
1✔
775
            schemas = []
1✔
776
            for schema_path in template_entry["schemas"]:
1✔
777
                logger.debug("Loading schema %s", schema_path)
1✔
778
                schemas.append(load_schema(schema_path, resolver=resolver))
1✔
779
            logger.debug("Rendering template at %s", template_entry["output"])
1✔
780
            outputs.append(
1✔
781
                GeneratedOutput(
782
                    template_entry["output"],
783
                    render_template(
784
                        schemas,
785
                        doc_mode=template_entry["doc_mode"],
786
                        value_mode=template_entry["value_mode"],
787
                        constants=constants,
788
                        dimensions=dimensions,
789
                        kind_map=kind_map,
790
                        kind_allowlist=kind_allowlist,
791
                        values=template_entry["values"],
792
                    ),
793
                )
794
            )
795
        except (FileNotFoundError, ValueError) as exc:
×
796
            raise click.ClickException(str(exc)) from exc
×
797

798
    return outputs
1✔
799

800

801
def _collect_f2py_outputs(
1✔
802
    loaded_entries: list[dict[str, Any]],
803
    *,
804
    helper_module: str,
805
    helper_buffer: int,
806
    kind_module: str,
807
    kind_map: dict[str, str],
808
    kind_allowlist: set[str],
809
    constants: dict[str, int],
810
    dimensions: dict[str, int],
811
    f2cmap_path: Path | None,
812
    f2py_c_types: F2pyCTypeMap,
813
    py_style: str,
814
    include_python: bool,
815
) -> list[GeneratedOutput]:
816
    outputs: list[GeneratedOutput] = []
1✔
817
    f2py_groups: dict[Path, list[dict[str, Any]]] = {}
1✔
818
    py_groups: dict[Path, list[tuple[dict[str, Any], Path]]] = {}
1✔
819
    for loaded in loaded_entries:
1✔
820
        entry = loaded["entry"]
1✔
821
        schema = loaded["schema"]
1✔
822
        f2py_path = entry["f2py_path"]
1✔
823
        if f2py_path is None:
1✔
824
            continue
1✔
825
        f2py_groups.setdefault(f2py_path, []).append(schema)
1✔
826
        py_path = entry["py_path"]
1✔
827
        if include_python and py_path is not None:
1✔
828
            py_groups.setdefault(py_path, []).append((schema, f2py_path))
1✔
829

830
    for f2py_path, schemas in f2py_groups.items():
1✔
831
        try:
1✔
832
            logger.debug("Rendering f2py wrappers at %s", f2py_path)
1✔
833
            outputs.append(
1✔
834
                GeneratedOutput(
835
                    f2py_path,
836
                    render_f2py_wrappers(
837
                        schemas,
838
                        file_name=f2py_path.name,
839
                        helper_module=helper_module,
840
                        kind_module=kind_module,
841
                        kind_map=kind_map,
842
                        kind_allowlist=kind_allowlist,
843
                        constants=constants,
844
                        dimensions=dimensions,
845
                        errmsg_len=helper_buffer,
846
                    ),
847
                )
848
            )
849
        except ValueError as exc:
×
850
            raise click.ClickException(str(exc)) from exc
×
851

852
    if f2cmap_path is not None:
1✔
853
        try:
1✔
854
            usage = merge_f2py_kind_usage(
1✔
855
                collect_f2py_kind_usage(schemas, constants=constants, dimensions=dimensions)
856
                for schemas in f2py_groups.values()
857
            )
858
            logger.debug("Rendering f2py kind map at %s", f2cmap_path)
1✔
859
            outputs.append(
1✔
860
                GeneratedOutput(
861
                    f2cmap_path,
862
                    render_f2cmap(usage, f2py_c_types),
863
                )
864
            )
865
        except ValueError as exc:
×
866
            raise click.ClickException(str(exc)) from exc
×
867

868
    if not include_python:
1✔
869
        return outputs
1✔
870
    for py_path, entries in py_groups.items():
1✔
871
        try:
1✔
872
            logger.debug("Rendering Python f2py wrappers at %s", py_path)
1✔
873
            specs = [
1✔
874
                (
875
                    build_f2py_namelist_spec(
876
                        schema,
877
                        helper_module=helper_module,
878
                        kind_module=kind_module,
879
                        kind_map=kind_map,
880
                        kind_allowlist=kind_allowlist,
881
                        constants=constants,
882
                        dimensions=dimensions,
883
                        errmsg_len=helper_buffer,
884
                    ),
885
                    f2py_path.stem,
886
                )
887
                for schema, f2py_path in entries
888
            ]
889
            outputs.append(
1✔
890
                GeneratedOutput(
891
                    py_path,
892
                    render_python_wrappers(
893
                        specs,
894
                        py_style=py_style,
895
                    ),
896
                )
897
            )
898
        except ValueError as exc:
×
899
            raise click.ClickException(str(exc)) from exc
×
900
    return outputs
1✔
901

902

903
def _generate_f2py_outputs(
1✔
904
    loaded_entries: list[dict[str, Any]],
905
    *,
906
    helper_module: str,
907
    helper_buffer: int,
908
    kind_module: str,
909
    kind_map: dict[str, str],
910
    kind_allowlist: set[str],
911
    constants: dict[str, int],
912
    dimensions: dict[str, int],
913
    f2cmap_path: Path | None,
914
    f2py_c_types: F2pyCTypeMap,
915
    py_style: str,
916
    include_python: bool,
917
) -> None:
918
    _write_generated_outputs(
1✔
919
        _collect_f2py_outputs(
920
            loaded_entries,
921
            helper_module=helper_module,
922
            helper_buffer=helper_buffer,
923
            kind_module=kind_module,
924
            kind_map=kind_map,
925
            kind_allowlist=kind_allowlist,
926
            constants=constants,
927
            dimensions=dimensions,
928
            f2cmap_path=f2cmap_path,
929
            f2py_c_types=f2py_c_types,
930
            py_style=py_style,
931
            include_python=include_python,
932
        )
933
    )
934

935

936
def _write_generated_outputs(outputs: list[GeneratedOutput]) -> None:
1✔
937
    for output in outputs:
1✔
938
        logger.info("Writing generated file %s", output.path)
1✔
939
        output.path.parent.mkdir(parents=True, exist_ok=True)
1✔
940
        output.path.write_text(output.content, encoding="ascii")
1✔
941

942

943
def _check_generated_outputs(outputs: list[GeneratedOutput], *, show_diff: bool) -> int:
1✔
944
    failed = 0
1✔
945
    for output in outputs:
1✔
946
        if not output.path.exists():
1✔
947
            failed += 1
1✔
948
            click.echo(f"MISSING: {output.path}", err=True)
1✔
949
            continue
1✔
950
        current = output.path.read_text(encoding="ascii")
1✔
951
        if current != output.content:
1✔
952
            failed += 1
1✔
953
            click.echo(f"DIFF: {output.path}", err=True)
1✔
954
            if show_diff:
1✔
955
                diff = unified_diff(
1✔
956
                    current.splitlines(keepends=True),
957
                    output.content.splitlines(keepends=True),
958
                    fromfile=f"current {output.path}",
959
                    tofile=f"generated {output.path}",
960
                )
961
                for line in diff:
1✔
962
                    click.echo(line, nl=False)
1✔
963
            continue
1✔
964
        logger.debug("OK: %s", output.path)
1✔
965
    return failed
1✔
966

967

968
@click.group(context_settings=_CONTEXT_SETTINGS)
1✔
969
@click.version_option(__version__, "-V", "--version", prog_name="nml-tools")
1✔
970
@click.option("--verbose", "-v", count=True, help="Increase verbosity (repeatable).")
1✔
971
@click.option("--quiet", "-q", count=True, help="Decrease verbosity (repeatable).")
1✔
972
def cli(verbose: int, quiet: int) -> None:
1✔
973
    """nml-tools command line interface."""
974
    _configure_logging(verbose, quiet)
1✔
975

976

977
@cli.command("generate", context_settings=_CONTEXT_SETTINGS)
1✔
978
@click.option(
1✔
979
    "--config",
980
    "config_path",
981
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
982
    default=None,
983
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
984
)
985
def generate(config_path: Path | None) -> None:
1✔
986
    """Generate outputs from a configuration file."""
987
    config, config_path = _load_config_checked(config_path)
1✔
988
    logger.info("Loading config from %s", config_path)
1✔
989
    _write_generated_outputs(_collect_generated_outputs(config, config_path))
1✔
990

991

992
@cli.command("check", context_settings=_CONTEXT_SETTINGS)
1✔
993
@click.option(
1✔
994
    "--config",
995
    "config_path",
996
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
997
    default=None,
998
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
999
)
1000
@click.option(
1✔
1001
    "--diff",
1002
    "show_diff",
1003
    is_flag=True,
1004
    help="Show unified diffs for generated files that differ.",
1005
)
1006
def check(config_path: Path | None, show_diff: bool) -> None:
1✔
1007
    """Check that configured generated files are up to date."""
1008
    config, config_path = _load_config_checked(config_path)
1✔
1009
    logger.debug("Loading config from %s", config_path)
1✔
1010
    failures = _check_generated_outputs(
1✔
1011
        _collect_generated_outputs(config, config_path),
1012
        show_diff=show_diff,
1013
    )
1014
    if failures:
1✔
1015
        raise click.ClickException(
1✔
1016
            f"generated files are out of date: {failures} file(s) differ or are missing"
1017
        )
1018

1019

1020
@cli.command("gen-fortran", context_settings=_CONTEXT_SETTINGS)
1✔
1021
@click.option(
1✔
1022
    "--config",
1023
    "config_path",
1024
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1025
    default=None,
1026
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
1027
)
1028
def gen_fortran(config_path: Path | None) -> None:
1✔
1029
    """Generate Fortran module(s)."""
1030
    config, config_path = _load_config_checked(config_path)
1✔
1031
    logger.info("Loading config from %s", config_path)
1✔
1032
    base_dir = config_path.parent
1✔
1033
    logger.debug("Base directory: %s", base_dir)
1✔
1034
    helper_path, helper_module, helper_buffer, helper_header = _load_helper_settings(
1✔
1035
        config,
1036
        base_dir,
1037
    )
1038
    module_doc, _, _, py_style = _load_documentation_settings(config)
1✔
1039
    constants, constant_specs = _load_constants(config)
1✔
1040
    dimensions, dimension_specs = _load_dimensions(config, constants)
1✔
1041
    kind_module, kind_map, kind_allowlist = _load_kind_settings(config)
1✔
1042
    f2cmap_path, f2py_c_types = _load_f2py_settings(config, base_dir)
1✔
1043
    resolver = SchemaResolver()
1✔
1044
    entries = _iter_namelists(config, base_dir)
1✔
1045
    logger.info("Found %d schema entries", len(entries))
1✔
1046
    loaded_entries: list[dict[str, Any]] = []
1✔
1047
    for entry in entries:
1✔
1048
        schema_path = entry["schema"]
1✔
1049
        if schema_path is None:
1✔
1050
            raise click.ClickException("namelists entry missing schema path")
×
1051
        try:
1✔
1052
            logger.info("Loading schema %s", schema_path)
1✔
1053
            schema = load_schema(schema_path, resolver=resolver)
1✔
1054
        except (FileNotFoundError, ValueError) as exc:
×
1055
            raise click.ClickException(str(exc)) from exc
×
1056
        loaded_entries.append({"entry": entry, "schema": schema})
1✔
1057
        mod_path = entry["mod_path"]
1✔
1058
        if mod_path is None:
1✔
1059
            continue
×
1060
        try:
1✔
1061
            logger.info("Generating Fortran module at %s", mod_path)
1✔
1062
            generate_fortran(
1✔
1063
                schema,
1064
                mod_path,
1065
                helper_module=helper_module,
1066
                kind_module=kind_module,
1067
                kind_map=kind_map,
1068
                kind_allowlist=kind_allowlist,
1069
                constants=constants,
1070
                dimensions=dimensions,
1071
                module_doc=module_doc,
1072
                f2py_handle_helpers=entry["f2py_path"] is not None,
1073
            )
1074
        except ValueError as exc:
×
1075
            raise click.ClickException(str(exc)) from exc
×
1076

1077
    try:
1✔
1078
        local_derived_types = collect_local_derived_types(
1✔
1079
            [loaded["schema"] for loaded in loaded_entries],
1080
            constants=constants,
1081
        )
1082
        if local_derived_types and helper_path is None:
1✔
NEW
1083
            raise ValueError("locally generated derived types require a configured helper output")
×
1084
        if helper_path is not None:
1✔
NEW
1085
            logger.info("Generating helper module at %s", helper_path)
×
NEW
1086
            generate_helper(
×
1087
                helper_path,
1088
                module_name=helper_module,
1089
                len_buf=helper_buffer,
1090
                constants=constant_specs + dimension_specs,
1091
                local_derived_types=local_derived_types,
1092
                kind_module=kind_module,
1093
                kind_map=kind_map,
1094
                kind_allowlist=kind_allowlist,
1095
                module_doc=module_doc,
1096
                helper_header=helper_header,
1097
            )
NEW
1098
    except ValueError as exc:
×
NEW
1099
        raise click.ClickException(str(exc)) from exc
×
1100

1101
    _generate_f2py_outputs(
1✔
1102
        loaded_entries,
1103
        helper_module=helper_module,
1104
        helper_buffer=helper_buffer,
1105
        kind_module=kind_module,
1106
        kind_map=kind_map,
1107
        kind_allowlist=kind_allowlist,
1108
        constants=constants,
1109
        dimensions=dimensions,
1110
        f2cmap_path=f2cmap_path,
1111
        f2py_c_types=f2py_c_types,
1112
        py_style=py_style,
1113
        include_python=False,
1114
    )
1115

1116

1117
@cli.command("validate", context_settings=_CONTEXT_SETTINGS)
1✔
1118
@click.option(
1✔
1119
    "--config",
1120
    "config_path",
1121
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1122
    default=None,
1123
)
1124
@click.option(
1✔
1125
    "--schema",
1126
    "schema_paths",
1127
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1128
    multiple=True,
1129
)
1130
@click.option(
1✔
1131
    "--input",
1132
    "input_option",
1133
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1134
)
1135
@click.option(
1✔
1136
    "--constants",
1137
    "constant_args",
1138
    metavar="NAME=INT",
1139
    type=_CONSTANT_TYPE,
1140
    multiple=True,
1141
    help="Additional integer constants as NAME=INT (repeatable).",
1142
)
1143
@click.option(
1✔
1144
    "--dimensions",
1145
    "dimension_args",
1146
    metavar="NAME=INT",
1147
    type=_DIMENSION_TYPE,
1148
    multiple=True,
1149
    help="Runtime dimensions as NAME=INT (repeatable).",
1150
)
1151
@click.argument(
1✔
1152
    "input_path",
1153
    required=False,
1154
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1155
)
1156
def validate(
1✔
1157
    config_path: Path | None,
1158
    schema_paths: tuple[Path, ...],
1159
    input_option: Path | None,
1160
    constant_args: tuple[tuple[str, int], ...],
1161
    dimension_args: tuple[tuple[str, int], ...],
1162
    input_path: Path | None,
1163
) -> None:
1164
    """Validate a namelist file against schema definitions."""
1165
    if input_option is not None and input_path is not None:
1✔
1166
        raise click.ClickException("input path provided twice")
×
1167
    input_path = input_option or input_path
1✔
1168
    if input_path is None:
1✔
1169
        raise click.ClickException("input path is required")
×
1170
    constants = _parse_cli_constants(constant_args)
1✔
1171
    dimension_overrides = _parse_cli_dimensions(dimension_args)
1✔
1172
    dimensions: dict[str, int] = {}
1✔
1173
    schemas: list[dict[str, Any]] = []
1✔
1174
    resolver = SchemaResolver()
1✔
1175

1176
    if schema_paths:
1✔
1177
        if config_path is not None:
1✔
1178
            config, config_path = _load_config_checked(config_path)
×
1179
            logger.info("Loading config from %s", config_path)
×
1180
            cfg_constants, _ = _load_constants(config)
×
1181
            dimensions, _ = _load_dimensions(config, cfg_constants)
×
1182
            constants = {**cfg_constants, **constants}
×
1183
            dimensions = {**dimensions, **dimension_overrides}
×
1184
        else:
1185
            dimensions = dimension_overrides
1✔
1186
        for schema_file in schema_paths:
1✔
1187
            try:
1✔
1188
                logger.info("Loading schema %s", schema_file)
1✔
1189
                schemas.append(load_schema(schema_file, resolver=resolver))
1✔
1190
            except (FileNotFoundError, ValueError) as exc:
×
1191
                raise click.ClickException(str(exc)) from exc
×
1192
        require_all = True
1✔
1193
    else:
1194
        config, config_path = _load_config_checked(config_path)
1✔
1195
        logger.info("Loading config from %s", config_path)
1✔
1196
        base_dir = config_path.parent
1✔
1197
        cfg_constants, _ = _load_constants(config)
1✔
1198
        dimensions, _ = _load_dimensions(config, cfg_constants)
1✔
1199
        constants = {**cfg_constants, **constants}
1✔
1200
        dimensions = {**dimensions, **dimension_overrides}
1✔
1201
        entries = _iter_namelists(config, base_dir)
1✔
1202
        logger.info("Found %d schema entries", len(entries))
1✔
1203
        for entry in entries:
1✔
1204
            schema_path = entry["schema"]
1✔
1205
            if schema_path is None:
1✔
1206
                raise click.ClickException("namelists entry missing schema path")
×
1207
            try:
1✔
1208
                logger.info("Loading schema %s", schema_path)
1✔
1209
                schemas.append(load_schema(schema_path, resolver=resolver))
1✔
1210
            except (FileNotFoundError, ValueError) as exc:
×
1211
                raise click.ClickException(str(exc)) from exc
×
1212
        require_all = False
1✔
1213

1214
    if not schemas:
1✔
1215
        raise click.ClickException("no schemas provided for validation")
×
1216
    _reject_constant_dimension_overlap(constants, dimensions)
1✔
1217

1218
    try:
1✔
1219
        logger.info("Reading namelist %s", input_path)
1✔
1220
        namelist_file = f90nml.read(input_path)
1✔
1221
    except Exception as exc:  # pragma: no cover - f90nml raises custom errors
1222
        raise click.ClickException(f"failed to read namelist: {exc}") from exc
1223

1224
    file_entries: dict[str, tuple[str, Any]] = {}
1✔
1225
    for name, values in namelist_file.items():
1✔
1226
        if not isinstance(name, str):
1✔
1227
            raise click.ClickException("namelist names must be strings")
×
1228
        key = name.lower()
1✔
1229
        if key in file_entries:
1✔
1230
            raise click.ClickException(f"namelist '{name}' appears multiple times")
×
1231
        file_entries[key] = (name, values)
1✔
1232

1233
    schema_entries: dict[str, dict[str, Any]] = {}
1✔
1234
    for schema in schemas:
1✔
1235
        namelist_name = schema.get("x-fortran-namelist")
1✔
1236
        if not isinstance(namelist_name, str) or not namelist_name.strip():
1✔
1237
            raise click.ClickException("schema must define non-empty 'x-fortran-namelist'")
×
1238
        key = namelist_name.lower()
1✔
1239
        if key in schema_entries:
1✔
1240
            raise click.ClickException(f"duplicate schema for namelist '{namelist_name}'")
×
1241
        schema_entries[key] = schema
1✔
1242

1243
    for key, (name, _) in file_entries.items():
1✔
1244
        if key not in schema_entries:
1✔
1245
            raise click.ClickException(f"input contains unknown namelist '{name}'")
×
1246

1247
    validated = 0
1✔
1248
    for key, schema in schema_entries.items():
1✔
1249
        if key not in file_entries:
1✔
1250
            if require_all:
×
1251
                raise click.ClickException(
×
1252
                    f"input is missing namelist '{schema.get('x-fortran-namelist')}'"
1253
                )
1254
            continue
×
1255
        try:
1✔
1256
            validate_namelist(
1✔
1257
                schema,
1258
                _normalize_f90nml_values(file_entries[key][1]),
1259
                constants=constants,
1260
                dimensions=dimensions,
1261
            )
1262
        except ValueError as exc:
×
1263
            raise click.ClickException(str(exc)) from exc
×
1264
        validated += 1
1✔
1265
    logger.info("Validation completed (%d namelist%s).", validated, "" if validated == 1 else "s")
1✔
1266

1267

1268
@cli.command("gen-markdown", context_settings=_CONTEXT_SETTINGS)
1✔
1269
@click.option(
1✔
1270
    "--config",
1271
    "config_path",
1272
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1273
    default=None,
1274
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
1275
)
1276
def gen_markdown(config_path: Path | None) -> None:
1✔
1277
    """Generate Markdown docs."""
1278
    config, config_path = _load_config_checked(config_path)
1✔
1279
    logger.info("Loading config from %s", config_path)
1✔
1280
    base_dir = config_path.parent
1✔
1281
    constants, _ = _load_constants(config)
1✔
1282
    dimensions, _ = _load_dimensions(config, constants)
1✔
1283
    _, md_doxygen_id_from_name, md_add_toc_statement, _ = _load_documentation_settings(
1✔
1284
        config
1285
    )
1286
    entries = _iter_namelists(config, base_dir)
1✔
1287
    resolver = SchemaResolver()
1✔
1288
    logger.info("Found %d schema entries", len(entries))
1✔
1289
    for entry in entries:
1✔
1290
        schema_path = entry["schema"]
1✔
1291
        if schema_path is None:
1✔
1292
            raise click.ClickException("namelists entry missing schema path")
×
1293
        try:
1✔
1294
            logger.info("Loading schema %s", schema_path)
1✔
1295
            schema = load_schema(schema_path, resolver=resolver)
1✔
1296
        except (FileNotFoundError, ValueError) as exc:
×
1297
            raise click.ClickException(str(exc)) from exc
×
1298
        doc_path = entry["doc_path"]
1✔
1299
        if doc_path is None:
1✔
1300
            continue
×
1301
        try:
1✔
1302
            logger.info("Generating Markdown docs at %s", doc_path)
1✔
1303
            generate_docs(
1✔
1304
                schema,
1305
                doc_path,
1306
                constants=constants,
1307
                dimensions=dimensions,
1308
                md_doxygen_id_from_name=md_doxygen_id_from_name,
1309
                md_add_toc_statement=md_add_toc_statement,
1310
            )
1311
        except ValueError as exc:
×
1312
            raise click.ClickException(str(exc)) from exc
×
1313

1314

1315
@cli.command("gen-template", context_settings=_CONTEXT_SETTINGS)
1✔
1316
@click.option(
1✔
1317
    "--config",
1318
    "config_path",
1319
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1320
    default=None,
1321
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
1322
)
1323
def gen_template(config_path: Path | None) -> None:
1✔
1324
    """Generate template namelist(s)."""
1325
    config, config_path = _load_config_checked(config_path)
1✔
1326
    logger.info("Loading config from %s", config_path)
1✔
1327
    base_dir = config_path.parent
1✔
1328
    constants, _ = _load_constants(config)
1✔
1329
    dimensions, _ = _load_dimensions(config, constants)
1✔
1330
    _, kind_map, kind_allowlist = _load_kind_settings(config)
1✔
1331
    templates = _iter_templates(config, base_dir)
1✔
1332
    resolver = SchemaResolver()
1✔
1333
    if not templates:
1✔
1334
        raise click.ClickException("config must define non-empty 'templates'")
×
1335
    logger.info("Found %d template entries", len(templates))
1✔
1336
    for entry in templates:
1✔
1337
        try:
1✔
1338
            schemas = []
1✔
1339
            for schema_path in entry["schemas"]:
1✔
1340
                logger.info("Loading schema %s", schema_path)
1✔
1341
                schemas.append(load_schema(schema_path, resolver=resolver))
1✔
1342
            logger.info("Generating template at %s", entry["output"])
1✔
1343
            generate_template(
1✔
1344
                schemas,
1345
                entry["output"],
1346
                doc_mode=entry["doc_mode"],
1347
                value_mode=entry["value_mode"],
1348
                constants=constants,
1349
                dimensions=dimensions,
1350
                kind_map=kind_map,
1351
                kind_allowlist=kind_allowlist,
1352
                values=entry["values"],
1353
            )
1354
        except (FileNotFoundError, ValueError) as exc:
×
1355
            raise click.ClickException(str(exc)) from exc
×
1356

1357

1358
def main(argv: list[str] | None = None) -> int:
1✔
1359
    """Entry point for the CLI."""
1360
    try:
×
1361
        cli.main(args=argv, prog_name="nml-tools", standalone_mode=False)
×
1362
    except Exit as exc:
×
1363
        return exc.exit_code
×
1364
    except click.ClickException as exc:
×
1365
        exc.show()
×
1366
        return 1
×
1367
    return 0
×
1368

1369

1370
if __name__ == "__main__":  # pragma: no cover - CLI entry point
1371
    raise SystemExit(main())
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc