Add ABC for data formats and refactor XSY module to match.

This commit is contained in:
Josh Lucas
2026-05-28 15:51:46 -05:00
parent 933cec6a0c
commit 59bb7ec24c
6 changed files with 126 additions and 57 deletions

View File

@@ -6,17 +6,21 @@ with a GUI application possibly to come later.
## Planned formats (non-exhaustive): ## Planned formats (non-exhaustive):
- PLC variable export: - PLC variable export:
- In Development:
- Schneider Electric ControlExpert XSY - Schneider Electric ControlExpert XSY
- Planned:
- Allen-Bradley CSV - Allen-Bradley CSV
- HMI/SCADA databases: - HMI/SCADA databases:
- In Development:
- Kepserver EX/Enterprise CSV - Kepserver EX/Enterprise CSV
- Planned:
- FactoryTalk View SE Alarm Database XLS and/or XML - FactoryTalk View SE Alarm Database XLS and/or XML
- PI Historian XLSX - PI Historian XLSX
## Dataframe format ## Dataframe format
| parent | name | type | address | bitrank | save | comment | | name | parent | type | address | bitrank | save | comment |
|:------:|:----:|:----:|:-------:|:-------:|:----:|:-------:| |:----:|:------:|:----:|:-------:|:-------:|:----:|:-------:|
## Dependencies ## Dependencies
- pandas - pandas

View File

13
src/AuDaGe/format.py Normal file
View File

@@ -0,0 +1,13 @@
import typing
from abc import ABC, abstractmethod
class Format(ABC):
""""""
@abstractmethod
def read(self):
pass
@abstractmethod
def write(self):
pass

View File

