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

MuellerSeb / nml-tools / 26883174675

03 Jun 2026 11:57AM UTC coverage: 84.127% (+0.1%) from 83.989%
26883174675

Pull #38

github

web-flow
Merge 2e60cb1d3 into dfc659dd6
Pull Request #38: Support legacy template schema inclusion

29 of 31 new or added lines in 1 file covered. (93.55%)

1 existing line in 1 file now uncovered.

3938 of 4681 relevant lines covered (84.13%)

0.84 hits per line

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

81.34
/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, validate_user_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
        try:
1✔
91
            validate_user_fortran_identifier(name, label=f"{self._label} '{name}'")
1✔
92
        except ValueError as exc:
1✔
93
            self.fail(str(exc), param, ctx)
1✔
94

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

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

107

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

111

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

116
    path: Path
1✔
117
    content: str
1✔
118

119

120
@dataclass(frozen=True)
1✔
121
class LoadedNamelist:
1✔
122
    """Configured namelist entry with its resolved schema."""
123

124
    entry: dict[str, Any]
1✔
125
    schema: dict[str, Any]
1✔
126
    name: str
1✔
127
    key: str
1✔
128

129

130
@dataclass(frozen=True)
1✔
131
class FileProfile:
1✔
132
    """Project-specific logical namelist file profile."""
133

134
    name: str
1✔
135
    key: str
1✔
136
    default_file: str
1✔
137
    namelists: list[str]
1✔
138
    title: str | None = None
1✔
139
    description: str | None = None
1✔
140

141

142
def _configure_logging(verbose: int, quiet: int) -> None:
1✔
143
    base_level = logging.INFO
1✔
144
    level = base_level - (10 * verbose) + (10 * quiet)
1✔
145
    level = max(logging.DEBUG, min(logging.CRITICAL, level))
1✔
146
    logging.basicConfig(level=level, format="%(levelname)s: %(message)s")
1✔
147

148

149
def _load_toml(path: Path) -> dict[str, Any]:
1✔
150
    with path.open("rb") as handle:
1✔
151
        data = tomllib.load(handle)
1✔
152
    if not isinstance(data, dict):
1✔
153
        raise click.ClickException("config must be a table")
×
154
    return data
1✔
155

156

157
def _load_toml_checked(path: Path) -> dict[str, Any]:
1✔
158
    try:
1✔
159
        return _load_toml(path)
1✔
160
    except FileNotFoundError as exc:
1✔
161
        raise click.ClickException(str(exc)) from exc
1✔
162
    except Exception as exc:  # pragma: no cover - tomllib may raise ValueError
163
        raise click.ClickException(f"failed to read config: {exc}") from exc
164

165

166
def _load_config_checked(path: Path | None) -> tuple[dict[str, Any], Path]:
1✔
167
    config_path = _resolve_config_path(path)
1✔
168
    raw_config = _load_toml_checked(config_path)
1✔
169
    if config_path.name == _PYPROJECT_CONFIG.name:
1✔
170
        config = _extract_pyproject_config(raw_config)
1✔
171
    else:
172
        config = raw_config
1✔
173
    _check_minimum_version(config)
1✔
174
    return config, config_path
1✔
175

176

177
def _resolve_config_path(path: Path | None) -> Path:
1✔
178
    if path is not None:
1✔
179
        return path
1✔
180
    if _DEFAULT_CONFIG.is_file():
1✔
181
        return _DEFAULT_CONFIG
1✔
182
    if _PYPROJECT_CONFIG.is_file():
1✔
183
        raw_config = _load_toml_checked(_PYPROJECT_CONFIG)
1✔
184
        if _has_pyproject_config(raw_config):
1✔
185
            return _PYPROJECT_CONFIG
1✔
186
    raise click.ClickException(
1✔
187
        "no config found; create nml-config.toml or add [tool.nml-tools] to pyproject.toml"
188
    )
189

190

191
def _has_pyproject_config(config: dict[str, Any]) -> bool:
1✔
192
    tool_raw = config.get("tool")
1✔
193
    return isinstance(tool_raw, dict) and isinstance(tool_raw.get("nml-tools"), dict)
1✔
194

195

196
def _extract_pyproject_config(config: dict[str, Any]) -> dict[str, Any]:
1✔
197
    tool_raw = config.get("tool")
1✔
198
    if not isinstance(tool_raw, dict):
1✔
199
        raise click.ClickException("pyproject.toml must define [tool.nml-tools]")
1✔
200
    nml_tools_raw = tool_raw.get("nml-tools")
1✔
201
    if not isinstance(nml_tools_raw, dict):
1✔
202
        raise click.ClickException("pyproject.toml must define [tool.nml-tools]")
1✔
203
    return nml_tools_raw
1✔
204

205

206
def _check_minimum_version(config: dict[str, Any]) -> None:
1✔
207
    minimum_raw = config.get("minimum-version")
1✔
208
    if minimum_raw is None:
1✔
209
        return
1✔
210
    if not isinstance(minimum_raw, str) or not minimum_raw.strip():
1✔
211
        raise click.ClickException("config 'minimum-version' must be a non-empty string")
1✔
212
    try:
1✔
213
        minimum = Version(minimum_raw.strip())
1✔
214
    except InvalidVersion as exc:
1✔
215
        raise click.ClickException(
1✔
216
            f"config 'minimum-version' is not a valid version: {minimum_raw}"
217
        ) from exc
218
    try:
1✔
219
        current = Version(__version__)
1✔
220
    except InvalidVersion as exc:  # pragma: no cover - package version should be valid
221
        raise click.ClickException(f"nml-tools version is not valid: {__version__}") from exc
222
    if current < minimum:
1✔
223
        raise click.ClickException(
1✔
224
            f"config requires nml-tools >= {minimum}, current version is {current}"
225
        )
226

227

228
def _resolve_optional_path(
1✔
229
    value: Any,
230
    *,
231
    base_dir: Path,
232
    key: str,
233
) -> Path | None:
234
    if value is None:
1✔
235
        return None
1✔
236
    if not isinstance(value, str):
1✔
237
        raise click.ClickException(f"config '{key}' must be a string")
×
238
    return base_dir / value
1✔
239

240

241
def _load_helper_settings(
1✔
242
    config: dict[str, Any],
243
    base_dir: Path,
244
) -> tuple[Path | None, str, int, str | None]:
245
    default_buffer = 1024
1✔
246
    helper_raw = config.get("helper")
1✔
247
    if helper_raw is None:
1✔
248
        helper_path = _resolve_optional_path(
1✔
249
            config.get("helper_path"),
250
            base_dir=base_dir,
251
            key="helper_path",
252
        )
253
        helper_module_raw = config.get("helper_module", "nml_helper")
1✔
254
        if not isinstance(helper_module_raw, str):
1✔
255
            raise click.ClickException("config 'helper_module' must be a string")
×
256
        helper_module = helper_module_raw.strip()
1✔
257
        if not helper_module:
1✔
258
            raise click.ClickException("config 'helper_module' must be a non-empty string")
×
259
        return helper_path, helper_module, default_buffer, None
1✔
260

261
    if not isinstance(helper_raw, dict):
1✔
262
        raise click.ClickException("config 'helper' must be a table")
×
263
    if "helper_path" in config or "helper_module" in config:
1✔
264
        logger.warning("config uses [helper]; ignoring legacy helper_path/helper_module")
×
265
    helper_path = _resolve_optional_path(
1✔
266
        helper_raw.get("path"),
267
        base_dir=base_dir,
268
        key="helper.path",
269
    )
270
    helper_module_raw = helper_raw.get("module", "nml_helper")
1✔
271
    if not isinstance(helper_module_raw, str):
1✔
272
        raise click.ClickException("config 'helper.module' must be a string")
×
273
    helper_module = helper_module_raw.strip()
1✔
274
    if not helper_module:
1✔
275
        raise click.ClickException("config 'helper.module' must be a non-empty string")
×
276
    header_raw = helper_raw.get("header")
1✔
277
    if header_raw is None:
1✔
278
        header = None
1✔
279
    else:
280
        if not isinstance(header_raw, str):
×
281
            raise click.ClickException("config 'helper.header' must be a string")
×
282
        header = header_raw.rstrip()
×
283
        if not header:
×
284
            header = None
×
285
    buffer_raw = helper_raw.get("buffer", default_buffer)
