diff --git a/src/AuDaGe/formats/controlexpert/xsy.py b/src/AuDaGe/formats/controlexpert/xsy.py index 6c95982..ab3664a 100644 --- a/src/AuDaGe/formats/controlexpert/xsy.py +++ b/src/AuDaGe/formats/controlexpert/xsy.py @@ -1,5 +1,6 @@ import os import sys +import typing from collections.abc import Sequence from importlib import resources @@ -50,12 +51,14 @@ class XSY(): self.def_paths: list[os.path] = [ os.path.join(dir_path, def_path) for def_path in def_paths ] - self.defs: DataFrame = DataFrame() + self.datablock = self._read_datablock() + self.defs: DataFrame = self._read_defs() + def __fspath__(self) -> os.path: return self.xsy_path - def expand_datablock(self) -> DataFrame: + def _expand_datablock(self) -> DataFrame: # Consider df.explode here. self.datablock = self.datablock.set_index('type').join( self.defs.set_index('parent'), @@ -65,89 +68,33 @@ class XSY(): ) 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(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: + def _read_datablock(self) -> 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: + with ( + 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 - def read_defs( - self, - xsy_path = None, - def_paths = None, - ss_path = None, - ) -> DataFrame: + def _read_defs(self) -> 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 + self.def_paths.append(self.xsy_path) + defs = 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 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) - diff --git a/tests/test_xsy.py b/tests/test_xsy.py index 520bc62..adc2c37 100644 --- a/tests/test_xsy.py +++ b/tests/test_xsy.py @@ -1,9 +1,14 @@ +import os + from pandas import DataFrame + from AuDaGe.formats.controlexpert.xsy import XSY def test_init(): assert isinstance(XSY("tests/data/hsps.xsy"), XSY) -def test_read_datablock(): +def test_export(): xsy = XSY("tests/data/hsps.xsy") - assert isinstance(xsy.read_datablock(), DataFrame) \ No newline at end of file + 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)