foamlib 0.2.6__py3-none-any.whl → 0.2.8__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.
- foamlib/__init__.py +1 -1
- foamlib/_dictionaries/_base.py +21 -4
- foamlib/_dictionaries/_files.py +25 -6
- foamlib/_dictionaries/_parsing.py +33 -12
- foamlib/_dictionaries/_serialization.py +24 -9
- {foamlib-0.2.6.dist-info → foamlib-0.2.8.dist-info}/METADATA +1 -1
- foamlib-0.2.8.dist-info/RECORD +14 -0
- foamlib-0.2.6.dist-info/RECORD +0 -14
- {foamlib-0.2.6.dist-info → foamlib-0.2.8.dist-info}/LICENSE.txt +0 -0
- {foamlib-0.2.6.dist-info → foamlib-0.2.8.dist-info}/WHEEL +0 -0
- {foamlib-0.2.6.dist-info → foamlib-0.2.8.dist-info}/top_level.txt +0 -0
foamlib/__init__.py
CHANGED
foamlib/_dictionaries/_base.py
CHANGED
@@ -4,9 +4,15 @@ from dataclasses import dataclass
|
|
4
4
|
from typing import Dict, NamedTuple, Optional, Union
|
5
5
|
|
6
6
|
if sys.version_info >= (3, 9):
|
7
|
-
from collections.abc import Sequence
|
7
|
+
from collections.abc import Mapping, Sequence
|
8
8
|
else:
|
9
|
-
from typing import Sequence
|
9
|
+
from typing import Mapping, Sequence
|
10
|
+
|
11
|
+
try:
|
12
|
+
import numpy as np
|
13
|
+
from numpy.typing import NDArray
|
14
|
+
except ModuleNotFoundError:
|
15
|
+
pass
|
10
16
|
|
11
17
|
|
12
18
|
class FoamDictionaryBase:
|
@@ -34,14 +40,25 @@ class FoamDictionaryBase:
|
|
34
40
|
if not isinstance(self.dimensions, FoamDictionaryBase.DimensionSet):
|
35
41
|
self.dimensions = FoamDictionaryBase.DimensionSet(*self.dimensions)
|
36
42
|
|
37
|
-
Value = Union[
|
43
|
+
Value = Union[
|
44
|
+
str,
|
45
|
+
int,
|
46
|
+
float,
|
47
|
+
bool,
|
48
|
+
Dimensioned,
|
49
|
+
DimensionSet,
|
50
|
+
Sequence["Value"],
|
51
|
+
Mapping[str, "Value"],
|
52
|
+
]
|
38
53
|
"""
|
39
54
|
A value that can be stored in an OpenFOAM dictionary.
|
40
55
|
"""
|
41
56
|
|
42
|
-
_Dict = Dict[str, Union["
|
57
|
+
_Dict = Dict[str, Union["Value", "_Dict"]]
|
43
58
|
|
44
59
|
@abstractmethod
|
45
60
|
def as_dict(self) -> _Dict:
|
46
61
|
"""Return a nested dict representation of the dictionary."""
|
47
62
|
raise NotImplementedError
|
63
|
+
|
64
|
+
_SetValue = Union[Value, "NDArray[np.generic]"]
|
foamlib/_dictionaries/_files.py
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
import sys
|
2
2
|
from pathlib import Path
|
3
|
+
from types import TracebackType
|
3
4
|
from typing import (
|
4
5
|
Any,
|
5
6
|
Optional,
|
6
7
|
Tuple,
|
8
|
+
Type,
|
7
9
|
Union,
|
8
10
|
cast,
|
9
11
|
)
|
@@ -48,7 +50,12 @@ class _FoamFileBase:
|
|
48
50
|
self.__defer_io += 1
|
49
51
|
return self
|
50
52
|
|
51
|
-
def __exit__(
|
53
|
+
def __exit__(
|
54
|
+
self,
|
55
|
+
exc_type: Optional[Type[BaseException]],
|
56
|
+
exc_val: Optional[BaseException],
|
57
|
+
exc_tb: Optional[TracebackType],
|
58
|
+
) -> None:
|
52
59
|
self.__defer_io -= 1
|
53
60
|
if self.__defer_io == 0 and self.__dirty:
|
54
61
|
assert self.__contents is not None
|
@@ -124,7 +131,7 @@ class FoamFile(
|
|
124
131
|
assume_dimensions=assume_dimensions,
|
125
132
|
)
|
126
133
|
|
127
|
-
def __setitem__(self, keyword: str, value:
|
134
|
+
def __setitem__(self, keyword: str, value: "FoamFile._SetValue") -> None:
|
128
135
|
self._setitem(keyword, value)
|
129
136
|
|
130
137
|
def __delitem__(self, keyword: str) -> None:
|
@@ -180,7 +187,7 @@ class FoamFile(
|
|
180
187
|
def _setitem(
|
181
188
|
self,
|
182
189
|
keywords: Union[str, Tuple[str, ...]],
|
183
|
-
value:
|
190
|
+
value: "FoamFile._SetValue",
|
184
191
|
*,
|
185
192
|
assume_field: bool = False,
|
186
193
|
assume_dimensions: bool = False,
|
@@ -210,7 +217,11 @@ class FoamFile(
|
|
210
217
|
f"{contents[:start]}\n{serialize_entry(keywords[-1], value, assume_field=assume_field, assume_dimensions=assume_dimensions)}\n{contents[end:]}"
|
211
218
|
)
|
212
219
|
|
213
|
-
def __setitem__(
|
220
|
+
def __setitem__(
|
221
|
+
self,
|
222
|
+
keywords: Union[str, Tuple[str, ...]],
|
223
|
+
value: "FoamFile._SetValue",
|
224
|
+
) -> None:
|
214
225
|
self._setitem(keywords, value)
|
215
226
|
|
216
227
|
def __delitem__(self, keywords: Union[str, Tuple[str, ...]]) -> None:
|
@@ -269,12 +280,20 @@ class FoamFieldFile(FoamFile):
|
|
269
280
|
|
270
281
|
class BoundariesDictionary(FoamFile.Dictionary):
|
271
282
|
def __getitem__(self, keyword: str) -> "FoamFieldFile.BoundaryDictionary":
|
272
|
-
|
283
|
+
value = super().__getitem__(keyword)
|
284
|
+
if not isinstance(value, FoamFieldFile.BoundaryDictionary):
|
285
|
+
assert not isinstance(value, FoamFile.Dictionary)
|
286
|
+
raise TypeError(f"boundary {keyword} is not a dictionary")
|
287
|
+
return value
|
273
288
|
|
274
289
|
class BoundaryDictionary(FoamFile.Dictionary):
|
275
290
|
"""An OpenFOAM dictionary representing a boundary condition as a mutable mapping."""
|
276
291
|
|
277
|
-
def __setitem__(
|
292
|
+
def __setitem__(
|
293
|
+
self,
|
294
|
+
key: str,
|
295
|
+
value: FoamFile._SetValue,
|
296
|
+
) -> None:
|
278
297
|
if key == "value":
|
279
298
|
self._setitem(key, value, assume_field=True)
|
280
299
|
else:
|
@@ -1,11 +1,16 @@
|
|
1
1
|
import sys
|
2
|
-
from typing import Optional, Tuple
|
2
|
+
from typing import Optional, Tuple, Union
|
3
3
|
|
4
4
|
if sys.version_info >= (3, 9):
|
5
5
|
from collections.abc import Mapping, MutableMapping, Sequence
|
6
6
|
else:
|
7
7
|
from typing import Mapping, MutableMapping, Sequence
|
8
8
|
|
9
|
+
if sys.version_info >= (3, 10):
|
10
|
+
from types import EllipsisType
|
11
|
+
else:
|
12
|
+
from typing import Any as EllipsisType
|
13
|
+
|
9
14
|
from pyparsing import (
|
10
15
|
Dict,
|
11
16
|
Forward,
|
@@ -56,6 +61,28 @@ def _list_of(elem: ParserElement) -> ParserElement:
|
|
56
61
|
)
|
57
62
|
|
58
63
|
|
64
|
+
def _dictionary_of(
|
65
|
+
keyword: ParserElement,
|
66
|
+
value: ParserElement,
|
67
|
+
*,
|
68
|
+
len: Union[int, EllipsisType] = ...,
|
69
|
+
located: bool = False,
|
70
|
+
) -> ParserElement:
|
71
|
+
subdict = Forward()
|
72
|
+
|
73
|
+
entry = keyword + (
|
74
|
+
(Literal("{").suppress() + subdict + Literal("}").suppress())
|
75
|
+
| (value + Literal(";").suppress())
|
76
|
+
)
|
77
|
+
|
78
|
+
if located:
|
79
|
+
entry = Located(entry)
|
80
|
+
|
81
|
+
subdict <<= Dict(Group(entry)[...], asdict=not located)
|
82
|
+
|
83
|
+
return Dict(Group(entry)[len], asdict=not located)
|
84
|
+
|
85
|
+
|
59
86
|
_TENSOR = _list_of(common.number) | common.number
|
60
87
|
_IDENTIFIER = Word(identbodychars + "$", printables.replace(";", ""))
|
61
88
|
_DIMENSIONED = (Opt(_IDENTIFIER) + _DIMENSIONS + _TENSOR).set_parse_action(
|
@@ -66,25 +93,19 @@ _FIELD = (Keyword("uniform").suppress() + _TENSOR) | (
|
|
66
93
|
)
|
67
94
|
_TOKEN = QuotedString('"', unquote_results=False) | _IDENTIFIER
|
68
95
|
_ITEM = Forward()
|
69
|
-
|
96
|
+
_ENTRY = _dictionary_of(_IDENTIFIER, _ITEM, len=1)
|
97
|
+
_LIST = _list_of(_ENTRY | _ITEM)
|
70
98
|
_ITEM <<= _FIELD | _LIST | _DIMENSIONED | _DIMENSIONS | common.number | _SWITCH | _TOKEN
|
99
|
+
|
71
100
|
_TOKENS = (
|
72
101
|
QuotedString('"', unquote_results=False) | Word(printables.replace(";", ""))
|
73
102
|
)[2, ...].set_parse_action(lambda tks: " ".join(tks))
|
74
103
|
|
75
104
|
_VALUE = _ITEM ^ _TOKENS
|
76
105
|
|
77
|
-
_ENTRY = Forward()
|
78
|
-
_DICTIONARY = Dict(Group(_ENTRY)[...])
|
79
|
-
_ENTRY <<= Located(
|
80
|
-
_TOKEN
|
81
|
-
+ (
|
82
|
-
(Literal("{").suppress() + _DICTIONARY + Literal("}").suppress())
|
83
|
-
| (Opt(_VALUE, default="") + Literal(";").suppress())
|
84
|
-
)
|
85
|
-
)
|
86
106
|
_FILE = (
|
87
|
-
|
107
|
+
_dictionary_of(_TOKEN, Opt(_VALUE, default=""), located=True)
|
108
|
+
.ignore(c_style_comment)
|
88
109
|
.ignore(cpp_style_comment)
|
89
110
|
.ignore(Literal("#include") + ... + LineEnd()) # type: ignore [no-untyped-call]
|
90
111
|
)
|
@@ -1,6 +1,5 @@
|
|
1
1
|
import sys
|
2
2
|
from contextlib import suppress
|
3
|
-
from typing import Any
|
4
3
|
|
5
4
|
if sys.version_info >= (3, 9):
|
6
5
|
from collections.abc import Mapping
|
@@ -11,7 +10,7 @@ from .._util import is_sequence
|
|
11
10
|
from ._base import FoamDictionaryBase
|
12
11
|
|
13
12
|
|
14
|
-
def _serialize_switch(value:
|
13
|
+
def _serialize_switch(value: FoamDictionaryBase._SetValue) -> str:
|
15
14
|
if value is True:
|
16
15
|
return "yes"
|
17
16
|
elif value is False:
|
@@ -20,14 +19,18 @@ def _serialize_switch(value: Any) -> str:
|
|
20
19
|
raise TypeError(f"Not a bool: {type(value)}")
|
21
20
|
|
22
21
|
|
23
|
-
def _serialize_list(
|
22
|
+
def _serialize_list(
|
23
|
+
value: FoamDictionaryBase._SetValue,
|
24
|
+
) -> str:
|
24
25
|
if is_sequence(value):
|
25
26
|
return f"({' '.join(_serialize_value(v) for v in value)})"
|
26
27
|
else:
|
27
28
|
raise TypeError(f"Not a valid sequence: {type(value)}")
|
28
29
|
|
29
30
|
|
30
|
-
def _serialize_field(
|
31
|
+
def _serialize_field(
|
32
|
+
value: FoamDictionaryBase._SetValue,
|
33
|
+
) -> str:
|
31
34
|
if is_sequence(value):
|
32
35
|
try:
|
33
36
|
s = _serialize_list(value)
|
@@ -54,14 +57,18 @@ def _serialize_field(value: Any) -> str:
|
|
54
57
|
return f"uniform {value}"
|
55
58
|
|
56
59
|
|
57
|
-
def _serialize_dimensions(
|
60
|
+
def _serialize_dimensions(
|
61
|
+
value: FoamDictionaryBase._SetValue,
|
62
|
+
) -> str:
|
58
63
|
if is_sequence(value) and len(value) == 7:
|
59
64
|
return f"[{' '.join(str(v) for v in value)}]"
|
60
65
|
else:
|
61
66
|
raise TypeError(f"Not a valid dimension set: {type(value)}")
|
62
67
|
|
63
68
|
|
64
|
-
def _serialize_dimensioned(
|
69
|
+
def _serialize_dimensioned(
|
70
|
+
value: FoamDictionaryBase._SetValue,
|
71
|
+
) -> str:
|
65
72
|
if isinstance(value, FoamDictionaryBase.Dimensioned):
|
66
73
|
if value.name is not None:
|
67
74
|
return f"{value.name} {_serialize_dimensions(value.dimensions)} {_serialize_value(value.value)}"
|
@@ -72,7 +79,10 @@ def _serialize_dimensioned(value: Any) -> str:
|
|
72
79
|
|
73
80
|
|
74
81
|
def _serialize_value(
|
75
|
-
value:
|
82
|
+
value: FoamDictionaryBase._SetValue,
|
83
|
+
*,
|
84
|
+
assume_field: bool = False,
|
85
|
+
assume_dimensions: bool = False,
|
76
86
|
) -> str:
|
77
87
|
if isinstance(value, FoamDictionaryBase.DimensionSet) or assume_dimensions:
|
78
88
|
with suppress(TypeError):
|
@@ -91,10 +101,15 @@ def _serialize_value(
|
|
91
101
|
with suppress(TypeError):
|
92
102
|
return _serialize_switch(value)
|
93
103
|
|
104
|
+
with suppress(TypeError):
|
105
|
+
return _serialize_dictionary(value)
|
106
|
+
|
94
107
|
return str(value)
|
95
108
|
|
96
109
|
|
97
|
-
def _serialize_dictionary(
|
110
|
+
def _serialize_dictionary(
|
111
|
+
value: FoamDictionaryBase._SetValue,
|
112
|
+
) -> str:
|
98
113
|
if isinstance(value, Mapping):
|
99
114
|
return "\n".join(serialize_entry(k, v) for k, v in value.items())
|
100
115
|
else:
|
@@ -103,7 +118,7 @@ def _serialize_dictionary(value: Any) -> str:
|
|
103
118
|
|
104
119
|
def serialize_entry(
|
105
120
|
keyword: str,
|
106
|
-
value:
|
121
|
+
value: FoamDictionaryBase._SetValue,
|
107
122
|
*,
|
108
123
|
assume_field: bool = False,
|
109
124
|
assume_dimensions: bool = False,
|
@@ -0,0 +1,14 @@
|
|
1
|
+
foamlib/__init__.py,sha256=y4oSMModvQ8ODy8CLcq4AzJpIAJMDFspldx67cdai5Y,344
|
2
|
+
foamlib/_cases.py,sha256=SjDTVauOlcrtyvnt5uxWGSu2RYZ0lhKRmvMnuRnRuzI,20973
|
3
|
+
foamlib/_util.py,sha256=PBTpBwt_j1GXASncSDZUR8pH2u_h8UyJXm8GeFKebTY,2552
|
4
|
+
foamlib/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
5
|
+
foamlib/_dictionaries/__init__.py,sha256=vxGpA7uaEbJwKFGOYdLFR6R9jcUj_HW60YzZBMjNXRo,160
|
6
|
+
foamlib/_dictionaries/_base.py,sha256=0GkUmj268EmrGfB553JGLuNM6xq8DTIYK2NdN1rvFYU,1836
|
7
|
+
foamlib/_dictionaries/_files.py,sha256=Sz7wnCjZZSHWEnLw-MpAGqSiM47NS2Vrrc5rfCgJR74,12942
|
8
|
+
foamlib/_dictionaries/_parsing.py,sha256=kzpuY_KlvkyMXIamxVwVvYSMpMY5sXCbIZ2ARNdWJtg,5512
|
9
|
+
foamlib/_dictionaries/_serialization.py,sha256=tbg63f0Nro89iqN2fsEnRzVPoxOVxYDZA4H9YhUiGtw,3776
|
10
|
+
foamlib-0.2.8.dist-info/LICENSE.txt,sha256=5Dte9TUnLZzPRs4NQzl-Jc2-Ljd-t_v0ZR5Ng5r0UsY,35131
|
11
|
+
foamlib-0.2.8.dist-info/METADATA,sha256=QZ6WZ8eZl9hUAjmEDZg8gahPqCQuonPWN9Kx6N2VluU,4650
|
12
|
+
foamlib-0.2.8.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
13
|
+
foamlib-0.2.8.dist-info/top_level.txt,sha256=ZdVYtetXGwPwyfL-WhlhbTFQGAwKX5P_gXxtH9JYFPI,8
|
14
|
+
foamlib-0.2.8.dist-info/RECORD,,
|
foamlib-0.2.6.dist-info/RECORD
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
foamlib/__init__.py,sha256=Y-_qOUsVNMpblI4RzK4j1yA1IsImanCa_v2miOUK_jQ,344
|
2
|
-
foamlib/_cases.py,sha256=SjDTVauOlcrtyvnt5uxWGSu2RYZ0lhKRmvMnuRnRuzI,20973
|
3
|
-
foamlib/_util.py,sha256=PBTpBwt_j1GXASncSDZUR8pH2u_h8UyJXm8GeFKebTY,2552
|
4
|
-
foamlib/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
5
|
-
foamlib/_dictionaries/__init__.py,sha256=vxGpA7uaEbJwKFGOYdLFR6R9jcUj_HW60YzZBMjNXRo,160
|
6
|
-
foamlib/_dictionaries/_base.py,sha256=RRbuZT_wzbhVyhpP4RIw5_pY3AW62MZJQveLJ2bVD-4,1587
|
7
|
-
foamlib/_dictionaries/_files.py,sha256=gYeimu-8ZXyUaHf5jXp8zLE-Kvnzb6x0TZlbjNTVA0I,12457
|
8
|
-
foamlib/_dictionaries/_parsing.py,sha256=s6t9JJxAjsSp3ypeQNeskgtYWsbb52iBjrVDTUv56jc,5008
|
9
|
-
foamlib/_dictionaries/_serialization.py,sha256=1QJMxK0zXilpQoWR0ZPW2OImF1WRhXfuVDy-iCuELJc,3476
|
10
|
-
foamlib-0.2.6.dist-info/LICENSE.txt,sha256=5Dte9TUnLZzPRs4NQzl-Jc2-Ljd-t_v0ZR5Ng5r0UsY,35131
|
11
|
-
foamlib-0.2.6.dist-info/METADATA,sha256=Y10E-baTUs-CytAKybJIZtvslXkjnKxwPylTdXpEbD8,4650
|
12
|
-
foamlib-0.2.6.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
|
13
|
-
foamlib-0.2.6.dist-info/top_level.txt,sha256=ZdVYtetXGwPwyfL-WhlhbTFQGAwKX5P_gXxtH9JYFPI,8
|
14
|
-
foamlib-0.2.6.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|