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

MuellerSeb / nml-tools / 26756350519

01 Jun 2026 12:58PM UTC coverage: 83.989% (+0.3%) from 83.722%
26756350519

push

github

web-flow
Merge pull request #36 from MuellerSeb/add_file_profiles

Add Namelist File Profiles

139 of 156 new or added lines in 2 files covered. (89.1%)

3908 of 4653 relevant lines covered (83.99%)

0.84 hits per line

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

80.96
/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✔
NEW
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✔
NEW
645
        except (FileNotFoundError, ValueError) as exc:
×
NEW
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✔
NEW
649
            raise click.ClickException("schema must define non-empty 'x-fortran-namelist'")
×
650
        key = namelist_name.lower()
1✔
651
        if key in seen:
1✔
NEW
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✔
NEW
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✔
NEW
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 _optional_string(
1✔
696
    entry: dict[str, Any],
697
    key: str,
698
    *,
699
    label: str,
700
) -> str | None:
701
    value = entry.get(key)
1✔
702
    if value is None:
1✔
703
        return None
1✔
704
    if not isinstance(value, str):
1✔
705
        raise click.ClickException(f"{label} '{key}' must be a string")
1✔
706
    return value
1✔
707

708

709
def _iter_file_profiles(
1✔
710
    config: dict[str, Any],
711
    registry: dict[str, LoadedNamelist],
712
) -> dict[str, FileProfile]:
713
    raw_entries = config.get("file_profiles")
1✔
714
    if raw_entries is None:
1✔
715
        return {}
1✔
716
    if not isinstance(raw_entries, list) or not raw_entries:
1✔
NEW
717
        raise click.ClickException("config 'file_profiles' must be a non-empty list")
×
718

719
    profiles: dict[str, FileProfile] = {}
1✔
720
    for entry in raw_entries:
1✔
721
        if not isinstance(entry, dict):
1✔
NEW
722
            raise click.ClickException("each file_profiles entry must be a table")
×
723
        name = entry.get("name")
1✔
724
        if not isinstance(name, str) or not name.strip():
1✔
725
            raise click.ClickException("file_profiles entry must define string 'name'")
1✔
726
        key = name.lower()
1✔
727
        if key in profiles:
1✔
728
            raise click.ClickException(f"file profile '{name}' duplicates another profile")
1✔
729
        default_file = entry.get("default_file")
1✔
730
        if not isinstance(default_file, str) or not default_file.strip():
1✔
731
            raise click.ClickException("file_profiles entry must define string 'default_file'")
1✔
732
        members = _resolve_namelist_members(
1✔
733
            entry.get("namelists"),
734
            registry=registry,
735
            label=f"file profile '{name}'",
736
        )
737
        profiles[key] = FileProfile(
1✔
738
            name=name,
739
            key=key,
740
            default_file=default_file,
741
            namelists=[member.key for member in members],
742
            title=_optional_string(entry, "title", label=f"file profile '{name}'"),
743
            description=_optional_string(
744
                entry,
745
                "description",
746
                label=f"file profile '{name}'",
747
            ),
748
        )
749
    return profiles
1✔
750

751

752
def _iter_templates(
1✔
753
    config: dict[str, Any],
754
    base_dir: Path,
755
    registry: dict[str, LoadedNamelist],
756
    profiles: dict[str, FileProfile],
757
) -> list[dict[str, Any]]:
758
    raw_entries = config.get("templates")
1✔
759
    if raw_entries is None:
1✔
760
        return []
1✔
761
    if not isinstance(raw_entries, list) or not raw_entries:
1✔
762
        raise click.ClickException("config 'templates' must be a non-empty list")
×
763

764
    entries: list[dict[str, Any]] = []
1✔
765
    for entry in raw_entries:
1✔
766
        if not isinstance(entry, dict):
1✔
767
            raise click.ClickException("each templates entry must be a table")
×
768
        if "output" in entry:
1✔
769
            raise click.ClickException("templates use 'path', not deprecated 'output'")
1✔
770
        if "schemas" in entry:
1✔
771
            raise click.ClickException("templates use 'namelists', not deprecated 'schemas'")
1✔
772
        path_raw = entry.get("path")
1✔
773
        if not isinstance(path_raw, str):
1✔
NEW
774
            raise click.ClickException("templates entry must define string 'path'")
×
775
        output_path = base_dir / path_raw
1✔
776

777
        profile_raw = entry.get("profile")
1✔
778
        has_profile = profile_raw is not None
1✔
779
        has_namelists = "namelists" in entry
1✔
780
        if has_profile == has_namelists:
1✔
NEW
781
            raise click.ClickException(
×
782
                "templates entry must define exactly one of 'profile' or 'namelists'"
783
            )
784
        title = _optional_string(entry, "title", label="templates entry")
