SciFile-Core 0.0.1__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.
@@ -0,0 +1,454 @@
1
+ """The wording of the shared types of diagnostics, and of shared situations.
2
+
3
+ One problem reads the same in every package. A package writes the
4
+ `~scifile_core.Diagnostic.details` of a diagnostic of a shared type with the
5
+ function of the type, and gives the field and the context that the contract asks
6
+ for:
7
+
8
+ .. code-block:: python
9
+
10
+ import scifile_core as core
11
+ from scifile_core import wording
12
+ core.create_diagnostic(
13
+ core.SharedDiagnosticType.VALUE_INVALID,
14
+ wording.value_invalid("SPACING", "abc", "a number"),
15
+ field="SPACING",
16
+ line=3,
17
+ context={"value": "abc", "expected": "a number"},
18
+ )
19
+
20
+ A package passes its own ``DiagnosticType`` member, which repeats the shared
21
+ one.
22
+ Some functions word a situation rather than a type: a form of a type that
23
+ has several, as `value_disallowed` and `value_discouraged` are of
24
+ ``VALUE_INVALID``, or a cause that diagnostics of several types share, as
25
+ `byte_order_mark` is.
26
+ A consequence is a clause in lower case, without a full stop, that says what
27
+ the reader did about the problem, such as ``"the line is ignored"``; an empty
28
+ consequence is left out. A key is spelled as the specification of the format
29
+ spells it, and a value is shortened as in the messages of exceptions:
30
+ a long string by its length.
31
+ """
32
+
33
+ from __future__ import annotations
34
+
35
+ from typing import Literal
36
+
37
+ from scifile_core._exception import describe
38
+
39
+
40
+ def undecodable_text(
41
+ offset: int, consequence: str = "they are read as U+FFFD"
42
+ ) -> str:
43
+ """Return the details of a diagnostic of the type ``UNDECODABLE_TEXT``.
44
+
45
+ `TextSource.diagnostics <scifile_core.TextSource.diagnostics>` builds the
46
+ whole diagnostic of a text input, with the default consequence.
47
+
48
+ Parameters
49
+ ----------
50
+ offset
51
+ Byte of the input at which the first undecodable byte is, counted
52
+ from 0.
53
+ consequence
54
+ What the reader did about the bytes, such as ``"they are read as
55
+ '?'"`` for a field of a binary file that a package decodes itself.
56
+
57
+ Returns
58
+ -------
59
+ The text, such as "The text holds bytes that its encoding cannot
60
+ decode, from byte 12 on; they are read as U+FFFD."
61
+ """
62
+ return _sentence(
63
+ "The text holds bytes that its encoding cannot decode, from byte"
64
+ f" {offset} on",
65
+ consequence,
66
+ )
67
+
68
+
69
+ def byte_order_mark(
70
+ consequence: str = "the line is ignored", *, unknown: str | None = None
71
+ ) -> str:
72
+ """Return the details of the diagnostic of the word of a byte-order mark.
73
+
74
+ A text that starts with U+FEFF, the byte-order mark of Unicode, is read
75
+ with the mark as part of its first line, as a program in C reads the
76
+ bytes of a file, so that the first word of the line holds it. The
77
+ diagnostic that the reader gives that word, of the type that fits what
78
+ the reader makes of it, takes this wording, which names a character that
79
+ the reader of the message cannot see.
80
+
81
+ Parameters
82
+ ----------
83
+ consequence
84
+ What the reader did about the line, such as ``"the line is read as
85
+ GRID_PARAMETER_FILE"``.
86
+ unknown
87
+ The rest of the word after the mark, as the file spells it, for a
88
+ reader that looks the key up without the mark and finds none that
89
+ the format defines; `None` or an empty string otherwise.
90
+
91
+ Returns
92
+ -------
93
+ The text, such as "The text starts with a byte-order mark, U+FEFF, which
94
+ is read as part of its first line; the line is ignored.", or with
95
+ ``unknown="COLOR"``, "…; the key 'COLOR' after the mark is none that the
96
+ format defines, and the line is ignored."
97
+ """
98
+ if unknown:
99
+ none = f"the key {describe(unknown)} after the mark is none that"
100
+ consequence = (
101
+ f"{none} the format defines, and {consequence}"
102
+ if consequence
103
+ else f"{none} the format defines"
104
+ )
105
+ return _sentence(
106
+ "The text starts with a byte-order mark, U+FEFF, which is read as part"
107
+ " of its first line",
108
+ consequence,
109
+ )
110
+
111
+
112
+ def file_incomplete(
113
+ where: str, consequence: str = "no model can be built"
114
+ ) -> str:
115
+ """Return the details of a diagnostic of the type ``FILE_INCOMPLETE``.
116
+
117
+ Parameters
118
+ ----------
119
+ where
120
+ The part of the file that it ends in, such as ``"its header"``.
121
+ consequence
122
+ What the reader did about it.
123
+
124
+ Returns
125
+ -------
126
+ The text, such as "The file ends within its header; no model can be
127
+ built."
128
+ """
129
+ return _sentence(f"The file ends within {where}", consequence)
130
+
131
+
132
+ def trailing_data(
133
+ where: str, extra: str, consequence: str = "it is ignored"
134
+ ) -> str:
135
+ """Return the details of a diagnostic of the type ``TRAILING_DATA``.
136
+
137
+ Parameters
138
+ ----------
139
+ where
140
+ What goes on, with its article: ``"The file"`` or ``"Line 4"``.
141
+ extra
142
+ What follows the data, such as ``"12 bytes"`` or ``"'1.0 2.0'"``.
143
+ consequence
144
+ What the reader did about it.
145
+
146
+ Returns
147
+ -------
148
+ The text, such as "The file goes on after its data with 12 bytes; it is
149
+ ignored."
150
+ """
151
+ return _sentence(
152
+ f"{where} goes on after its data with {extra}", consequence
153
+ )
154
+
155
+
156
+ def marker_missing(marker: str, consequence: str = "it is read anyway") -> str:
157
+ """Return the details of a diagnostic of the type ``MARKER_MISSING``.
158
+
159
+ Parameters
160
+ ----------
161
+ marker
162
+ The marker that the format prescribes, as it is written, such as
163
+ ``"'MAP '"``.
164
+ consequence
165
+ What the reader did about it.
166
+
167
+ Returns
168
+ -------
169
+ The text, such as "The file lacks the marker 'MAP '; it is read
170
+ anyway."
171
+ """
172
+ return _sentence(f"The file lacks the marker {marker}", consequence)
173
+
174
+
175
+ def field_missing(key: str, consequence: str) -> str:
176
+ """Return the details of a diagnostic of the type ``FIELD_MISSING``.
177
+
178
+ Parameters
179
+ ----------
180
+ key
181
+ The key.
182
+ consequence
183
+ What the reader did about it, such as ``"it is taken as 1"``, or
184
+ why no model can be built.
185
+
186
+ Returns
187
+ -------
188
+ The text, such as "SPACING is missing; it is taken as 1."
189
+ """
190
+ return _sentence(f"{key} is missing", consequence)
191
+
192
+
193
+ def field_duplicate(
194
+ key: str, first_line: int | None, kept: Literal["first", "last", "both"]
195
+ ) -> str:
196
+ """Return the details of a diagnostic of the type ``FIELD_DUPLICATE``.
197
+
198
+ Parameters
199
+ ----------
200
+ key
201
+ The key.
202
+ first_line
203
+ Line of the first occurrence, counted from 1, or `None` where it is
204
+ unknown, as for the verifier of a model.
205
+ kept
206
+ Which of the occurrences the reader keeps, or ``"both"`` for a model
207
+ that keeps each of them.
208
+
209
+ Returns
210
+ -------
211
+ The text, such as "SPACING occurs again, after line 3; the first one is
212
+ kept.", or "SPACING occurs again; the first one is kept." without a line.
213
+ """
214
+ after = "" if first_line is None else f", after line {first_line}"
215
+ consequence = (
216
+ "both are kept" if kept == "both" else f"the {kept} one is kept"
217
+ )
218
+ return f"{key} occurs again{after}; {consequence}."
219
+
220
+
221
+ def field_unknown(key: str, consequence: str = "the line is ignored") -> str:
222
+ """Return the details of a diagnostic of the type ``FIELD_UNKNOWN``.
223
+
224
+ Parameters
225
+ ----------
226
+ key
227
+ The key, as the file spells it.
228
+ consequence
229
+ What the reader did about it.
230
+
231
+ Returns
232
+ -------
233
+ The text, such as "The key 'COLOR' is none that the format defines; the
234
+ line is ignored."
235
+ """
236
+ return _sentence(
237
+ f"The key {describe(key)} is none that the format defines", consequence
238
+ )
239
+
240
+
241
+ def field_order(
242
+ key: str, after: str, consequence: str = "it is read anyway"
243
+ ) -> str:
244
+ """Return the details of a diagnostic of the type ``FIELD_ORDER``.
245
+
246
+ Parameters
247
+ ----------
248
+ key
249
+ The key that is out of order.
250
+ after
251
+ What it follows, but should precede, such as ``"the values"``.
252
+ consequence
253
+ What the reader did about it.
254
+
255
+ Returns
256
+ -------
257
+ The text, such as "SPACING comes after the values, which the format
258
+ puts after it; it is read anyway."
259
+ """
260
+ return _sentence(
261
+ f"{key} comes after {after}, which the format puts after it",
262
+ consequence,
263
+ )
264
+
265
+
266
+ def header_gap(
267
+ key: str, gap: str, consequence: str = "it is read anyway"
268
+ ) -> str:
269
+ """Return the details of a diagnostic of the type ``HEADER_GAP``.
270
+
271
+ Parameters
272
+ ----------
273
+ key
274
+ The key of the field that follows the gap.
275
+ gap
276
+ What sits before it, such as ``"a blank line"``, ``"a comment"`` or
277
+ ``"an ignored line"``.
278
+ consequence
279
+ What the reader did about it.
280
+
281
+ Returns
282
+ -------
283
+ The text, such as "NELEMENTS comes after a blank line, inside a header
284
+ that the format reads by position; it is read anyway."
285
+ """
286
+ return _sentence(
287
+ f"{key} comes after {gap}, inside a header that the format reads by"
288
+ " position",
289
+ consequence,
290
+ )
291
+
292
+
293
+ def value_invalid(
294
+ key: str,
295
+ value: object,
296
+ expected: str,
297
+ consequence: str = "the line is ignored",
298
+ ) -> str:
299
+ """Return the details of a value that cannot be read, of ``VALUE_INVALID``.
300
+
301
+ Parameters
302
+ ----------
303
+ key
304
+ The key, or the name of the model's field for a value without a key.
305
+ value
306
+ The value as the file gives it.
307
+ expected
308
+ What the key takes, as a noun phrase, such as ``"a number"``.
309
+ consequence
310
+ What the reader did about it.
311
+
312
+ Returns
313
+ -------
314
+ The text, such as "SPACING has 'abc', which cannot be read: SPACING
315
+ takes a number. The line is ignored."
316
+ """
317
+ return _sentences(
318
+ f"{key} has {describe(value)}, which cannot be read:"
319
+ f" {key} takes {expected}",
320
+ consequence,
321
+ )
322
+
323
+
324
+ def value_disallowed(
325
+ key: str, value: object, expected: str, consequence: str = ""
326
+ ) -> str:
327
+ """Return the details of a value that breaks a rule, of ``VALUE_INVALID``.
328
+
329
+ A verifier reports a value that a model holds, but the format does not
330
+ allow, with this wording, such as an unknown mode or a spacing that is
331
+ not finite.
332
+
333
+ Parameters
334
+ ----------
335
+ key
336
+ The key, or the name of the model's field for a value without a key.
337
+ value
338
+ The value.
339
+ expected
340
+ What the key takes, as a noun phrase, such as ``"a positive number"``.
341
+ consequence
342
+ What the reader did about it.
343
+
344
+ Returns
345
+ -------
346
+ The text, such as "SPACING is -0.5, which the format does not allow:
347
+ SPACING takes a positive number."
348
+ """
349
+ return _sentences(
350
+ f"{key} is {describe(value)}, which the format does not allow:"
351
+ f" {key} takes {expected}",
352
+ consequence,
353
+ )
354
+
355
+
356
+ def value_discouraged(
357
+ key: str, value: object, expected: str, consequence: str = ""
358
+ ) -> str:
359
+ """Return the details of a discouraged value, of ``VALUE_INVALID``.
360
+
361
+ A verifier reports a value that a model holds, and that the format
362
+ allows but advises against, with this wording: a rule that the
363
+ specification gives with "should", which `value_disallowed` would
364
+ overstate.
365
+
366
+ Parameters
367
+ ----------
368
+ key
369
+ The key, or the name of the model's field for a value without a key.
370
+ value
371
+ The value.
372
+ expected
373
+ What the key should take, as a noun phrase, such as ``"a version from
374
+ 20141 to 20999 in a new file of mode 12"``.
375
+ consequence
376
+ What the reader did about it.
377
+
378
+ Returns
379
+ -------
380
+ The text, such as "NVERSION is 20140, which the format advises against:
381
+ NVERSION should take a version from 20141 to 20999."
382
+ """
383
+ return _sentences(
384
+ f"{key} is {describe(value)}, which the format advises against:"
385
+ f" {key} should take {expected}",
386
+ consequence,
387
+ )
388
+
389
+
390
+ def count_mismatch(
391
+ what: str, found: int, expected: int, consequence: str
392
+ ) -> str:
393
+ """Return the details of a diagnostic of the type ``COUNT_MISMATCH``.
394
+
395
+ Parameters
396
+ ----------
397
+ what
398
+ What is counted, in the plural, such as ``"values"``.
399
+ found
400
+ How many the file has.
401
+ expected
402
+ How many the file declares.
403
+ consequence
404
+ What the reader did about it.
405
+
406
+ Returns
407
+ -------
408
+ The text, such as "The file has 3 values, but declares 4; the missing
409
+ ones are read as NaN."
410
+ """
411
+ return _sentence(
412
+ f"The file has {found} {what}, but declares {expected}", consequence
413
+ )
414
+
415
+
416
+ def feature_unsupported(
417
+ feature: str,
418
+ consequence: str = "it is ignored",
419
+ *,
420
+ lacked: str = "keep",
421
+ ) -> str:
422
+ """Return the details of a diagnostic of the type ``FEATURE_UNSUPPORTED``.
423
+
424
+ Parameters
425
+ ----------
426
+ feature
427
+ The feature, with its article, such as ``"the keyword 'constant'"``.
428
+ consequence
429
+ What the reader did about it.
430
+ lacked
431
+ What the package does not do with the feature, such as ``"apply"``
432
+ for a feature whose data are kept but not acted on.
433
+
434
+ Returns
435
+ -------
436
+ The text, such as "The format defines the keyword 'constant', which the
437
+ package does not keep; it is ignored."
438
+ """
439
+ return _sentence(
440
+ f"The format defines {feature}, which the package does not {lacked}",
441
+ consequence,
442
+ )
443
+
444
+
445
+ def _sentence(statement: str, consequence: str) -> str:
446
+ """Return a statement and its consequence as one sentence."""
447
+ return f"{statement}; {consequence}." if consequence else f"{statement}."
448
+
449
+
450
+ def _sentences(statement: str, consequence: str) -> str:
451
+ """Return a statement and its consequence as two sentences."""
452
+ if not consequence:
453
+ return f"{statement}."
454
+ return f"{statement}. {consequence[:1].upper()}{consequence[1:]}."
@@ -0,0 +1,6 @@
1
+ Metadata-Version: 2.4
2
+ Name: SciFile-Core
3
+ Version: 0.0.1
4
+ Summary: Shared foundation of the SciFile file-format packages
5
+ Requires-Python: >=3.12
6
+ Requires-Dist: FileEx<0.3,>=0.2.10
@@ -0,0 +1,16 @@
1
+ scifile_core/__init__.py,sha256=UlVxBdpSFFRgqik6OKpV6_Ozs_-qKA55Kbn9BuUS8z0,3300
2
+ scifile_core/_content.py,sha256=TA5r5OABlI1l-B-3B2Je80Y0x7vr-fTzO5PBYMS2xoI,17811
3
+ scifile_core/_diagnostic.py,sha256=En9UeF0vSFI-vgNUJJAy42LdFud03TshFN2ZUV3ZSg4,10861
4
+ scifile_core/_exception.py,sha256=MznGFjUWiB1s0vfkF5GroAdUNT36eiohNUNEaRimmjU,6771
5
+ scifile_core/_io.py,sha256=UTbCm_qAOgfktOEn0Nqm55Tl9FbKzjJS2SnslprijU8,22657
6
+ scifile_core/_mapping.py,sha256=T5TpdxvakBjQuf41sqAgXKUGBfpd5DaPvW8mwSCY4A4,4607
7
+ scifile_core/_model.py,sha256=RrU9Bni1GfBbRsCWO1bt5mGkByktjET3oEKrBO6muKM,36886
8
+ scifile_core/_number.py,sha256=A_RLXdnSdbQw1yCRW8UrsvEOSIrSoa1iYnonDp7gRsI,1529
9
+ scifile_core/_optional.py,sha256=1upzbn1EtzrccoqbXawOAZaXsBIt8jbghxbj6YmyFac,1521
10
+ scifile_core/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
11
+ scifile_core/testing.py,sha256=QNfhOl0z1WcImSV9G8KCa-LLbvMYlCuy3Z-WHUpL_kQ,23949
12
+ scifile_core/wording.py,sha256=UgkF8EFf11dhUq6fKc_3zuFr0MpN3zf_XVEZ07U-Zmw,13715
13
+ scifile_core-0.0.1.dist-info/METADATA,sha256=RIFEXUlzfZHUqmCfAyxd6LjXGgfIBU9Et2Q20C2YyfU,178
14
+ scifile_core-0.0.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
15
+ scifile_core-0.0.1.dist-info/top_level.txt,sha256=Ti-Td6LbXP5OelfLrtvdjEHdW9Ix-jOdNeO_q59hTsU,13
16
+ scifile_core-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1 @@
1
+ scifile_core