Skip to content

Commit

Permalink
#348 cleanup and add error thrown test
Browse files Browse the repository at this point in the history
  • Loading branch information
MRVermeulenDeltares committed Mar 30, 2023
1 parent 731c86e commit 3fdff20
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
2 changes: 1 addition & 1 deletion hydrolib/core/dflowfm/tim/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TimModel(ParsableFileModel):
"""Class representing a tim (*.tim) file.
Attributes:
data: Dict
data: Dictionary with keys \"comment\" & time as numeric and value as List of floats".\n
serializer_config: TimSerializerConfig
"""

Expand Down
13 changes: 9 additions & 4 deletions hydrolib/core/dflowfm/tim/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
class TimParser:
"""
A parser for .tim files.
Full line comments are supported.
Partial comments will raise an error.
Full line comments at the start of the file are supported by * and #.
No other comments are supported.
"""

@staticmethod
Expand All @@ -25,11 +25,16 @@ def parse(filepath: Path) -> Dict:
"""
data = {}
data["comments"] = [str]
savecomments = True
with filepath.open() as file:
for line in file.readlines():
if TimParser._line_is_comment(line):
data["comments"].append(line.lstrip("#*"))
continue
if savecomments:
data["comments"].append(line.lstrip("#*"))
continue
raise ValueError(f"Error parsing tim file '{filepath}', comments in between data not supported.")

savecomments = False

try:
TimParser._add_timeseries(line, data)
Expand Down
1 change: 1 addition & 0 deletions hydrolib/core/dflowfm/tim/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def serialize(
path (Path): The path to the destination file.
data (Dict): The data to be serialized.
config (TimSerializerConfig): The serialization configuration.
save_settings (ModelSaveSettings): The model save settings.
"""
path.parent.mkdir(parents=True, exist_ok=True)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#comments
*this is another comment
10 1.232 2.343 3.454
20 4.565 5.676 6.787
30 1.5 2.6 3.7
* 40 1.5 2.6 3.7
# 50 1.5 2.6 3.7
30 1.5 2.6 3.7
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#comments
10 1.232 2.343 3.454
20 4.565 5.676 6.787
30 1.5 2.6 3.7
* 40 1.5 2.6 3.7
# 50 1.5 2.6 3.7
26 changes: 23 additions & 3 deletions tests/dflowfm/test_tim.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ class TestTimParser:
"comments": [
str,
"comments\n",
" 40 1.5 2.6 3.7\n",
" 50 1.5 2.6 3.7",
"this is another comment\n",
],
10: [1.232, 2.343, 3.454],
20: [4.565, 5.676, 6.787],
Expand Down Expand Up @@ -216,10 +215,31 @@ def test_parse_data(self, expected_output, input_path):
),
],
)
def test_parse_data_throws_exception(self, input_path):
def test_parse_data_throws_exception_error_parsing_tim_file(self, input_path):
with pytest.raises(ValueError) as error:
TimParser.parse(input_path)
found_msg = error.value.args[0]

expected_error_msg = f"Error parsing tim file '{input_path}'."
assert found_msg == expected_error_msg

@pytest.mark.parametrize(
"input_path",
[
pytest.param(
Path(
test_input_dir
/ "tim"
/ "triple_data_for_timeseries_with_comments_between_data.tim"
),
id="triple_data_for_timeseries_with_comments_between_data",
),
],
)
def test_parse_data_throws_exception_error_parsing_tim_file_comments_between_data_not_supported(self, input_path):
with pytest.raises(ValueError) as error:
TimParser.parse(input_path)
found_msg = error.value.args[0]

expected_error_msg = f"Error parsing tim file '{input_path}', comments in between data not supported."
assert found_msg == expected_error_msg

0 comments on commit 3fdff20

Please sign in to comment.