1✔
785
        description = _optional_string(entry, "description", label="templates entry")
1✔
786
        if has_profile:
1✔
787
            if not isinstance(profile_raw, str) or not profile_raw.strip():
1✔
NEW
788
                raise click.ClickException("templates 'profile' must be a string")
×
789
            profile = profiles.get(profile_raw.lower())
1✔
790
            if profile is None:
1✔
NEW
791
                raise click.ClickException(
×
792
                    f"templates entry references unknown profile '{profile_raw}'"
793
                )
794
            loaded_namelists = [registry[key] for key in profile.namelists]
1✔
795
            if title is None:
1✔
796
                title = profile.title
1✔
797
            if description is None:
1✔
NEW
798
                description = profile.description
×
799
        else:
800
            loaded_namelists = _resolve_namelist_members(
1✔
801
                entry.get("namelists"),
802
                registry=registry,
803
                label="templates entry",
804
            )
805

806
        doc_mode = entry.get("doc_mode", "plain")
1✔
807
        if not isinstance(doc_mode, str):
1✔
808
            raise click.ClickException("templates 'doc_mode' must be a string")
×
809
        value_mode = entry.get("value_mode", "empty")
1✔
810
        if not isinstance(value_mode, str):
1✔
811
            raise click.ClickException("templates 'value_mode' must be a string")
×
812
        values_raw = entry.get("values", {})
1✔
813
        if values_raw is None:
1✔
814
            values_raw = {}
×
815
        if not isinstance(values_raw, dict):
1✔
816
            raise click.ClickException("templates 'values' must be a table")
×
817

818
        entries.append(
1✔
819
            {
820
                "path": output_path,
821
                "schemas": [loaded.schema for loaded in loaded_namelists],
822
                "doc_mode": doc_mode,
823
                "value_mode": value_mode,
824
                "title": title,
825
                "description": description,
826
                "values": values_raw,
827
            }
828
        )
829
    return entries
1✔
830

831

832
def _collect_generated_outputs(
1✔
833
    config: dict[str, Any],
834
    config_path: Path,
835
) -> list[GeneratedOutput]:
836
    base_dir = config_path.parent
1✔
837
    logger.debug("Base directory: %s", base_dir)
1✔
838
    helper_path, helper_module, helper_buffer, helper_header = _load_helper_settings(
1✔
839
        config,
840
        base_dir,
841
    )
842
    (
1✔
843
        module_doc,
844
        md_doxygen_id_from_name,
845
        md_add_toc_statement,
846
        py_style,
847
    ) = _load_documentation_settings(config)
848
    constants, constant_specs = _load_constants(config)
1✔
849
    dimensions, dimension_specs = _load_dimensions(config, constants)
1✔
850
    kind_module, kind_map, kind_allowlist = _load_kind_settings(config)
1✔
851
    f2cmap_path, f2py_c_types = _load_f2py_settings(config, base_dir)
1✔
852
    resolver = SchemaResolver()
1✔
853
    outputs: list[GeneratedOutput] = []
1✔
854

855
    loaded_namelists = _load_namelist_registry(config, base_dir, resolver)
1✔
856
    loaded_by_key = _namelist_registry_by_key(loaded_namelists)
1✔
857
    profiles = _iter_file_profiles(config, loaded_by_key)
1✔
858
    logger.debug("Found %d schema entries", len(loaded_namelists))
1✔
859
    loaded_entries: list[dict[str, Any]] = [
1✔
860
        {"entry": loaded.entry, "schema": loaded.schema} for loaded in loaded_namelists
861
    ]
862
    for loaded in loaded_namelists:
1✔
863
        namelist_entry = loaded.entry
1✔
864
        schema = loaded.schema
1✔
865

866
        mod_path = namelist_entry["mod_path"]
1✔
867
        if mod_path is not None:
1✔
868
            try:
1✔
869
                logger.debug("Rendering Fortran module at %s", mod_path)
1✔
870
                outputs.append(
1✔
871
                    GeneratedOutput(
872
                        mod_path,
873
                        render_fortran(
874
                            schema,
875
                            file_name=mod_path.name,
876
                            helper_module=helper_module,
877
                            kind_module=kind_module,
878
                            kind_map=kind_map,
879
                            kind_allowlist=kind_allowlist,
880
                            constants=constants,
881
                            dimensions=dimensions,
882
                            module_doc=module_doc,
883
                            f2py_handle_helpers=namelist_entry["f2py_path"] is not None,
884
                        ),
885
                    )
886
                )
887
            except ValueError as exc:
×
888
                raise click.ClickException(str(exc)) from exc
×
889

890
        doc_path = namelist_entry["doc_path"]
1✔
891
        if doc_path is not None:
1✔
892
            try:
