diff --git a/src/AuDaGe/formats/controlexpert/xsy.py b/src/AuDaGe/formats/controlexpert/xsy.py index ab3664a..07abbd4 100644 --- a/src/AuDaGe/formats/controlexpert/xsy.py +++ b/src/AuDaGe/formats/controlexpert/xsy.py @@ -8,7 +8,7 @@ import pandas as pd from pandas import DataFrame import AuDaGe -from AuDaGe.iec import datatypes, ElementaryVariable, UDT +from AuDaGe.iec import DataTypes, ElementaryVariable, UDT class XSY(): """Schneider Electric EcoStruxure Control Expert variable exchange file @@ -53,26 +53,26 @@ class XSY(): ] self.datablock = self._read_datablock() self.defs: DataFrame = self._read_defs() - + self.datablock_expanded = self._expand_datablock() def __fspath__(self) -> os.path: return self.xsy_path def _expand_datablock(self) -> DataFrame: # Consider df.explode here. - self.datablock = self.datablock.set_index('type').join( - self.defs.set_index('parent'), - # how = 'cross', - lsuffix = 'db', - rsuffix = 'ddt', + self.datablock.set_index("type") + datablock = ( + self.datablock + .join( + self.defs, + how="cross", + rsuffix="_def" + ) + .query( + "type==parent_def" + ) ) - return self.datablock - - # def _read(self) -> DataFrame: - # """Read *and* expand the dataBlock using the definitions within the XSY - # file and from the external definition files, if provided. - # """ - # pass + return datablock def _read_datablock(self) -> DataFrame: """Read the dataBlock of an XSY file into a dataframe.""" @@ -80,19 +80,19 @@ class XSY(): open(self.xsy_path) as xsy_file, open(self.db_ss_path) as ss_file ): - self.datablock = pd.read_xml(xsy_file, stylesheet=ss_file) - return self.datablock + 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 = DataFrame() + 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) - defs = pd.concat([defs, def_df]) - return defs + defs.append(def_df) + return pd.concat(defs) def write(self, ): """Write dataframe to XSY file.""" diff --git a/tests/test_xsy.py b/tests/test_xsy.py index adc2c37..4b83b8f 100644 --- a/tests/test_xsy.py +++ b/tests/test_xsy.py @@ -10,5 +10,15 @@ def test_init(): def test_export(): xsy = XSY("tests/data/hsps.xsy") out_path = "tests/output" - xsy.datablock.to_csv(os.path.join(out_path, 'test_db.csv'), index=False) - xsy.defs.to_csv(os.path.join(out_path, 'test_ddt.csv'), index=False) + xsy.datablock.to_csv( + os.path.join(out_path, 'datablock.csv'), + index=False + ) + xsy.defs.to_csv( + os.path.join(out_path, 'defs.csv'), + index=False + ) + xsy.datablock_expanded.to_csv( + os.path.join(out_path, 'expanded.csv'), + index=False + )