Skip to content

Commit 49f58a0

Browse files
committed
Update serialization
1 parent 412a235 commit 49f58a0

File tree

2 files changed

+15
-46
lines changed

2 files changed

+15
-46
lines changed

foamlib/_files/_parsing.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,24 +253,24 @@ def _unpack_binary_field(
253253
)
254254
)
255255
_TOKEN = QuotedString('"', unquote_results=False) | _IDENTIFIER
256-
_DATA = Forward()
257-
_KEYWORD = _TOKEN | _list_of(_IDENTIFIER).set_parse_action(
256+
DATA = Forward()
257+
KEYWORD = _TOKEN | _list_of(_IDENTIFIER).set_parse_action(
258258
lambda tks: "(" + " ".join(tks[0]) + ")"
259259
)
260-
_KEYWORD_ENTRY = Dict(Group(_keyword_entry_of(_KEYWORD, _DATA)), asdict=True)
260+
_KEYWORD_ENTRY = Dict(Group(_keyword_entry_of(KEYWORD, DATA)), asdict=True)
261261
_DATA_ENTRY = Forward()
262262
_LIST_ENTRY = _KEYWORD_ENTRY | _DATA_ENTRY
263263
_LIST = _list_of(_LIST_ENTRY)
264264
_NUMBER = common.signed_integer ^ common.ieee_float
265265
_DATA_ENTRY <<= _FIELD | _LIST | _DIMENSIONED | _DIMENSIONS | _NUMBER | _SWITCH | _TOKEN
266266

267-
_DATA <<= _DATA_ENTRY[1, ...].set_parse_action(
267+
DATA <<= _DATA_ENTRY[1, ...].set_parse_action(
268268
lambda tks: tuple(tks) if len(tks) > 1 else [tks[0]]
269269
)
270270

271271
_FILE = (
272272
Dict(
273-
Group(_keyword_entry_of(_KEYWORD, Opt(_DATA, default=""), located=True))[...]
273+
Group(_keyword_entry_of(KEYWORD, Opt(DATA, default=""), located=True))[...]
274274
+ Opt(
275275
Group(
276276
Located(
@@ -280,7 +280,7 @@ def _unpack_binary_field(
280280
)
281281
)
282282
)
283-
+ Group(_keyword_entry_of(_KEYWORD, Opt(_DATA, default=""), located=True))[...]
283+
+ Group(_keyword_entry_of(KEYWORD, Opt(DATA, default=""), located=True))[...]
284284
)
285285
.ignore(cpp_style_comment)
286286
.ignore(Literal("#include") + ... + LineEnd()) # type: ignore [no-untyped-call]

foamlib/_files/_serialization.py

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from __future__ import annotations
22

33
import array
4-
import contextlib
54
import itertools
6-
import re
75
import sys
86
from enum import Enum, auto
97
from typing import cast, overload
@@ -13,6 +11,7 @@
1311
else:
1412
from typing import Mapping, Sequence
1513

14+
from ._parsing import DATA, KEYWORD
1615
from ._types import Data, DataEntry, Dimensioned, DimensionSet
1716
from ._util import is_sequence
1817

@@ -34,9 +33,6 @@ class Kind(Enum):
3433
DIMENSIONS = auto()
3534

3635

37-
_TOKENS = re.compile(r'(?:[^\s"]|"(?:[^"])*")+')
38-
39-
4036
@overload
4137
def normalize(data: DataEntry, *, kind: Kind = Kind.DEFAULT) -> DataEntry: ...
4238

@@ -69,46 +65,19 @@ def normalize(data: Data, *, kind: Kind = Kind.DEFAULT) -> Data:
6965

7066
return [normalize(d, kind=Kind.SINGLE_ENTRY) for d in data]
7167

72-
if isinstance(data, str):
73-
data = data.strip()
74-
75-
if data.startswith("(") and data.endswith(")"):
76-
data = data[1:-1].split()
77-
if kind == Kind.KEYWORD:
78-
return "(" + " ".join(data) + ")"
79-
return [normalize(d, kind=Kind.SINGLE_ENTRY) for d in data]
80-
81-
if data.startswith("[") and data.endswith("]"):
82-
data = data[1:-1].split()
83-
return normalize(data, kind=Kind.DIMENSIONS)
84-
85-
with contextlib.suppress(ValueError):
86-
return int(data)
87-
88-
with contextlib.suppress(ValueError):
89-
return float(data)
90-
91-
if kind != Kind.KEYWORD:
92-
if data in ("yes", "true", "on", "y", "t"):
93-
return True
94-
if data in ("no", "false", "off", "n", "f"):
95-
return False
96-
97-
tokens: list[str] = re.findall(_TOKENS, data)
98-
99-
if len(tokens) == 1:
100-
return tokens[0]
101-
102-
if kind == Kind.KEYWORD:
103-
return " ".join(tokens)
104-
105-
return tuple(tokens)
106-
10768
if isinstance(data, Dimensioned):
10869
value = normalize(data.value, kind=Kind.SINGLE_ENTRY)
10970
assert isinstance(value, (int, float, list))
11071
return Dimensioned(value, data.dimensions, data.name)
11172

73+
if isinstance(data, str):
74+
if kind == Kind.KEYWORD:
75+
data = KEYWORD.parse_string(data)[0]
76+
assert isinstance(data, str)
77+
return data
78+
79+
return cast(DataEntry, DATA.parse_string(data)[0])
80+
11281
if isinstance(
11382
data,
11483
(int, float, bool, tuple, DimensionSet),

0 commit comments

Comments
 (0)