1✔
893
                logger.debug("Rendering Markdown docs at %s", doc_path)
1✔
894
                outputs.append(
1✔
895
                    GeneratedOutput(
896
                        doc_path,
897
                        render_docs(
898
                            schema,
899
                            constants=constants,
900
                            dimensions=dimensions,
901
                            md_doxygen_id_from_name=md_doxygen_id_from_name,
902
                            md_add_toc_statement=md_add_toc_statement,
903
                        ),
904
                    )
905
                )
906
            except ValueError as exc:
×
907
                raise click.ClickException(str(exc)) from exc
×
908

909
    try:
1✔
910
        local_derived_types = collect_local_derived_types(
1✔
911
            [loaded.schema for loaded in loaded_namelists],
912
            constants=constants,
913
        )
914
        if local_derived_types and helper_path is None:
1✔
915
            raise ValueError("locally generated derived types require a configured helper output")
×
916
        if helper_path is not None:
1✔
917
            logger.debug("Rendering helper module at %s", helper_path)
1✔
918
            outputs.append(
1✔
919
                GeneratedOutput(
920
                    helper_path,
921
                    render_helper(
922
                        file_name=helper_path.name,
923
                        module_name=helper_module,
924
                        len_buf=helper_buffer,
925
                        constants=constant_specs + dimension_specs,
926
                        local_derived_types=local_derived_types,
927
                        kind_module=kind_module,
928
                        kind_map=kind_map,
929
                        kind_allowlist=kind_allowlist,
930
                        module_doc=module_doc,
931
                        helper_header=helper_header,
932
                    ),
933
                )
934
            )
935
    except ValueError as exc:
×
936
        raise click.ClickException(str(exc)) from exc
×
937

938
    outputs.extend(
1✔
939
        _collect_f2py_outputs(
940
            loaded_entries,
941
            helper_module=helper_module,
942
            helper_buffer=helper_buffer,
943
            kind_module=kind_module,
944
            kind_map=kind_map,
945
            kind_allowlist=kind_allowlist,
946
            constants=constants,
947
            dimensions=dimensions,
948
            f2cmap_path=f2cmap_path,
949
            f2py_c_types=f2py_c_types,
950
            py_style=py_style,
951
            include_python=True,
952
        )
953
    )
954

955
    template_entries = _iter_templates(config, base_dir, loaded_by_key, profiles)
1✔
956
    if template_entries:
1✔
957
        logger.debug("Found %d template entries", len(template_entries))
1✔
958
    for template_entry in template_entries:
1✔
959
        try:
1✔
960
            logger.debug("Rendering template at %s", template_entry["path"])
1✔
961
            outputs.append(
1✔
962
                GeneratedOutput(
963
                    template_entry["path"],
964
                    render_template(
965
                        template_entry["schemas"],
966
                        doc_mode=template_entry["doc_mode"],
967
                        value_mode=template_entry["value_mode"],
968
                        title=template_entry["title"],
969
                        description=template_entry["description"],
970
                        constants=constants,
971
                        dimensions=dimensions,
972
                        kind_map=kind_map,
973
                        kind_allowlist=kind_allowlist,
974
                        values=template_entry["values"],
975
                    ),
976
                )
977
            )
NEW
978
        except ValueError as exc:
×
979
            raise click.ClickException(str(exc)) from exc
×
980

981
    return outputs
1✔
982

983

984
def _collect_f2py_outputs(
1✔
985
    loaded_entries: list[dict[str, Any]],
986
    *,
987
    helper_module: str,
988
    helper_buffer: int,
989
    kind_module: str,
990
    kind_map: dict[str, str],
991
    kind_allowlist: set[str],
992
    constants: dict[str, int],
993
    dimensions: dict[str, int],
994
    f2cmap_path: Path | None,
995
    f2py_c_types: F2pyCTypeMap,
996
    py_style: str,
997
    include_python: bool,
998
) -> list[GeneratedOutput]:
999
    outputs: list[GeneratedOutput] = []
1✔
1000
    f2py_groups: dict[Path, list[dict[str, Any]]] = {}
1✔
1001
    py_groups: dict[Path, list[tuple[dict[str, Any], Path]]] = {}
1✔
1002
    for loaded in loaded_entries:
1✔
1003
        entry = loaded["entry"]
1✔
1004
        schema = loaded["schema"]
1✔
1005
        f2py_path = entry["f2py_path"]
1✔
1006
        if f2py_path is None:
1✔
1007
            continue
1✔
1008
        f2py_groups.setdefault(f2py_path, []).append(schema)
1✔
1009
        py_path = entry["py_path"]
1✔
1010
        if include_python and py_path is not None:
1✔
1011
            py_groups.setdefault(py_path, []).append((schema, f2py_path))
1✔
1012

