Add placeholder files for the various import and export formats.

This commit is contained in:
Silcantar
2026-09-14 14:42:07 -05:00
parent 1d61db067f
commit 50a81c3a5c
8 changed files with 198 additions and 151 deletions

View File

@@ -9,6 +9,7 @@ with a GUI application possibly to come later.
- In Development:
- Schneider Electric ControlExpert XSY
- Planned:
- Schneider Electric ControlExpert TSV (TXT)
- Allen-Bradley CSV
- HMI/SCADA databases:
@@ -19,8 +20,8 @@ with a GUI application possibly to come later.
- PI Historian XLSX
## Dataframe format
| name | parent | type | address | bitrank | save | comment |
|:----:|:------:|:----:|:-------:|:-------:|:----:|:-------:|
| name | [parent] | type | address | [bitrank] | [save] | comment |
|:----:|:--------:|:----:|:-------:|:---------:|:------:|:-------:|
## Dependencies
- pandas

View File

View File

@@ -1,20 +1,103 @@
import os
from collections.abc import Sequence
from importlib import resources
from collections.abc import Iterable
from enum import StrEnum
from timeit import default_timer as timer
from math import ceil, floor
import pandas as pd
from pandas import DataFrame
import AuDaGe.iec
from AuDaGe.audage_frame import AuDaGeFrame
from AuDaGe.format import Format
from AuDaGe.iec import WORD_LENGTH, DataTypes, iec_definitions
class XSY_Block(AuDaGeFrame):
"""Base class for DataBlock and Definitions that handles shared attributes.
"""
_stylesheet_path = None
def __init__(
self,
xsy_path: os.path,
stylesheet_path: os.path
):
"""Load an XSY file into a dataframe using the specified stylesheet."""
self.stylesheet_path = stylesheet_path
with (
open(xsy_path) as xsy_file,
open(self.stylesheet_path) as stylesheet_file
):
self.dataframe = pd.read_xml(xsy_file, stylesheet=stylesheet_file)
@property
def stylesheet_path(self) -> os.path:
return self._stylesheet_path
@stylesheet_path.setter
def stylesheet_path(self, path: os.path):
if os.path.isabs(path):
self._stylesheet_path = path
else:
self._stylesheet_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
path
)
class DataBlock(XSY_Block):
""""""
DEFAULT_SS_PATH = "assets/flatten_datablock.xslt"
def __init__(
self,
xsy_path: os.path,
stylesheet_path: os.path = DEFAULT_SS_PATH
):
"""Read the dataBlock of an XSY file into a DataFrame."""
super().__init__(xsy_path, stylesheet_path)
def expand(self, definitions: Definitions):
"""Expand the UDTs in the dataBlock using the specified Definitions."""
pass
class Definitions(XSY_Block):
""""""
DEFAULT_SS_PATH = "assets/flatten_ddtsource.xslt"
DEFAULT_BUILTINS_PATH = "assets/builtins.xsy"
_builtins_path = None
def __init__(
self,
xsy_path: os.path,
stylesheet_path: os.path = DEFAULT_SS_PATH,
builtins_path: os.path = DEFAULT_BUILTINS_PATH
):
"""Read the DDTSource nodes of an XSY file into a DataFrame."""
self.builtins_path = builtins_path
super().__init__(xsy_path, stylesheet_path)
@property
def builtins_path(self) -> os.path:
return self._builtins_path
@builtins_path.setter
def builtins_path(self, path: os.path):
if os.path.isabs(path):
self._builtins_path = path
else:
self._builtins_path = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
path
)
def calculate_offsets(self):
"""Calculate the memory offset of each UDT member from the root address
of the UDT.
"""
pass
class DefaultPath(StrEnum):
PACKAGE = "AuDaGe.formats.controlexpert.xsy"
DB_SS = "assets/flatten_datablock.xslt"
DDT_SS = "assets/flatten_ddtsource.xslt"
BUILTINS = "assets/builtins.xsy"
class XSY(Format):
"""Schneider Electric EcoStruxure Control Expert variable exchange file
@@ -41,165 +124,128 @@ class XSY(Format):
def __init__(
self,
def_paths: Sequence[os.path] = [],
def_paths: Iterable[os.path] = [],
builtins_path: os.path = None,
db_ss_path: os.path = None,
ddt_ss_path: os.path = None
):
self.defs = None
self.dataBlock_stylesheet = db_ss_path
self.definition_stylesheet = ddt_ss_path
self.builtins = builtins_path
# self.dataBlock_stylesheet = db_ss_path
# self.definition_stylesheet = ddt_ss_path
# self.builtins = builtins_path
self.definition_paths = def_paths
@property
def builtins(self) -> os.path[str]:
return self._builtins
# @property
# def builtins(self) -> os.path[str]:
# return self._builtins
@builtins.setter
def builtins(
self,
path: os.PathLike = None
):
if path is None:
path = DefaultPath.BUILTINS
self._builtins = resources.files(DefaultPath.PACKAGE).joinpath(path)
@property
def dataBlock_stylesheet(self) -> os.path[str]:
return self._db_ss_path
@dataBlock_stylesheet.setter
def dataBlock_stylesheet(
self,
path: os.Pathlike = None
):
if path is None:
path = DefaultPath.DB_SS
self._db_ss_path = resources.files(DefaultPath.PACKAGE).joinpath(path)
@property
def definition_stylesheet(self) -> os.path[str]:
return self._ddt_ss_path
@definition_stylesheet.setter
def definition_stylesheet(
self,
path: os.PathLike = None
):
if path is None:
path = DefaultPath.DDT_SS
self._ddt_ss_path = resources.files(DefaultPath.PACKAGE).joinpath(path)
@property
def definition_paths(self) -> set[os.path[str]]:
return self._definition_paths
@definition_paths.setter
def definition_paths(
self,
paths: (os.PathLike | Sequence[os.PathLike]) = set()
):
paths_set = set(paths)
try:
self._definition_paths.add(paths_set)
except AttributeError:
self._definition_paths = paths_set
self.refresh_definitions()
# @builtins.setter
# def builtins(
# self,
# path: os.PathLike = None
# ):
# if path is None:
# path = DEFAULT_BUILTINS_PATH
# # self._builtins = resources.files(DefaultPath.PACKAGE).joinpath(path)
# self._builtins = os.path.join(os.path.dirname(__file__), path)
def read(
self,
xsy_path: os.path,
expand: bool = True
) -> tuple[DataFrame, DataFrame]:
"""Read the dataBlock of an XSY file into a dataframe."""
with (
open(xsy_path) as xsy_file,
open(self.dataBlock_stylesheet) as stylesheet
):
datablock = pd.read_xml(xsy_file, stylesheet=stylesheet)
with (
open(xsy_path) as xsy_file,
open(self.definition_stylesheet) as stylesheet
):
definitions = pd.read_xml(xsy_file, stylesheet=stylesheet)
if expand:
return (self.expand_datablock(datablock, definitions), definitions)
else:
return (datablock, definitions)
def refresh_definitions(self):
defs: list[DataFrame] = [AuDaGe.iec.iec_definitions()]
definition_paths = self._definition_paths | {self.builtins}
for path in definition_paths:
with (
open(path) as def_file,
open(self.definition_stylesheet) as stylesheet
):
defs.append(pd.read_xml(def_file, stylesheet=stylesheet))
# defs.append(AuDaGe.iec.iec_definitions())
self.definitions = pd.concat(defs)
"""Read the dataBlock and definitions of an XSY file into a dataframe.
"""
self.datablock = DataBlock(xsy_path)
self.definitions = Definitions(xsy_path)
# if not expand:
return (self.datablock, self.definitions)
def write(self):
"""Write dataframe to XSY file."""
raise NotImplementedError()
def expand_datablock(
self,
datablock: DataFrame,
definitions: DataFrame = None
) -> DataFrame:
start = timer()
if definitions is None:
definitions = self.definitions
else:
definitions = self._calculate_offsets(
pd.concat([definitions, self.definitions])
)
print(f"Calculate Offsets: {timer() - start}")
datablock_expanded = datablock.join(
definitions,
how="cross",
rsuffix="_def"
raise NotImplementedError(
"Writing to XSY is not currently (or probably ever) implemented."
)
print(f"Cross join: {timer() - start}")
datablock_queried = datablock_expanded[
datablock_expanded.type == datablock_expanded.parent_def
]
print(f"Query: {timer() - start}")
return datablock_queried
def _calculate_offsets(
self,
definitions: DataFrame
) -> DataFrame:
unique_parents = definitions.parent.unique()
for parent in unique_parents:
definitions[definitions.parent == parent]
return definitions
# def read_datablock(self, xsy_path: os.path) -> DataFrame:
# """Read the dataBlock of an XSY file into a DataFrame."""
# with (
# open(xsy_path) as xsy_file,
# open(self.dataBlock_stylesheet) as stylesheet
# ):
# return pd.read_xml(xsy_file, stylesheet=stylesheet)
# def read_definitions(self, xsy_path: os.path) -> DataFrame:
# """Read the DDTSource nodes from an XSY file into a DataFrame."""
# with (
# open(xsy_path) as xsy_file,
# open(self.definition_stylesheet) as stylesheet
# ):
# return pd.read_xml(xsy_file, stylesheet=stylesheet)
# def _expand_datablock(
# self,
# datablock: DataFrame,
# definitions: DataFrame = None
# ) -> DataFrame:
# start = timer()
# if definitions is None:
# definitions = self.definitions
# # else:
# # definitions = self._calculate_offsets(
# # pd.concat([definitions, self.definitions])
# # )
# # print(f"Calculate Offsets: {timer() - start}")
# datablock_expanded = datablock.join(
# definitions,
# how="cross",
# rsuffix="_def"
# )
# print(f"Cross join: {timer() - start}")
# datablock_queried = datablock_expanded[
# datablock_expanded.type == datablock_expanded.parent_def
# ]
# print(f"Query: {timer() - start}")
# return datablock_queried
if __name__ == "__main__":
start = timer()
xsy = XSY()
(datablock, definitions) = xsy.read("tests/assets/rowlett2.xsy", expand=False)
out_path = "tests/output"
datablock.to_csv(
os.path.join(out_path, "datablock.csv"),
index=False
)
definitions.to_csv(
os.path.join(out_path, "defs.csv"),
index=False
)
xsy.definitions.to_csv(
os.path.join(out_path, "builtins.csv"),
index=False
)
xsy.expand_datablock(datablock, definitions).to_csv(
os.path.join(out_path, "expanded.csv"),
index=False
)
end = timer()
print(end - start)
# def _iterate_parents(self) -> DataFrame:
# unique_parents = definitions.parent.unique()
# definitions_list: list[dict] = []
# for parent in unique_parents:
# children = definitions[definitions.parent == parent]
# definitions_list.extend(self._calculate_offsets(children))
# def _calculate_offsets(
# self,
# children: DataFrame
# ) -> list[dict]:
# offset: float = 0
# for child in children.itertuples():
# if offset < 0:
# raise ValueError("Offset is less than zero!")
# length = DataTypes[child.type]
# if length > WORD_LENGTH / 2 and offset > floor(offset):
# offset = ceil(offset)
# child_dict = child._asdict()
# child_dict["address"] = offset
# definitions_list.append(child_dict)
# if "BOOLS" in child.name:
# pass
# elif length > WORD_LENGTH / 2:
# offset += ceil(length / WORD_LENGTH)
# else:
# offset += length / 100
# if offset - floor(offset) >= WORD_LENGTH / 100:
# offset = ceil(offset)
# return pd.DataFrame(definitions_list)
# def _refresh_definitions(self):
# defs: list[DataFrame] = [iec_definitions()]
# definition_paths = self._definition_paths | {self.builtins}
# for path in definition_paths:
# with (
# open(path) as def_file,
# open(self.definition_stylesheet) as stylesheet
# ):
# defs.append(pd.read_xml(def_file, stylesheet=stylesheet))
# self.definitions = pd.concat(defs)

View File

View File

View File

View File