1✔
286
    if isinstance(buffer_raw, bool) or not isinstance(buffer_raw, int):
1✔
287
        raise click.ClickException("config 'helper.buffer' must be an integer")
×
288
    if buffer_raw <= 0:
1✔
289
        raise click.ClickException("config 'helper.buffer' must be positive")
×
290
    return helper_path, helper_module, buffer_raw, header
1✔
291

292

293
def _load_kind_settings(config: dict[str, Any]) -> tuple[str, dict[str, str], set[str]]:
1✔
294
    kinds_raw = config.get("kinds")
1✔
295
    if not isinstance(kinds_raw, dict):
1✔
296
        raise click.ClickException("config must define a [kinds] table")
×
297
    module_raw = kinds_raw.get("module")
1✔
298
    if not isinstance(module_raw, str) or not module_raw.strip():
1✔
299
        raise click.ClickException("config 'kinds.module' must be a non-empty string")
×
300
    module = module_raw.strip()
1✔
301

302
    map_raw = kinds_raw.get("map", {})
1✔
303
    if map_raw is None:
1✔
304
        map_raw = {}
×
305
    if not isinstance(map_raw, dict):
1✔
306
        raise click.ClickException("config 'kinds.map' must be a table")
×
307
    kind_map: dict[str, str] = {}
1✔
308
    for alias, target in map_raw.items():
1✔
309
        if not isinstance(alias, str) or not isinstance(target, str):
1✔
310
            raise click.ClickException("config 'kinds.map' keys and values must be strings")
×
311
        kind_map[alias] = target
1✔
312

313
    real_raw = kinds_raw.get("real", [])
1✔
314
    integer_raw = kinds_raw.get("integer", [])
1✔
315
    if not isinstance(real_raw, list) or not all(isinstance(item, str) for item in real_raw):
1✔
316
        raise click.ClickException("config 'kinds.real' must be a list of strings")
×
317
    if not isinstance(integer_raw, list) or not all(isinstance(item, str) for item in integer_raw):
1✔
318
        raise click.ClickException("config 'kinds.integer' must be a list of strings")
×
319
    allowlist = set(real_raw) | set(integer_raw)
1✔
320

321
    return module, kind_map, allowlist
1✔
322

323

324
def _load_f2py_settings(
1✔
325
    config: dict[str, Any],
326
    base_dir: Path,
327
) -> tuple[Path | None, F2pyCTypeMap]:
328
    f2py_raw = config.get("f2py")
1✔
329
    if f2py_raw is None:
1✔
330
        return None, F2pyCTypeMap(real={}, integer={})
1✔
331
    if not isinstance(f2py_raw, dict):
1✔
332
        raise click.ClickException("config 'f2py' must be a table")
×
333
    if "python_package" in f2py_raw:
1✔
334
        raise click.ClickException(
×
335
            "config 'f2py.python_package' is no longer supported; "
336
            "place the f2py extension next to the generated Python wrapper"
337
        )
338

339
    f2cmap_path = _resolve_optional_path(
1✔
340
        f2py_raw.get("f2cmap_path"),
341
        base_dir=base_dir,
342
        key="f2py.f2cmap_path",
343
    )
344

345
    c_types_raw = f2py_raw.get("c_types", {})
1✔
346
    if c_types_raw is None:
1✔
347
        c_types_raw = {}
×
348
    if not isinstance(c_types_raw, dict):
1✔
349
        raise click.ClickException("config 'f2py.c_types' must be a table")
×
350

351
    real = _load_f2py_ctype_table(c_types_raw, "real")
1✔
352
    integer = _load_f2py_ctype_table(c_types_raw, "integer")
1✔
353
    return f2cmap_path, F2pyCTypeMap(real=real, integer=integer)
1✔
354

355

356
def _load_f2py_ctype_table(c_types: dict[str, Any], key: str) -> dict[str, str]:
1✔
357
    raw = c_types.get(key, {})
1✔
358
    if raw is None:
1✔
359
        raw = {}
×
360
    if not isinstance(raw, dict):
1✔
361
        raise click.ClickException(f"config 'f2py.c_types.{key}' must be a table")
×
362
    values: dict[str, str] = {}
1✔
363
    for kind, c_type in raw.items():
1✔
364
        if not isinstance(kind, str) or not kind.strip():
1✔
365
            raise click.ClickException(
×
366
                f"config 'f2py.c_types.{key}' keys must be non-empty strings"
367
            )
368
        if not isinstance(c_type, str) or not c_type.strip():
1✔
369
            raise click.ClickException(
×
370
                f"config 'f2py.c_types.{key}.{kind}' must be a non-empty string"
371
            )
372
        values[kind.strip()] = c_type.strip()
1✔
373
    return values
1✔
374

375

376
def _format_constant_literal(value: int) -> tuple[str, str]:
1✔
377
    if isinstance(value, bool):
1✔
378
        raise click.ClickException("config constants must not be boolean")
×
379
    if isinstance(value, int):
1✔
380
        return "integer", str(value)
1✔
381
    raise click.ClickException("config constants must be integers")
×
382

383

384
def _load_constants(config: dict[str, Any]) -> tuple[dict[str, int], list[ConstantSpec]]:
1✔
385
    constants_raw = config.get("constants", {})
1✔
386
    if constants_raw is None:
1✔
387
        constants_raw = {}
×
388
    if not isinstance(constants_raw, dict):
1✔
389
        raise click.ClickException("config 'constants' must be a table")
×
390

391
    constants: dict[str, int] = {}
1✔
392
    specs: list[ConstantSpec] = []
1✔
393
    for name_raw, entry in constants_raw.items():
1✔
394
        if not isinstance(name_raw, str):
1✔
395
            raise click.ClickException("config constants must use string keys")
×
396
        name = name_raw.strip()
1✔
397
        if not name:
1✔
398
            raise click.ClickException("config constants must have non-empty names")
×
399
        try:
1✔
400
            validate_user_fortran_identifier(name, label=f"config constant '{name}'")
1✔
401
        except ValueError as exc:
1✔
402
            raise click.ClickException(str(exc)) from exc
1✔
403
        canonical_name = name.lower()
1✔
404
        if canonical_name in constants:
1✔
405
            raise click.ClickException(
1✔
406
                f"config constant '{name}' duplicates another constant name"
407
            )
408
        if not isinstance(entry, dict):
1✔
409
            raise click.ClickException(f"config constant '{name}' must be a table with 'value'")
×
410
        if "value" not in entry:
1✔
411
            raise click.ClickException(f"config constant '{name}' must define 'value'")
×
412
        value = entry.get("value")
1✔
413
        if isinstance(value, bool) or not isinstance(value, int):
1✔
414
            raise click.ClickException("config constants must be integers")
1✔
415
        type_spec, literal = _format_constant_literal(value)
1✔
416
        doc = entry.get("doc")
1✔
417
        if doc is not None:
1✔
418
            if not isinstance(doc, str):
1✔
419
                raise click.ClickException(f"config constant '{name}' doc must be a string")
×
420
            doc = " ".join(doc.splitlines()).strip() or None
1✔
421
        specs.append(
1✔
422
            ConstantSpec(
423
                name=canonical_name,
424
                type_spec=type_spec,
425
                value=literal,
426
                doc=doc,
427
            )
428
        )
429
        constants[canonical_name] = value
1✔
430
    return constants, specs
1✔
431

432

433
def _load_dimensions(
1✔
434
    config: dict[str, Any],
435
    constants: dict[str, int],
436
) -> tuple[dict[str, int], list[ConstantSpec]]:
437
    dimensions_raw = config.get("dimensions", {})
1✔
438
    if dimensions_raw is None:
1✔
439
        dimensions_raw = {}
×
440
    if not isinstance(dimensions_raw, dict):
1✔
441
        raise click.ClickException("config 'dimensions' must be a table")
×
442

443
    dimensions: dict[str, int] = {}
1✔
444
    specs: list[ConstantSpec] = []
1✔
445
    constant_names = {name.lower() for name in constants}
1✔
446
    for name_raw, entry in dimensions_raw.items():
1✔
447
        if not isinstance(name_raw, str):
1✔
448
            raise click.ClickException("config dimensions must use string keys")
×
449
        name = name_raw.strip()
1✔
450
        if not name:
1✔
451
            raise click.ClickException("config dimensions must have non-empty names")
×
452
        try:
1✔
453
            validate_user_fortran_identifier(name, label=f"config dimension '{name}'")