1013
    for f2py_path, schemas in f2py_groups.items():
1✔
1014
        try:
1✔
1015
            logger.debug("Rendering f2py wrappers at %s", f2py_path)
1✔
1016
            outputs.append(
1✔
1017
                GeneratedOutput(
1018
                    f2py_path,
1019
                    render_f2py_wrappers(
1020
                        schemas,
1021
                        file_name=f2py_path.name,
1022
                        helper_module=helper_module,
1023
                        kind_module=kind_module,
1024
                        kind_map=kind_map,
1025
                        kind_allowlist=kind_allowlist,
1026
                        constants=constants,
1027
                        dimensions=dimensions,
1028
                        errmsg_len=helper_buffer,
1029
                    ),
1030
                )
1031
            )
1032
        except ValueError as exc:
×
1033
            raise click.ClickException(str(exc)) from exc
×
1034

1035
    if f2cmap_path is not None:
1✔
1036
        try:
1✔
1037
            usage = merge_f2py_kind_usage(
1✔
1038
                collect_f2py_kind_usage(schemas, constants=constants, dimensions=dimensions)
1039
                for schemas in f2py_groups.values()
1040
            )
1041
            logger.debug("Rendering f2py kind map at %s", f2cmap_path)
1✔
1042
            outputs.append(
1✔
1043
                GeneratedOutput(
1044
                    f2cmap_path,
1045
                    render_f2cmap(usage, f2py_c_types),
1046
                )
1047
            )
1048
        except ValueError as exc:
×
1049
            raise click.ClickException(str(exc)) from exc
×
1050

1051
    if not include_python:
1✔
1052
        return outputs
1✔
1053
    for py_path, entries in py_groups.items():
1✔
1054
        try:
1✔
1055
            logger.debug("Rendering Python f2py wrappers at %s", py_path)
1✔
1056
            specs = [
1✔
1057
                (
1058
                    build_f2py_namelist_spec(
1059
                        schema,
1060
                        helper_module=helper_module,
1061
                        kind_module=kind_module,
1062
                        kind_map=kind_map,
1063
                        kind_allowlist=kind_allowlist,
1064
                        constants=constants,
1065
                        dimensions=dimensions,
1066
                        errmsg_len=helper_buffer,
1067
                    ),
1068
                    f2py_path.stem,
1069
                )
1070
                for schema, f2py_path in entries
1071
            ]
1072
            outputs.append(
1✔
1073
                GeneratedOutput(
1074
                    py_path,
1075
                    render_python_wrappers(
1076
                        specs,
1077
                        py_style=py_style,
1078
                    ),
1079
                )
1080
            )
1081
        except ValueError as exc:
×
1082
            raise click.ClickException(str(exc)) from exc
×
1083
    return outputs
1✔
1084

1085

1086
def _generate_f2py_outputs(
1✔
1087
    loaded_entries: list[dict[str, Any]],
1088
    *,
1089
    helper_module: str,
1090
    helper_buffer: int,
1091
    kind_module: str,
1092
    kind_map: dict[str, str],
1093
    kind_allowlist: set[str],
1094
    constants: dict[str, int],
1095
    dimensions: dict[str, int],
1096
    f2cmap_path: Path | None,
1097
    f2py_c_types: F2pyCTypeMap,
1098
    py_style: str,
1099
    include_python: bool,
1100
) -> None:
1101
    _write_generated_outputs(
1✔
1102
        _collect_f2py_outputs(
1103
            loaded_entries,
1104
            helper_module=helper_module,
1105
            helper_buffer=helper_buffer,
1106
            kind_module=kind_module,
1107
            kind_map=kind_map,
1108
            kind_allowlist=kind_allowlist,
1109
            constants=constants,
1110
            dimensions=dimensions,
1111
            f2cmap_path=f2cmap_path,
1112
            f2py_c_types=f2py_c_types,
1113
            py_style=py_style,
1114
            include_python=include_python,
1115
        )
1116
    )
1117

1118

1119
def _write_generated_outputs(outputs: list[GeneratedOutput]) -> None:
1✔
1120
    for output in outputs:
1✔
1121
        logger.info("Writing generated file %s", output.path)
1✔
1122
        output.path.parent.mkdir(parents=True, exist_ok=True)
1✔
1123
        output.path.write_text(output.content, encoding="ascii")
1✔
1124

1125

1126
def _check_generated_outputs(outputs: list[GeneratedOutput], *, show_diff: bool) -> int:
1✔
1127
    failed = 0
1✔
1128
    for output in outputs:
1✔
1129
        if not output.path.exists():
1✔
1130
            failed += 1
1✔
1131
            click.echo(f"MISSING: {output.path}", err=True)
1✔
1132
            continue
1✔
1133
        current = output.path.read_text(encoding="ascii")
