Move bool_parents enum out of XSY module into custom_lengths YAML file.

This commit is contained in:
2026-11-17 12:33:38 -06:00
parent 4aa7a65d0f
commit 608df7ce9b
3 changed files with 80 additions and 63 deletions

View File

@@ -1 +0,0 @@
PARA_PI_B: 8

View File

@@ -0,0 +1,2 @@
PARA_PI_B:
BOOL: 8

View File

@@ -10,7 +10,7 @@ desc: AuDaGe module for handling Schneider Electric ControlExpert XSY files.
import os
import warnings
from collections.abc import Iterable
from collections.abc import Collection, Iterable
from dataclasses import dataclass
from enum import IntEnum, StrEnum, auto
from importlib import resources
@@ -18,18 +18,43 @@ from itertools import pairwise
from pathlib import Path
import pandas as pd
import yaml
from numpy import nan
import AuDaGe
from AuDaGe import frame, iec
from AuDaGe.better_enum import BetterIntEnum
from AuDaGe.debug_tools import MeasureTime, TimeUnit
from AuDaGe.file_utilities import find_file
ASSETS_PATH = "../../extensions/controlexpert/assets"
DEFAULT_ASSETS_PATH = "../../extensions/controlexpert/assets"
MAX_RECURSION = 5
DEF_SUFFIX = "_def"
class XSYColumns(StrEnum):
TAGNAME = auto()
DATATYPE = auto()
ADDRESS = auto()
COMMENT = auto()
PARENT = auto()
BITRANK = auto()
SAVE = auto()
LENGTH = auto()
XC = XSYColumns
def get_custom_lengths(
filepath: str = "controlexpert/assets/custom_lengths.yaml",
folders: Collection[Path] = (),
) -> dict[str, dict[str, int]]:
file_path = find_file(filepath, folders)
with open(file_path) as file:
return yaml.safe_load(file)
class XSYBlock(frame.AuDaGeFrame):
"""Base class for DataBlock and Definitions that handles shared attributes.
@@ -38,7 +63,7 @@ class XSYBlock(frame.AuDaGeFrame):
"""
DEFAULT_SS_PATH = resources.files(AuDaGe).joinpath(
ASSETS_PATH, "import_datablock.xslt"
DEFAULT_ASSETS_PATH, "import_datablock.xslt"
)
@classmethod
@@ -69,7 +94,7 @@ class XSYBlock(frame.AuDaGeFrame):
expand_elementary: bool = True,
) -> frame.AuDaGeFrame:
"""Make definitions for the expansion of arrays of UDTs."""
frame_grouped = self.groupby("type")
frame_grouped = self.groupby(XC.DATATYPE)
array_definitions_list = []
for name, _ in frame_grouped:
data_type = iec.IECType.from_string(str(name))
@@ -89,21 +114,15 @@ class Definitions(XSYBlock):
# ty: ignore[invalid-argument-type]
DEFAULT_SS_PATH = Path(
resources.files(AuDaGe).joinpath(ASSETS_PATH, "import_definitions.xslt")
resources.files(AuDaGe).joinpath(
DEFAULT_ASSETS_PATH, "import_definitions.xslt"
)
)
# ty: ignore[invalid-argument-type]
DEFAULT_BUILTINS_PATH = Path(
resources.files(AuDaGe).joinpath(ASSETS_PATH, "builtins.xsy")
resources.files(AuDaGe).joinpath(DEFAULT_ASSETS_PATH, "builtins.xsy")
)
class BoolParents(BetterIntEnum):
"""Enum of UDTs that pack booleans into data blocks other than bits or
words. Each member name is the name of the UDT and the value is the
number of bits that each boolean is packed into.
"""
PARA_PI_B = 8
@classmethod
def from_file(
cls,
@@ -152,35 +171,30 @@ class Definitions(XSYBlock):
cls,
row,
udt_lengths: dict[str, int],
bool_parents: type[BetterIntEnum] = BoolParents,
custom_lengths: dict[str, dict[str, int]] | None = None,
) -> float:
custom_lengths = (
get_custom_lengths() if custom_lengths is None else custom_lengths
)
# Special case to fix the BOOLs that are packed into data units other
# than bits or words.
if (
bool_parents.contains(row["parent"].upper())
and row["type"] == "BOOL"
):
return bool_parents[row["parent"].upper()].value
# Bit- and byte- length variables that are packed into words have
# functionally zero length for our purposes.
if row["type"] in (
"BOOL",
"EBOOL",
"BYTE",
"SINT",
"USINT",
"CHAR",
) and pd.notna(row["bitrank"]):
return 0
try:
return custom_lengths[row[XC.PARENT].upper()][
row[XC.DATATYPE].upper()
]
except KeyError:
pass
length = iec.IECType.from_string(
str(row["type"]),
str(row[XC.DATATYPE]),
udt_lengths=udt_lengths,
unknown_length=nan,
).length
if pd.isna(length):
return length
# Bit- and byte- length variables that are packed into words have
# functionally zero length for our purposes.
if length < 16:
return 0
else:
return max(length, 16)
return length
@classmethod
def _calculate_padding(
@@ -228,7 +242,7 @@ class Definitions(XSYBlock):
# We don't care about DFBs so set their length at zero.
udt_lengths["DFB"] = 0
# Create a column with the length of each member.
definitions["length"] = definitions.apply(
definitions[XC.LENGTH] = definitions.apply(
lambda row: cls._get_length(row, udt_lengths),
axis=1,
)
@@ -238,24 +252,25 @@ class Definitions(XSYBlock):
# Calculate the cumulative sum of the data lengths for each UDT
# definition. This is the offset of the subsequent member.
definitions_calculated_list = []
for name, group in definitions.groupby("parent"):
for name, group in definitions.groupby(XC.PARENT):
group_dict = group.to_dict("records")
group_dict[0]["address"] = 0
group_dict[0][XC.ADDRESS] = 0
group_dict_calc = [group_dict[0]]
for prev_row, row in pairwise(group_dict):
row["address"] = cls._calculate_padding(
prev_row["address"] + prev_row["length"], row["length"]
row[XC.ADDRESS] = cls._calculate_padding(
prev_row[XC.ADDRESS] + prev_row[XC.LENGTH],
row[XC.LENGTH],
)
group_dict_calc.append(row)
group_calc = frame.AuDaGeFrame(group_dict_calc)
# Check if all offsets for this UDT has been found. If so, we
# know its length so we save it to the `udt_lengths` dictionary
# for future reference.
if group_calc["address"].notna().all():
if group_calc[XC.ADDRESS].notna().all():
udt_lengths[str(name)] = (
group_calc["address"].tail(1).values[0]
group_calc[XC.ADDRESS].tail(1).values[0]
+ iec.IECType.from_string(
group_calc["type"].tail(1).values[0]
group_calc[XC.DATATYPE].tail(1).values[0]
).length
)
else:
@@ -379,15 +394,15 @@ class XSY:
return datablock_expanded
def _combine_addresses(self, row: pd.Series) -> str:
if pd.isna(row["address"]) or row["address"] == "":
if pd.isna(row[XC.ADDRESS]) or row[XC.ADDRESS] == "":
return ""
address_object = iec.Address.from_string(row["address"])
address_object = iec.Address.from_string(row[XC.ADDRESS])
if pd.notna(row["address_def"]):
offset = row["address_def"] // 16
else:
offset = 0
if pd.notna(row["bitrank_def"]):
bit = float(row["bitrank_def"])
if pd.notna(row[XC.BITRANK + DEF_SUFFIX]):
bit = float(row[XC.BITRANK + DEF_SUFFIX])
if address_object.bit is None:
address_object.bit = int(bit)
else:
@@ -409,20 +424,21 @@ class XSY:
"""Compile a cross-joined and filtered DataBlock by combining the
columns from the datablock with the columns from the definitions."""
datablock_compiled = pd.DataFrame()
datablock_compiled["tagname"] = datablock.apply(
lambda row: f"{row['tagname']}.{row['tagname_def']}", axis=1
datablock_compiled[XC.TAGNAME] = datablock.apply(
lambda row: f"{row[XC.TAGNAME]}.{row[XC.TAGNAME + DEF_SUFFIX]}",
axis=1,
)
datablock_compiled["type"] = datablock["type_def"]
datablock_compiled["address"] = datablock.apply(
datablock_compiled[XC.DATATYPE] = datablock[XC.DATATYPE + DEF_SUFFIX]
datablock_compiled[XC.ADDRESS] = datablock.apply(
lambda row: self._combine_addresses(row),
axis=1,
)
datablock_compiled["parent"] = ""
datablock_compiled["bitrank"] = datablock["bitrank_def"]
datablock_compiled["save"] = datablock["save"]
datablock_compiled["comment"] = datablock.apply(
datablock_compiled[XC.PARENT] = ""
datablock_compiled[XC.BITRANK] = datablock[XC.BITRANK + DEF_SUFFIX]
datablock_compiled[XC.SAVE] = datablock[XC.SAVE]
datablock_compiled[XC.COMMENT] = datablock.apply(
lambda row: self._combine_comments(
row["comment"], row["comment_def"]
row[XC.COMMENT], row[XC.COMMENT + DEF_SUFFIX]
),
axis=1,
)
@@ -455,7 +471,7 @@ class XSY:
_current_recursion: int
Current recursion depth. Not to be used except within this method.
"""
datablock_grouped = datablock.groupby("type")
datablock_grouped = datablock.groupby(XC.DATATYPE)
edt_list: list[pd.DataFrame] = []
udt_list: list[pd.DataFrame] = []
for name, group in datablock_grouped:
@@ -470,8 +486,8 @@ class XSY:
udts = pd.concat(udt_list)
if debug_step == self.DebugStep.GROUP:
return XSYBlock(pd.concat((edts, udts)))
definitions_grouped = definitions.groupby("parent")
udts_grouped = udts.groupby("type")
definitions_grouped = definitions.groupby(XC.PARENT)
udts_grouped = udts.groupby(XC.DATATYPE)
group_list = []
# Cross-join each type group in udts_grouped with its corresponding
# group from definitions_grouped.
@@ -497,7 +513,7 @@ class XSY:
)
),
how="cross",
rsuffix="_def",
rsuffix=DEF_SUFFIX,
)
)
except KeyError:
@@ -511,7 +527,7 @@ class XSY:
if all(
udts_compiled.apply(
lambda row: iec.IECType.from_string(
str(row["type"])
str(row[XC.DATATYPE])
).is_elementary(),
axis=1,
)