Skip to content

Commit

Permalink
#425: Use construct function for create IO INI models. This will skip…
Browse files Browse the repository at this point in the history
… validation.
  • Loading branch information
priscavdsluis committed Nov 18, 2022
1 parent 8e0530c commit 423372f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
8 changes: 4 additions & 4 deletions hydrolib/core/io/dflowfm/bc/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ class QuantityUnitPair(BaseModel):
def _to_properties(self):
"""Generator function that yields the ini Property objects for a single
QuantityUnitPair object."""
yield Property(key="quantity", value=self.quantity)
yield Property(key="unit", value=self.unit)
yield Property.construct(key="quantity", value=self.quantity)
yield Property.construct(key="unit", value=self.unit)
if self.vertpositionindex is not None:
yield Property(key="vertPositionIndex", value=self.vertpositionindex)
yield Property.construct(key="vertPositionIndex", value=self.vertpositionindex)


class VectorQuantityUnitPairs(BaseModel):
Expand Down Expand Up @@ -147,7 +147,7 @@ def __str__(self) -> str:
def _to_properties(self):
"""Generator function that yields the ini Property objects for a single
VectorQuantityUnitPairs object."""
yield Property(key="vector", value=str(self))
yield Property.construct(key="vector", value=str(self))

for qup in self.quantityunitpair:
for prop in qup._to_properties():
Expand Down
8 changes: 4 additions & 4 deletions hydrolib/core/io/dflowfm/ini/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ def _to_section(self, config: INISerializerConfig) -> Section:
if key in self.__fields__:
key = self.__fields__[key].alias

prop = Property(
prop = Property.construct(
key=key,
value=self.__class__._convert_value(field_key, value, config),
comment=getattr(self.comments, key.lower(), None),
)
props.append(prop)
return Section(header=self._header, content=props)
return Section.construct(header=self._header, content=props)


class DataBlockINIBasedModel(INIBasedModel):
Expand Down Expand Up @@ -245,7 +245,7 @@ def _get_parser(cls) -> Callable:
return Parser.parse_as_dict

def _to_document(self) -> Document:
header = CommentBlock(lines=[f"written by HYDROLIB-core {version}"])
header = CommentBlock.construct(lines=[f"written by HYDROLIB-core {version}"])
sections = []
for key, value in self:
if key in self._exclude_fields() or value is None:
Expand All @@ -255,7 +255,7 @@ def _to_document(self) -> Document:
sections.append(v._to_section(self.serializer_config))
else:
sections.append(value._to_section(self.serializer_config))
return Document(header_comment=[header], sections=sections)
return Document.construct(header_comment=[header], sections=sections)

def _serialize(self, _: dict) -> None:
write_ini(
Expand Down
14 changes: 7 additions & 7 deletions hydrolib/core/io/dflowfm/ini/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def add_comment_line(self, line: str) -> None:
self.lines.append(line)

def finalize(self) -> CommentBlock:
return CommentBlock(
return CommentBlock.construct(
lines=self.lines,
)

Expand All @@ -90,7 +90,7 @@ def add_property(self, property: Property) -> None:

def add_comment(self, comment: str, line: int) -> None:
if self.curr_comment_block is None:
self.curr_comment_block = _IntermediateCommentBlock(start_line=line)
self.curr_comment_block = _IntermediateCommentBlock.construct(start_line=line)

self.curr_comment_block.add_comment_line(comment)

Expand All @@ -106,7 +106,7 @@ def _finalize_comment_block(self) -> None:

def finalize(self) -> Section:
self._finalize_comment_block()
return Section(
return Section.construct(
header=self.header,
content=self.content,
datablock=self.datablock if self.datablock else None,
Expand All @@ -132,7 +132,7 @@ def __init__(self, config: ParserConfig) -> None:
config (ParserConfig): The configuration of this Parser
"""
self._config = config
self._document = Document()
self._document = Document.construct()
self._current_section: Optional[_IntermediateSection] = None
self._current_header_block: Optional[_IntermediateCommentBlock] = None

Expand Down Expand Up @@ -207,7 +207,7 @@ def _handle_next_section_header(self, line: str) -> None:

def _handle_new_section_header(self, line: str) -> None:
section_header = line.strip()[1:-1].strip()
self._current_section = _IntermediateSection(
self._current_section = _IntermediateSection.construct(
header=section_header, start_line=self._line_index
)

Expand All @@ -217,7 +217,7 @@ def _finalise_current_section(self) -> None:

def _handle_header_comment(self, line: str) -> None:
if self._current_header_block is None:
self._current_header_block = _IntermediateCommentBlock(
self._current_header_block = _IntermediateCommentBlock.construct(
start_line=self._line_index
)

Expand All @@ -235,7 +235,7 @@ def _handle_property(self, line: str) -> None:
else:
comment, value = None, None

prop = Property(key=key, value=value, comment=comment)
prop = Property.construct(key=key, value=value, comment=comment)
self._current_section.add_property(prop) # type: ignore

def _handle_new_datarow(self, line: str) -> None:
Expand Down

0 comments on commit 423372f

Please sign in to comment.