1✔
1134
        if current != output.content:
1✔
1135
            failed += 1
1✔
1136
            click.echo(f"DIFF: {output.path}", err=True)
1✔
1137
            if show_diff:
1✔
1138
                diff = unified_diff(
1✔
1139
                    current.splitlines(keepends=True),
1140
                    output.content.splitlines(keepends=True),
1141
                    fromfile=f"current {output.path}",
1142
                    tofile=f"generated {output.path}",
1143
                )
1144
                for line in diff:
1✔
1145
                    click.echo(line, nl=False)
1✔
1146
            continue
1✔
1147
        logger.debug("OK: %s", output.path)
1✔
1148
    return failed
1✔
1149

1150

1151
@click.group(context_settings=_CONTEXT_SETTINGS)
1✔
1152
@click.version_option(__version__, "-V", "--version", prog_name="nml-tools")
1✔
1153
@click.option("--verbose", "-v", count=True, help="Increase verbosity (repeatable).")
1✔
1154
@click.option("--quiet", "-q", count=True, help="Decrease verbosity (repeatable).")
1✔
1155
def cli(verbose: int, quiet: int) -> None:
1✔
1156
    """nml-tools command line interface."""
1157
    _configure_logging(verbose, quiet)
1✔
1158

1159

1160
@cli.command("generate", context_settings=_CONTEXT_SETTINGS)
1✔
1161
@click.option(
1✔
1162
    "--config",
1163
    "config_path",
1164
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1165
    default=None,
1166
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
1167
)
1168
def generate(config_path: Path | None) -> None:
1✔
1169
    """Generate outputs from a configuration file."""
1170
    config, config_path = _load_config_checked(config_path)
1✔
1171
    logger.info("Loading config from %s", config_path)
1✔
1172
    _write_generated_outputs(_collect_generated_outputs(config, config_path))
1✔
1173

1174

1175
@cli.command("check", context_settings=_CONTEXT_SETTINGS)
1✔
1176
@click.option(
1✔
1177
    "--config",
1178
    "config_path",
1179
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1180
    default=None,
1181
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
1182
)
1183
@click.option(
1✔
1184
    "--diff",
1185
    "show_diff",
1186
    is_flag=True,
1187
    help="Show unified diffs for generated files that differ.",
1188
)
1189
def check(config_path: Path | None, show_diff: bool) -> None:
1✔
1190
    """Check that configured generated files are up to date."""
1191
    config, config_path = _load_config_checked(config_path)
1✔
1192
    logger.debug("Loading config from %s", config_path)
1✔
1193
    failures = _check_generated_outputs(
1✔
1194
        _collect_generated_outputs(config, config_path),
1195
        show_diff=show_diff,
1196
    )
1197
    if failures:
1✔
1198
        raise click.ClickException(
1✔
1199
            f"generated files are out of date: {failures} file(s) differ or are missing"
1200
        )
1201

1202

1203
@cli.command("gen-fortran", context_settings=_CONTEXT_SETTINGS)
1✔
1204
@click.option(
1✔
1205
    "--config",
1206
    "config_path",
1207
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1208
    default=None,
1209
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
1210
)
1211
def gen_fortran(config_path: Path | None) -> None:
1✔
1212
    """Generate Fortran module(s)."""
1213
    config, config_path = _load_config_checked(config_path)
1✔
1214
    logger.info("Loading config from %s", config_path)
1✔
1215
    base_dir = config_path.parent
1✔
1216
    logger.debug("Base directory: %s", base_dir)
1✔
1217
    helper_path, helper_module, helper_buffer, helper_header = _load_helper_settings(
1✔
1218
        config,
1219
        base_dir,
1220
    )
1221
    module_doc, _, _, py_style = _load_documentation_settings(config)
1✔
1222
    constants, constant_specs = _load_constants(config)
1✔
1223
    dimensions, dimension_specs = _load_dimensions(config, constants)
1✔
1224
    kind_module, kind_map, kind_allowlist = _load_kind_settings(config)
1✔
1225
    f2cmap_path, f2py_c_types = _load_f2py_settings(config, base_dir)
1✔
1226
    resolver = SchemaResolver()
1✔
1227
    entries = _iter_namelists(config, base_dir)
1✔
1228
    logger.info("Found %d schema entries", len(entries))
1✔
1229
    loaded_entries: list[dict[str, Any]] = []
1✔
1230
    for entry in entries:
1✔
1231
        schema_path = entry["schema"]
1✔
1232
        if schema_path is None:
1✔
1233
            raise click.ClickException("namelists entry missing schema path")
×
1234
        try:
1✔
1235
            logger.info("Loading schema %s", schema_path)
1✔
1236
            schema = load_schema(schema_path, resolver=resolver)
