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