1✔
454
        except ValueError as exc:
1✔
455
            raise click.ClickException(str(exc)) from exc
1✔
456
        canonical_name = name.lower()
1✔
457
        if canonical_name in constant_names:
1✔
458
            raise click.ClickException(
1✔
459
                f"config dimension '{name}' duplicates a constant name"
460
            )
461
        if canonical_name in dimensions:
1✔
462
            raise click.ClickException(
1✔
463
                f"config dimension '{name}' duplicates another dimension name"
464
            )
465
        default_name = f"{canonical_name}__default"
1✔
466
        if default_name in constant_names:
1✔
467
            raise click.ClickException(
1✔
468
                f"config dimension '{name}' default name duplicates a constant name"
469
            )
470
        if not isinstance(entry, dict):
1✔
471
            raise click.ClickException(f"config dimension '{name}' must be a table with 'default'")
×
472
        if "value" in entry:
1✔
473
            raise click.ClickException(
1✔
474
                f"config dimension '{name}' must use 'default', not 'value'"
475
            )
476
        if "default" not in entry:
1✔
477
            raise click.ClickException(f"config dimension '{name}' must define 'default'")
×
478
        value = entry.get("default")
1✔
479
        if isinstance(value, bool) or not isinstance(value, int):
1✔
480
            raise click.ClickException(f"config dimension '{name}' default must be an integer")
×
481
        if value <= 0:
1✔
482
            raise click.ClickException(f"config dimension '{name}' default must be positive")
1✔
483
        doc = entry.get("doc")
1✔
484
        if doc is not None:
1✔
485
            if not isinstance(doc, str):
1✔
486
                raise click.ClickException(f"config dimension '{name}' doc must be a string")
×
487
            doc = " ".join(doc.splitlines()).strip() or None
1✔
488
        specs.append(
1✔
489
            ConstantSpec(
490
                name=default_name,
491
                type_spec="integer",
492
                value=str(value),
493
                doc=doc,
494
            )
495
        )
496
        dimensions[canonical_name] = value
1✔
497
    return dimensions, specs
1✔
498

499

500
def _load_bool_field(section: dict[str, Any], key: str, *, label: str) -> bool:
1✔
501
    value = section.get(key, False)
×
502
    if isinstance(value, bool):
×
503
        return value
×
504
    raise click.ClickException(f"config '{label}' must be a boolean")
×
505

506

507
def _load_documentation_settings(
1✔
508
    config: dict[str, Any],
509
) -> tuple[str | None, bool, bool, str]:
510
    doc_raw = config.get("documentation")
1✔
511
    if doc_raw is None:
1✔
512
        return None, False, False, "numpy"
1✔
513
    if not isinstance(doc_raw, dict):
×
514
        raise click.ClickException("config 'documentation' must be a table")
×
515
    module_doc = None
×
516
    if "module" in doc_raw:
×
517
        module_raw = doc_raw.get("module")
×
518
        if not isinstance(module_raw, str):
×
519
            raise click.ClickException("config 'documentation.module' must be a string")
×
520
        module_doc = module_raw.strip() or None
×
521
    md_doxygen_id_from_name = _load_bool_field(
×
522
        doc_raw,
523
        "md_doxygen_id_from_name",
524
        label="documentation.md_doxygen_id_from_name",
525
    )
526
    md_add_toc_statement = _load_bool_field(
×
527
        doc_raw,
528
        "md_add_toc_statement",
529
        label="documentation.md_add_toc_statement",
530
    )
531
    py_style_raw = doc_raw.get("py-style", "numpy")
×
532
    if not isinstance(py_style_raw, str):
×
533
        raise click.ClickException("config 'documentation.py-style' must be a string")
×
534
    py_style = py_style_raw.strip().lower()
×
535
    if py_style not in {"numpy", "doxygen"}:
×
536
        raise click.ClickException(
×
537
            "config 'documentation.py-style' must be 'numpy' or 'doxygen'"
538
        )
539
    return module_doc, md_doxygen_id_from_name, md_add_toc_statement, py_style
×
540

541

542
def _parse_cli_constants(values: tuple[tuple[str, int], ...]) -> dict[str, int]:
1✔
543
    constants: dict[str, int] = {}
1✔
544
    for name, value in values:
1✔
545
        if name in constants:
1✔
546
            raise click.ClickException(f"constant '{name}' duplicates another constant name")
1✔
547
        constants[name] = value
1✔
548
    return constants
1✔
549

550

551
def _parse_cli_dimensions(values: tuple[tuple[str, int], ...]) -> dict[str, int]:
1✔
552
    dimensions: dict[str, int] = {}
1✔
553
    for name, value in values:
1✔
554
        if name in dimensions:
1✔
555
            raise click.ClickException(f"dimension '{name}' duplicates another dimension name")
1✔
556
        if value <= 0:
1✔
557
            raise click.ClickException(f"dimension '{name}' value must be positive")
×
558
        dimensions[name] = value
1✔
559
    return dimensions
1✔
560

561

562
def _reject_constant_dimension_overlap(
1✔
563
    constants: dict[str, int],
564
    dimensions: dict[str, int],
565
) -> None:
566
    duplicate_names = constant_dimension_overlap(constants, dimensions)
1✔
567
    if duplicate_names:
1✔
568
        raise click.ClickException(
×
569
            "constants and dimensions must not share names: " + ", ".join(duplicate_names)
570
        )
571

572

573
def _iter_namelists(config: dict[str, Any], base_dir: Path) -> list[dict[str, Any]]:
1✔
574
    raw_entries = config.get("namelists")
1✔
575
    if raw_entries is None and "nml-files" in config:
1✔
576
        raise click.ClickException("config uses deprecated 'nml-files'; rename to 'namelists'")
×
577
    if not isinstance(raw_entries, list) or not raw_entries:
1✔
578
        raise click.ClickException("config must define non-empty 'namelists'")
×
579

580
    entries: list[dict[str, Path | None]] = []
1✔
581
    for entry in raw_entries:
1✔
582
        if not isinstance(entry, dict):
1✔
583
            raise click.ClickException("each namelists entry must be a table")
×
584
        schema_raw = entry.get("schema")
1✔
585
        if not isinstance(schema_raw, str):
1✔
586
            raise click.ClickException("namelists entry must define string 'schema'")
×
587
        schema_path = base_dir / schema_raw
1✔
588
        f2py_path = _resolve_optional_path(
1✔
589
            entry.get("f2py_path"),
590
            base_dir=base_dir,
591
            key="f2py_path",
592
        )
593
        py_path = _resolve_optional_path(
1✔
594
            entry.get("py_path"),
595
            base_dir=base_dir,
596
            key="py_path",
597
        )
598
        mod_path = _resolve_optional_path(
1✔
599
            entry.get("mod_path"),
600
            base_dir=base_dir,
601
            key="mod_path",
602
        )
603
        if f2py_path is not None and mod_path is None:
1✔
604
            raise click.ClickException(
×
605
                "namelists entry with 'f2py_path' must define 'mod_path'"
606
            )
607
        if py_path is not None and f2py_path is None:
1✔
608
            raise click.ClickException("namelists entry with 'py_path' must define 'f2py_path'")
×
609
        entries.append(
1✔
610
            {
611
                "schema": schema_path,
612
                "mod_path": mod_path,
613
                "doc_path": _resolve_optional_path(
614
                    entry.get("doc_path"),
615
                    base_dir=base_dir,
616
                    key="doc_path",
617
                ),
618
                "temp_path": _resolve_optional_path(
619
                    entry.get("temp_path"),
620
                    base_dir=base_dir,
621
                    key="temp_path",
622
                ),
623
                "f2py_path": f2py_path,
624
                "py_path": py_path,
625
            }
626
        )
627
    return entries
1✔
628

629

630
def _load_namelist_registry(
1✔
631
    config: dict[str, Any],
632
    base_dir: Path,
633
    resolver: SchemaResolver,
634
) -> list[LoadedNamelist]:
635
    entries = _iter_namelists(config, base_dir)
1✔
636
    loaded: list[LoadedNamelist] = []
1✔
637
    seen: dict[str, str] = {}
1✔
638
    for entry in entries:
1✔
639
        schema_path = entry["schema"]
1✔
640
        if schema_path is None:
1✔
641
            raise click.ClickException("namelists entry missing schema path")
×
642
        try:
