Update xsy and kepserver csv modules to match new iec_type; debug iec_type.
This commit is contained in:
@@ -8,7 +8,6 @@ date: 2026-09-14
|
|||||||
desc: AuDaGe module for handling Schneider Electric ControlExpert XSY files.
|
desc: AuDaGe module for handling Schneider Electric ControlExpert XSY files.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import logging
|
|
||||||
import os
|
import os
|
||||||
import warnings
|
import warnings
|
||||||
from collections.abc import Iterable
|
from collections.abc import Iterable
|
||||||
@@ -29,9 +28,6 @@ ASSETS_PATH = "AuDaGe.formats.controlexpert.assets"
|
|||||||
MAX_RECURSION = 5
|
MAX_RECURSION = 5
|
||||||
|
|
||||||
|
|
||||||
module_logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
|
|
||||||
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.
|
||||||
May be instantiated directly from a dataframe or from an XSY file using the
|
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")
|
frame_grouped = frame.groupby("type")
|
||||||
array_definitions_list = []
|
array_definitions_list = []
|
||||||
for name, group in frame_grouped:
|
for name, group in frame_grouped:
|
||||||
try:
|
data_type = iec.IECType.from_string(name)
|
||||||
array = iec.Array(name)
|
if data_type.is_array() and not data_type.is_elementary():
|
||||||
if not array.is_elementary():
|
array_definitions_list.append(data_type.array_expansion())
|
||||||
array_definitions_list.append(array.make_definition(""))
|
|
||||||
except iec.InvalidSpecError:
|
|
||||||
continue
|
|
||||||
try:
|
try:
|
||||||
return pd.concat(array_definitions_list)
|
return pd.concat(array_definitions_list)
|
||||||
# Return `None` if there is nothing to concatenate.
|
# Return `None` if there is nothing to concatenate.
|
||||||
@@ -184,11 +177,11 @@ class Definitions(XSYBlock):
|
|||||||
"CHAR",
|
"CHAR",
|
||||||
) and pd.notna(row["bitrank"]):
|
) and pd.notna(row["bitrank"]):
|
||||||
return 0
|
return 0
|
||||||
length = iec.get_data_length(
|
length = iec.IECType.from_string(
|
||||||
row["type"],
|
str(row["type"]),
|
||||||
udt_lengths=udt_lengths,
|
udt_lengths=udt_lengths,
|
||||||
unknown_length=nan,
|
unknown_length=nan,
|
||||||
)
|
).length
|
||||||
if pd.isna(length):
|
if pd.isna(length):
|
||||||
return length
|
return length
|
||||||
else:
|
else:
|
||||||
@@ -461,12 +454,12 @@ class XSY:
|
|||||||
edts = pd.concat(
|
edts = pd.concat(
|
||||||
group
|
group
|
||||||
for (name, group) in datablock_grouped
|
for (name, group) in datablock_grouped
|
||||||
if name in iec.ElementaryTypes
|
if iec.IECType.from_string(name).is_elementary()
|
||||||
)
|
)
|
||||||
udts = pd.concat(
|
udts = pd.concat(
|
||||||
group
|
group
|
||||||
for (name, group) in datablock_grouped
|
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:
|
if debug_step == self.DebugStep.GROUP:
|
||||||
return pd.concat((edts, udts))
|
return pd.concat((edts, udts))
|
||||||
@@ -505,7 +498,10 @@ class XSY:
|
|||||||
return udts_compiled
|
return udts_compiled
|
||||||
if all(
|
if all(
|
||||||
udts_compiled.apply(
|
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(
|
print(
|
||||||
@@ -544,7 +540,7 @@ class XSYReader(frame.Reader):
|
|||||||
definitions_ss_path: os.path | None = None,
|
definitions_ss_path: os.path | None = None,
|
||||||
even_addresses: bool = True,
|
even_addresses: bool = True,
|
||||||
):
|
):
|
||||||
"""Initialize XSYReader object with default definition files and
|
"""Initialize XSYReader object with default definition files and
|
||||||
stylesheets.
|
stylesheets.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ class TranslatorsFromAF:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def data_type(row: pd.Series) -> str:
|
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:
|
class TranslatorsToAF:
|
||||||
@@ -115,7 +115,7 @@ class TranslatorsToAF:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def type(row: pd.Series) -> str:
|
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
|
@staticmethod
|
||||||
def address(row: pd.Series) -> str:
|
def address(row: pd.Series) -> str:
|
||||||
|
|||||||
@@ -94,10 +94,13 @@ class IECType:
|
|||||||
self.array_type = array_type
|
self.array_type = array_type
|
||||||
|
|
||||||
def __eq__(self, other) -> bool:
|
def __eq__(self, other) -> bool:
|
||||||
return all(
|
return isinstance(other, IECType) and all(
|
||||||
this == that for this, that in zip(self.__dict__, other.__dict__)
|
this == that for this, that in zip(self.__dict__, other.__dict__)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def __str__(self) -> str:
|
||||||
|
return self.name.upper()
|
||||||
|
|
||||||
# Constructors
|
# Constructors
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_opcua(
|
def from_opcua(
|
||||||
@@ -114,7 +117,10 @@ class IECType:
|
|||||||
spec: str,
|
spec: str,
|
||||||
udt_lengths: dict | None = None,
|
udt_lengths: dict | None = None,
|
||||||
allow_unknown: bool = True,
|
allow_unknown: bool = True,
|
||||||
|
unknown_length: int = UNKNOWN_LENGTH,
|
||||||
) -> IECType:
|
) -> IECType:
|
||||||
|
if not isinstance(spec, str):
|
||||||
|
raise TypeError("`spec` must be a string.")
|
||||||
if udt_lengths is None:
|
if udt_lengths is None:
|
||||||
udt_lengths = {}
|
udt_lengths = {}
|
||||||
try:
|
try:
|
||||||
@@ -134,7 +140,7 @@ class IECType:
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
if allow_unknown:
|
if allow_unknown:
|
||||||
return cls(spec, IECClass.UDT, UNKNOWN_LENGTH)
|
return cls(spec, IECClass.UDT, unknown_length)
|
||||||
raise InvalidSpecError(
|
raise InvalidSpecError(
|
||||||
"IECType.from_string",
|
"IECType.from_string",
|
||||||
spec,
|
spec,
|
||||||
@@ -152,6 +158,10 @@ class IECType:
|
|||||||
"""
|
"""
|
||||||
return round_function(self.length / unit)
|
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:
|
def is_basic(self) -> bool:
|
||||||
"""Return whether this data type is basic, i.e. not a UDT, array, or
|
"""Return whether this data type is basic, i.e. not a UDT, array, or
|
||||||
custom-length string.
|
custom-length string.
|
||||||
@@ -171,12 +181,13 @@ class IECType:
|
|||||||
|
|
||||||
def is_scalar(self) -> bool:
|
def is_scalar(self) -> bool:
|
||||||
"""Return whether this data type is scalar, i.e. not a UDT or array."""
|
"""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:
|
def to_opcua(self) -> str:
|
||||||
if not self.is_scalar():
|
if not self.is_scalar():
|
||||||
raise ValueError(
|
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()
|
spec_cleaned = self.name.split("[")[0].strip()
|
||||||
return IECtoOPCUA[spec_cleaned]
|
return IECtoOPCUA[spec_cleaned]
|
||||||
@@ -184,11 +195,11 @@ class IECType:
|
|||||||
def to_string(self) -> str:
|
def to_string(self) -> str:
|
||||||
return self.name.upper()
|
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:
|
if self.array_bounds is None or self.array_type is None:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"The `array_expansion` method can only be called on an array "
|
f"The `array_expansion` method can only be called on an array "
|
||||||
"type."
|
f"type, not {self.name}."
|
||||||
)
|
)
|
||||||
all_indices = None
|
all_indices = None
|
||||||
for i, (lbound, ubound) in enumerate(self.array_bounds):
|
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)}]",
|
lambda row: f"{name}[{','.join(str(dim) for dim in row)}]",
|
||||||
axis=1,
|
axis=1,
|
||||||
),
|
),
|
||||||
"type": self.array_type,
|
"type": str(self.array_type),
|
||||||
"address": nan,
|
"address": nan,
|
||||||
"comment": comment,
|
"comment": comment,
|
||||||
"parent": self.name.upper(),
|
"parent": self.name.upper(),
|
||||||
|
|||||||
Reference in New Issue
Block a user