1✔
1237
        except (FileNotFoundError, ValueError) as exc:
×
1238
            raise click.ClickException(str(exc)) from exc
×
1239
        loaded_entries.append({"entry": entry, "schema": schema})
1✔
1240
        mod_path = entry["mod_path"]
1✔
1241
        if mod_path is None:
1✔
1242
            continue
×
1243
        try:
1✔
1244
            logger.info("Generating Fortran module at %s", mod_path)
1✔
1245
            generate_fortran(
1✔
1246
                schema,
1247
                mod_path,
1248
                helper_module=helper_module,
1249
                kind_module=kind_module,
1250
                kind_map=kind_map,
1251
                kind_allowlist=kind_allowlist,
1252
                constants=constants,
1253
                dimensions=dimensions,
1254
                module_doc=module_doc,
1255
                f2py_handle_helpers=entry["f2py_path"] is not None,
1256
            )
1257
        except ValueError as exc:
×
1258
            raise click.ClickException(str(exc)) from exc
×
1259

1260
    try:
1✔
1261
        local_derived_types = collect_local_derived_types(
1✔
1262
            [loaded["schema"] for loaded in loaded_entries],
1263
            constants=constants,
1264
        )
1265
        if local_derived_types and helper_path is None:
1✔
1266
            raise ValueError("locally generated derived types require a configured helper output")
×
1267
        if helper_path is not None:
1✔
1268
            logger.info("Generating helper module at %s", helper_path)
×
1269
            generate_helper(
×
1270
                helper_path,
1271
                module_name=helper_module,
1272
                len_buf=helper_buffer,
1273
                constants=constant_specs + dimension_specs,
1274
                local_derived_types=local_derived_types,
1275
                kind_module=kind_module,
1276
                kind_map=kind_map,
1277
                kind_allowlist=kind_allowlist,
1278
                module_doc=module_doc,
1279
                helper_header=helper_header,
1280
            )
1281
    except ValueError as exc:
×
1282
        raise click.ClickException(str(exc)) from exc
×
1283

1284
    _generate_f2py_outputs(
1✔
1285
        loaded_entries,
1286
        helper_module=helper_module,
1287
        helper_buffer=helper_buffer,
1288
        kind_module=kind_module,
1289
        kind_map=kind_map,
1290
        kind_allowlist=kind_allowlist,
1291
        constants=constants,
1292
        dimensions=dimensions,
1293
        f2cmap_path=f2cmap_path,
1294
        f2py_c_types=f2py_c_types,
1295
        py_style=py_style,
1296
        include_python=False,
1297
    )
1298

1299

1300
@cli.command("validate", context_settings=_CONTEXT_SETTINGS)
1✔
1301
@click.option(
1✔
1302
    "--config",
1303
    "config_path",
1304
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1305
    default=None,
1306
)
1307
@click.option(
1✔
1308
    "--schema",
1309
    "schema_paths",
1310
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1311
    multiple=True,
1312
)
1313
@click.option(
1✔
1314
    "--input",
1315
    "input_option",
1316
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1317
)
1318
@click.option(
1✔
1319
    "--profile",
1320
    "profile_name",
1321
    help="Validate against one configured file profile.",
1322
)
1323
@click.option(
1✔
1324
    "--constants",
1325
    "constant_args",
1326
    metavar="NAME=INT",
1327
    type=_CONSTANT_TYPE,
1328
    multiple=True,
1329
    help="Additional integer constants as NAME=INT (repeatable).",
1330
)
1331
@click.option(
1✔
1332
    "--dimensions",
1333
    "dimension_args",
1334
    metavar="NAME=INT",
1335
    type=_DIMENSION_TYPE,
1336
    multiple=True,
1337
    help="Runtime dimensions as NAME=INT (repeatable).",
1338
)
1339
@click.argument(
1✔
1340
    "input_path",
1341
    required=False,
1342
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1343
)
1344
def validate(
1✔
1345
    config_path: Path | None,
1346
    schema_paths: tuple[Path, ...],
1347
    input_option: Path | None,
1348
    profile_name: str | None,
1349
    constant_args: tuple[tuple[str, int], ...],
1350
    dimension_args: tuple[tuple[str, int], ...],
1351
    input_path: Path | None,
1352
) -> None:
1353
    """Validate a namelist file against schema definitions."""
1354
    if input_option is not None and input_path is not None:
1✔
1355
        raise click.ClickException("input path provided twice")
×
1356
    input_path = input_option or input_path
1✔
1357
    if input_path is None:
1✔
1358
        raise click.ClickException("input path is required")
×
1359
    constants = _parse_cli_constants(constant_args)
1✔
1360
    dimension_overrides = _parse_cli_dimensions(dimension_args)
1✔
1361
    dimensions: dict[str, int] = {}