1✔
643
            logger.debug("Loading schema %s", schema_path)
1✔
644
            schema = load_schema(schema_path, resolver=resolver)
1✔
645
        except (FileNotFoundError, ValueError) as exc:
×
646
            raise click.ClickException(str(exc)) from exc
×
647
        namelist_name = schema.get("x-fortran-namelist")
1✔
648
        if not isinstance(namelist_name, str) or not namelist_name.strip():
1✔
649
            raise click.ClickException("schema must define non-empty 'x-fortran-namelist'")
×
650
        key = namelist_name.lower()
1✔
651
        if key in seen:
1✔
652
            raise click.ClickException(f"duplicate schema for namelist '{namelist_name}'")
×
653
        seen[key] = namelist_name
1✔
654
        loaded.append(
1✔
655
            LoadedNamelist(
656
                entry=entry,
657
                schema=schema,
658
                name=namelist_name,
659
                key=key,
660
            )
661
        )
662
    return loaded
1✔
663

664

665
def _namelist_registry_by_key(
1✔
666
    registry: list[LoadedNamelist],
667
) -> dict[str, LoadedNamelist]:
668
    return {loaded.key: loaded for loaded in registry}
1✔
669

670

671
def _resolve_namelist_members(
1✔
672
    raw_names: Any,
673
    *,
674
    registry: dict[str, LoadedNamelist],
675
    label: str,
676
) -> list[LoadedNamelist]:
677
    if not isinstance(raw_names, list) or not raw_names:
1✔
678
        raise click.ClickException(f"{label} must define non-empty 'namelists'")
×
679
    resolved: list[LoadedNamelist] = []
1✔
680
    seen: set[str] = set()
1✔
681
    for raw_name in raw_names:
1✔
682
        if not isinstance(raw_name, str) or not raw_name.strip():
1✔
683
            raise click.ClickException(f"{label} 'namelists' entries must be strings")
×
684
        key = raw_name.lower()
1✔
685
        if key in seen:
1✔
686
            raise click.ClickException(f"{label} namelist '{raw_name}' duplicates another name")
1✔
687
        seen.add(key)
1✔
688
        loaded = registry.get(key)
1✔
689
        if loaded is None:
1✔
690
            raise click.ClickException(f"{label} references unknown namelist '{raw_name}'")
1✔
691
        resolved.append(loaded)
1✔
692
    return resolved
1✔
693

694

695
def _resolve_template_schema_members(
1✔
696
    raw_paths: Any,
697
    *,
698
    base_dir: Path,
699
    registry: dict[str, LoadedNamelist],
700
) -> list[LoadedNamelist]:
701
    """Resolve legacy template schema paths to configured namelist entries."""
702
    if not isinstance(raw_paths, list) or not raw_paths:
1✔
NEW
703
        raise click.ClickException("templates 'schemas' must be a non-empty list")
×
704
    by_schema_path: dict[Path, LoadedNamelist] = {}
1✔
705
    for loaded in registry.values():
1✔
706
        schema_path = loaded.entry["schema"]
1✔
707
        if isinstance(schema_path, Path):
1✔
708
            by_schema_path[schema_path.resolve()] = loaded
1✔
709

710
    resolved: list[LoadedNamelist] = []
1✔
711
    seen: set[Path] = set()
1✔
712
    for raw_path in raw_paths:
1✔
713
        if not isinstance(raw_path, str) or not raw_path.strip():
1✔
NEW
714
            raise click.ClickException("templates 'schemas' entries must be strings")
×
715
        schema_path = Path(raw_path)
1✔
716
        if not schema_path.is_absolute():
1✔
717
            schema_path = base_dir / schema_path
1✔
718
        canonical_path = schema_path.resolve()
1✔
719
        if canonical_path in seen:
1✔
720
            raise click.ClickException(
1✔
721
                f"templates schema '{raw_path}' duplicates another schema"
722
            )
723
        seen.add(canonical_path)
1✔
724
        matched = by_schema_path.get(canonical_path)
1✔
725
        if matched is None:
1✔
726
            raise click.ClickException(
1✔
727
                f"templates schema '{raw_path}' does not match a configured namelist schema"
728
            )
729
        resolved.append(matched)
1✔
730
    return resolved
1✔
731

732

733
def _optional_string(
1✔
734
    entry: dict[str, Any],
735
    key: str,
736
    *,
737
    label: str,
738
) -> str | None:
739
    value = entry.get(key)
1✔
740
    if value is None:
1✔
741
        return None
1✔
742
    if not isinstance(value, str):
1✔
743
        raise click.ClickException(f"{label} '{key}' must be a string")
1✔
744
    return value
1✔
745

746

747
def _iter_file_profiles(
1✔
748
    config: dict[str, Any],
749
    registry: dict[str, LoadedNamelist],
750
) -> dict[str, FileProfile]:
751
    raw_entries = config.get("file_profiles")
1✔
752
    if raw_entries is None:
1✔
753
        return {}
1✔
754
    if not isinstance(raw_entries, list) or not raw_entries:
1✔
755
        raise click.ClickException("config 'file_profiles' must be a non-empty list")
×
756

757
    profiles: dict[str, FileProfile] = {}
1✔
758
    for entry in raw_entries:
1✔
759
        if not isinstance(entry, dict):
1✔
760
            raise click.ClickException("each file_profiles entry must be a table")
×
761
        name = entry.get("name")
1✔
762
        if not isinstance(name, str) or not name.strip():
1✔
763
            raise click.ClickException("file_profiles entry must define string 'name'")
1✔
764
        key = name.lower()
1✔
765
        if key in profiles:
1✔
766
            raise click.ClickException(f"file profile '{name}' duplicates another profile")
1✔
767
        default_file = entry.get("default_file")
1✔
768
        if not isinstance(default_file, str) or not default_file.strip():
1✔
769
            raise click.ClickException("file_profiles entry must define string 'default_file'")
1✔
770
        members = _resolve_namelist_members(
1✔
771
            entry.get("namelists"),
772
            registry=registry,
773
            label=f"file profile '{name}'",
774
        )
775
        profiles[key] = FileProfile(
1✔
776
            name=name,
777
            key=key,
778
            default_file=default_file,
779
            namelists=[member.key for member in members],
780
            title=_optional_string(entry, "title", label=f"file profile '{name}'"),
781
            description=_optional_string(
782
                entry,
783
                "description",
784
                label=f"file profile '{name}'",
785
            ),
786
        )
787
    return profiles
1✔
788

789

790
def _iter_templates(
1✔
791
    config: dict[str, Any],
792
    base_dir: Path,
793
    registry: dict[str, LoadedNamelist],
794
    profiles: dict[str, FileProfile],
795
) -> list[dict[str, Any]]:
796
    raw_entries = config.get("templates")
1✔
797
    if raw_entries is None:
1✔
798
        return []
1✔
799
    if not isinstance(raw_entries, list) or not raw_entries:
1✔
800
        raise click.ClickException("config 'templates' must be a non-empty list")
×
801

802
    entries: list[dict[str, Any]] = []
1✔
803
    for entry in raw_entries:
1✔
804
        if not isinstance(entry, dict):
1✔
805
            raise click.ClickException("each templates entry must be a table")
×
806
        if "output" in entry:
1✔
807
            raise click.ClickException("templates use 'path', not deprecated 'output'")
1✔
808
        path_raw = entry.get("path")
1✔
809
        if not isinstance(path_raw, str):
1✔
810
            raise click.ClickException("templates entry must define string 'path'")
×
811
        output_path = base_dir / path_raw
1✔
812

813
        profile_raw = entry.get("profile")
1✔
814
        has_profile = profile_raw is not None
1✔
815
        has_namelists = "namelists" in entry
1✔
816
        has_schemas = "schemas" in entry
1✔
817
        if has_schemas and (has_profile or has_namelists):
1✔
818
            raise click.ClickException(
1✔
819
                "templates entry with 'schemas' must not define 'profile' or 'namelists'"
820
            )
821
        if not has_schemas and has_profile == has_namelists:
1✔
UNCOV
822
            raise click.ClickException(
×
823
                "templates entry must define exactly one of 'profile' or 'namelists'"
824
            )
825
        title = _optional_string(entry, "title", label="templates entry")
1✔
826
        description = _optional_string(entry, "description", label="templates entry")
1✔
827
        if has_profile:
