fortranread 1.0rc1__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.
- fortranread/__init__.py +152 -0
- fortranread/_complex.py +83 -0
- fortranread/_execution.py +167 -0
- fortranread/_real.py +445 -0
- fortranread/_sentinels.py +26 -0
- fortranread/_streams.py +226 -0
- fortranread/binary.py +233 -0
- fortranread/conversion.py +734 -0
- fortranread/data/__init__.py +1 -0
- fortranread/data/profiles/__init__.py +1 -0
- fortranread/data/profiles/gfortran-15.1.0-x86_64-linux-gnu.json +305 -0
- fortranread/data/profiles/gfortran-15.1.0-x86_64-w64-mingw32.json +285 -0
- fortranread/data/supplements/__init__.py +1 -0
- fortranread/data/supplements/gnu-15.1.0-linux-gnu-v1-complex.json +92 -0
- fortranread/data/supplements/gnu-15.1.0-mingw32-v1-complex.json +89 -0
- fortranread/errors.py +167 -0
- fortranread/formats.py +282 -0
- fortranread/formatted.py +440 -0
- fortranread/list_directed.py +432 -0
- fortranread/plans.py +823 -0
- fortranread/profiles.py +888 -0
- fortranread/py.typed +0 -0
- fortranread/reader.py +753 -0
- fortranread/results.py +414 -0
- fortranread/sources.py +1293 -0
- fortranread/state.py +229 -0
- fortranread/types.py +449 -0
- fortranread/typing.py +73 -0
- fortranread/unformatted.py +734 -0
- fortranread-1.0rc1.dist-info/METADATA +273 -0
- fortranread-1.0rc1.dist-info/RECORD +34 -0
- fortranread-1.0rc1.dist-info/WHEEL +5 -0
- fortranread-1.0rc1.dist-info/licenses/LICENSE +21 -0
- fortranread-1.0rc1.dist-info/top_level.txt +1 -0
fortranread/__init__.py
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"""Typed Fortran sequential input, record sources, positioning and conversion."""
|
|
2
|
+
|
|
3
|
+
from ._complex import ComplexInfo, ComplexValue
|
|
4
|
+
from ._real import RealClass, RealInfo, RealNaN, RoundingMode
|
|
5
|
+
from ._sentinels import UNCHANGED, UNDEFINED
|
|
6
|
+
from .binary import BinaryConverter
|
|
7
|
+
from .conversion import (
|
|
8
|
+
ConversionContext,
|
|
9
|
+
ConversionResult,
|
|
10
|
+
TextConverter,
|
|
11
|
+
convert_text,
|
|
12
|
+
round_decimal,
|
|
13
|
+
)
|
|
14
|
+
from .errors import (
|
|
15
|
+
EndOfFileError,
|
|
16
|
+
EndOfRecordError,
|
|
17
|
+
FortranReadError,
|
|
18
|
+
InputError,
|
|
19
|
+
ReaderStateError,
|
|
20
|
+
ReadPlanError,
|
|
21
|
+
UnsupportedFeatureError,
|
|
22
|
+
)
|
|
23
|
+
from .formats import Format
|
|
24
|
+
from .plans import (
|
|
25
|
+
Field,
|
|
26
|
+
PendingDestination,
|
|
27
|
+
PlanExecution,
|
|
28
|
+
PlanProgress,
|
|
29
|
+
ReadContext,
|
|
30
|
+
ReadPlan,
|
|
31
|
+
Repeat,
|
|
32
|
+
)
|
|
33
|
+
from .profiles import DEFAULT_PROFILE, ProcessorProfile
|
|
34
|
+
from .reader import FortranReader
|
|
35
|
+
from .results import (
|
|
36
|
+
AssignmentState,
|
|
37
|
+
DefinitionState,
|
|
38
|
+
DestinationPath,
|
|
39
|
+
DestinationResult,
|
|
40
|
+
Diagnostic,
|
|
41
|
+
GroupResult,
|
|
42
|
+
IOCondition,
|
|
43
|
+
ReadResult,
|
|
44
|
+
)
|
|
45
|
+
from .sources import (
|
|
46
|
+
ConnectionModes,
|
|
47
|
+
FormattedSource,
|
|
48
|
+
RecordEvent,
|
|
49
|
+
SourceCapabilities,
|
|
50
|
+
SourceRecord,
|
|
51
|
+
TextStreamPolicy,
|
|
52
|
+
)
|
|
53
|
+
from .state import (
|
|
54
|
+
EndfileState,
|
|
55
|
+
FilePosition,
|
|
56
|
+
PositionKnowledge,
|
|
57
|
+
ReaderLifecycle,
|
|
58
|
+
ReaderState,
|
|
59
|
+
)
|
|
60
|
+
from .types import (
|
|
61
|
+
Character,
|
|
62
|
+
Complex,
|
|
63
|
+
DestinationType,
|
|
64
|
+
DoublePrecision,
|
|
65
|
+
Integer,
|
|
66
|
+
Logical,
|
|
67
|
+
Real,
|
|
68
|
+
ResolvedType,
|
|
69
|
+
)
|
|
70
|
+
from .typing import (
|
|
71
|
+
BinaryStream,
|
|
72
|
+
InitialValue,
|
|
73
|
+
ReadValue,
|
|
74
|
+
ScalarValue,
|
|
75
|
+
StateMarker,
|
|
76
|
+
StreamCookie,
|
|
77
|
+
TextStream,
|
|
78
|
+
)
|
|
79
|
+
from .unformatted import UnformattedEvent, UnformattedSource
|
|
80
|
+
|
|
81
|
+
__version__ = "1.0rc1"
|
|
82
|
+
|
|
83
|
+
__all__ = [
|
|
84
|
+
"FortranReader",
|
|
85
|
+
"Format",
|
|
86
|
+
"UNCHANGED",
|
|
87
|
+
"UNDEFINED",
|
|
88
|
+
"Integer",
|
|
89
|
+
"Real",
|
|
90
|
+
"DoublePrecision",
|
|
91
|
+
"Logical",
|
|
92
|
+
"Complex",
|
|
93
|
+
"ComplexValue",
|
|
94
|
+
"ComplexInfo",
|
|
95
|
+
"Character",
|
|
96
|
+
"DestinationType",
|
|
97
|
+
"ResolvedType",
|
|
98
|
+
"ProcessorProfile",
|
|
99
|
+
"DEFAULT_PROFILE",
|
|
100
|
+
"ScalarValue",
|
|
101
|
+
"ReadValue",
|
|
102
|
+
"InitialValue",
|
|
103
|
+
"StateMarker",
|
|
104
|
+
"StreamCookie",
|
|
105
|
+
"BinaryStream",
|
|
106
|
+
"TextStream",
|
|
107
|
+
"Field",
|
|
108
|
+
"Repeat",
|
|
109
|
+
"ReadPlan",
|
|
110
|
+
"ReadContext",
|
|
111
|
+
"PlanExecution",
|
|
112
|
+
"PlanProgress",
|
|
113
|
+
"PendingDestination",
|
|
114
|
+
"IOCondition",
|
|
115
|
+
"DefinitionState",
|
|
116
|
+
"AssignmentState",
|
|
117
|
+
"DestinationPath",
|
|
118
|
+
"DestinationResult",
|
|
119
|
+
"GroupResult",
|
|
120
|
+
"Diagnostic",
|
|
121
|
+
"ReadResult",
|
|
122
|
+
"FilePosition",
|
|
123
|
+
"PositionKnowledge",
|
|
124
|
+
"EndfileState",
|
|
125
|
+
"ReaderLifecycle",
|
|
126
|
+
"ReaderState",
|
|
127
|
+
"FortranReadError",
|
|
128
|
+
"EndOfFileError",
|
|
129
|
+
"EndOfRecordError",
|
|
130
|
+
"InputError",
|
|
131
|
+
"UnsupportedFeatureError",
|
|
132
|
+
"ReaderStateError",
|
|
133
|
+
"ReadPlanError",
|
|
134
|
+
"FormattedSource",
|
|
135
|
+
"SourceRecord",
|
|
136
|
+
"RecordEvent",
|
|
137
|
+
"SourceCapabilities",
|
|
138
|
+
"TextStreamPolicy",
|
|
139
|
+
"ConnectionModes",
|
|
140
|
+
"ConversionContext",
|
|
141
|
+
"ConversionResult",
|
|
142
|
+
"RealClass",
|
|
143
|
+
"RealInfo",
|
|
144
|
+
"RoundingMode",
|
|
145
|
+
"TextConverter",
|
|
146
|
+
"convert_text",
|
|
147
|
+
"round_decimal",
|
|
148
|
+
"RealNaN",
|
|
149
|
+
"BinaryConverter",
|
|
150
|
+
"UnformattedSource",
|
|
151
|
+
"UnformattedEvent",
|
|
152
|
+
]
|
fortranread/_complex.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""Exact complex carriers and independent component representation metadata."""
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
from ._real import RealInfo, RealNaN, representation_bits
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass(frozen=True)
|
|
9
|
+
class ComplexValue:
|
|
10
|
+
"""A complex destination value with independently preserved real components.
|
|
11
|
+
|
|
12
|
+
Parameters
|
|
13
|
+
----------
|
|
14
|
+
real : float or RealNaN
|
|
15
|
+
Real component. Integers are not implicitly converted.
|
|
16
|
+
imag : float or RealNaN
|
|
17
|
+
Imaginary component. Integers are not implicitly converted.
|
|
18
|
+
|
|
19
|
+
Notes
|
|
20
|
+
-----
|
|
21
|
+
This carrier is independent of a destination kind. Resolving or assigning it
|
|
22
|
+
checks that both components fit the selected real model exactly. Use
|
|
23
|
+
as_complex only when a Python complex is needed.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
real: float | RealNaN
|
|
27
|
+
imag: float | RealNaN
|
|
28
|
+
|
|
29
|
+
def __post_init__(self) -> None:
|
|
30
|
+
"""Require each component to be a float or an exact RealNaN."""
|
|
31
|
+
if any(
|
|
32
|
+
type(part) is not float and not isinstance(part, RealNaN)
|
|
33
|
+
for part in (self.real, self.imag)
|
|
34
|
+
):
|
|
35
|
+
raise TypeError("complex components must be floats or exact RealNaN values")
|
|
36
|
+
|
|
37
|
+
def as_complex(self) -> complex:
|
|
38
|
+
"""Return a Python complex using the two components.
|
|
39
|
+
|
|
40
|
+
Converting RealNaN components may quiet signaling NaNs or change payloads.
|
|
41
|
+
The exact components in this object remain unchanged.
|
|
42
|
+
"""
|
|
43
|
+
return complex(
|
|
44
|
+
self.real.as_float() if isinstance(self.real, RealNaN) else self.real,
|
|
45
|
+
self.imag.as_float() if isinstance(self.imag, RealNaN) else self.imag,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass(frozen=True)
|
|
50
|
+
class ComplexInfo:
|
|
51
|
+
"""Immutable conversion metadata for both components of a complex value.
|
|
52
|
+
|
|
53
|
+
Parameters
|
|
54
|
+
----------
|
|
55
|
+
real : RealInfo
|
|
56
|
+
Real component's representation, rounding and conversion flags.
|
|
57
|
+
imag : RealInfo
|
|
58
|
+
Imaginary component's independent representation and conversion flags.
|
|
59
|
+
Both components must have the same storage width.
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
real: RealInfo
|
|
63
|
+
imag: RealInfo
|
|
64
|
+
|
|
65
|
+
def __post_init__(self) -> None:
|
|
66
|
+
"""Require RealInfo components with equal representation widths."""
|
|
67
|
+
if not isinstance(self.real, RealInfo) or not isinstance(self.imag, RealInfo):
|
|
68
|
+
raise TypeError("complex metadata requires two RealInfo values")
|
|
69
|
+
if len(self.real.bits) != len(self.imag.bits):
|
|
70
|
+
raise ValueError(
|
|
71
|
+
"complex components must have the same representation width"
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _validate_metadata(value: ComplexValue, metadata: ComplexInfo, width: int) -> None:
|
|
76
|
+
if not isinstance(metadata, ComplexInfo):
|
|
77
|
+
raise TypeError("complex representation metadata must be ComplexInfo")
|
|
78
|
+
for part, details in [(value.real, metadata.real), (value.imag, metadata.imag)]:
|
|
79
|
+
if (
|
|
80
|
+
len(details.bits) * 4 != width
|
|
81
|
+
or representation_bits(part, width) != details.bits
|
|
82
|
+
):
|
|
83
|
+
raise ValueError("complex value and component representation disagree")
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
"""One preflight, completion and condition path for input engines."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from dataclasses import replace
|
|
7
|
+
from typing import Generic, Literal, NoReturn, Protocol, TypeVar, cast
|
|
8
|
+
|
|
9
|
+
from .conversion import ConversionResult, TextConverter
|
|
10
|
+
from .plans import Field, PendingDestination, ReadPlan
|
|
11
|
+
from .profiles import ProcessorProfile
|
|
12
|
+
from .results import DestinationPath, Diagnostic, IOCondition, ReadResult
|
|
13
|
+
from .sources import FormattedSource
|
|
14
|
+
from .state import FilePosition, ReaderLifecycle
|
|
15
|
+
from .types import DestinationType, ResolvedType
|
|
16
|
+
from .typing import _DefinedValue
|
|
17
|
+
from .unformatted import UnformattedSource
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class _Stop(Exception):
|
|
21
|
+
def __init__(self, condition: IOCondition, diagnostic: Diagnostic | None) -> None:
|
|
22
|
+
self.condition, self.diagnostic = condition, diagnostic
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
_SourceT = TypeVar("_SourceT", FormattedSource, UnformattedSource)
|
|
26
|
+
_DataT = TypeVar("_DataT")
|
|
27
|
+
_DataT_contra = TypeVar("_DataT_contra", contravariant=True)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class _PreparedConverter(Protocol):
|
|
31
|
+
@property
|
|
32
|
+
def type(self) -> ResolvedType: ...
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class _ConverterFactory(Protocol):
|
|
36
|
+
def __call__(
|
|
37
|
+
self, destination: DestinationType, *, profile: ProcessorProfile
|
|
38
|
+
) -> _PreparedConverter: ...
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class _Converter(Protocol[_DataT_contra]):
|
|
42
|
+
"""Convert a positional payload without prescribing its parameter name."""
|
|
43
|
+
|
|
44
|
+
def convert(
|
|
45
|
+
self, data: _DataT_contra, /, *, path: DestinationPath | None = None
|
|
46
|
+
) -> ConversionResult: ...
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class _Statement(Generic[_SourceT]):
|
|
50
|
+
def __init__(
|
|
51
|
+
self,
|
|
52
|
+
source: _SourceT,
|
|
53
|
+
plan: ReadPlan,
|
|
54
|
+
*,
|
|
55
|
+
converter: _ConverterFactory = TextConverter,
|
|
56
|
+
) -> None:
|
|
57
|
+
self.source: _SourceT = source
|
|
58
|
+
self.execution = plan.start(source.profile)
|
|
59
|
+
self.pending: PendingDestination | None = None
|
|
60
|
+
self.current: PendingDestination | None = None
|
|
61
|
+
self.component: Literal["real", "imag"] | None = None
|
|
62
|
+
self.types: dict[DestinationType, ResolvedType] = {}
|
|
63
|
+
|
|
64
|
+
def prepare(body: ReadPlan) -> None:
|
|
65
|
+
for item in body.items:
|
|
66
|
+
descriptor = item.type if isinstance(item, Field) else item.body
|
|
67
|
+
if isinstance(descriptor, ReadPlan):
|
|
68
|
+
prepare(descriptor)
|
|
69
|
+
elif descriptor not in self.types:
|
|
70
|
+
# Validate every conversion kind, even unentered groups.
|
|
71
|
+
self.types[descriptor] = converter(
|
|
72
|
+
descriptor, profile=source.profile
|
|
73
|
+
).type
|
|
74
|
+
|
|
75
|
+
prepare(plan)
|
|
76
|
+
|
|
77
|
+
def next_destination(self) -> PendingDestination | None:
|
|
78
|
+
if self.pending is None:
|
|
79
|
+
self.pending = self.execution.next_destination()
|
|
80
|
+
self.current = self.pending
|
|
81
|
+
return self.pending
|
|
82
|
+
|
|
83
|
+
def retain(self) -> None:
|
|
84
|
+
self.execution.retain()
|
|
85
|
+
self.pending = None
|
|
86
|
+
|
|
87
|
+
def convert(
|
|
88
|
+
self,
|
|
89
|
+
converter: _Converter[_DataT],
|
|
90
|
+
data: _DataT,
|
|
91
|
+
position: FilePosition,
|
|
92
|
+
*,
|
|
93
|
+
assign: bool = True,
|
|
94
|
+
) -> ConversionResult:
|
|
95
|
+
converted = converter.convert(
|
|
96
|
+
data, path=cast(PendingDestination, self.pending).path
|
|
97
|
+
)
|
|
98
|
+
if converted.condition is not IOCondition.OK:
|
|
99
|
+
raise _Stop(
|
|
100
|
+
converted.condition,
|
|
101
|
+
replace(
|
|
102
|
+
cast(Diagnostic, converted.diagnostic),
|
|
103
|
+
position=position,
|
|
104
|
+
source=self.source.identity,
|
|
105
|
+
),
|
|
106
|
+
)
|
|
107
|
+
if assign:
|
|
108
|
+
self.execution.assign(
|
|
109
|
+
cast(_DefinedValue, converted.value),
|
|
110
|
+
real=converted.real,
|
|
111
|
+
complex=converted.complex,
|
|
112
|
+
)
|
|
113
|
+
self.pending = None
|
|
114
|
+
return converted
|
|
115
|
+
|
|
116
|
+
def record(self) -> None:
|
|
117
|
+
event = self.source.next_record()
|
|
118
|
+
if event.condition is not IOCondition.OK:
|
|
119
|
+
diagnostic = (
|
|
120
|
+
replace(event.diagnostic, position=event.position)
|
|
121
|
+
if event.diagnostic
|
|
122
|
+
else None
|
|
123
|
+
)
|
|
124
|
+
raise _Stop(event.condition, diagnostic)
|
|
125
|
+
|
|
126
|
+
def fail(
|
|
127
|
+
self,
|
|
128
|
+
message: str,
|
|
129
|
+
*,
|
|
130
|
+
position: FilePosition | None = None,
|
|
131
|
+
excerpt: str | None = None,
|
|
132
|
+
category: str = "format",
|
|
133
|
+
) -> NoReturn:
|
|
134
|
+
raise _Stop(
|
|
135
|
+
IOCondition.ERROR,
|
|
136
|
+
Diagnostic(
|
|
137
|
+
message,
|
|
138
|
+
position=position or self.source.position,
|
|
139
|
+
source=self.source.identity,
|
|
140
|
+
excerpt=excerpt,
|
|
141
|
+
category=category,
|
|
142
|
+
),
|
|
143
|
+
)
|
|
144
|
+
|
|
145
|
+
def run(self, transfer: Callable[[], bool]) -> ReadResult:
|
|
146
|
+
self.source.begin()
|
|
147
|
+
try:
|
|
148
|
+
terminated = transfer()
|
|
149
|
+
position = self.source.complete()
|
|
150
|
+
return self.execution.finish(position=position, terminated=terminated)
|
|
151
|
+
except _Stop as stop:
|
|
152
|
+
diagnostic = stop.diagnostic
|
|
153
|
+
if self.current is not None and diagnostic is not None:
|
|
154
|
+
diagnostic = replace(
|
|
155
|
+
diagnostic,
|
|
156
|
+
path=self.current.path,
|
|
157
|
+
expected=self.current.type,
|
|
158
|
+
component=diagnostic.component or self.component,
|
|
159
|
+
)
|
|
160
|
+
position = self.source.complete(stop.condition)
|
|
161
|
+
return self.execution.finish(
|
|
162
|
+
stop.condition, position=position, diagnostic=diagnostic
|
|
163
|
+
)
|
|
164
|
+
except BaseException:
|
|
165
|
+
if self.source.state.lifecycle is ReaderLifecycle.EXECUTING:
|
|
166
|
+
self.source.abort()
|
|
167
|
+
raise
|