1✔
1362
    schemas: list[dict[str, Any]] = []
1✔
1363
    resolver = SchemaResolver()
1✔
1364

1365
    if schema_paths:
1✔
1366
        if profile_name is not None:
1✔
1367
            raise click.ClickException("--profile can only be used with config-based validation")
1✔
1368
        if config_path is not None:
1✔
1369
            config, config_path = _load_config_checked(config_path)
×
1370
            logger.info("Loading config from %s", config_path)
×
1371
            cfg_constants, _ = _load_constants(config)
×
1372
            dimensions, _ = _load_dimensions(config, cfg_constants)
×
1373
            constants = {**cfg_constants, **constants}
×
1374
            dimensions = {**dimensions, **dimension_overrides}
×
1375
        else:
1376
            dimensions = dimension_overrides
1✔
1377
        for schema_file in schema_paths:
1✔
1378
            try:
1✔
1379
                logger.info("Loading schema %s", schema_file)
1✔
1380
                schemas.append(load_schema(schema_file, resolver=resolver))
1✔
1381
            except (FileNotFoundError, ValueError) as exc:
×
1382
                raise click.ClickException(str(exc)) from exc
×
1383
        require_all = True
1✔
1384
    else:
1385
        config, config_path = _load_config_checked(config_path)
1✔
1386
        logger.info("Loading config from %s", config_path)
1✔
1387
        base_dir = config_path.parent
1✔
1388
        cfg_constants, _ = _load_constants(config)
1✔
1389
        dimensions, _ = _load_dimensions(config, cfg_constants)
1✔
1390
        constants = {**cfg_constants, **constants}
1✔
1391
        dimensions = {**dimensions, **dimension_overrides}
1✔
1392
        loaded_namelists = _load_namelist_registry(config, base_dir, resolver)
1✔
1393
        loaded_by_key = _namelist_registry_by_key(loaded_namelists)
1✔
1394
        if profile_name is not None:
1✔
1395
            profiles = _iter_file_profiles(config, loaded_by_key)
1✔
1396
            profile = profiles.get(profile_name.lower())
1✔
1397
            if profile is None:
1✔
NEW
1398
                raise click.ClickException(f"unknown file profile '{profile_name}'")
×
1399
            loaded_namelists = [loaded_by_key[key] for key in profile.namelists]
1✔
1400
        logger.info("Found %d schema entries", len(loaded_namelists))
1✔
1401
        schemas = [loaded.schema for loaded in loaded_namelists]
1✔
1402
        require_all = False
1✔
1403

1404
    if not schemas:
1✔
1405
        raise click.ClickException("no schemas provided for validation")
×
1406
    _reject_constant_dimension_overlap(constants, dimensions)
1✔
1407

1408
    try:
1✔
1409
        logger.info("Reading namelist %s", input_path)
1✔
1410
        namelist_file = f90nml.read(input_path)
1✔
1411
    except Exception as exc:  # pragma: no cover - f90nml raises custom errors
1412
        raise click.ClickException(f"failed to read namelist: {exc}") from exc
1413

1414
    file_entries: dict[str, tuple[str, Any]] = {}
1✔
1415
    for name, values in namelist_file.items():
1✔
1416
        if not isinstance(name, str):
1✔
1417
            raise click.ClickException("namelist names must be strings")
×
1418
        key = name.lower()
1✔
1419
        if key in file_entries:
1✔
1420
            raise click.ClickException(f"namelist '{name}' appears multiple times")
×
1421
        file_entries[key] = (name, values)
1✔
1422

1423
    schema_entries: dict[str, dict[str, Any]] = {}
1✔
1424
    for schema in schemas:
1✔
1425
        namelist_name = schema.get("x-fortran-namelist")
1✔
1426
        if not isinstance(namelist_name, str) or not namelist_name.strip():
1✔
1427
            raise click.ClickException("schema must define non-empty 'x-fortran-namelist'")
×
1428
        key = namelist_name.lower()
1✔
1429
        if key in schema_entries:
1✔
1430
            raise click.ClickException(f"duplicate schema for namelist '{namelist_name}'")
×
1431
        schema_entries[key] = schema
1✔
1432

1433
    for key, (name, _) in file_entries.items():
1✔
1434
        if key not in schema_entries:
1✔
1435
            raise click.ClickException(f"input contains unknown namelist '{name}'")
1✔
1436

1437
    validated = 0
1✔
1438
    for key, schema in schema_entries.items():
1✔
1439
        if key not in file_entries:
1✔
1440
            if require_all:
×
1441
                raise click.ClickException(
×
1442
                    f"input is missing namelist '{schema.get('x-fortran-namelist')}'"
1443
                )
1444
            continue
×
1445
        try:
1✔
1446
            validate_namelist(
1✔
1447
                schema,
1448
                _normalize_f90nml_values(file_entries[key][1]),
1449
                constants=constants,
1450
                dimensions=dimensions,
1451
            )
1452
        except ValueError as exc:
×
1453
            raise click.ClickException(str(exc)) from exc
×
1454
        validated += 1
1✔
1455
    logger.info("Validation completed (%d namelist%s).", validated, "" if validated == 1 else "s")
1✔
1456

1457

1458
@cli.command("gen-markdown", context_settings=_CONTEXT_SETTINGS)
1✔
1459
@click.option(
1✔
1460
    "--config",
1461
    "config_path",
1462
    type=click.Path(exists=True, dir_okay=False, path_type=Path),
1463
    default=None,
1464
    show_default="nml-config.toml, then pyproject.toml [tool.nml-tools]",
1465
)
1466
def gen_markdown(config_path: Path | None) -> None:
1✔
1467
    """Generate Markdown docs."""
1468
    config, config_path = _load_config_checked(config_path)
1✔
1469
    logger.info("Loading config from %s", config_path)
1✔
1470
    base_dir = config_path.parent
1✔
1471
    constants, _ = _load_constants(config)
1✔
1472
    dimensions, _ = _load_dimensions(config, constants)
1✔
1473
    _, md_doxygen_id_from_name, md_add_toc_statement, _ = _load_documentation_settings(
1✔
1474
        config
1475
    )
1476
    entries = _iter_namelists(config, base_dir)
1✔
1477
    resolver = SchemaResolver()
1✔
1478
    logger.info("Found %d schema entries", len(entries))
1✔
1479
    for entry in entries:
1✔
1480
        schema_path = entry["schema"]
1✔
1481
        if schema_path is None:
1✔
1482
            raise click.ClickException("namelists entry missing schema path")
×
1483
        try:
1✔
1484
            logger.info("Loading schema %s", schema_path)
1✔
1485
            schema = load_schema(schema_path, resolver=resolver)
1✔
1486
        except (FileNotFoundError, ValueError) as exc:
×
1487
            raise click.ClickException(str(exc)) from exc
×
1488
        doc_path = entry["doc_path"]
1✔
1489
        if doc_path is None:
1✔
1490
            continue
×
1491
        try:
1✔
1492
            logger.info("Generating Markdown docs at %s", doc_path)
1✔
1493
            generate_docs(
1✔
1494
                schema,
1495
                doc_path,
1496
                constants=constants,
1497
                dimensions=dimensions,
1498
                md_doxygen_id_from_name=md_doxygen_id_from_name,
1499
                md_add_toc_statement=md_add_toc_statement,
1500
            )
1501
        except ValueError as exc:
×
1502
            raise click.ClickException(str(exc)) from exc
×
1503

1504

1505
@cli.command("gen-template", 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_template(config_path: Path | None) -> None:
1✔
1514
    """Generate template namelist(s)."""
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
    _, kind_map, kind_allowlist = _load_kind_settings(config)
1✔
1521
    resolver = SchemaResolver()
1✔
1522
    loaded_namelists = _load_namelist_registry(config, base_dir, resolver)
1✔
1523
    loaded_by_key = _namelist_registry_by_key(loaded_namelists)
1✔
1524
    profiles = _iter_file_profiles(config, loaded_by_key)
1✔
1525
    templates = _iter_templates(config, base_dir, loaded_by_key, profiles)
1✔
1526
    if not templates:
1✔
1527
        raise click.ClickException("config must define non-empty 'templates'")
×
1528
    logger.info("Found %d template entries", len(templates))
1✔
1529
    for entry in templates:
1✔
1530
        try:
1✔
1531
            logger.info("Generating template at %s", entry["path"])
1✔
1532
            generate_template(
1✔
1533
                entry["schemas"],
1534
                entry["path"],
1535
                doc_mode=entry["doc_mode"],
1536
                value_mode=entry["value_mode"],
1537
                title=entry["title"],
1538
                description=entry["description"],
1539
                constants=constants,
1540
                dimensions=dimensions,
1541
                kind_map=kind_map,
1542
                kind_allowlist=kind_allowlist,
1543
                values=entry["values"],
1544
            )
NEW
1545
        except ValueError as exc:
×
1546
            raise click.ClickException(str(exc)) from exc
×
1547

1548

1549
def main(argv: list[str] | None = None) -> int:
1✔
1550
    """Entry point for the CLI."""
1551
    try:
×
1552
        cli.main(args=argv, prog_name="nml-tools", standalone_mode=False)
×
1553
    except Exit as exc:
×
1554
        return exc.exit_code
×
1555
    except click.ClickException as exc:
×
1556
        exc.show()
×
1557
        return 1
×
1558
    return 0
×
1559

1560

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