1✔
828
            if not isinstance(profile_raw, str) or not profile_raw.strip():
1✔
829
                raise click.ClickException("templates 'profile' must be a string")
×
830
            profile = profiles.get(profile_raw.lower())
1✔
831
            if profile is None:
1✔
832
                raise click.ClickException(
×
833
                    f"templates entry references unknown profile '{profile_raw}'"
834
                )
835
            loaded_namelists = [registry[key] for key in profile.namelists]
1✔
836
            if title is None:
1✔
837
                title = profile.title
1✔
838
            if description is None:
1✔
839
                description = profile.description
×
840
        elif has_schemas:
1✔
841
            loaded_namelists = _resolve_template_schema_members(
1✔
842
                entry.get("schemas"),
843
                base_dir=base_dir,
844
                registry=registry,
845
            )
846
        else:
847
            loaded_namelists = _resolve_namelist_members(
1✔
848
                entry.get("namelists"),
849
                registry=registry,
850
                label="templates entry",
851
            )
852

853
        doc_mode = entry.get("doc_mode", "plain")
1✔
854
        if not isinstance(doc_mode, str):
1✔
855
            raise click.ClickException("templates 'doc_mode' must be a string")
×
856
        value_mode = entry.get("value_mode", "empty")
1✔
857
        if not isinstance(value_mode, str):
1✔
858
            raise click.ClickException("templates 'value_mode' must be a string")
×
859
        values_raw = entry.get("values", {})
1✔
860
        if values_raw is None:
1✔
861
            values_raw = {}
×
862
        if not isinstance(values_raw, dict):
1✔
863
            raise click.ClickException("templates 'values' must be a table")
×
864

865
        entries.append(
1✔
866
            {
867
                "path": output_path,
868
                "schemas": [loaded.schema for loaded in loaded_namelists],
869
                "doc_mode": doc_mode,
870
                "value_mode": value_mode,
871
                "title": title,
872
                "description": description,
873
                "values": values_raw,
874
            }
875
        )
876
    return entries
1✔
877

878

879
def _collect_generated_outputs(
1✔
880
    config: dict[str, Any],
881
    config_path: Path,
882
) -> list[GeneratedOutput]:
883
    base_dir = config_path.parent
1✔
884
    logger.debug("Base directory: %s", base_dir)
1✔
885
    helper_path, helper_module, helper_buffer, helper_header = _load_helper_settings(
1✔
886
        config,
887
        base_dir,
888
    )
889
    (
1✔
890
        module_doc,
891
        md_doxygen_id_from_name,
892
        md_add_toc_statement,
893
        py_style,
894
    ) = _load_documentation_settings(config)
895
    constants, constant_specs = _load_constants(config)
1✔
896
    dimensions, dimension_specs = _load_dimensions(config, constants)
1✔
897
    kind_module, kind_map, kind_allowlist = _load_kind_settings(config)
1✔
898
    f2cmap_path, f2py_c_types = _load_f2py_settings(config, base_dir)
1✔
899
    resolver = SchemaResolver()
1✔
900
    outputs: list[GeneratedOutput] = []
1✔
901

902
    loaded_namelists = _load_namelist_registry(config, base_dir, resolver)
1✔
903
    loaded_by_key = _namelist_registry_by_key(loaded_namelists)
1✔
904
    profiles = _iter_file_profiles(config, loaded_by_key)
1✔
905
    logger.debug("Found %d schema entries", len(loaded_namelists))
1✔
906
    loaded_entries: list[dict[str, Any]] = [
1✔
907
        {"entry": loaded.entry, "schema": loaded.schema} for loaded in loaded_namelists
908
    ]
909
    for loaded in loaded_namelists:
1✔
910
        namelist_entry = loaded.entry
1✔
911
        schema = loaded.schema
1✔
912

913
        mod_path = namelist_entry["mod_path"]
1✔
914
        if mod_path is not None:
1✔
915
            try:
1✔
916
                logger.debug("Rendering Fortran module at %s", mod_path)
1✔
917
                outputs.append(
1✔
918
                    GeneratedOutput(
919
                        mod_path,
920
                        render_fortran(
921
                            schema,
922
                            file_name=mod_path.name,
923
                            helper_module=helper_module,
924
                            kind_module=kind_module,
925
                            kind_map=kind_map,
926
                            kind_allowlist=kind_allowlist,
927
                            constants=constants,
928
                            dimensions=dimensions,
929
                            module_doc=module_doc,
930
                            f2py_handle_helpers=namelist_entry["f2py_path"] is not None,
931
                        ),
932
                    )
933
                )
934
            except ValueError as exc:
×
935
                raise click.ClickException(str(exc)) from exc
×
936

937
        doc_path = namelist_entry["doc_path"]
1✔
938
        if doc_path is not None:
1✔
939
            try:
1✔
940
                logger.debug("Rendering Markdown docs at %s", doc_path)
1✔
941
                outputs.append(
1✔
942
                    GeneratedOutput(
943
                        doc_path,
944
                        render_docs(
945
                            schema,
946
                            constants=constants,
947
                            dimensions=dimensions,
948
                            md_doxygen_id_from_name=md_doxygen_id_from_name,
949
                            md_add_toc_statement=md_add_toc_statement,
950
                        ),
951
                    )
952
                )
953
            except ValueError as exc:
×
954
                raise click.ClickException(str(exc)) from exc
×
955

956
    try:
1✔
957
        local_derived_types = collect_local_derived_types(
1✔
958
            [loaded.schema for loaded in loaded_namelists],
959
            constants=constants,
960
        )
961
        if local_derived_types and helper_path is None:
1✔
962
            raise ValueError("locally generated derived types require a configured helper output")
×
963
        if helper_path is not None:
1✔
964
            logger.debug("Rendering helper module at %s", helper_path)
1✔
965
            outputs.append(
1✔
966
                GeneratedOutput(
967
                    helper_path,
968
                    render_helper(
969
                        file_name=helper_path.name,
970
                        module_name=helper_module,
971
                        len_buf=helper_buffer,
972
                        constants=constant_specs + dimension_specs,
973
                        local_derived_types=local_derived_types,
974
                        kind_module=kind_module,
975
                        kind_map=kind_map,
976
                        kind_allowlist=kind_allowlist,
977
                        module_doc=module_doc,
978
                        helper_header=helper_header,
979
                    ),
980
                )
981
            )
982
    except ValueError as exc:
×
983
        raise click.ClickException(str(exc)) from exc
×
984

985
    outputs.extend(
1✔
986
        _collect_f2py_outputs(
987
            loaded_entries,
988
            helper_module=helper_module,
989
            helper_buffer=helper_buffer,
990
            kind_module=kind_module,
991
            kind_map=kind_map,
992
            kind_allowlist=kind_allowlist,
993
            constants=constants,
994
            dimensions=dimensions,
995
            f2cmap_path=f2cmap_path,
996
            f2py_c_types=f2py_c_types,
997
            py_style=py_style,
998
            include_python=True,
999
        )
1000
    )
1001

1002
    template_entries = _iter_templates(config, base_dir, loaded_by_key, profiles)
1✔
1003
    if template_entries:
1✔
1004
        logger.debug("Found %d template entries", len(template_entries))
1✔
1005
    for template_entry in template_entries:
1✔
1006
        try:
1✔
1007
            logger.debug("Rendering template at %s", template_entry["path"])
1✔
1008
            outputs.append(
1✔
1009
                GeneratedOutput(
1010
                    template_entry["path"],
1011
                    render_template(
1012
                        template_entry["schemas"],
1013
                        doc_mode=template_entry["doc_mode"],
1014
                        value_mode=template_entry["value_mode"],
1015
                        title=template_entry["title"],
1016
                        description=template_entry["description"],
1017
                        constants=constants,
1018
                        dimensions=dimensions,
1019
                        kind_map=kind_map,
1020
                        kind_allowlist=kind_allowlist,
1021
                        values=template_entry["values"],
1022
                    ),
1023
                )
1024
            )
1025
        except ValueError as exc:
×
1026
            raise click.ClickException(str(exc)) from exc
×
1027

1028
    return outputs
1✔
1029

1030

