Skip to content

Commit cf0cc87

Browse files
committed
Merge branch 'main' into joss
2 parents b7e8b6c + 9627c72 commit cf0cc87

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

foamlib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""A Python interface for interacting with OpenFOAM."""
22

3-
__version__ = "0.8.3"
3+
__version__ = "0.8.4"
44

55
from ._cases import (
66
AsyncFoamCase,

foamlib/_files/_files.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,15 @@ def __setitem__(self, keywords: str | tuple[str, ...] | None, data: Entry) -> No
308308
self[(*keywords, k)] = v
309309

310310
elif keywords:
311+
val = dumps(data, kind=kind)
311312
parsed.put(
312313
keywords,
313314
normalize(data, kind=kind),
314315
before
315316
+ indentation
316317
+ dumps(keywords[-1])
317-
+ b" "
318-
+ dumps(data, kind=kind)
319-
+ b";"
318+
+ ((b" " + val) if val else b"")
319+
+ (b";" if not keywords[-1].startswith("#") else b"")
320320
+ after,
321321
)
322322

foamlib/_files/_parsing.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,19 @@ def count_parse_action(tks: ParseResults) -> None:
153153

154154

155155
def _dict_of(
156-
keyword: ParserElement, data: ParserElement, *, located: bool = False
156+
keyword: ParserElement,
157+
data: ParserElement,
158+
*,
159+
directive: ParserElement | None = None,
160+
located: bool = False,
157161
) -> ParserElement:
158162
dict_ = Forward()
159163

160164
keyword_entry = keyword + (dict_ | (data + Literal(";").suppress()))
161165

166+
if directive is not None:
167+
keyword_entry |= directive + data + LineEnd().suppress() # type: ignore [no-untyped-call]
168+
162169
if located:
163170
keyword_entry = Located(keyword_entry)
164171

@@ -175,12 +182,17 @@ def _keyword_entry_of(
175182
keyword: ParserElement,
176183
data: ParserElement,
177184
*,
185+
directive: ParserElement | None = None,
178186
located: bool = False,
179187
) -> ParserElement:
180188
keyword_entry = keyword + (
181-
_dict_of(keyword, data, located=located) | (data + Literal(";").suppress())
189+
_dict_of(keyword, data, directive=directive, located=located)
190+
| (data + Literal(";").suppress())
182191
)
183192

193+
if directive is not None:
194+
keyword_entry |= directive + data + LineEnd().suppress() # type: ignore [no-untyped-call]
195+
184196
if located:
185197
keyword_entry = Located(keyword_entry)
186198
else:
@@ -240,7 +252,8 @@ def _keyword_entry_of(
240252
| _tensor_list(TensorKind.TENSOR, ignore=_COMMENT)
241253
)
242254
)
243-
_TOKEN = dbl_quoted_string | _IDENTIFIER
255+
_DIRECTIVE = Word("#", _IDENTBODYCHARS)
256+
_TOKEN = dbl_quoted_string | _IDENTIFIER | _DIRECTIVE
244257
_DATA = Forward()
245258
_KEYWORD_ENTRY = _keyword_entry_of(_TOKEN | _list_of(_IDENTIFIER), _DATA)
246259
_DICT = _dict_of(_TOKEN, _DATA)
@@ -259,18 +272,21 @@ def _keyword_entry_of(
259272

260273

261274
def parse_data(s: str) -> Data:
275+
if not s.strip():
276+
return ""
262277
return cast(Data, _DATA.parse_string(s, parse_all=True)[0])
263278

264279

265280
_LOCATED_DICTIONARY = Group(
266-
_keyword_entry_of(_TOKEN, Opt(_DATA, default=""), located=True)
281+
_keyword_entry_of(
282+
_TOKEN, Opt(_DATA, default=""), directive=_DIRECTIVE, located=True
283+
)
267284
)[...]
268285
_LOCATED_DATA = Group(Located(_DATA.copy().add_parse_action(lambda tks: ["", tks[0]])))
269286

270287
_FILE = (
271288
Dict(_LOCATED_DICTIONARY + Opt(_LOCATED_DATA) + _LOCATED_DICTIONARY)
272289
.ignore(_COMMENT)
273-
.ignore(Literal("#include") + ... + LineEnd()) # type: ignore [no-untyped-call]
274290
.parse_with_tabs()
275291
)
276292

foamlib/_files/_serialization.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ def dumps(
111111

112112
if isinstance(data, tuple) and kind == Kind.SINGLE_ENTRY and len(data) == 2:
113113
k, v = data
114-
ret = dumps(k) + b" " + dumps(v)
114+
ret = dumps(k)
115+
val = dumps(v)
116+
if val:
117+
ret += b" " + val
115118
if not isinstance(v, Mapping):
116119
ret += b";"
117120
return ret

tests/test_files/test_parsing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,11 @@ def test_parse_value() -> None:
9898
]
9999
assert Parsed(b"[]")[()] == FoamFile.DimensionSet()
100100
assert Parsed(b"object f.1;")[("object",)] == "f.1"
101+
102+
103+
def test_parse_directive() -> None:
104+
assert Parsed(b'#include "filename"')[("#include",)] == '"filename"'
105+
assert (
106+
Parsed(b"functions\n{\n#includeFunc funcName\n}")[("functions", "#includeFunc")]
107+
== "funcName"
108+
)

0 commit comments

Comments
 (0)