@@ -9,8 +9,9 @@ from pandas import DataFrame
import AuDaGe import AuDaGe
from AuDaGe.iec import DataTypes, ElementaryVariable, UDT from AuDaGe.iec import DataTypes, ElementaryVariable, UDT
from AuDaGe.format import Format
class XSY(): class XSY(Format):
"""Schneider Electric EcoStruxure Control Expert variable exchange file """Schneider Electric EcoStruxure Control Expert variable exchange file
(XSY). (XSY).
@@ -19,7 +20,7 @@ class XSY():
Parameters Parameters
---------- ----------
xsy_path : pathlike xsy_path : pathlike
Path (relative or absolute) to the XSY file to be read. Path (relative or absolute) to the main XSY file to be read.
dir_path : pathlike (optional) dir_path : pathlike (optional)
Absolute path to the directory containing the XSY file if it is not in Absolute path to the directory containing the XSY file if it is not in
the active directory. the active directory.
@@ -32,37 +33,61 @@ class XSY():
Path to XSLT stylesheet to flatten the XSY file and extract the Path to XSLT stylesheet to flatten the XSY file and extract the
DDTSource nodes before importing into pandas. DDTSource nodes before importing into pandas.
""" """
def __init__( def __init__(
self, self,
xsy_path: os.path, dir_path: os.path = "",
dir_path: os.path = '',
def_paths: Sequence[os.path] = [], def_paths: Sequence[os.path] = [],
db_ss_path: os.path = 'data/flatten_datablock.xslt', builtins_path: os.path = None,
ddt_ss_path: os.path = 'data/flatten_ddtsource.xslt', db_ss_path: os.path = None,
) -> None: ddt_ss_path: os.path = None
self.dir_path: os.path = dir_path ):
self.xsy_path: os.path = os.path.join(dir_path, xsy_path) self.dir_path = ""
self.db_ss_path: os.path = resources.files( self.def_paths = []
"AuDaGe.formats.controlexpert.xsy" self.defs = None
).joinpath(db_ss_path) self._set_fields(
self.ddt_ss_path: os.path = resources.files( dir_path=dir_path,
"AuDaGe.formats.controlexpert.xsy" def_paths=def_paths,
).joinpath(ddt_ss_path) builtins_path=builtins_path,
self.def_paths: list[os.path] = [ db_ss_path=db_ss_path,
os.path.join(dir_path, def_path) for def_path in def_paths ddt_ss_path=ddt_ss_path
] )
self.datablock = self._read_datablock()
self.defs: DataFrame = self._read_defs()
self.datablock_expanded = self._expand_datablock()
def __fspath__(self) -> os.path: def read(
return self.xsy_path self,
xsy_path: os.path,
expand: bool = True,
dir_path: os.path = "",
def_paths: Sequence[os.path] = [],
db_ss_path: os.path = None,
ddt_ss_path: os.path = None,
) -> DataFrame:
"""Read the dataBlock of an XSY file into a dataframe."""
self._set_fields(
dir_path=dir_path,
def_paths=def_paths,
db_ss_path=db_ss_path,
ddt_ss_path=ddt_ss_path
)
xsy_path = os.path.join(dir_path, xsy_path)
with (
open(xsy_path) as xsy_file,
open(self.db_ss_path) as ss_file
):
datablock = pd.read_xml(xsy_file, stylesheet=ss_file)
if expand:
return self.expand_datablock(datablock)
else:
return datablock
def _expand_datablock(self) -> DataFrame: def write(self):
# Consider df.explode here. """Write dataframe to XSY file."""
self.datablock.set_index("type") raise NotImplementedError()
datablock = (
self.datablock def expand_datablock(self, datablock: DataFrame) -> DataFrame:
datablock.set_index("type")
datablock_expanded = (
datablock
.join( .join(
self.defs, self.defs,
how="cross", how="cross",
@@ -72,29 +97,57 @@ class XSY():
"type==parent_def" "type==parent_def"
) )
) )
return datablock return datablock_expanded
def _read_datablock(self) -> DataFrame: def _read_defs(self, xsy_path: os.path = None) -> DataFrame:
"""Read the dataBlock of an XSY file into a dataframe.""" """Read the DDTSource nodes of one or more XSY files into a dataframe.
with ( """
open(self.xsy_path) as xsy_file, if xsy_path is not None:
open(self.db_ss_path) as ss_file self.def_paths.append(xsy_path)
):
datablock = pd.read_xml(xsy_file, stylesheet=ss_file)
return datablock
def _read_defs(self) -> DataFrame:
"""Read the DDTSource nodes of an XSY file into a dataframe."""
self.def_paths.append(self.xsy_path)
defs: list[DataFrame] = [] defs: list[DataFrame] = []
with open(self.ddt_ss_path) as ss_file: with open(self.ddt_ss_path) as ss_file:
for def_path in self.def_paths: for def_path in self.def_paths:
with open(def_path) as def_file: with open(def_path) as def_file:
def_df = pd.read_xml(def_file, stylesheet = ss_file) def_df = pd.read_xml(def_file, stylesheet=ss_file)
defs.append(def_df) defs.append(def_df)
return pd.concat(defs) return pd.concat(defs)
def write(self, ): def _set_fields(
"""Write dataframe to XSY file.""" self,
pass dir_path: os.path = "",
def_paths: Sequence[os.path] = [],
builtins_path: os.path = None,
db_ss_path: os.path = None,
ddt_ss_path: os.path = None
):
# Directory
if dir_path != "":
self.dir_path = dir_path
# Builtins
if builtins_path is None:
builtins_path = "data/builtins.xsy"
self.def_paths.append(resources.files(
"AuDaGe.formats.controlexpert.xsy"
).joinpath(builtins_path))
# Stylesheets
if db_ss_path is None:
db_ss_path = "data/flatten_datablock.xslt"
self.db_ss_path: os.path = resources.files(
"AuDaGe.formats.controlexpert.xsy"
).joinpath(db_ss_path)
if ddt_ss_path is None:
ddt_ss_path = "data/flatten_ddtsource.xslt"
self.ddt_ss_path: os.path = resources.files(
"AuDaGe.formats.controlexpert.xsy"
).joinpath(ddt_ss_path)
# Definitions
if def_paths != []:
self.def_paths.extend([
os.path.join(self.dir_path, def_path)
for def_path in def_paths
])
# Update definitions.
self.defs = self._read_defs()
elif self.defs is None:
self.defs = self._read_defs()

View File

@@ -9,11 +9,9 @@ class DataType:
"""Class for IEC 61131-3 data types.""" """Class for IEC 61131-3 data types."""
def __init__( def __init__(
self, self,
# tagname: str,
length_bits: int, length_bits: int,
word_length = 16, word_length = 16,
): ):
# self.tagname = tagname
self.length_bits = length_bits self.length_bits = length_bits
self.length_bytes = length_bits // 8 self.length_bytes = length_bits // 8
self.length_words = length_bits // word_length self.length_words = length_bits // word_length

View File

@@ -8,9 +8,10 @@ def test_init():
assert isinstance(XSY("tests/data/hsps.xsy"), XSY) assert isinstance(XSY("tests/data/hsps.xsy"), XSY)
def test_export(): def test_export():
xsy = XSY("tests/data/hsps.xsy") xsy = XSY()
datablock = xsy.read("tests/data/hsps.xsy", expand=False)
out_path = "tests/output" out_path = "tests/output"
xsy.datablock.to_csv( datablock.to_csv(
os.path.join(out_path, 'datablock.csv'), os.path.join(out_path, 'datablock.csv'),
index=False index=False
) )
@@ -18,7 +19,7 @@ def test_export():
os.path.join(out_path, 'defs.csv'), os.path.join(out_path, 'defs.csv'),
index=False index=False
) )
xsy.datablock_expanded.to_csv( xsy.expand_datablock(datablock).to_csv(
os.path.join(out_path, 'expanded.csv'), os.path.join(out_path, 'expanded.csv'),
index=False index=False
) )