1031
def _collect_f2py_outputs(
1✔
1032
    loaded_entries: list[dict[str, Any]],
1033
    *,
1034
    helper_module: str,
1035
    helper_buffer: int,
1036
    kind_module: str,
1037
    kind_map: dict[str, str],
1038
    kind_allowlist: set[str],
1039
    constants: dict[str, int],
1040
    dimensions: dict[str, int],
1041
    f2cmap_path: Path | None,
1042
    f2py_c_types: F2pyCTypeMap,
1043
    py_style: str,
1044
    include_python: bool,
1045
) -> list[GeneratedOutput]:
1046
    outputs: list[GeneratedOutput] = []
1✔
1047
    f2py_groups: dict[Path, list[dict[str, Any]]] = {}
1✔
1048
    py_groups: dict[Path, list[tuple[dict[str, Any], Path]]] = {}
1✔
1049
    for loaded in loaded_entries:
1✔
1050
        entry = loaded["entry"]
1✔
1051
        schema = loaded["schema"]
1✔
1052
        f2py_path = entry["f2py_path"]
1✔
1053
        if f2py_path is None:
1✔
1054
            continue
1✔
1055
        f2py_groups.setdefault(f2py_path, []).append(schema)
1✔
1056
        py_path = entry["py_path"]
1✔
1057
        if include_python and py_path is not None:
1✔
1058
            py_groups.setdefault(py_path, []).append((schema, f2py_path))
1✔
1059

1060
    for f2py_path, schemas in f2py_groups.items():
1✔
1061
        try:
1✔
1062
            logger.debug("Rendering f2py wrappers at %s", f2py_path)
1✔
1063
            outputs.append(
1✔
1064
                GeneratedOutput(
1065
                    f2py_path,
1066
                    render_f2py_wrappers(
1067
                        schemas,
1068
                        file_name=f2py_path.name,
1069
                        helper_module=helper_module,
1070
                        kind_module=kind_module,
1071
                        kind_map=kind_map,
1072
                        kind_allowlist=kind_allowlist,
1073
                        constants=constants,
1074
                        dimensions=dimensions,
1075
                        errmsg_len=helper_buffer,
1076
                    ),
1077
                )
1078
            )
1079
        except ValueError as exc:
×
1080
            raise click.ClickException(str(exc)) from exc
×
1081

1082
    if f2cmap_path is not None:
1✔
1083
        try:
1✔
1084
            usage = merge_f2py_kind_usage(
1✔
1085
                collect_f2py_kind_usage(schemas, constants=constants, dimensions=dimensions)
1086
                for schemas in f2py_groups.values()
1087
            )
1088
            logger.debug("Rendering f2py kind map at %s", f2cmap_path)
1✔
1089
            outputs.append(
1✔
1090
                GeneratedOutput(
1091
                    f2cmap_path,
1092
                    render_f2cmap(usage, f2py_c_types),
1093
                )
1094
            )
1095
        except ValueError as exc:
×
1096
            raise click.ClickException(str(exc)) from exc
×
1097

1098
    if not include_python:
1✔
1099
        return outputs
1✔
1100
    for py_path, entries in py_groups.items():
1✔
1101
        try:
1✔
1102
            logger.debug("Rendering Python f2py wrappers at %s", py_path)
1✔
1103
            specs = [
1✔
1104
                (
1105
                    build_f2py_namelist_spec(
1106
                        schema,
1107
                        helper_module=helper_module,
1108
                        kind_module=kind_module,
1109
                        kind_map=kind_map,
1110
                        kind_allowlist=kind_allowlist,
1111
                        constants=constants,
1112
                        dimensions=dimensions,
1113
                        errmsg_len=helper_buffer,
1114
                    ),
1115
                    f2py_path.stem,
1116
                )
1117
                for schema, f2py_path in entries
1118
            ]
1119
            outputs.append(
1✔
1120
                GeneratedOutput(
1121
                    py_path,
1122
                    render_python_wrappers(
1123
                        specs,
1124
                        py_style=py_style,
1125
                    ),
1126
                )
1127
            )
1128
        except ValueError as exc:
×
1129
            raise click.ClickException(str(exc)) from exc
×
1130
    return outputs
1✔
1131

1132

1133
def _generate_f2py_outputs(
1✔
1134
    loaded_entries: list[dict[str, Any]],
1135
    *,
1136
    helper_module: str,
1137
    helper_buffer: int,
1138
    kind_module: str,
1139
    kind_map: dict[str, str],
1140
    kind_allowlist: set[str],
1141
    constants: dict[str, int],
1142
    dimensions: dict[str, int],
1143
    f2cmap_path: Path | None,
1144
    f2py_c_types: F2pyCTypeMap,
1145
    py_style: str,
1146
    include_python: bool,
1147
) -> None:
1148
    _write_generated_outputs(
1✔
1149
        _collect_f2py_outputs(
1150
            loaded_entries,
1151
            helper_module=helper_module,
1152
            helper_buffer=helper_buffer,
1153
            kind_module=kind_module,
1154
            kind_map=kind_map,
1155
            kind_allowlist=kind_allowlist,
1156
            constants=constants,
1157
            dimensions=dimensions,
1158
            f2cmap_path=f2cmap_path,
1159
            f2py_c_types=f2py_c_types,
1160
            py_style=py_style,
1161
            include_python=include_python,
1162
        )
1163
    )
1164

1165

1166
def _write_generated_outputs(outputs: list[GeneratedOutput]) -> None:
1✔
1167
    for output in outputs:
1✔
1168
        logger.info("Writing generated file %s", output.path)
1✔
1169
        output.path.parent.mkdir(parents=True, exist_ok=True)
1✔
1170
        output.path.write_text(output.content, encoding="ascii")
1✔
1171

1172

1173
def _check_generated_outputs(outputs: list[GeneratedOutput], *, show_diff: bool) -> int:
1✔
1174
    failed = 0
1✔
1175
    for output in outputs:
1✔
1176
        if not output.path.exists():
1✔
1177
            failed += 1
1✔
1178
            click.echo(f"MISSING: {output.path}", err=True)
1✔
1179
            continue
1✔
1180
        current = output.path.read_text(encoding="ascii")
1✔
1181
        if current != output.content:
1✔
1182
            failed += 1
1✔
1183
            click.echo(f"DIFF: {output.path}", err=True)
1✔
1184
            if show_diff:
1✔
1185
                diff = unified_diff(
1✔
1186
                    current.splitlines(keepends=True),
1187
                    output.content.splitlines(keepends=True),
1188
                    fromfile=f"current {output.path}",
1189
                    tofile=f"generated {output.path}",
1190
                )
1191
                for line in diff:
1✔
1192
                    click.echo(line, nl=False)
1✔
1193
            continue
1✔
1194
        logger.debug("OK: %s", output.path)
1✔
1195
    return failed
1✔
1196

1197

1198
@click.group(context_settings=_CONTEXT_SETTINGS)
1✔
1199
@click.version_option(__version__, "-V", "--version", prog_name="nml-tools")
1✔
1200
@click.option("--verbose", "-v", count=True, help="Increase verbosity (repeatable).")
1✔
1201
@click.option("--quiet", "-q", count=True, help="Decrease verbosity (repeatable).")
1✔
1202
def cli(verbose: int, quiet: int) -> None:
1✔
1203
    """nml-tools command line interface."""
1204
    _configure_logging(verbose, quiet)
1✔
1205

1206

1207
@cli.command("generate", context_settings=_CONTEXT_SETTINGS)
1✔
1208
@click.option(
1✔
1209
    "--config",
1210
    "config_path",
1211
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1212
    default=None,
1213
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
1214
)
1215
def generate(config_path: Path | None) -> None:
1✔
1216
    """Generate outputs from a configuration file."""
1217
    config, config_path = _load_config_checked(config_path)
1✔
1218
    logger.info("Loading config from %s", config_path)
1✔
1219
    _write_generated_outputs(_collect_generated_outputs(config, config_path))
1✔
1220

1221

1222
@cli.command("check", context_settings=_CONTEXT_SETTINGS)
1✔
1223
@click.option(
1✔
1224
    "--config",
1225
    "config_path",
1226
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1227
    default=None,
1228
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
1229
)
1230
@click.option(
1✔
1231
    "--diff",
1232
    "show_diff",
1233
    is_flag=True,
1234
    help="Show unified diffs for generated files that differ.",
1235
)
1236
def check(config_path: Path | None, show_diff: bool) -> None:
1✔
1237
    """Check that configured generated files are up to date."""
1238
    config, config_path = _load_config_checked(config_path)
1✔
1239
    logger.debug("Loading config from %s", config_path)
