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)