Skip to content

Commit

Permalink
Add read/write example programs
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Günther <[email protected]>
  • Loading branch information
tom-hg57 committed Jan 13, 2025
1 parent 740c76c commit 7e7c68e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 15 deletions.
14 changes: 7 additions & 7 deletions examples/chevron_writer_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"),
Expand All @@ -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}")

Expand Down
2 changes: 1 addition & 1 deletion examples/custom_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)
40 changes: 40 additions & 0 deletions examples/reader_chevron_writer_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# SPDX-FileCopyrightText: 2025 Thomas Guenther <[email protected]>
#
# 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)
39 changes: 39 additions & 0 deletions examples/reader_writer_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# SPDX-FileCopyrightText: 2025 Thomas Guenther <[email protected]>
#
# 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)
14 changes: 7 additions & 7 deletions examples/writer_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"),
Expand All @@ -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}")

Expand Down

0 comments on commit 7e7c68e

Please sign in to comment.