1✔
1240
    failures = _check_generated_outputs(
1✔
1241
        _collect_generated_outputs(config, config_path),
1242
        show_diff=show_diff,
1243
    )
1244
    if failures:
1✔
1245
        raise click.ClickException(
1✔
1246
            f"generated files are out of date: {failures} file(s) differ or are missing"
1247
        )
1248

1249

1250
@cli.command("gen-fortran", context_settings=_CONTEXT_SETTINGS)
1✔
1251
@click.option(
1✔
1252
    "--config",
1253
    "config_path",
1254
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1255
    default=None,
1256
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
1257
)
1258
def gen_fortran(config_path: Path | None) -> None:
1✔
1259
    """Generate Fortran module(s)."""
1260
    config, config_path = _load_config_checked(config_path)
1✔
1261
    logger.info("Loading config from %s", config_path)
1✔
1262
    base_dir = config_path.parent
1✔
1263
    logger.debug("Base directory: %s", base_dir)
1✔
1264
    helper_path, helper_module, helper_buffer, helper_header = _load_helper_settings(
1✔
1265
        config,
1266
        base_dir,
1267
    )
1268
    module_doc, _, _, py_style = _load_documentation_settings(config)
1✔
1269
    constants, constant_specs = _load_constants(config)
1✔
1270
    dimensions, dimension_specs = _load_dimensions(config, constants)
1✔
1271
    kind_module, kind_map, kind_allowlist = _load_kind_settings(config)
1✔
1272
    f2cmap_path, f2py_c_types = _load_f2py_settings(config, base_dir)
1✔
1273
    resolver = SchemaResolver()
1✔
1274
    entries = _iter_namelists(config, base_dir)
1✔
1275
    logger.info("Found %d schema entries", len(entries))
1✔
1276
    loaded_entries: list[dict[str, Any]] = []
1✔
1277
    for entry in entries:
1✔
1278
        schema_path = entry["schema"]
1✔
1279
        if schema_path is None:
1✔
1280
            raise click.ClickException("namelists entry missing schema path")
×
1281
        try:
1✔
1282
            logger.info("Loading schema %s", schema_path)
1✔
1283
            schema = load_schema(schema_path, resolver=resolver)
1✔
1284
        except (FileNotFoundError, ValueError) as exc:
×
1285
            raise click.ClickException(str(exc)) from exc
×
1286
        loaded_entries.append({"entry": entry, "schema": schema})
1✔
1287
        mod_path = entry["mod_path"]
1✔
1288
        if mod_path is None:
1✔
1289
            continue
×
1290
        try:
1✔
1291
            logger.info("Generating Fortran module at %s", mod_path)
1✔
1292
            generate_fortran(
1✔
1293
                schema,
1294
                mod_path,
1295
                helper_module=helper_module,
1296
                kind_module=kind_module,
1297
                kind_map=kind_map,
1298
                kind_allowlist=kind_allowlist,
1299
                constants=constants,
1300
                dimensions=dimensions,
1301
                module_doc=module_doc,
1302
                f2py_handle_helpers=entry["f2py_path"] is not None,
1303
            )
1304
        except ValueError as exc:
×
1305
            raise click.ClickException(str(exc)) from exc
×
1306

1307
    try:
1✔
1308
        local_derived_types = collect_local_derived_types(
1✔
1309
            [loaded["schema"] for loaded in loaded_entries],
1310
            constants=constants,
1311
        )
1312
        if local_derived_types and helper_path is None:
1✔
1313
            raise ValueError("locally generated derived types require a configured helper output")
×
1314
        if helper_path is not None:
1✔
1315
            logger.info("Generating helper module at %s", helper_path)
×
1316
            generate_helper(
×
1317
                helper_path,
1318
                module_name=helper_module,
1319
                len_buf=helper_buffer,
1320
                constants=constant_specs + dimension_specs,
1321
                local_derived_types=local_derived_types,
1322
                kind_module=kind_module,
1323
                kind_map=kind_map,
1324
                kind_allowlist=kind_allowlist,
1325
                module_doc=module_doc,
1326
                helper_header=helper_header,
1327
            )
1328
    except ValueError as exc:
×
1329
        raise click.ClickException(str(exc)) from exc
×
1330

1331
    _generate_f2py_outputs(
1✔
1332
        loaded_entries,
1333
        helper_module=helper_module,
1334
        helper_buffer=helper_buffer,
1335
        kind_module=kind_module,
1336
        kind_map=kind_map,
1337
        kind_allowlist=kind_allowlist,
1338
        constants=constants,
1339
        dimensions=dimensions,
1340
        f2cmap_path=f2cmap_path,
1341
        f2py_c_types=f2py_c_types,
1342
        py_style=py_style,
1343
        include_python=False,
1344
    )
1345

1346

1347
@cli.command("validate", context_settings=_CONTEXT_SETTINGS)
1✔
1348
@click.option(
1✔
1349
    "--config",
1350
    "config_path",
1351
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1352
    default=None,
1353
)
1354
@click.option(
1✔
1355
    "--schema",
1356
    "schema_paths",
1357
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1358
    multiple=True,
1359
)
1360
@click.option(
1✔
1361
    "--input",
1362
    "input_option",
1363
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1364
)
1365
@click.option(
1✔
1366
    "--profile",
1367
    "profile_name",
1368
    help="Validate against one configured file profile.",
1369
)
1370
@click.option(
1✔
1371
    "--constants",
1372
    "constant_args",
1373
    metavar="NAME=INT",
1374
    type=_CONSTANT_TYPE,
1375
    multiple=True,
1376
    help="Additional integer constants as NAME=INT (repeatable).",
1377
)
1378
@click.option(
1✔
1379
    "--dimensions",
1380
    "dimension_args",
1381
    metavar="NAME=INT",
1382
    type=_DIMENSION_TYPE,
1383
    multiple=True,
1384
    help="Runtime dimensions as NAME=INT (repeatable).",
1385
)
1386
@click.argument(
1✔
1387
    "input_path",
1388
    required=False,
1389
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1390
)
1391
def validate(
1✔
1392
    config_path: Path | None,
1393
    schema_paths: tuple[Path, ...],
1394
    input_option: Path | None,
1395
    profile_name: str | None,
1396
    constant_args: tuple[tuple[str, int], ...],
1397
    dimension_args: tuple[tuple[str, int], ...],
1398
    input_path: Path | None,
1399
) -> None:
1400
    """Validate a namelist file against schema definitions."""
1401
    if input_option is not None and input_path is not None:
1✔
1402
        raise click.ClickException("input path provided twice")
×
1403
    input_path = input_option or input_path
1✔
1404
    if input_path is None:
1✔
1405
        raise click.ClickException("input path is required")
×
1406
    constants = _parse_cli_constants(constant_args)
1✔
1407
    dimension_overrides = _parse_cli_dimensions(dimension_args)
1✔
1408
    dimensions: dict[str, int] = {}
1✔
1409
    schemas: list[dict[str, Any]] = []
1✔
1410
    resolver = SchemaResolver()
1✔
1411

1412
    if schema_paths:
1✔
1413
        if profile_name is not None:
1✔
1414
            raise click.ClickException("--profile can only be used with config-based validation")
1✔
1415
        if config_path is not None:
1✔
1416
            config, config_path = _load_config_checked(config_path)
×
1417
            logger.info("Loading config from %s", config_path)
×
1418
            cfg_constants, _ = _load_constants(config)
×
1419
            dimensions, _ = _load_dimensions(config, cfg_constants)
×
1420
            constants = {**cfg_constants, **constants}
×
1421
            dimensions = {**dimensions, **dimension_overrides}
×
1422
        else:
1423
            dimensions = dimension_overrides
1✔
1424
        for schema_file in schema_paths:
1✔
1425
            try:
1✔
1426
                logger.info("Loading schema %s", schema_file)
1✔
1427
                schemas.append(load_schema(schema_file, resolver=resolver))
1✔
1428
            except (FileNotFoundError, ValueError) as exc:
×
1429
                raise click.ClickException(str(exc)) from exc
×
1430
        require_all = True
1✔
1431
    else:
1432
        config, config_path = _load_config_checked(config_path)
1✔
1433
        logger.info("Loading config from %s", config_path)
1✔
1434
        base_dir = config_path.parent
1✔
1435
        cfg_constants, _ = _load_constants(config)
