From 7e7c68e116bd99867f9346ba52433c418287a7be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=BCnther?= Date: Mon, 13 Jan 2025 01:03:59 +0100 Subject: [PATCH] Add read/write example programs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Günther --- examples/chevron_writer_example.py | 14 ++++---- examples/custom_attributes.py | 2 +- examples/reader_chevron_writer_example.py | 40 +++++++++++++++++++++++ examples/reader_writer_example.py | 39 ++++++++++++++++++++++ examples/writer_example.py | 14 ++++---- 5 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 examples/reader_chevron_writer_example.py create mode 100644 examples/reader_writer_example.py diff --git a/examples/chevron_writer_example.py b/examples/chevron_writer_example.py index 6449dbc..4c6a688 100644 --- a/examples/chevron_writer_example.py +++ b/examples/chevron_writer_example.py @@ -2,7 +2,7 @@ # # SPDX-License-Identifier: Apache-2.0 -import os +from pathlib import Path from pycgmes.resources.Analog import Analog from pycgmes.resources.AnalogValue import AnalogValue @@ -15,12 +15,12 @@ from pycgmes.utils.chevron_writer import ChevronWriter from pycgmes.utils.profile import Profile -_curr_dir = os.path.dirname(os.path.realpath(__file__)) +_curr_dir = Path(__file__).resolve().parent def main() -> None: - output_dir = _curr_dir + "/output" - os.makedirs(output_dir, exist_ok=True) + output_dir = _curr_dir / "output" + output_dir.mkdir(parents=True, exist_ok=True) objects = { "BaseVoltage.20": BaseVoltage(mRID="BaseVoltage.20", nominalVoltage=20.0), "VoltageLevel.98": VoltageLevel(mRID="VoltageLevel.98", name="98", BaseVoltage="BaseVoltage.20"), @@ -40,14 +40,14 @@ def main() -> None: ), "AnalogValue.N0.Voltage": AnalogValue(mRID="AnalogValue.N0.Voltage", Analog="Analog.N0.Voltage"), } - write(output_dir + "/Example_Model", "Example_Model", objects) + write(output_dir / "Example_Model", "Example_Model", objects) -def write(outputfile: str, model_id: str, objects: dict[str, Base]) -> None: +def write(outputfile: Path, model_id: str, objects: dict[str, Base]) -> None: writer = ChevronWriter(objects) class_profile_map = ChevronWriter.get_class_profile_map(writer.objects.values()) class_profile_map["Terminal"] = Profile.TP # override recommended profile - profile_file_map = writer.write(outputfile, model_id, class_profile_map) + profile_file_map = writer.write(str(outputfile), model_id, class_profile_map) for idx, (profile, file) in enumerate(profile_file_map.items()): print(f"CIM outputfile {idx + 1} for {profile}: {file}") diff --git a/examples/custom_attributes.py b/examples/custom_attributes.py index 549f8c2..53edb34 100644 --- a/examples/custom_attributes.py +++ b/examples/custom_attributes.py @@ -67,7 +67,7 @@ def namespace(self) -> str: print("Attributes in profile MYOWN:") print( json.dumps( - {qualname: attr for qualname, attr in my_resource.cgmes_attributes_in_profile(CustomProfile.MYOWN).items()}, + my_resource.cgmes_attributes_in_profile(CustomProfile.MYOWN), indent=2, ) ) diff --git a/examples/reader_chevron_writer_example.py b/examples/reader_chevron_writer_example.py new file mode 100644 index 0000000..a973faa --- /dev/null +++ b/examples/reader_chevron_writer_example.py @@ -0,0 +1,40 @@ +# SPDX-FileCopyrightText: 2025 Thomas Guenther +# +# SPDX-License-Identifier: Apache-2.0 + +import sys +from pathlib import Path + +from pycgmes.utils.base import Base +from pycgmes.utils.chevron_writer import ChevronWriter +from pycgmes.utils.reader import Reader + +_curr_dir = Path(__file__).resolve().parent + + +def main(input_files: list[str]) -> None: + output_dir = _curr_dir / "output" + output_dir.mkdir(parents=True, exist_ok=True) + import_result = read(input_files) + objects = import_result["topology"] + write(output_dir / "Example_Model", "Example_Model", objects) + + +def read(input_files: list[str]) -> dict: + reader = Reader(cgmes_version_path="pycgmes.resources") + return reader.parse_profiles(input_files) + + +def write(outputfile: Path, model_id: str, objects: dict[str, Base]) -> None: + writer = ChevronWriter(objects) + class_profile_map = ChevronWriter.get_class_profile_map(writer.objects.values()) + profile_file_map = writer.write(str(outputfile), model_id, class_profile_map) + for idx, (profile, file) in enumerate(profile_file_map.items()): + print(f"CIM outputfile {idx + 1} for {profile}: {file}") + + +if __name__ == "__main__": + if len(sys.argv) <= 1: + sys.exit("Too few arguments") + input_files = sys.argv[1:] + main(input_files) diff --git a/examples/reader_writer_example.py b/examples/reader_writer_example.py new file mode 100644 index 0000000..403d48e --- /dev/null +++ b/examples/reader_writer_example.py @@ -0,0 +1,39 @@ +# SPDX-FileCopyrightText: 2025 Thomas Guenther +# +# SPDX-License-Identifier: Apache-2.0 + +import sys +from pathlib import Path + +from pycgmes.utils.base import Base +from pycgmes.utils.reader import Reader +from pycgmes.utils.writer import Writer + +_curr_dir = Path(__file__).resolve().parent + + +def main(input_files: list[str]) -> None: + output_dir = _curr_dir / "output" + output_dir.mkdir(parents=True, exist_ok=True) + import_result = read(input_files) + objects = import_result["topology"] + write(output_dir / "Example_Model", "Example_Model", objects) + + +def read(input_files: list[str]) -> dict: + reader = Reader(cgmes_version_path="pycgmes.resources") + return reader.parse_profiles(input_files) + + +def write(outputfile: Path, model_id: str, objects: dict[str, Base]) -> None: + writer = Writer(objects=objects) + profile_file_map = writer.write(str(outputfile), model_id) + for idx, (profile, file) in enumerate(profile_file_map.items()): + print(f"CIM outputfile {idx + 1} for {profile}: {file}") + + +if __name__ == "__main__": + if len(sys.argv) <= 1: + sys.exit("Too few arguments") + input_files = sys.argv[1:] + main(input_files) diff --git a/examples/writer_example.py b/examples/writer_example.py index b653dd8..4399255 100644 --- a/examples/writer_example.py +++ b/examples/writer_example.py @@ -2,7 +2,7 @@ # # SPDX-License-Identifier: Apache-2.0 -import os +from pathlib import Path from pycgmes.resources.Analog import Analog from pycgmes.resources.AnalogValue import AnalogValue @@ -14,12 +14,12 @@ from pycgmes.utils.base import Base from pycgmes.utils.writer import Writer -_curr_dir = os.path.dirname(os.path.realpath(__file__)) +_curr_dir = Path(__file__).resolve().parent def main() -> None: - output_dir = _curr_dir + "/output" - os.makedirs(output_dir, exist_ok=True) + output_dir = _curr_dir / "output" + output_dir.mkdir(parents=True, exist_ok=True) objects = { "BaseVoltage.20": BaseVoltage(mRID="BaseVoltage.20", nominalVoltage=20.0), "VoltageLevel.98": VoltageLevel(mRID="VoltageLevel.98", name="98", BaseVoltage="BaseVoltage.20"), @@ -39,12 +39,12 @@ def main() -> None: ), "AnalogValue.N0.Voltage": AnalogValue(mRID="AnalogValue.N0.Voltage", Analog="Analog.N0.Voltage"), } - write(output_dir + "/Example_Model", "Example_Model", objects) + write(output_dir / "Example_Model", "Example_Model", objects) -def write(outputfile: str, model_id: str, objects: dict[str, Base]) -> None: +def write(outputfile: Path, model_id: str, objects: dict[str, Base]) -> None: writer = Writer(objects=objects) - profile_file_map = writer.write(outputfile, model_id) + profile_file_map = writer.write(str(outputfile), model_id) for idx, (profile, file) in enumerate(profile_file_map.items()): print(f"CIM outputfile {idx + 1} for {profile}: {file}")