Skip to content

Commit

Permalink
Merge branch 'fix/#535-importing-networks-with-unused-files-in-export…
Browse files Browse the repository at this point in the history
…-component-paths' into dev
  • Loading branch information
fwitte committed Aug 7, 2024
2 parents 16b78d2 + e70aad2 commit 9408ac1
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ What's New

Discover notable new features and improvements in each release

.. include:: whats_new/v0-7-7.rst
.. include:: whats_new/v0-7-6-001.rst
.. include:: whats_new/v0-7-6.rst
.. include:: whats_new/v0-7-5.rst
Expand Down
13 changes: 13 additions & 0 deletions docs/whats_new/v0-7-7.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Under development
+++++++++++++++++

Bug Fixes
#########
- Only :code:`.json` format files are loaded by the `load_network` method.
Furthermore, it is checked whether a file is represented by a class
available in the namespace via the :code:`@component_registry` decorator
(`PR #536 <https://github.com/oemof/tespy/pull/536>`__).

Contributors
############
- Francesco Witte (`@fwitte <https://github.com/fwitte>`__)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ exclude = ["docs/_build"]

[project]
name = "tespy"
version = "0.7.6.post1"
version = "0.7.7.dev0"
description = "Thermal Engineering Systems in Python (TESPy)"
readme = "README.rst"
authors = [
Expand Down
2 changes: 1 addition & 1 deletion src/tespy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os

__datapath__ = os.path.join(importlib.resources.files("tespy"), "data")
__version__ = '0.7.6.post1 - Newton\'s Nature'
__version__ = '0.7.7 - Newton\'s Nature'

# tespy data and connections import
from . import connections # noqa: F401
Expand Down
15 changes: 14 additions & 1 deletion src/tespy/networks/network_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,22 @@ def load_network(path):

files = os.listdir(path_comps)
for f in files:
fn = os.path.join(path_comps, f)
if not f.endswith(".json"):
continue

component = f.replace(".json", "")

if component not in component_registry.items:
msg = (
f"A class {component} is not available through the "
"tespy.components.component.component_registry decorator. "
"If you are using a custom component make sure to decorate the "
"class."
)
logger.warning(msg)
continue

fn = os.path.join(path_comps, f)
msg = f"Reading component data ({component}) from {fn}."
logger.debug(msg)

Expand Down
34 changes: 33 additions & 1 deletion tests/test_networks/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
SPDX-License-Identifier: MIT
"""

import json
import os

from pytest import mark
Expand Down Expand Up @@ -149,6 +149,38 @@ def test_Network_reader_checked(self, tmp_path):
'should have been successful, too, but it is not.')
assert imported_nwk.checked, msg

def test_Network_reader_superflouus_files(self, tmp_path):
"""Test state of network if loaded successfully from export."""
a = Connection(self.source, 'out1', self.sink, 'in1')
self.nw.add_conns(a)
a.set_attr(fluid={"H2O": 1})
self.nw.solve('design', init_only=True)
self.nw.export(tmp_path)
with open(os.path.join(tmp_path, "components", "test.csv"), "w") as f:
f.write("nothing to see here")

imported_nwk = load_network(tmp_path)
imported_nwk.solve('design', init_only=True)
msg = ('If the network import was successful the network check '
'should have been successful, too, but it is not.')
assert imported_nwk.checked, msg

def test_Network_reader_unknown_component_class(self, tmp_path):
"""Test state of network if loaded successfully from export."""
a = Connection(self.source, 'out1', self.sink, 'in1')
self.nw.add_conns(a)
a.set_attr(fluid={"H2O": 1})
self.nw.solve('design', init_only=True)
self.nw.export(tmp_path)
with open(os.path.join(tmp_path, "components", "Test.json"), "w") as f:
json.dump({}, f)

imported_nwk = load_network(tmp_path)
imported_nwk.solve('design', init_only=True)
msg = ('If the network import was successful the network check '
'should have been successful, too, but it is not.')
assert imported_nwk.checked, msg

def test_Network_missing_data_in_design_case_files(self, tmp_path_factory):
"""Test for missing data in design case files."""
tmp_path = tmp_path_factory.mktemp("tmp")
Expand Down

0 comments on commit 9408ac1

Please sign in to comment.