Skip to content

Commit

Permalink
Merge pull request #45 from gerlero/dictionaries
Browse files Browse the repository at this point in the history
Update parsing
  • Loading branch information
gerlero authored Mar 28, 2024
2 parents 6113bbd + c0ebd15 commit e77129f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
60 changes: 34 additions & 26 deletions foamlib/_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ def _cmd(self, args: Sequence[str], *, key: Optional[str] = None) -> str:
) from None

def __getitem__(self, key: str) -> Union["FoamFile.Value", "_FoamDictionary"]:
contents = self._file.path.read_text()
value = _DICTIONARY.parse_string(contents, parse_all=True).as_dict()
value = _DICTIONARY.parse_file(self._file.path, parse_all=True).as_dict()

for key in [*self._keywords, key]:
value = value[key]
Expand Down Expand Up @@ -356,8 +355,15 @@ def boundary_field(self) -> "FoamFieldFile.BoundariesDictionary":
)
| (
common.integer + Literal("{").suppress() + _ITEM + Literal("}").suppress()
).set_parse_action(lambda tks: [tks[1]] * tks[0])
).set_parse_action(lambda tks: [[tks[1].as_list()] * tks[0]])
)
_DIMENSIONED = (Opt(common.identifier) + _DIMENSIONS + _ITEM).set_parse_action(
lambda tks: FoamFile.Dimensioned(*reversed(tks.as_list()))
)
_ITEM <<= (
_LIST | _SUBDICT | _DIMENSIONED | _DIMENSIONS | common.number | _YES | _NO | _TOKEN
)

_FIELD = (
Keyword("uniform").suppress()
+ (
Expand All @@ -371,43 +377,45 @@ def boundary_field(self) -> "FoamFieldFile.BoundariesDictionary":
) | (
Keyword("nonuniform").suppress()
+ Opt(Literal("List") + Literal("<") + common.identifier + Literal(">")).suppress()
+ Opt(common.integer).suppress()
+ (
Literal("(").suppress()
+ Group(
(
(
Opt(common.integer).suppress()
+ (
Literal("(").suppress()
+ Group(
(
common.number
| (
Literal("(").suppress()
+ Group(common.number[3, 9])
+ Literal(")").suppress()
)
)[...]
)
+ Literal(")").suppress()
)
)
| (
common.integer
+ Literal("{").suppress()
+ (
common.number
| (
Literal("(").suppress()
+ Group(common.number[3, 9])
+ Literal(")").suppress()
)
)[...]
)
+ Literal(")").suppress()
)
+ Literal("}").suppress()
).set_parse_action(lambda tks: [[tks[1].as_list()] * tks[0]])
)
)
_DIMENSIONED = (Opt(common.identifier) + _DIMENSIONS + _ITEM).set_parse_action(
lambda tks: FoamFile.Dimensioned(*reversed(tks.as_list()))
)
_ITEM <<= (
_FIELD
| _LIST
| _SUBDICT
| _DIMENSIONED
| _DIMENSIONS
| common.number
| _YES
| _NO
| _TOKEN
)

_TOKENS = (
QuotedString('"', unquote_results=False)
| Word(printables.replace(";", "").replace("{", "").replace("}", ""))
)[2, ...].set_parse_action(lambda tks: " ".join(tks))

_VALUE = (_ITEM ^ _TOKENS).ignore(c_style_comment).ignore(cpp_style_comment)
_VALUE = ((_FIELD | _ITEM) ^ _TOKENS).ignore(c_style_comment).ignore(cpp_style_comment)

_KEYWORD = QuotedString('"', unquote_results=False) | Word(
identchars + "$(,.)", identbodychars + "$(,.)"
Expand Down
8 changes: 8 additions & 0 deletions tests/test_dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,20 @@ def test_parse_value() -> None:
[1, 2, 3],
[4, 5, 6],
]
assert _VALUE.parse_string("2{(1 2 3)}").as_list()[0] == [
[1, 2, 3],
[1, 2, 3],
]
assert _VALUE.parse_string("nonuniform List<vector> 2((1 2 3) (4 5 6))").as_list()[
0
] == [
[1, 2, 3],
[4, 5, 6],
]
assert _VALUE.parse_string("nonuniform List<vector> 2{(1 2 3)}").as_list()[0] == [
[1, 2, 3],
[1, 2, 3],
]
assert _VALUE.parse_string("[1 1 -2 0 0 0 0]").as_list()[
0
] == FoamFile.DimensionSet(mass=1, length=1, time=-2)
Expand Down

0 comments on commit e77129f

Please sign in to comment.