Format with Ruff.
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +1,6 @@
|
||||
# Backup files.
|
||||
*.bak
|
||||
|
||||
# Test output files.
|
||||
tests/output/*
|
||||
|
||||
|
||||
@@ -13,8 +13,10 @@ from enum import StrEnum, auto
|
||||
import pandas as pd
|
||||
from pandas import DataFrame
|
||||
|
||||
|
||||
class MandatoryColumns(StrEnum):
|
||||
"""Enum of columns that are mandatory for a valid AuDaGe DataFrame."""
|
||||
|
||||
NAME = auto()
|
||||
TYPE = auto()
|
||||
ADDRESS = auto()
|
||||
@@ -35,7 +37,7 @@ class AuDaGeFrame:
|
||||
label
|
||||
for label in MandatoryColumns
|
||||
if not label in self.dataframe.columns
|
||||
]
|
||||
]
|
||||
|
||||
def is_valid(self) -> bool:
|
||||
"""Check whether the DataFrame contains all mandatory columns."""
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
class Format(ABC):
|
||||
""""""
|
||||
"""Abstract base class for AuDaGe import and export formats."""
|
||||
|
||||
@abstractmethod
|
||||
def read(self):
|
||||
@@ -9,4 +20,4 @@ class Format(ABC):
|
||||
|
||||
@abstractmethod
|
||||
def write(self):
|
||||
pass
|
||||
pass
|
||||
|
||||
@@ -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
|
||||
from collections.abc import Iterable
|
||||
from enum import StrEnum
|
||||
from math import ceil, floor
|
||||
|
||||
import pandas as pd
|
||||
@@ -10,23 +19,21 @@ from AuDaGe.audage_frame import AuDaGeFrame
|
||||
from AuDaGe.format import Format
|
||||
from AuDaGe.iec import WORD_LENGTH, DataTypes, iec_definitions
|
||||
|
||||
MAX_ITERATIONS = 5
|
||||
|
||||
|
||||
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
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
xsy_path: os.path,
|
||||
stylesheet_path: os.path
|
||||
):
|
||||
def __init__(self, xsy_path: os.path, stylesheet_path: os.path):
|
||||
"""Load an XSY file into a dataframe using the specified stylesheet."""
|
||||
self.stylesheet_path = stylesheet_path
|
||||
with (
|
||||
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)
|
||||
|
||||
@property
|
||||
@@ -39,29 +46,12 @@ class XSY_Block(AuDaGeFrame):
|
||||
self._stylesheet_path = path
|
||||
else:
|
||||
self._stylesheet_path = os.path.join(
|
||||
os.path.dirname(os.path.abspath(__file__)),
|
||||
path
|
||||
)
|
||||
os.path.dirname(os.path.abspath(__file__)), 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):
|
||||
""""""
|
||||
"""Read and parse the DDTSource nodes of an XSY file."""
|
||||
|
||||
DEFAULT_SS_PATH = "assets/flatten_ddtsource.xslt"
|
||||
DEFAULT_BUILTINS_PATH = "assets/builtins.xsy"
|
||||
@@ -72,8 +62,8 @@ class Definitions(XSY_Block):
|
||||
self,
|
||||
xsy_path: os.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."""
|
||||
self.builtins_path = builtins_path
|
||||
super().__init__(xsy_path, stylesheet_path)
|
||||
@@ -88,15 +78,35 @@ class Definitions(XSY_Block):
|
||||
self._builtins_path = path
|
||||
else:
|
||||
self._builtins_path = os.path.join(
|
||||
os.path.dirname(os.path.abspath(__file__)),
|
||||
path
|
||||
)
|
||||
os.path.dirname(os.path.abspath(__file__)), path
|
||||
)
|
||||
|
||||
def calculate_offsets(self):
|
||||
"""Calculate the memory offset of each UDT member from the root address
|
||||
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):
|
||||
@@ -125,37 +135,17 @@ class XSY(Format):
|
||||
def __init__(
|
||||
self,
|
||||
def_paths: Iterable[os.path] = [],
|
||||
builtins_path: os.path = None,
|
||||
db_ss_path: os.path = None,
|
||||
ddt_ss_path: os.path = None
|
||||
):
|
||||
builtins_path: os.path | None = None,
|
||||
db_ss_path: os.path | None = None,
|
||||
ddt_ss_path: os.path | None = 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
|
||||
|
||||
# @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(
|
||||
self,
|
||||
xsy_path: os.path,
|
||||
expand: bool = True
|
||||
) -> tuple[DataFrame, DataFrame]:
|
||||
"""Read the dataBlock and definitions of an XSY file into a dataframe.
|
||||
"""
|
||||
self, xsy_path: os.path, expand: bool = True
|
||||
) -> tuple[DataFrame, DataFrame]:
|
||||
"""Read the dataBlock and definitions of an XSY file into a dataframe."""
|
||||
self.datablock = DataBlock(xsy_path)
|
||||
self.definitions = Definitions(xsy_path)
|
||||
# if not expand:
|
||||
@@ -165,87 +155,4 @@ class XSY(Format):
|
||||
"""Write dataframe to XSY file."""
|
||||
raise NotImplementedError(
|
||||
"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)
|
||||
)
|
||||
|
||||
@@ -8,64 +8,66 @@ date: 2026-09-14
|
||||
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
|
||||
|
||||
|
||||
WORD_LENGTH = 16
|
||||
|
||||
|
||||
class DataTypes(IntEnum):
|
||||
"""The IEC 61131-3 data types and their lengths in bits."""
|
||||
|
||||
# Binary
|
||||
BOOL = 1
|
||||
BYTE = 8
|
||||
WORD = 16
|
||||
BOOL = 1
|
||||
BYTE = 8
|
||||
WORD = 16
|
||||
DWORD = 32
|
||||
LWORD = 64
|
||||
# Signed Integer
|
||||
SINT = 8
|
||||
INT = 16
|
||||
DINT = 32
|
||||
LINT = 64
|
||||
SINT = 8
|
||||
INT = 16
|
||||
DINT = 32
|
||||
LINT = 64
|
||||
# Unsigned Integer
|
||||
USINT = 8
|
||||
UINT = 16
|
||||
UINT = 16
|
||||
UDINT = 32
|
||||
ULINT = 64
|
||||
# Float
|
||||
REAL = 32
|
||||
REAL = 32
|
||||
LREAL = 64
|
||||
# Date/Time
|
||||
DATE = 32
|
||||
DATE = 32
|
||||
LDATE = 64
|
||||
DT = 32
|
||||
LDT = 64
|
||||
TIME = 32
|
||||
DT = 32
|
||||
LDT = 64
|
||||
TIME = 32
|
||||
LTIME = 64
|
||||
TOD = 32
|
||||
LTOD = 64
|
||||
TOD = 32
|
||||
LTOD = 64
|
||||
# Character
|
||||
CHAR = 8
|
||||
CHAR = 8
|
||||
WCHAR = 16
|
||||
|
||||
|
||||
def iec_definitions() -> AuDaGeFrame:
|
||||
"""Generate a DataFrame containing the standard IEC 61131-3 data types."""
|
||||
empty = Series([NA] * len(DataTypes.__members__))
|
||||
return AuDaGeFrame({
|
||||
"name": empty,
|
||||
"parent": empty,
|
||||
"type": DataTypes.__members__,
|
||||
"address": empty,
|
||||
"bitrank": empty,
|
||||
"save": empty,
|
||||
"comment": empty
|
||||
})
|
||||
return AuDaGeFrame(
|
||||
{
|
||||
"name": empty,
|
||||
"parent": empty,
|
||||
"type": DataTypes.__members__,
|
||||
"address": empty,
|
||||
"bitrank": empty,
|
||||
"save": empty,
|
||||
"comment": empty,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
iec_definitions().to_csv("tests/output/iec_definitions.csv", index=False)
|
||||
iec_definitions().to_csv("tests/output/iec_definitions.csv", index=False)
|
||||
|
||||
@@ -2,22 +2,28 @@ import unittest
|
||||
|
||||
from AuDaGe.audage_frame import AuDaGeFrame
|
||||
|
||||
|
||||
class TestAuDaGeFrame(unittest.TestCase):
|
||||
def test_init(self):
|
||||
valid_adf = AuDaGeFrame({
|
||||
"name": ["a", "b", "c"],
|
||||
"type": ["d", "e", "f"],
|
||||
"address": [1, 2, 3],
|
||||
"comment": ["g", "h", "i"]
|
||||
})
|
||||
valid_adf = AuDaGeFrame(
|
||||
{
|
||||
"name": ["a", "b", "c"],
|
||||
"type": ["d", "e", "f"],
|
||||
"address": [1, 2, 3],
|
||||
"comment": ["g", "h", "i"],
|
||||
}
|
||||
)
|
||||
self.assertTrue(valid_adf.is_valid())
|
||||
invalid_adf = AuDaGeFrame({
|
||||
"name": ["a", "b", "c"],
|
||||
# "type": ["d", "e", "f"],
|
||||
"address": [1, 2, 3],
|
||||
"comment": ["g", "h", "i"]
|
||||
})
|
||||
invalid_adf = AuDaGeFrame(
|
||||
{
|
||||
"name": ["a", "b", "c"],
|
||||
# "type": ["d", "e", "f"],
|
||||
"address": [1, 2, 3],
|
||||
"comment": ["g", "h", "i"],
|
||||
}
|
||||
)
|
||||
self.assertFalse(invalid_adf.is_valid())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(failfast=True)
|
||||
unittest.main(failfast=True)
|
||||
|
||||
@@ -8,4 +8,4 @@ date: 2026-09-14
|
||||
desc: Unit tests for the AuDaGe format module.
|
||||
"""
|
||||
|
||||
import unittest
|
||||
import unittest
|
||||
|
||||
@@ -12,6 +12,7 @@ import unittest
|
||||
|
||||
from AuDaGe import iec
|
||||
|
||||
|
||||
class TestIEC(unittest.TestCase):
|
||||
""""""
|
||||
|
||||
@@ -19,9 +20,9 @@ class TestIEC(unittest.TestCase):
|
||||
iec_definitions = iec.iec_definitions()
|
||||
self.assertTrue(iec_definitions.is_valid())
|
||||
self.assertEqual(
|
||||
iec_definitions.dataframe.shape,
|
||||
(len(iec.DataTypes.__members__), 7)
|
||||
)
|
||||
iec_definitions.dataframe.shape, (len(iec.DataTypes.__members__), 7)
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(failfast=True)
|
||||
unittest.main(failfast=True)
|
||||
|
||||
@@ -10,17 +10,15 @@ desc: Unit tests for the AuDaGe XSY module.
|
||||
|
||||
import os
|
||||
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
|
||||
|
||||
IN_PATH = "tests/assets"
|
||||
OUT_PATH = "tests/output"
|
||||
|
||||
class TestXSY(unittest.TestCase):
|
||||
|
||||
class TestXSY(unittest.TestCase):
|
||||
def test_init(self):
|
||||
assert isinstance(XSY(os.path.join(IN_PATH, "hsps.xsy")), XSY)
|
||||
|
||||
@@ -28,12 +26,10 @@ class TestXSY(unittest.TestCase):
|
||||
xsy = XSY()
|
||||
xsy.read(os.path.join(IN_PATH, "hsps.xsy"), expand=False)
|
||||
xsy.datablock.dataframe.to_csv(
|
||||
os.path.join(OUT_PATH, "datablock.csv"),
|
||||
index=False
|
||||
os.path.join(OUT_PATH, "datablock.csv"), index=False
|
||||
)
|
||||
xsy.definitions.dataframe.to_csv(
|
||||
os.path.join(OUT_PATH, "defs.csv"),
|
||||
index=False
|
||||
os.path.join(OUT_PATH, "defs.csv"), index=False
|
||||
)
|
||||
# xsy.expand_datablock(datablock).to_csv(
|
||||
# os.path.join(OUT_PATH, "expanded.csv"),
|
||||
@@ -42,4 +38,4 @@ class TestXSY(unittest.TestCase):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main(failfast=True)
|
||||
unittest.main(failfast=True)
|
||||
|
||||
Reference in New Issue
Block a user