From 444ce662e36d4f652b5bbcc1f1a9443927777dca Mon Sep 17 00:00:00 2001 From: Joshua Lucas Date: Fri, 30 Jan 2026 16:38:48 -0600 Subject: [PATCH] - Create XSY-handling class. - Finish code for reading XSY files. - Start on code for expanding UDTs in XSY. --- formats/controlexpert/read_xsy.py | 38 -------- formats/controlexpert/xsy.py | 146 ++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+), 38 deletions(-) delete mode 100644 formats/controlexpert/read_xsy.py create mode 100644 formats/controlexpert/xsy.py diff --git a/formats/controlexpert/read_xsy.py b/formats/controlexpert/read_xsy.py deleted file mode 100644 index f07f53e..0000000 --- a/formats/controlexpert/read_xsy.py +++ /dev/null @@ -1,38 +0,0 @@ -import pandas as pd -import sys - -from iec import datatypes, ElementaryVariable, UDT -from io import StringIO -from lxml import etree -from pandas import DataFrame - -path = 'D:/OneDrive - Prime Controls/Repositories/AuDaGe/formats/controlexpert/' - -def read_xsy(xsypath, sspath, outpath) -> DataFrame: - """Read an XSY file into a dataframe.""" - with open(sspath) as ssfile: - ssxml = etree.XML(ssfile.read()) - sstransform = etree.XSLT(ssxml) - with open(xsypath) as xsyfile: - xsyxml = etree.parse(xsyfile) - xsy = sstransform(xsyxml) - xsy.write_output(outpath + '.xml') - df = pd.read_xml(StringIO(str(xsy))) - # print(df) - df.to_csv(outpath + '.csv', index=False) - -if __name__ == '__main__': - try: - xsypath = sys.argv[1] - except: - xsypath = 'builtins.xsy' - try: - sspath = sys.argv[2] - except: - sspath = 'flatten_xsy.xslt' - try: - outpath = sys.argv[3] - except: - outpath = 'test/test' - - read_xsy(path + xsypath, path + sspath, path + outpath) \ No newline at end of file diff --git a/formats/controlexpert/xsy.py b/formats/controlexpert/xsy.py new file mode 100644 index 0000000..0d73dcf --- /dev/null +++ b/formats/controlexpert/xsy.py @@ -0,0 +1,146 @@ +import os +import pandas as pd +import sys + +from collections.abc import Iterable +from iec import datatypes, ElementaryVariable, UDT +from pandas import DataFrame + +class XSY(): + """Schneider Electric EcoStruxure Control Expert variable exchange file + (XSY). + + Includes methods for reading and (to do) ~~writing~~ said files. + + Parameters + ---------- + xsy_path : pathlike + Path (relative or absolute) to the 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. + def_paths : list of pathlike (optional) + List of additional XSY files to read UDT (DDT) definitions from. + db_ss_path: pathlike (optional) + Path to XSLT stylesheet to flatten the XSY file and extract the + dataBlock node before importing into pandas. + ddt_ss_path: pathlike (optional) + 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 = '', + def_paths: Iterable[os.path] = [], + db_ss_path: os.path = 'flatten_datablock.xslt', + ddt_ss_path: os.path = '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 = os.path.join(dir_path, db_ss_path) + self.ddt_ss_path: os.path = os.path.join(dir_path, ddt_ss_path) + self.def_paths: list[os.path] = [ + os.path.join(dir_path, def_path) for def_path in def_paths + ] + self.defs: DataFrame = DataFrame() + + 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', + ) + 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 + + def read_datablock( + self, + xsy_path = None, + ss_path = None, + ) -> DataFrame: + """Read the dataBlock of an XSY file into a dataframe.""" + if xsy_path is None: + xsy_path = self.xsy_path + if ss_path is None: + ss_path = self.db_ss_path + with open(xsy_path) as xsy_file, open(ss_path) as ss_file: + self.datablock = pd.read_xml(xsy_file, stylesheet=ss_file) + return self.datablock + + def read_defs( + self, + xsy_path = None, + def_paths = None, + ss_path = None, + ) -> DataFrame: + """Read the DDTSource nodes of an XSY file into a dataframe.""" + if xsy_path is None: + xsy_path = self.xsy_path + if def_paths is None: + def_paths = self.def_paths + if ss_path is None: + ss_path = self.ddt_ss_path + def_paths.append(xsy_path) + for def_path in def_paths: + with open(def_path) as def_file, open(ss_path) as ss_file: + def_df = pd.read_xml(def_file, stylesheet = ss_file) + self.defs = pd.concat([self.defs, def_df]) + return self.defs + + def write(self, ): + """Write dataframe to XSY file.""" + pass + +# Run this module as a script to test. +if __name__ == '__main__': + # Argument 1: path to XSY + try: + xsy_path = sys.argv[1] + except: + xsy_path = 'test/hsps.xsy' + # Argument 2: path to dataBlock stylesheet. + try: + db_ss_path = sys.argv[2] + except: + db_ss_path = 'flatten_datablock.xslt' + # Argument 3: path to DDTSource stylesheet. + try: + ddt_ss_path = sys.argv[3] + except: + ddt_ss_path = 'flatten_ddtsource.xslt' + # Argument 4: path to other definition files + try: + def_paths = [sys.argv[3].split(',')] + except: + def_paths = ['builtins.xsy'] + # Argument 5: path to output file. + try: + out_path = sys.argv[5] + except: + out_path = 'test/test' + dir_path: os.path = ('D:/OneDrive - Prime Controls/Repositories' + '/AuDaGe/formats/controlexpert/') + xsy = XSY( + xsy_path, + dir_path, + def_paths = def_paths, + db_ss_path = db_ss_path, + ddt_ss_path = ddt_ss_path, + ) + xsy.read_datablock() + xsy.read_defs() + xsy.expand_datablock() + xsy.datablock.to_csv(out_path + '_db.csv', index=False) + xsy.defs.to_csv(out_path + '_ddt.csv', index=False) +