Remove test code from main module.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import typing
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from importlib import resources
|
from importlib import resources
|
||||||
|
|
||||||
@@ -50,12 +51,14 @@ class XSY():
|
|||||||
self.def_paths: list[os.path] = [
|
self.def_paths: list[os.path] = [
|
||||||
os.path.join(dir_path, def_path) for def_path in def_paths
|
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:
|
def __fspath__(self) -> os.path:
|
||||||
return self.xsy_path
|
return self.xsy_path
|
||||||
|
|
||||||
def expand_datablock(self) -> DataFrame:
|
def _expand_datablock(self) -> DataFrame:
|
||||||
# Consider df.explode here.
|
# Consider df.explode here.
|
||||||
self.datablock = self.datablock.set_index('type').join(
|
self.datablock = self.datablock.set_index('type').join(
|
||||||
self.defs.set_index('parent'),
|
self.defs.set_index('parent'),
|
||||||
@@ -65,89 +68,33 @@ class XSY():
|
|||||||
)
|
)
|
||||||
return self.datablock
|
return self.datablock
|
||||||
|
|
||||||
def read(self) -> DataFrame:
|
# def _read(self) -> DataFrame:
|
||||||
"""Read *and* expand the dataBlock using the definitions within the XSY
|
# """Read *and* expand the dataBlock using the definitions within the XSY
|
||||||
file and from the external definition files, if provided.
|
# file and from the external definition files, if provided.
|
||||||
"""
|
# """
|
||||||
pass
|
# pass
|
||||||
|
|
||||||
def read_datablock(
|
def _read_datablock(self) -> DataFrame:
|
||||||
self,
|
|
||||||
xsy_path = None,
|
|
||||||
ss_path = None,
|
|
||||||
) -> DataFrame:
|
|
||||||
"""Read the dataBlock of an XSY file into a dataframe."""
|
"""Read the dataBlock of an XSY file into a dataframe."""
|
||||||
if xsy_path is None:
|
with (
|
||||||
xsy_path = self.xsy_path
|
open(self.xsy_path) as xsy_file,
|
||||||
if ss_path is None:
|
open(self.db_ss_path) as ss_file
|
||||||
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)
|
self.datablock = pd.read_xml(xsy_file, stylesheet=ss_file)
|
||||||
return self.datablock
|
return self.datablock
|
||||||
|
|
||||||
def read_defs(
|
def _read_defs(self) -> DataFrame:
|
||||||
self,
|
|
||||||
xsy_path = None,
|
|
||||||
def_paths = None,
|
|
||||||
ss_path = None,
|
|
||||||
) -> DataFrame:
|
|
||||||
"""Read the DDTSource nodes of an XSY file into a dataframe."""
|
"""Read the DDTSource nodes of an XSY file into a dataframe."""
|
||||||
if xsy_path is None:
|
self.def_paths.append(self.xsy_path)
|
||||||
xsy_path = self.xsy_path
|
defs = DataFrame()
|
||||||
if def_paths is None:
|
with open(self.ddt_ss_path) as ss_file:
|
||||||
def_paths = self.def_paths
|
for def_path in self.def_paths:
|
||||||
if ss_path is None:
|
with open(def_path) as def_file:
|
||||||
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)
|
def_df = pd.read_xml(def_file, stylesheet = ss_file)
|
||||||
self.defs = pd.concat([self.defs, def_df])
|
defs = pd.concat([defs, def_df])
|
||||||
return self.defs
|
return defs
|
||||||
|
|
||||||
def write(self, ):
|
def write(self, ):
|
||||||
"""Write dataframe to XSY file."""
|
"""Write dataframe to XSY file."""
|
||||||
pass
|
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)
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
from pandas import DataFrame
|
from pandas import DataFrame
|
||||||
|
|
||||||
from AuDaGe.formats.controlexpert.xsy import XSY
|
from AuDaGe.formats.controlexpert.xsy import XSY
|
||||||
|
|
||||||
def test_init():
|
def test_init():
|
||||||
assert isinstance(XSY("tests/data/hsps.xsy"), XSY)
|
assert isinstance(XSY("tests/data/hsps.xsy"), XSY)
|
||||||
|
|
||||||
def test_read_datablock():
|
def test_export():
|
||||||
xsy = XSY("tests/data/hsps.xsy")
|
xsy = XSY("tests/data/hsps.xsy")
|
||||||
assert isinstance(xsy.read_datablock(), DataFrame)
|
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)
|
||||||
|
|||||||
Reference in New Issue
Block a user