From 59bb7ec24c7967e3bbbc0ec4d70098b8c971c9eb Mon Sep 17 00:00:00 2001 From: Josh Lucas Date: Thu, 28 May 2026 15:51:46 -0500 Subject: [PATCH] Add ABC for data formats and refactor XSY module to match. --- readme.md | 18 +-- src/AuDaGe/AuDaGe.py | 0 src/AuDaGe/format.py | 13 +++ src/AuDaGe/formats/controlexpert/xsy.py | 143 ++++++++++++++++-------- src/AuDaGe/iec.py | 2 - tests/test_xsy.py | 7 +- 6 files changed, 126 insertions(+), 57 deletions(-) delete mode 100644 src/AuDaGe/AuDaGe.py create mode 100644 src/AuDaGe/format.py diff --git a/readme.md b/readme.md index 17c431c..9612402 100644 --- a/readme.md +++ b/readme.md @@ -6,17 +6,21 @@ with a GUI application possibly to come later. ## Planned formats (non-exhaustive): - PLC variable export: - - Schneider Electric ControlExpert XSY - - Allen-Bradley CSV + - In Development: + - Schneider Electric ControlExpert XSY + - Planned: + - Allen-Bradley CSV - HMI/SCADA databases: - - Kepserver EX/Enterprise CSV - - FactoryTalk View SE Alarm Database XLS and/or XML - - PI Historian XLSX + - In Development: + - Kepserver EX/Enterprise CSV + - Planned: + - FactoryTalk View SE Alarm Database XLS and/or XML + - PI Historian XLSX ## Dataframe format -| parent | name | type | address | bitrank | save | comment | -|:------:|:----:|:----:|:-------:|:-------:|:----:|:-------:| +| name | parent | type | address | bitrank | save | comment | +|:----:|:------:|:----:|:-------:|:-------:|:----:|:-------:| ## Dependencies - pandas \ No newline at end of file diff --git a/src/AuDaGe/AuDaGe.py b/src/AuDaGe/AuDaGe.py deleted file mode 100644 index e69de29..0000000 diff --git a/src/AuDaGe/format.py b/src/AuDaGe/format.py new file mode 100644 index 0000000..5f63db3 --- /dev/null +++ b/src/AuDaGe/format.py @@ -0,0 +1,13 @@ +import typing +from abc import ABC, abstractmethod + +class Format(ABC): + """""" + + @abstractmethod + def read(self): + pass + + @abstractmethod + def write(self): + pass \ No newline at end of file diff --git a/src/AuDaGe/formats/controlexpert/xsy.py b/src/AuDaGe/formats/controlexpert/xsy.py index 07abbd4..4c5967a 100644 --- a/src/AuDaGe/formats/controlexpert/xsy.py +++ b/src/AuDaGe/formats/controlexpert/xsy.py @@ -9,8 +9,9 @@ from pandas import DataFrame import AuDaGe 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 (XSY). @@ -19,7 +20,7 @@ class XSY(): Parameters ---------- 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) Absolute path to the directory containing the XSY file if it is not in the active directory. @@ -32,37 +33,61 @@ class XSY(): Path to XSLT stylesheet to flatten the XSY file and extract the DDTSource nodes before importing into pandas. """ + def __init__( self, - xsy_path: os.path, - dir_path: os.path = '', + dir_path: os.path = "", def_paths: Sequence[os.path] = [], - db_ss_path: os.path = 'data/flatten_datablock.xslt', - ddt_ss_path: os.path = 'data/flatten_ddtsource.xslt', - ) -> None: - self.dir_path: os.path = dir_path - self.xsy_path: os.path = os.path.join(dir_path, xsy_path) - self.db_ss_path: os.path = resources.files( - "AuDaGe.formats.controlexpert.xsy" - ).joinpath(db_ss_path) - self.ddt_ss_path: os.path = resources.files( - "AuDaGe.formats.controlexpert.xsy" - ).joinpath(ddt_ss_path) - self.def_paths: list[os.path] = [ - os.path.join(dir_path, def_path) for def_path in def_paths - ] - self.datablock = self._read_datablock() - self.defs: DataFrame = self._read_defs() - self.datablock_expanded = self._expand_datablock() + builtins_path: os.path = None, + db_ss_path: os.path = None, + ddt_ss_path: os.path = None + ): + self.dir_path = "" + self.def_paths = [] + self.defs = None + self._set_fields( + dir_path=dir_path, + def_paths=def_paths, + builtins_path=builtins_path, + db_ss_path=db_ss_path, + ddt_ss_path=ddt_ss_path + ) - def __fspath__(self) -> os.path: - return self.xsy_path + def read( + 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: - # Consider df.explode here. - self.datablock.set_index("type") - datablock = ( - self.datablock + def write(self): + """Write dataframe to XSY file.""" + raise NotImplementedError() + + def expand_datablock(self, datablock: DataFrame) -> DataFrame: + datablock.set_index("type") + datablock_expanded = ( + datablock .join( self.defs, how="cross", @@ -72,29 +97,57 @@ class XSY(): "type==parent_def" ) ) - return datablock + return datablock_expanded - def _read_datablock(self) -> DataFrame: - """Read the dataBlock of an XSY file into a dataframe.""" - with ( - open(self.xsy_path) as xsy_file, - open(self.db_ss_path) as ss_file - ): - 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) + def _read_defs(self, xsy_path: os.path = None) -> DataFrame: + """Read the DDTSource nodes of one or more XSY files into a dataframe. + """ + if xsy_path is not None: + self.def_paths.append(xsy_path) defs: list[DataFrame] = [] with open(self.ddt_ss_path) as ss_file: for def_path in self.def_paths: 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) return pd.concat(defs) - def write(self, ): - """Write dataframe to XSY file.""" - pass + def _set_fields( + self, + 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() diff --git a/src/AuDaGe/iec.py b/src/AuDaGe/iec.py index dc4dd03..5c28185 100644 --- a/src/AuDaGe/iec.py +++ b/src/AuDaGe/iec.py @@ -9,11 +9,9 @@ class DataType: """Class for IEC 61131-3 data types.""" def __init__( self, - # tagname: str, length_bits: int, word_length = 16, ): - # self.tagname = tagname self.length_bits = length_bits self.length_bytes = length_bits // 8 self.length_words = length_bits // word_length diff --git a/tests/test_xsy.py b/tests/test_xsy.py index 4b83b8f..edae380 100644 --- a/tests/test_xsy.py +++ b/tests/test_xsy.py @@ -8,9 +8,10 @@ def test_init(): assert isinstance(XSY("tests/data/hsps.xsy"), XSY) def test_export(): - xsy = XSY("tests/data/hsps.xsy") + xsy = XSY() + datablock = xsy.read("tests/data/hsps.xsy", expand=False) out_path = "tests/output" - xsy.datablock.to_csv( + datablock.to_csv( os.path.join(out_path, 'datablock.csv'), index=False ) @@ -18,7 +19,7 @@ def test_export(): os.path.join(out_path, 'defs.csv'), index=False ) - xsy.datablock_expanded.to_csv( + xsy.expand_datablock(datablock).to_csv( os.path.join(out_path, 'expanded.csv'), index=False )