From 976ce737b32c73bea45eaa90eb7f81d5abddfe83 Mon Sep 17 00:00:00 2001 From: Silcantar <1491897+Silcantar@users.noreply.github.com> Date: Fri, 16 Oct 2026 12:16:39 -0500 Subject: [PATCH] Update xsy and kepserver csv modules to match new iec_type; debug iec_type. --- src/AuDaGe/formats/controlexpert/xsy.py | 30 +++++++++++-------------- src/AuDaGe/formats/kepserver/csv.py | 4 ++-- src/AuDaGe/iec/iec_type.py | 27 +++++++++++++++------- 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/src/AuDaGe/formats/controlexpert/xsy.py b/src/AuDaGe/formats/controlexpert/xsy.py index 566f189..6147132 100644 --- a/src/AuDaGe/formats/controlexpert/xsy.py +++ b/src/AuDaGe/formats/controlexpert/xsy.py @@ -8,7 +8,6 @@ date: 2026-09-14 desc: AuDaGe module for handling Schneider Electric ControlExpert XSY files. """ -import logging import os import warnings from collections.abc import Iterable @@ -29,9 +28,6 @@ ASSETS_PATH = "AuDaGe.formats.controlexpert.assets" MAX_RECURSION = 5 -module_logger = logging.getLogger(__name__) - - class XSYBlock(frame.AuDaGeFrame): """Base class for DataBlock and Definitions that handles shared attributes. May be instantiated directly from a dataframe or from an XSY file using the @@ -67,12 +63,9 @@ class XSYBlock(frame.AuDaGeFrame): frame_grouped = frame.groupby("type") array_definitions_list = [] for name, group in frame_grouped: - try: - array = iec.Array(name) - if not array.is_elementary(): - array_definitions_list.append(array.make_definition("")) - except iec.InvalidSpecError: - continue + data_type = iec.IECType.from_string(name) + if data_type.is_array() and not data_type.is_elementary(): + array_definitions_list.append(data_type.array_expansion()) try: return pd.concat(array_definitions_list) # Return `None` if there is nothing to concatenate. @@ -184,11 +177,11 @@ class Definitions(XSYBlock): "CHAR", ) and pd.notna(row["bitrank"]): return 0 - length = iec.get_data_length( - row["type"], + length = iec.IECType.from_string( + str(row["type"]), udt_lengths=udt_lengths, unknown_length=nan, - ) + ).length if pd.isna(length): return length else: @@ -461,12 +454,12 @@ class XSY: edts = pd.concat( group for (name, group) in datablock_grouped - if name in iec.ElementaryTypes + if iec.IECType.from_string(name).is_elementary() ) udts = pd.concat( group for (name, group) in datablock_grouped - if name not in iec.ElementaryTypes + if not iec.IECType.from_string(name).is_elementary() ) if debug_step == self.DebugStep.GROUP: return pd.concat((edts, udts)) @@ -505,7 +498,10 @@ class XSY: return udts_compiled if all( udts_compiled.apply( - lambda row: row["type"] in iec.ElementaryTypes, axis=1 + lambda row: iec.IECType.from_string( + str(row["type"]) + ).is_elementary(), + axis=1, ) ): print( @@ -544,7 +540,7 @@ class XSYReader(frame.Reader): definitions_ss_path: os.path | None = None, even_addresses: bool = True, ): - """Initialize XSYReader object with default definition files and + """Initialize XSYReader object with default definition files and stylesheets. Parameters diff --git a/src/AuDaGe/formats/kepserver/csv.py b/src/AuDaGe/formats/kepserver/csv.py index 772d5a7..b7115a2 100644 --- a/src/AuDaGe/formats/kepserver/csv.py +++ b/src/AuDaGe/formats/kepserver/csv.py @@ -101,7 +101,7 @@ class TranslatorsFromAF: @staticmethod def data_type(row: pd.Series) -> str: - return iec.type_iec_to_opcua[row[AC.TYPE]] + return iec.IECType.from_string(row[AC.TYPE]).to_opcua() class TranslatorsToAF: @@ -115,7 +115,7 @@ class TranslatorsToAF: @staticmethod def type(row: pd.Series) -> str: - return iec.type_opcua_to_iec[row[KC.DATA_TYPE]] + return str(iec.IECType.from_opcua(row[KC.DATA_TYPE]).value) @staticmethod def address(row: pd.Series) -> str: diff --git a/src/AuDaGe/iec/iec_type.py b/src/AuDaGe/iec/iec_type.py index 5828132..e7b93d3 100644 --- a/src/AuDaGe/iec/iec_type.py +++ b/src/AuDaGe/iec/iec_type.py @@ -94,10 +94,13 @@ class IECType: self.array_type = array_type def __eq__(self, other) -> bool: - return all( + return isinstance(other, IECType) and all( this == that for this, that in zip(self.__dict__, other.__dict__) ) + def __str__(self) -> str: + return self.name.upper() + # Constructors @classmethod def from_opcua( @@ -114,7 +117,10 @@ class IECType: spec: str, udt_lengths: dict | None = None, allow_unknown: bool = True, + unknown_length: int = UNKNOWN_LENGTH, ) -> IECType: + if not isinstance(spec, str): + raise TypeError("`spec` must be a string.") if udt_lengths is None: udt_lengths = {} try: @@ -134,7 +140,7 @@ class IECType: except KeyError: pass if allow_unknown: - return cls(spec, IECClass.UDT, UNKNOWN_LENGTH) + return cls(spec, IECClass.UDT, unknown_length) raise InvalidSpecError( "IECType.from_string", spec, @@ -152,6 +158,10 @@ class IECType: """ return round_function(self.length / unit) + def is_array(self) -> bool: + """Return whether this data type is an array.""" + return self.array_bounds is not None + def is_basic(self) -> bool: """Return whether this data type is basic, i.e. not a UDT, array, or custom-length string. @@ -171,12 +181,13 @@ class IECType: def is_scalar(self) -> bool: """Return whether this data type is scalar, i.e. not a UDT or array.""" - return self.is_elementary() and self.bounds is None + return self.is_elementary() and self.array_bounds is None def to_opcua(self) -> str: if not self.is_scalar(): raise ValueError( - "The `to_opcua` method can only be called on scalar types." + f"The `to_opcua` method can only be called on scalar types, " + f"not {self.name}." ) spec_cleaned = self.name.split("[")[0].strip() return IECtoOPCUA[spec_cleaned] @@ -184,11 +195,11 @@ class IECType: def to_string(self) -> str: return self.name.upper() - def array_expansion(self, name: str, comment: str = "") -> AuDaGeFrame: + def array_expansion(self, name: str = "", comment: str = "") -> AuDaGeFrame: if self.array_bounds is None or self.array_type is None: raise ValueError( - "The `array_expansion` method can only be called on an array " - "type." + f"The `array_expansion` method can only be called on an array " + f"type, not {self.name}." ) all_indices = None for i, (lbound, ubound) in enumerate(self.array_bounds): @@ -204,7 +215,7 @@ class IECType: lambda row: f"{name}[{','.join(str(dim) for dim in row)}]", axis=1, ), - "type": self.array_type, + "type": str(self.array_type), "address": nan, "comment": comment, "parent": self.name.upper(),