Format with Ruff.

This commit is contained in:
Silcantar
2026-09-14 15:39:21 -05:00
parent 50a81c3a5c
commit bcdd674c4e
9 changed files with 134 additions and 206 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,6 @@
# Backup files.
*.bak
# Test output files. # Test output files.
tests/output/* tests/output/*

View File

@@ -13,8 +13,10 @@ from enum import StrEnum, auto
import pandas as pd import pandas as pd
from pandas import DataFrame from pandas import DataFrame
class MandatoryColumns(StrEnum): class MandatoryColumns(StrEnum):
"""Enum of columns that are mandatory for a valid AuDaGe DataFrame.""" """Enum of columns that are mandatory for a valid AuDaGe DataFrame."""
NAME = auto() NAME = auto()
TYPE = auto() TYPE = auto()
ADDRESS = auto() ADDRESS = auto()
@@ -35,7 +37,7 @@ class AuDaGeFrame:
label label
for label in MandatoryColumns for label in MandatoryColumns
if not label in self.dataframe.columns if not label in self.dataframe.columns
] ]
def is_valid(self) -> bool: def is_valid(self) -> bool:
"""Check whether the DataFrame contains all mandatory columns.""" """Check whether the DataFrame contains all mandatory columns."""

View File

@@ -1,7 +1,18 @@
"""
AuDaGe format module
name: format.py
by: Josh Lucas
date: 2026-09-14
desc: Abstract base class for AuDaGe import and export formats.
"""
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
class Format(ABC): class Format(ABC):
"""""" """Abstract base class for AuDaGe import and export formats."""
@abstractmethod @abstractmethod
def read(self): def read(self):

View File

@@ -1,6 +1,15 @@
"""
AuDaGe XSY module
name: test_xsy.py
by: Josh Lucas
date: 2026-09-14
desc: AuDaGe module for handling Schneider Electric ControlExpert XSY files.
"""
import os import os
from collections.abc import Iterable from collections.abc import Iterable
from enum import StrEnum
from math import ceil, floor from math import ceil, floor
import pandas as pd import pandas as pd
@@ -10,23 +19,21 @@ from AuDaGe.audage_frame import AuDaGeFrame
from AuDaGe.format import Format from AuDaGe.format import Format
from AuDaGe.iec import WORD_LENGTH, DataTypes, iec_definitions from AuDaGe.iec import WORD_LENGTH, DataTypes, iec_definitions
MAX_ITERATIONS = 5
class XSY_Block(AuDaGeFrame): class XSY_Block(AuDaGeFrame):
"""Base class for DataBlock and Definitions that handles shared attributes. """Base class for DataBlock and Definitions that handles shared attributes."""
"""
_stylesheet_path = None _stylesheet_path = None
def __init__( def __init__(self, xsy_path: os.path, stylesheet_path: os.path):
self,
xsy_path: os.path,
stylesheet_path: os.path
):
"""Load an XSY file into a dataframe using the specified stylesheet.""" """Load an XSY file into a dataframe using the specified stylesheet."""
self.stylesheet_path = stylesheet_path self.stylesheet_path = stylesheet_path
with ( with (
open(xsy_path) as xsy_file, open(xsy_path) as xsy_file,
open(self.stylesheet_path) as stylesheet_file open(self.stylesheet_path) as stylesheet_file,
): ):
self.dataframe = pd.read_xml(xsy_file, stylesheet=stylesheet_file) self.dataframe = pd.read_xml(xsy_file, stylesheet=stylesheet_file)
@property @property
@@ -39,29 +46,12 @@ class XSY_Block(AuDaGeFrame):
self._stylesheet_path = path self._stylesheet_path = path
else: else:
self._stylesheet_path = os.path.join( self._stylesheet_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), os.path.dirname(os.path.abspath(__file__)), path
path )
)
class DataBlock(XSY_Block):
""""""
DEFAULT_SS_PATH = "assets/flatten_datablock.xslt"
def __init__(
self,
xsy_path: os.path,
stylesheet_path: os.path = DEFAULT_SS_PATH
):
"""Read the dataBlock of an XSY file into a DataFrame."""
super().__init__(xsy_path, stylesheet_path)
def expand(self, definitions: Definitions):
"""Expand the UDTs in the dataBlock using the specified Definitions."""
pass
class Definitions(XSY_Block): class Definitions(XSY_Block):
"""""" """Read and parse the DDTSource nodes of an XSY file."""
DEFAULT_SS_PATH = "assets/flatten_ddtsource.xslt" DEFAULT_SS_PATH = "assets/flatten_ddtsource.xslt"
DEFAULT_BUILTINS_PATH = "assets/builtins.xsy" DEFAULT_BUILTINS_PATH = "assets/builtins.xsy"
@@ -72,8 +62,8 @@ class Definitions(XSY_Block):
self, self,
xsy_path: os.path, xsy_path: os.path,
stylesheet_path: os.path = DEFAULT_SS_PATH, stylesheet_path: os.path = DEFAULT_SS_PATH,
builtins_path: os.path = DEFAULT_BUILTINS_PATH builtins_path: os.path = DEFAULT_BUILTINS_PATH,
): ):
"""Read the DDTSource nodes of an XSY file into a DataFrame.""" """Read the DDTSource nodes of an XSY file into a DataFrame."""
self.builtins_path = builtins_path self.builtins_path = builtins_path
super().__init__(xsy_path, stylesheet_path) super().__init__(xsy_path, stylesheet_path)
@@ -88,15 +78,35 @@ class Definitions(XSY_Block):
self._builtins_path = path self._builtins_path = path
else: else:
self._builtins_path = os.path.join( self._builtins_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)), os.path.dirname(os.path.abspath(__file__)), path
path )
)
def calculate_offsets(self): def calculate_offsets(self):
"""Calculate the memory offset of each UDT member from the root address """Calculate the memory offset of each UDT member from the root address
of the UDT. of the UDT.
""" """
pass # Find all UDT names in the parent column of the definitions.
unique_parents = self.dataframe.parent.unique()
definitions_list: list[dict] = []
for parent in unique_parents:
# Get all the members of the current UDT.
children = self.dataframe[self.dataframe.parent == parent]
definitions_list.extend(self._calculate_offsets(children))
class DataBlock(XSY_Block):
"""Read and parse the dataBlock node of an XSY file."""
DEFAULT_SS_PATH = "assets/flatten_datablock.xslt"
def __init__(
self, xsy_path: os.path, stylesheet_path: os.path = DEFAULT_SS_PATH
):
"""Read the dataBlock of an XSY file into a DataFrame."""
super().__init__(xsy_path, stylesheet_path)
def expand(self, definitions: Definitions):
"""Expand the UDTs in the dataBlock using the specified Definitions."""
class XSY(Format): class XSY(Format):
@@ -125,37 +135,17 @@ class XSY(Format):
def __init__( def __init__(
self, self,
def_paths: Iterable[os.path] = [], def_paths: Iterable[os.path] = [],
builtins_path: os.path = None, builtins_path: os.path | None = None,
db_ss_path: os.path = None, db_ss_path: os.path | None = None,
ddt_ss_path: os.path = None ddt_ss_path: os.path | None = None,
): ):
self.defs = None self.defs = None
# self.dataBlock_stylesheet = db_ss_path
# self.definition_stylesheet = ddt_ss_path
# self.builtins = builtins_path
self.definition_paths = def_paths self.definition_paths = def_paths
# @property
# def builtins(self) -> os.path[str]:
# return self._builtins
# @builtins.setter
# def builtins(
# self,
# path: os.PathLike = None
# ):
# if path is None:
# path = DEFAULT_BUILTINS_PATH
# # self._builtins = resources.files(DefaultPath.PACKAGE).joinpath(path)
# self._builtins = os.path.join(os.path.dirname(__file__), path)
def read( def read(
self, self, xsy_path: os.path, expand: bool = True
xsy_path: os.path, ) -> tuple[DataFrame, DataFrame]:
expand: bool = True """Read the dataBlock and definitions of an XSY file into a dataframe."""
) -> tuple[DataFrame, DataFrame]:
"""Read the dataBlock and definitions of an XSY file into a dataframe.
"""
self.datablock = DataBlock(xsy_path) self.datablock = DataBlock(xsy_path)
self.definitions = Definitions(xsy_path) self.definitions = Definitions(xsy_path)
# if not expand: # if not expand:
@@ -165,87 +155,4 @@ class XSY(Format):
"""Write dataframe to XSY file.""" """Write dataframe to XSY file."""
raise NotImplementedError( raise NotImplementedError(
"Writing to XSY is not currently (or probably ever) implemented." "Writing to XSY is not currently (or probably ever) implemented."
) )
# def read_datablock(self, xsy_path: os.path) -> DataFrame:
# """Read the dataBlock of an XSY file into a DataFrame."""
# with (
# open(xsy_path) as xsy_file,
# open(self.dataBlock_stylesheet) as stylesheet
# ):
# return pd.read_xml(xsy_file, stylesheet=stylesheet)
# def read_definitions(self, xsy_path: os.path) -> DataFrame:
# """Read the DDTSource nodes from an XSY file into a DataFrame."""
# with (
# open(xsy_path) as xsy_file,
# open(self.definition_stylesheet) as stylesheet
# ):
# return pd.read_xml(xsy_file, stylesheet=stylesheet)
# def _expand_datablock(
# self,
# datablock: DataFrame,
# definitions: DataFrame = None
# ) -> DataFrame:
# start = timer()
# if definitions is None:
# definitions = self.definitions
# # else:
# # definitions = self._calculate_offsets(
# # pd.concat([definitions, self.definitions])
# # )
# # print(f"Calculate Offsets: {timer() - start}")
# datablock_expanded = datablock.join(
# definitions,
# how="cross",
# rsuffix="_def"
# )
# print(f"Cross join: {timer() - start}")
# datablock_queried = datablock_expanded[
# datablock_expanded.type == datablock_expanded.parent_def
# ]
# print(f"Query: {timer() - start}")
# return datablock_queried
# def _iterate_parents(self) -> DataFrame:
# unique_parents = definitions.parent.unique()
# definitions_list: list[dict] = []
# for parent in unique_parents:
# children = definitions[definitions.parent == parent]
# definitions_list.extend(self._calculate_offsets(children))
# def _calculate_offsets(
# self,
# children: DataFrame
# ) -> list[dict]:
# offset: float = 0
# for child in children.itertuples():
# if offset < 0:
# raise ValueError("Offset is less than zero!")
# length = DataTypes[child.type]
# if length > WORD_LENGTH / 2 and offset > floor(offset):
# offset = ceil(offset)
# child_dict = child._asdict()
# child_dict["address"] = offset
# definitions_list.append(child_dict)
# if "BOOLS" in child.name:
# pass
# elif length > WORD_LENGTH / 2:
# offset += ceil(length / WORD_LENGTH)
# else:
# offset += length / 100
# if offset - floor(offset) >= WORD_LENGTH / 100:
# offset = ceil(offset)
# return pd.DataFrame(definitions_list)
# def _refresh_definitions(self):
# defs: list[DataFrame] = [iec_definitions()]
# definition_paths = self._definition_paths | {self.builtins}
# for path in definition_paths:
# with (
# open(path) as def_file,
# open(self.definition_stylesheet) as stylesheet
# ):
# defs.append(pd.read_xml(def_file, stylesheet=stylesheet))
# self.definitions = pd.concat(defs)

View File

@@ -8,63 +8,65 @@ date: 2026-09-14
desc: Definitions for IEC 61131 programming paradigms. desc: Definitions for IEC 61131 programming paradigms.
""" """
from enum import Enum, IntEnum from enum import IntEnum
from pandas import Series, NA from pandas import NA, Series
from AuDaGe.audage_frame import AuDaGeFrame from AuDaGe.audage_frame import AuDaGeFrame
WORD_LENGTH = 16 WORD_LENGTH = 16
class DataTypes(IntEnum): class DataTypes(IntEnum):
"""The IEC 61131-3 data types and their lengths in bits.""" """The IEC 61131-3 data types and their lengths in bits."""
# Binary # Binary
BOOL = 1 BOOL = 1
BYTE = 8 BYTE = 8
WORD = 16 WORD = 16
DWORD = 32 DWORD = 32
LWORD = 64 LWORD = 64
# Signed Integer # Signed Integer
SINT = 8 SINT = 8
INT = 16 INT = 16
DINT = 32 DINT = 32
LINT = 64 LINT = 64
# Unsigned Integer # Unsigned Integer
USINT = 8 USINT = 8
UINT = 16 UINT = 16
UDINT = 32 UDINT = 32
ULINT = 64 ULINT = 64
# Float # Float
REAL = 32 REAL = 32
LREAL = 64 LREAL = 64
# Date/Time # Date/Time
DATE = 32 DATE = 32
LDATE = 64 LDATE = 64
DT = 32 DT = 32
LDT = 64 LDT = 64
TIME = 32 TIME = 32
LTIME = 64 LTIME = 64
TOD = 32 TOD = 32
LTOD = 64 LTOD = 64
# Character # Character
CHAR = 8 CHAR = 8
WCHAR = 16 WCHAR = 16
def iec_definitions() -> AuDaGeFrame: def iec_definitions() -> AuDaGeFrame:
"""Generate a DataFrame containing the standard IEC 61131-3 data types.""" """Generate a DataFrame containing the standard IEC 61131-3 data types."""
empty = Series([NA] * len(DataTypes.__members__)) empty = Series([NA] * len(DataTypes.__members__))
return AuDaGeFrame({ return AuDaGeFrame(
"name": empty, {
"parent": empty, "name": empty,
"type": DataTypes.__members__, "parent": empty,
"address": empty, "type": DataTypes.__members__,
"bitrank": empty, "address": empty,
"save": empty, "bitrank": empty,
"comment": empty "save": empty,
}) "comment": empty,
}
)
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -2,22 +2,28 @@ import unittest
from AuDaGe.audage_frame import AuDaGeFrame from AuDaGe.audage_frame import AuDaGeFrame
class TestAuDaGeFrame(unittest.TestCase): class TestAuDaGeFrame(unittest.TestCase):
def test_init(self): def test_init(self):
valid_adf = AuDaGeFrame({ valid_adf = AuDaGeFrame(
"name": ["a", "b", "c"], {
"type": ["d", "e", "f"], "name": ["a", "b", "c"],
"address": [1, 2, 3], "type": ["d", "e", "f"],
"comment": ["g", "h", "i"] "address": [1, 2, 3],
}) "comment": ["g", "h", "i"],
}
)
self.assertTrue(valid_adf.is_valid()) self.assertTrue(valid_adf.is_valid())
invalid_adf = AuDaGeFrame({ invalid_adf = AuDaGeFrame(
"name": ["a", "b", "c"], {
# "type": ["d", "e", "f"], "name": ["a", "b", "c"],
"address": [1, 2, 3], # "type": ["d", "e", "f"],
"comment": ["g", "h", "i"] "address": [1, 2, 3],
}) "comment": ["g", "h", "i"],
}
)
self.assertFalse(invalid_adf.is_valid()) self.assertFalse(invalid_adf.is_valid())
if __name__ == "__main__": if __name__ == "__main__":
unittest.main(failfast=True) unittest.main(failfast=True)

View File

@@ -12,6 +12,7 @@ import unittest
from AuDaGe import iec from AuDaGe import iec
class TestIEC(unittest.TestCase): class TestIEC(unittest.TestCase):
"""""" """"""
@@ -19,9 +20,9 @@ class TestIEC(unittest.TestCase):
iec_definitions = iec.iec_definitions() iec_definitions = iec.iec_definitions()
self.assertTrue(iec_definitions.is_valid()) self.assertTrue(iec_definitions.is_valid())
self.assertEqual( self.assertEqual(
iec_definitions.dataframe.shape, iec_definitions.dataframe.shape, (len(iec.DataTypes.__members__), 7)
(len(iec.DataTypes.__members__), 7) )
)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main(failfast=True) unittest.main(failfast=True)

View File

@@ -10,17 +10,15 @@ desc: Unit tests for the AuDaGe XSY module.
import os import os
import unittest import unittest
from timeit import default_timer as timer
# from pandas import DataFrame
# from timeit import default_timer as timer
from AuDaGe.formats.controlexpert.xsy import XSY from AuDaGe.formats.controlexpert.xsy import XSY
IN_PATH = "tests/assets" IN_PATH = "tests/assets"
OUT_PATH = "tests/output" OUT_PATH = "tests/output"
class TestXSY(unittest.TestCase):
class TestXSY(unittest.TestCase):
def test_init(self): def test_init(self):
assert isinstance(XSY(os.path.join(IN_PATH, "hsps.xsy")), XSY) assert isinstance(XSY(os.path.join(IN_PATH, "hsps.xsy")), XSY)
@@ -28,12 +26,10 @@ class TestXSY(unittest.TestCase):
xsy = XSY() xsy = XSY()
xsy.read(os.path.join(IN_PATH, "hsps.xsy"), expand=False) xsy.read(os.path.join(IN_PATH, "hsps.xsy"), expand=False)
xsy.datablock.dataframe.to_csv( xsy.datablock.dataframe.to_csv(
os.path.join(OUT_PATH, "datablock.csv"), os.path.join(OUT_PATH, "datablock.csv"), index=False
index=False
) )
xsy.definitions.dataframe.to_csv( xsy.definitions.dataframe.to_csv(
os.path.join(OUT_PATH, "defs.csv"), os.path.join(OUT_PATH, "defs.csv"), index=False
index=False
) )
# xsy.expand_datablock(datablock).to_csv( # xsy.expand_datablock(datablock).to_csv(
# os.path.join(OUT_PATH, "expanded.csv"), # os.path.join(OUT_PATH, "expanded.csv"),