Skip to content

Commit

Permalink
Merge branch 'main' into feat/622-give-warning-for-unknown-keywords
Browse files Browse the repository at this point in the history
# Conflicts:
#	hydrolib/core/dflowfm/ini/models.py
  • Loading branch information
MRVermeulenDeltares committed May 10, 2024
2 parents 790dfe4 + 7648b01 commit 4ee0166
Show file tree
Hide file tree
Showing 8 changed files with 635 additions and 119 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ jobs:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
os: [ubuntu-latest, windows-latest]
os: [ubuntu-latest, windows-latest, macos-13]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v1
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand Down Expand Up @@ -54,4 +54,6 @@ jobs:
git checkout $GITHUB_HEAD_REF
git commit -am "autoformat: isort & black" && git push || true
- uses: codecov/codecov-action@v1
- uses: codecov/codecov-action@v4
env:
CODECOV_TOKEN: ${{secrets.CODECOV_TOKEN}}
22 changes: 22 additions & 0 deletions hydrolib/core/dflowfm/ini/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from abc import ABC
from enum import Enum
from math import isnan
from re import compile
from typing import Any, Callable, List, Literal, Optional, Set, Type, Union

from pydantic.v1 import Extra, Field, root_validator
Expand Down Expand Up @@ -47,6 +48,9 @@ class INIBasedModel(BaseModel, ABC):
_header: str = ""
_file_path_style_converter = FilePathStyleConverter()
_unknown_keyword_error_manager = UnknownKeywordErrorManager()
_scientific_notation_regex = compile(
r"([\d.]+)([dD])([+-]?\d{1,3})"
) # matches a float: 1d9, 1D-3, 1.D+4, etc.

class Config:
extra = Extra.ignore
Expand Down Expand Up @@ -133,6 +137,24 @@ def comments_matches_has_comments(cls, v):
v = None
return v

@validator("*", pre=True, allow_reuse=True)
def replace_fortran_scientific_notation_for_floats(cls, value, field):
if field.type_ != float:
return value

return cls._replace_fortran_scientific_notation(value)

@classmethod
def _replace_fortran_scientific_notation(cls, value):
if isinstance(value, str):
return cls._scientific_notation_regex.sub(r"\1e\3", value)
if isinstance(value, list):
for i, v in enumerate(value):
if isinstance(v, str):
value[i] = cls._scientific_notation_regex.sub(r"\1e\3", v)

return value

@classmethod
def validate(cls: Type["INIBasedModel"], value: Any) -> "INIBasedModel":
if isinstance(value, Section):
Expand Down
20 changes: 0 additions & 20 deletions hydrolib/core/dflowfm/ini/parser.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
from enum import IntEnum
from pathlib import Path
from typing import Callable, Dict, List, Optional, Tuple, Union
Expand Down Expand Up @@ -372,27 +371,8 @@ def parse(cls, filepath: Path, config: ParserConfig = None) -> Document:
config = ParserConfig()
parser = cls(config)

progline = re.compile(
r"^([^#]*=\s*)([^#]*)(#.*)?"
) # matches whole line: "Field = Value Maybe more # optional comment"
progfloat = re.compile(
r"([\d.]+)([dD])([+\-]?\d{1,3})"
) # matches a float value: 1d9, 1D-3, 1.D+4, etc.

with filepath.open(encoding="utf8") as f:
for line in f:
# Replace Fortran scientific notation for doubles
# Match number d/D +/- number (e.g. 1d-05 or 1.23D+01 or 1.d-4)
match = progline.match(line)
if match: # Only process value
line = (
match.group(1)
+ progfloat.sub(r"\1e\3", match.group(2))
+ str(match.group(3) or "")
)
else: # Process full line
line = progfloat.sub(r"\1e\3", line)

parser.feed_line(line)

return parser.finalize()

0 comments on commit 4ee0166

Please sign in to comment.