1✔
1436
        dimensions, _ = _load_dimensions(config, cfg_constants)
1✔
1437
        constants = {**cfg_constants, **constants}
1✔
1438
        dimensions = {**dimensions, **dimension_overrides}
1✔
1439
        loaded_namelists = _load_namelist_registry(config, base_dir, resolver)
1✔
1440
        loaded_by_key = _namelist_registry_by_key(loaded_namelists)
1✔
1441
        if profile_name is not None:
1✔
1442
            profiles = _iter_file_profiles(config, loaded_by_key)
1✔
1443
            profile = profiles.get(profile_name.lower())
1✔
1444
            if profile is None:
1✔
1445
                raise click.ClickException(f"unknown file profile '{profile_name}'")
×
1446
            loaded_namelists = [loaded_by_key[key] for key in profile.namelists]
1✔
1447
        logger.info("Found %d schema entries", len(loaded_namelists))
1✔
1448
        schemas = [loaded.schema for loaded in loaded_namelists]
1✔
1449
        require_all = False
1✔
1450

1451
    if not schemas:
1✔
1452
        raise click.ClickException("no schemas provided for validation")
×
1453
    _reject_constant_dimension_overlap(constants, dimensions)
1✔
1454

1455
    try:
1✔
1456
        logger.info("Reading namelist %s", input_path)
1✔
1457
        namelist_file = f90nml.read(input_path)
1✔
1458
    except Exception as exc:  # pragma: no cover - f90nml raises custom errors
1459
        raise click.ClickException(f"failed to read namelist: {exc}") from exc
1460

1461
    file_entries: dict[str, tuple[str, Any]] = {}
1✔
1462
    for name, values in namelist_file.items():
1✔
1463
        if not isinstance(name, str):
1✔
1464
            raise click.ClickException("namelist names must be strings")
×
1465
        key = name.lower()
1✔
1466
        if key in file_entries:
1✔
1467
            raise click.ClickException(f"namelist '{name}' appears multiple times")
×
1468
        file_entries[key] = (name, values)
1✔
1469

1470
    schema_entries: dict[str, dict[str, Any]] = {}
1✔
1471
    for schema in schemas:
1✔
1472
        namelist_name = schema.get("x-fortran-namelist")
1✔
1473
        if not isinstance(namelist_name, str) or not namelist_name.strip():
1✔
1474
            raise click.ClickException("schema must define non-empty 'x-fortran-namelist'")
×
1475
        key = namelist_name.lower()
1✔
1476
        if key in schema_entries:
1✔
1477
            raise click.ClickException(f"duplicate schema for namelist '{namelist_name}'")
×
1478
        schema_entries[key] = schema
1✔
1479

1480
    for key, (name, _) in file_entries.items():
1✔
1481
        if key not in schema_entries:
1✔
1482
            raise click.ClickException(f"input contains unknown namelist '{name}'")
1✔
1483

1484
    validated = 0
1✔
1485
    for key, schema in schema_entries.items():
1✔
1486
        if key not in file_entries:
1✔
1487
            if require_all:
×
1488
                raise click.ClickException(
×
1489
                    f"input is missing namelist '{schema.get('x-fortran-namelist')}'"
1490
                )
1491
            continue
×
1492
        try:
1✔
1493
            validate_namelist(
1✔
1494
                schema,
1495
                _normalize_f90nml_values(file_entries[key][1]),
1496
                constants=constants,
1497
                dimensions=dimensions,
1498
            )
1499
        except ValueError as exc:
×
1500
            raise click.ClickException(str(exc)) from exc
×
1501
        validated += 1
1✔
1502
    logger.info("Validation completed (%d namelist%s).", validated, "" if validated == 1 else "s")
1✔
1503

1504

1505
@cli.command("gen-markdown", context_settings=_CONTEXT_SETTINGS)
1✔
1506
@click.option(
1✔
1507
    "--config",
1508
    "config_path",
1509
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1510
    default=None,
1511
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
1512
)
1513
def gen_markdown(config_path: Path | None) -> None:
1✔
1514
    """Generate Markdown docs."""
1515
    config, config_path = _load_config_checked(config_path)
1✔
1516
    logger.info("Loading config from %s", config_path)
1✔
1517
    base_dir = config_path.parent
1✔
1518
    constants, _ = _load_constants(config)
1✔
1519
    dimensions, _ = _load_dimensions(config, constants)
1✔
1520
    _, md_doxygen_id_from_name, md_add_toc_statement, _ = _load_documentation_settings(
1✔
1521
        config
1522
    )
1523
    entries = _iter_namelists(config, base_dir)
1✔
1524
    resolver = SchemaResolver()
1✔
1525
    logger.info("Found %d schema entries", len(entries))
1✔
1526
    for entry in entries:
1✔
1527
        schema_path = entry["schema"]
1✔
1528
        if schema_path is None:
1✔
1529
            raise click.ClickException("namelists entry missing schema path")
×
1530
        try:
1✔
1531
            logger.info("Loading schema %s", schema_path)
1✔
1532
            schema = load_schema(schema_path, resolver=resolver)
1✔
1533
        except (FileNotFoundError, ValueError) as exc:
×
1534
            raise click.ClickException(str(exc)) from exc
×
1535
        doc_path = entry["doc_path"]
1✔
1536
        if doc_path is None:
1✔
1537
            continue
×
1538
        try:
1✔
1539
            logger.info("Generating Markdown docs at %s", doc_path)
1✔
1540
            generate_docs(
1✔
1541
                schema,
1542
                doc_path,
1543
                constants=constants,
1544
                dimensions=dimensions,
1545
                md_doxygen_id_from_name=md_doxygen_id_from_name,
1546
                md_add_toc_statement=md_add_toc_statement,
1547
            )
1548
        except ValueError as exc:
×
1549
            raise click.ClickException(str(exc)) from exc
×
1550

1551

1552
@cli.command("gen-template", context_settings=_CONTEXT_SETTINGS)
1✔
1553
@click.option(
1✔
1554
    "--config",
1555
    "config_path",
1556
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1557
    default=None,
1558
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
1559
)
1560
def gen_template(config_path: Path | None) -> None:
1✔
1561
    """Generate template namelist(s)."""
1562
    config, config_path = _load_config_checked(config_path)
1✔
1563
    logger.info("Loading config from %s", config_path)
1✔
1564
    base_dir = config_path.parent
1✔
1565
    constants, _ = _load_constants(config)
1✔
1566
    dimensions, _ = _load_dimensions(config, constants)
1✔
1567
    _, kind_map, kind_allowlist = _load_kind_settings(config)
1✔
1568
    resolver = SchemaResolver()
1✔
1569
    loaded_namelists = _load_namelist_registry(config, base_dir, resolver)
1✔
1570
    loaded_by_key = _namelist_registry_by_key(loaded_namelists)
1✔
1571
    profiles = _iter_file_profiles(config, loaded_by_key)
1✔
1572
    templates = _iter_templates(config, base_dir, loaded_by_key, profiles)
1✔
1573
    if not templates:
1✔
1574
        raise click.ClickException("config must define non-empty 'templates'")
×
1575
    logger.info("Found %d template entries", len(templates))
1✔
1576
    for entry in templates:
1✔
1577
        try:
1✔
1578
            logger.info("Generating template at %s", entry["path"])
1✔
1579
            generate_template(
1✔
1580
                entry["schemas"],
1581
                entry["path"],
1582
                doc_mode=entry["doc_mode"],
1583
                value_mode=entry["value_mode"],
1584
                title=entry["title"],
1585
                description=entry["description"],
1586
                constants=constants,
1587
                dimensions=dimensions,
1588
                kind_map=kind_map,
1589
                kind_allowlist=kind_allowlist,
1590
                values=entry["values"],
1591
            )
1592
        except ValueError as exc:
×
1593
            raise click.ClickException(str(exc)) from exc
×
1594

1595

1596
def main(argv: list[str] | None = None) -> int:
1✔
1597
    """Entry point for the CLI."""
1598
    try:
×
1599
        cli.main(args=argv, prog_name="nml-tools", standalone_mode=False)
×
1600
    except Exit as exc:
×
1601
        return exc.exit_code
×
1602
    except click.ClickException as exc:
×
1603
        exc.show()
×
1604
        return 1
×
1605
    return 0
×
1606

1607

1608
if __name__ == "__main__":  # pragma: no cover - CLI entry point
1609
    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