jcmsuite 7.4.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of jcmsuite might be problematic. Click here for more details.
- jcmsuite/__init__.py +22 -0
- jcmsuite/_build_info.py +4 -0
- jcmsuite/context/__init__.py +44 -0
- jcmsuite/context/interface_metadata.py +158 -0
- jcmsuite/context/models.py +213 -0
- jcmsuite/context/scope.py +210 -0
- jcmsuite/context/solver_context.py +569 -0
- jcmsuite/context/subprocess_tools.py +172 -0
- jcmsuite/contract.py +47 -0
- jcmsuite/daemon/__init__.py +19 -0
- jcmsuite/daemon/handler.py +925 -0
- jcmsuite/daemon/internal/__init__.py +4 -0
- jcmsuite/daemon/internal/bundle_protocol.py +64 -0
- jcmsuite/daemon/internal/handler_helpers.py +368 -0
- jcmsuite/daemon/internal/handler_resource_jobs.py +227 -0
- jcmsuite/daemon/internal/handler_wait.py +564 -0
- jcmsuite/daemon/models.py +349 -0
- jcmsuite/daemon/namespaces.py +250 -0
- jcmsuite/daemon/protocol.py +801 -0
- jcmsuite/daemon/resources.py +407 -0
- jcmsuite/daemon/scope.py +53 -0
- jcmsuite/daemon/transport.py +223 -0
- jcmsuite/editing/__init__.py +10 -0
- jcmsuite/editing/editor.py +274 -0
- jcmsuite/exceptions/__init__.py +32 -0
- jcmsuite/exceptions/errors.py +173 -0
- jcmsuite/geo/__init__.py +10 -0
- jcmsuite/geo/mesh.py +302 -0
- jcmsuite/info/__init__.py +10 -0
- jcmsuite/info/runtime_info.py +45 -0
- jcmsuite/internal/__init__.py +2 -0
- jcmsuite/internal/diagnostics.py +74 -0
- jcmsuite/internal/progress.py +80 -0
- jcmsuite/loaders/__init__.py +18 -0
- jcmsuite/loaders/blob_header.py +315 -0
- jcmsuite/loaders/cartesian_field_reader.py +249 -0
- jcmsuite/loaders/cartesian_fields.py +324 -0
- jcmsuite/loaders/nested_mapping.py +69 -0
- jcmsuite/loaders/powerflux.py +136 -0
- jcmsuite/loaders/table.py +637 -0
- jcmsuite/logging/__init__.py +22 -0
- jcmsuite/logging/runtime.py +210 -0
- jcmsuite/results/__init__.py +126 -0
- jcmsuite/results/artifacts.py +806 -0
- jcmsuite/results/discovery.py +82 -0
- jcmsuite/results/field_exports.py +1062 -0
- jcmsuite/results/field_payloads.py +453 -0
- jcmsuite/results/internal/__init__.py +4 -0
- jcmsuite/results/internal/export/__init__.py +4 -0
- jcmsuite/results/internal/export/common.py +56 -0
- jcmsuite/results/internal/export/pandas/__init__.py +4 -0
- jcmsuite/results/internal/export/pandas/common.py +165 -0
- jcmsuite/results/internal/export/pandas/payload.py +480 -0
- jcmsuite/results/internal/export/pandas/rows.py +484 -0
- jcmsuite/results/internal/export/preview.py +178 -0
- jcmsuite/results/internal/export/xarray/__init__.py +4 -0
- jcmsuite/results/internal/export/xarray/export.py +688 -0
- jcmsuite/results/internal/export/xarray/grouped.py +608 -0
- jcmsuite/results/internal/export/xarray/headers.py +290 -0
- jcmsuite/results/internal/export/xarray/runtime.py +99 -0
- jcmsuite/results/internal/export/xarray/shared.py +246 -0
- jcmsuite/results/internal/export/xarray/tabular.py +304 -0
- jcmsuite/results/internal/export/xarray/types.py +28 -0
- jcmsuite/results/internal/models/__init__.py +4 -0
- jcmsuite/results/internal/models/batch.py +1081 -0
- jcmsuite/results/internal/models/batch_postprocess.py +1728 -0
- jcmsuite/results/internal/models/bundle.py +343 -0
- jcmsuite/results/internal/models/core.py +556 -0
- jcmsuite/results/internal/models/submission.py +1157 -0
- jcmsuite/results/internal/resultbag/__init__.py +4 -0
- jcmsuite/results/internal/resultbag/access.py +489 -0
- jcmsuite/results/internal/resultbag/iteration.py +199 -0
- jcmsuite/results/internal/resultbag/query.py +226 -0
- jcmsuite/results/internal/resultbag/raw.py +407 -0
- jcmsuite/results/internal/resultbag/source_files.py +142 -0
- jcmsuite/results/internal/resultbag/store.py +881 -0
- jcmsuite/results/internal/resultbag/utils.py +258 -0
- jcmsuite/results/models.py +76 -0
- jcmsuite/results/opening.py +55 -0
- jcmsuite/results/pandas_export.py +325 -0
- jcmsuite/results/payloads.py +478 -0
- jcmsuite/results/postprocess.py +97 -0
- jcmsuite/results/postprocess_payloads.py +757 -0
- jcmsuite/results/postprocess_record.py +418 -0
- jcmsuite/results/resultbag.py +279 -0
- jcmsuite/results/resultbag_management.py +433 -0
- jcmsuite/results/structured_derivatives.py +387 -0
- jcmsuite/solve/__init__.py +10 -0
- jcmsuite/solve/daemon_bundle_staging.py +100 -0
- jcmsuite/solve/daemon_bundle_wait.py +89 -0
- jcmsuite/solve/orchestration.py +1576 -0
- jcmsuite/solve/orchestration_execution.py +180 -0
- jcmsuite/solve/orchestration_routing.py +128 -0
- jcmsuite/solve/orchestration_support.py +323 -0
- jcmsuite/solve/path_resolution.py +98 -0
- jcmsuite/solve/progress.py +20 -0
- jcmsuite/solve/solver.py +709 -0
- jcmsuite/solve/staging.py +264 -0
- jcmsuite/solve/types.py +25 -0
- jcmsuite/solve/working_dirs.py +227 -0
- jcmsuite/templates/__init__.py +11 -0
- jcmsuite/templates/converter.py +71 -0
- jcmsuite/templates/formatting.py +335 -0
- jcmsuite/templates/formatting_cli.py +47 -0
- jcmsuite/templates/rendering.py +666 -0
- jcmsuite/torch/__init__.py +25 -0
- jcmsuite/torch/autograd.py +488 -0
- jcmsuite/torch/coordinate_policy.py +82 -0
- jcmsuite/torch/core.py +312 -0
- jcmsuite/torch/results/__init__.py +51 -0
- jcmsuite/torch/types.py +84 -0
- jcmsuite/torch/validation.py +208 -0
- jcmsuite/torch/wrappers.py +4332 -0
- jcmsuite/view/__init__.py +10 -0
- jcmsuite/view/viewer.py +122 -0
- jcmsuite-7.4.2.dist-info/METADATA +180 -0
- jcmsuite-7.4.2.dist-info/RECORD +119 -0
- jcmsuite-7.4.2.dist-info/WHEEL +4 -0
- jcmsuite-7.4.2.dist-info/entry_points.txt +3 -0
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""Conservative formatting for JCMsuite input and template files."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import re
|
|
9
|
+
from collections.abc import Iterator
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
_SCRIPT_TAG_PATTERN: re.Pattern[str] = re.compile(r"<\?(?:Python)?|\?>")
|
|
13
|
+
_ASSIGNMENT_PATTERN: re.Pattern[str] = re.compile(r"(?<![<>=!])\s*=\s*(?!=)")
|
|
14
|
+
_COMMA_PATTERN: re.Pattern[str] = re.compile(r"\s*,\s*")
|
|
15
|
+
_OPEN_BRACE_PATTERN: re.Pattern[str] = re.compile(r"[ \t]*\{")
|
|
16
|
+
_CLOSE_BRACE_PATTERN: re.Pattern[str] = re.compile(r"[ \t]*\}")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def format_jcm(source: str, *, indent: str = " ") -> str:
|
|
20
|
+
"""Format JCM source while preserving values and embedded Python.
|
|
21
|
+
|
|
22
|
+
Parameters
|
|
23
|
+
----------
|
|
24
|
+
source : str
|
|
25
|
+
Source text that contains JCM syntax and optional template Python.
|
|
26
|
+
indent : str, default=" "
|
|
27
|
+
Text to insert for each brace nesting level. The value must not be empty.
|
|
28
|
+
|
|
29
|
+
Returns
|
|
30
|
+
-------
|
|
31
|
+
str
|
|
32
|
+
Formatted source.
|
|
33
|
+
|
|
34
|
+
Raises
|
|
35
|
+
------
|
|
36
|
+
ValueError
|
|
37
|
+
If the source contains unbalanced braces, an unterminated string, an
|
|
38
|
+
invalid template boundary, or an empty ``indent`` value.
|
|
39
|
+
"""
|
|
40
|
+
if not indent:
|
|
41
|
+
raise ValueError("indent must not be empty")
|
|
42
|
+
|
|
43
|
+
formatted_parts: list[str] = []
|
|
44
|
+
cursor: int = 0
|
|
45
|
+
depth: int = 0
|
|
46
|
+
in_python: bool = False
|
|
47
|
+
|
|
48
|
+
for tag_match in _SCRIPT_TAG_PATTERN.finditer(source):
|
|
49
|
+
if in_python:
|
|
50
|
+
formatted_parts.append(source[cursor : tag_match.start()])
|
|
51
|
+
formatted_parts.append(tag_match.group())
|
|
52
|
+
cursor = tag_match.end()
|
|
53
|
+
if tag_match.group() == "?>":
|
|
54
|
+
in_python = False
|
|
55
|
+
continue
|
|
56
|
+
|
|
57
|
+
if tag_match.group() == "?>":
|
|
58
|
+
raise ValueError("template Python block closes without opening")
|
|
59
|
+
jcm_text: str = source[cursor : tag_match.start()]
|
|
60
|
+
formatted_jcm, depth = _format_jcm_fragment(jcm_text, depth, indent)
|
|
61
|
+
formatted_parts.append(formatted_jcm)
|
|
62
|
+
formatted_parts.append(tag_match.group())
|
|
63
|
+
cursor = tag_match.end()
|
|
64
|
+
if tag_match.group().startswith("<?"):
|
|
65
|
+
in_python = True
|
|
66
|
+
|
|
67
|
+
if in_python:
|
|
68
|
+
raise ValueError("unterminated template Python block")
|
|
69
|
+
else:
|
|
70
|
+
formatted_jcm, depth = _format_jcm_fragment(source[cursor:], depth, indent)
|
|
71
|
+
formatted_parts.append(formatted_jcm)
|
|
72
|
+
|
|
73
|
+
if depth != 0:
|
|
74
|
+
raise ValueError(f"unbalanced braces: nesting level {depth}")
|
|
75
|
+
return "".join(formatted_parts)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _format_jcm_fragment(source: str, depth: int, indent: str) -> tuple[str, int]:
|
|
79
|
+
"""Format one JCM fragment and return its resulting brace depth."""
|
|
80
|
+
expanded_source: str = _expand_brace_lines(source)
|
|
81
|
+
lines: list[str] = _expand_assignment_lines(expanded_source).splitlines(
|
|
82
|
+
keepends=True
|
|
83
|
+
)
|
|
84
|
+
formatted_lines: list[str] = []
|
|
85
|
+
for line in lines:
|
|
86
|
+
content: str = line.rstrip("\r\n")
|
|
87
|
+
line_ending: str = line[len(content) :]
|
|
88
|
+
formatted, depth = _format_jcm_line(content, depth, indent)
|
|
89
|
+
formatted_lines.append(formatted + line_ending)
|
|
90
|
+
return "".join(formatted_lines), depth
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _expand_assignment_lines(source: str) -> str:
|
|
94
|
+
"""Put consecutive top-level assignments on separate lines."""
|
|
95
|
+
expanded_lines: list[str] = []
|
|
96
|
+
for line in source.splitlines(keepends=True):
|
|
97
|
+
content: str = line.rstrip("\r\n")
|
|
98
|
+
line_ending: str = line[len(content) :]
|
|
99
|
+
assignment_starts: list[int] = _assignment_starts(content)
|
|
100
|
+
|
|
101
|
+
boundaries: list[int] = assignment_starts[1:]
|
|
102
|
+
start: int = 0
|
|
103
|
+
for boundary in boundaries:
|
|
104
|
+
expanded_lines.append(content[start:boundary].rstrip() + "\n")
|
|
105
|
+
start = boundary
|
|
106
|
+
expanded_lines.append(content[start:] + line_ending)
|
|
107
|
+
return "".join(expanded_lines)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def _assignment_starts(line: str) -> list[int]:
|
|
111
|
+
"""Return assignment-key starts outside strings and comments."""
|
|
112
|
+
assignment_starts: list[int] = []
|
|
113
|
+
quote: str | None = None
|
|
114
|
+
escaped: bool = False
|
|
115
|
+
in_comment: bool = False
|
|
116
|
+
for index, character in enumerate(line):
|
|
117
|
+
if in_comment:
|
|
118
|
+
continue
|
|
119
|
+
if escaped:
|
|
120
|
+
escaped = False
|
|
121
|
+
continue
|
|
122
|
+
if quote is not None:
|
|
123
|
+
if character == "\\":
|
|
124
|
+
escaped = True
|
|
125
|
+
elif character == quote:
|
|
126
|
+
quote = None
|
|
127
|
+
continue
|
|
128
|
+
if character in ('"', "'"):
|
|
129
|
+
quote = character
|
|
130
|
+
elif character == "#":
|
|
131
|
+
in_comment = True
|
|
132
|
+
elif character == "=" and _is_assignment_operator(line, index):
|
|
133
|
+
key_end: int = index
|
|
134
|
+
while key_end > 0 and line[key_end - 1].isspace():
|
|
135
|
+
key_end -= 1
|
|
136
|
+
key_start: int = key_end
|
|
137
|
+
while key_start > 0 and not line[key_start - 1].isspace():
|
|
138
|
+
key_start -= 1
|
|
139
|
+
assignment_starts.append(key_start)
|
|
140
|
+
return assignment_starts
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _is_assignment_operator(line: str, index: int) -> bool:
|
|
144
|
+
"""Return whether an equals sign is a standalone assignment operator."""
|
|
145
|
+
previous: str = line[index - 1] if index > 0 else ""
|
|
146
|
+
following: str = line[index + 1] if index + 1 < len(line) else ""
|
|
147
|
+
return previous not in "<>=!" and following not in "<=>"
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _expand_brace_lines(source: str) -> str:
|
|
151
|
+
"""Put structural braces on separate lines without changing literals."""
|
|
152
|
+
expanded: list[str] = []
|
|
153
|
+
quote: str | None = None
|
|
154
|
+
escaped: bool = False
|
|
155
|
+
in_comment: bool = False
|
|
156
|
+
|
|
157
|
+
for index, character in enumerate(source):
|
|
158
|
+
if in_comment:
|
|
159
|
+
expanded.append(character)
|
|
160
|
+
if character == "\n":
|
|
161
|
+
in_comment = False
|
|
162
|
+
continue
|
|
163
|
+
if escaped:
|
|
164
|
+
expanded.append(character)
|
|
165
|
+
escaped = False
|
|
166
|
+
continue
|
|
167
|
+
if quote is not None:
|
|
168
|
+
expanded.append(character)
|
|
169
|
+
if character == "\\":
|
|
170
|
+
escaped = True
|
|
171
|
+
elif character == quote:
|
|
172
|
+
quote = None
|
|
173
|
+
continue
|
|
174
|
+
if character in ('"', "'"):
|
|
175
|
+
quote = character
|
|
176
|
+
expanded.append(character)
|
|
177
|
+
elif character == "#":
|
|
178
|
+
in_comment = True
|
|
179
|
+
expanded.append(character)
|
|
180
|
+
elif character in "{}":
|
|
181
|
+
current_line: str = "".join(expanded).rsplit("\n", 1)[-1]
|
|
182
|
+
if character == "{" and current_line.strip():
|
|
183
|
+
line_without_trailing_space: str = current_line.rstrip()
|
|
184
|
+
block_name: str = line_without_trailing_space.rsplit(None, 1)[-1]
|
|
185
|
+
block_name_start: int = len(line_without_trailing_space) - len(
|
|
186
|
+
block_name
|
|
187
|
+
)
|
|
188
|
+
prefix: str = line_without_trailing_space[:block_name_start]
|
|
189
|
+
if _assignment_starts(prefix):
|
|
190
|
+
line_start: int = len(expanded) - len(current_line)
|
|
191
|
+
expanded.insert(line_start + block_name_start, "\n")
|
|
192
|
+
if character == "}" and current_line.strip():
|
|
193
|
+
expanded.append("\n")
|
|
194
|
+
expanded.append(character)
|
|
195
|
+
next_character: str = source[index + 1] if index + 1 < len(source) else ""
|
|
196
|
+
if next_character and next_character != "\n":
|
|
197
|
+
expanded.append("\n")
|
|
198
|
+
else:
|
|
199
|
+
expanded.append(character)
|
|
200
|
+
|
|
201
|
+
return "".join(expanded)
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _format_jcm_line(line: str, depth: int, indent: str) -> tuple[str, int]:
|
|
205
|
+
"""Format one JCM line and update brace nesting."""
|
|
206
|
+
stripped: str = line.strip()
|
|
207
|
+
if not stripped:
|
|
208
|
+
return "", depth
|
|
209
|
+
|
|
210
|
+
code, comment = _split_comment(stripped)
|
|
211
|
+
if _has_unterminated_quote(code):
|
|
212
|
+
raise ValueError("unterminated string")
|
|
213
|
+
normalized_code: str = _normalize_code(code.strip())
|
|
214
|
+
closing_count: int = _leading_closing_braces(normalized_code)
|
|
215
|
+
line_depth: int = depth - closing_count
|
|
216
|
+
if line_depth < 0:
|
|
217
|
+
raise ValueError("unbalanced closing brace")
|
|
218
|
+
|
|
219
|
+
content: str = normalized_code
|
|
220
|
+
if comment:
|
|
221
|
+
separator: str = " " if content else ""
|
|
222
|
+
content = f"{content}{separator}{comment}"
|
|
223
|
+
formatted: str = f"{indent * line_depth}{content}"
|
|
224
|
+
|
|
225
|
+
opens, closes = _brace_counts(code)
|
|
226
|
+
new_depth: int = depth + opens - closes
|
|
227
|
+
if new_depth < 0:
|
|
228
|
+
raise ValueError("unbalanced closing brace")
|
|
229
|
+
return formatted, new_depth
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def _split_comment(line: str) -> tuple[str, str]:
|
|
233
|
+
"""Split a line at a comment marker outside quoted strings."""
|
|
234
|
+
quote: str | None = None
|
|
235
|
+
escaped: bool = False
|
|
236
|
+
for index, character in enumerate(line):
|
|
237
|
+
if escaped:
|
|
238
|
+
escaped = False
|
|
239
|
+
elif character == "\\" and quote is not None:
|
|
240
|
+
escaped = True
|
|
241
|
+
elif quote is not None and character == quote:
|
|
242
|
+
quote = None
|
|
243
|
+
elif quote is None and character in ('"', "'"):
|
|
244
|
+
quote = character
|
|
245
|
+
elif quote is None and character == "#":
|
|
246
|
+
return line[:index], line[index:].rstrip()
|
|
247
|
+
return line, ""
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def _normalize_code(code: str) -> str:
|
|
251
|
+
"""Normalize separators outside quoted strings."""
|
|
252
|
+
parts: list[str] = []
|
|
253
|
+
cursor: int = 0
|
|
254
|
+
for match in re.finditer(r'"(?:\\.|[^"\\])*"|\'(?:\\.|[^\'\\])*\'', code):
|
|
255
|
+
parts.append(_normalize_plain(code[cursor : match.start()]))
|
|
256
|
+
parts.append(match.group())
|
|
257
|
+
cursor = match.end()
|
|
258
|
+
parts.append(_normalize_plain(code[cursor:]))
|
|
259
|
+
return "".join(parts).strip()
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def _normalize_plain(code: str) -> str:
|
|
263
|
+
"""Normalize punctuation in a non-string code fragment."""
|
|
264
|
+
normalized: str = _ASSIGNMENT_PATTERN.sub(" = ", code)
|
|
265
|
+
normalized = _COMMA_PATTERN.sub(", ", normalized)
|
|
266
|
+
normalized = _OPEN_BRACE_PATTERN.sub(" {", normalized)
|
|
267
|
+
normalized = _CLOSE_BRACE_PATTERN.sub("}", normalized)
|
|
268
|
+
return normalized
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def _leading_closing_braces(code: str) -> int:
|
|
272
|
+
"""Count closing braces at the start of a normalized code line."""
|
|
273
|
+
return len(code) - len(code.lstrip("}"))
|
|
274
|
+
|
|
275
|
+
|
|
276
|
+
def _brace_counts(code: str) -> tuple[int, int]:
|
|
277
|
+
"""Count braces outside quoted strings and comments."""
|
|
278
|
+
opens: int = 0
|
|
279
|
+
closes: int = 0
|
|
280
|
+
quote: str | None = None
|
|
281
|
+
escaped: bool = False
|
|
282
|
+
for character in code:
|
|
283
|
+
if escaped:
|
|
284
|
+
escaped = False
|
|
285
|
+
elif character == "\\" and quote is not None:
|
|
286
|
+
escaped = True
|
|
287
|
+
elif quote is not None and character == quote:
|
|
288
|
+
quote = None
|
|
289
|
+
elif quote is None and character in ('"', "'"):
|
|
290
|
+
quote = character
|
|
291
|
+
elif quote is None:
|
|
292
|
+
opens += character == "{"
|
|
293
|
+
closes += character == "}"
|
|
294
|
+
return opens, closes
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
def _has_unterminated_quote(code: str) -> bool:
|
|
298
|
+
"""Return whether a line ends inside a quoted string."""
|
|
299
|
+
quote: str | None = None
|
|
300
|
+
escaped: bool = False
|
|
301
|
+
for character in code:
|
|
302
|
+
if escaped:
|
|
303
|
+
escaped = False
|
|
304
|
+
elif character == "\\" and quote is not None:
|
|
305
|
+
escaped = True
|
|
306
|
+
elif quote is not None and character == quote:
|
|
307
|
+
quote = None
|
|
308
|
+
elif quote is None and character in ('"', "'"):
|
|
309
|
+
quote = character
|
|
310
|
+
return quote is not None
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def format_path(path: str | Path, *, write: bool = False) -> str:
|
|
314
|
+
"""Format one JCM-family file and optionally write the result."""
|
|
315
|
+
file_path: Path = Path(path)
|
|
316
|
+
source: str = file_path.read_text(encoding="utf-8")
|
|
317
|
+
formatted: str = format_jcm(source)
|
|
318
|
+
if write:
|
|
319
|
+
file_path.write_text(formatted, encoding="utf-8")
|
|
320
|
+
return formatted
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def iter_jcm_files(path: str | Path) -> Iterator[Path]:
|
|
324
|
+
"""Yield JCM-family files below a file or directory path."""
|
|
325
|
+
root: Path = Path(path)
|
|
326
|
+
if root.is_file():
|
|
327
|
+
if root.suffix in {".jcm", ".jcmp", ".jcmt", ".jcmpt"}:
|
|
328
|
+
yield root
|
|
329
|
+
return
|
|
330
|
+
yield from sorted(
|
|
331
|
+
candidate
|
|
332
|
+
for candidate in root.rglob("*")
|
|
333
|
+
if candidate.is_file()
|
|
334
|
+
and candidate.suffix in {".jcm", ".jcmp", ".jcmt", ".jcmpt"}
|
|
335
|
+
)
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""Command-line interface for conservative JCM formatting."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import argparse
|
|
9
|
+
import sys
|
|
10
|
+
from pathlib import Path
|
|
11
|
+
|
|
12
|
+
from .formatting import format_path, iter_jcm_files
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def main() -> int:
|
|
16
|
+
"""Format JCM-family files or check whether they are formatted."""
|
|
17
|
+
parser: argparse.ArgumentParser = argparse.ArgumentParser(
|
|
18
|
+
description="Format JCM input and template files."
|
|
19
|
+
)
|
|
20
|
+
parser.add_argument("paths", nargs="+", type=Path)
|
|
21
|
+
parser.add_argument(
|
|
22
|
+
"--check", action="store_true", help="Check without writing files."
|
|
23
|
+
)
|
|
24
|
+
args: argparse.Namespace = parser.parse_args()
|
|
25
|
+
|
|
26
|
+
try:
|
|
27
|
+
changed: bool = False
|
|
28
|
+
files: list[Path] = [
|
|
29
|
+
file_path for path in args.paths for file_path in iter_jcm_files(path)
|
|
30
|
+
]
|
|
31
|
+
for file_path in files:
|
|
32
|
+
original: str = file_path.read_text(encoding="utf-8")
|
|
33
|
+
formatted: str = format_path(file_path)
|
|
34
|
+
if formatted != original:
|
|
35
|
+
changed = True
|
|
36
|
+
if args.check and formatted != original:
|
|
37
|
+
print(f"would reformat {file_path}")
|
|
38
|
+
elif not args.check and formatted != original:
|
|
39
|
+
file_path.write_text(formatted, encoding="utf-8")
|
|
40
|
+
return int(args.check and changed)
|
|
41
|
+
except (OSError, ValueError) as error:
|
|
42
|
+
print(f"jcmsuite-format: {error}", file=sys.stderr)
|
|
43
|
+
return 2
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
if __name__ == "__main__":
|
|
47
|
+
raise SystemExit(main())
|