Add placeholder files for the various import and export formats.
This commit is contained in:
@@ -9,6 +9,7 @@ with a GUI application possibly to come later.
|
|||||||
- In Development:
|
- In Development:
|
||||||
- Schneider Electric ControlExpert XSY
|
- Schneider Electric ControlExpert XSY
|
||||||
- Planned:
|
- Planned:
|
||||||
|
- Schneider Electric ControlExpert TSV (TXT)
|
||||||
- Allen-Bradley CSV
|
- Allen-Bradley CSV
|
||||||
|
|
||||||
- HMI/SCADA databases:
|
- HMI/SCADA databases:
|
||||||
@@ -19,8 +20,8 @@ with a GUI application possibly to come later.
|
|||||||
- PI Historian XLSX
|
- PI Historian XLSX
|
||||||
|
|
||||||
## Dataframe format
|
## Dataframe format
|
||||||
| name | parent | type | address | bitrank | save | comment |
|
| name | [parent] | type | address | [bitrank] | [save] | comment |
|
||||||
|:----:|:------:|:----:|:-------:|:-------:|:----:|:-------:|
|
|:----:|:--------:|:----:|:-------:|:---------:|:------:|:-------:|
|
||||||
|
|
||||||
## Dependencies
|
## Dependencies
|
||||||
- pandas
|
- pandas
|
||||||
0
src/AuDaGe/formats/controlexpert/tsv.py
Normal file
0
src/AuDaGe/formats/controlexpert/tsv.py
Normal file
@@ -1,20 +1,103 @@
|
|||||||
import os
|
import os
|
||||||
from collections.abc import Sequence
|
from collections.abc import Iterable
|
||||||
from importlib import resources
|
|
||||||
from enum import StrEnum
|
from enum import StrEnum
|
||||||
from timeit import default_timer as timer
|
from math import ceil, floor
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from pandas import DataFrame
|
from pandas import DataFrame
|
||||||
|
|
||||||
import AuDaGe.iec
|
from AuDaGe.audage_frame import AuDaGeFrame
|
||||||
from AuDaGe.format import Format
|
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):
|
class XSY(Format):
|
||||||
"""Schneider Electric EcoStruxure Control Expert variable exchange file
|
"""Schneider Electric EcoStruxure Control Expert variable exchange file
|
||||||
@@ -41,165 +124,128 @@ class XSY(Format):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
def_paths: Sequence[os.path] = [],
|
def_paths: Iterable[os.path] = [],
|
||||||
builtins_path: os.path = None,
|
builtins_path: os.path = None,
|
||||||
db_ss_path: os.path = None,
|
db_ss_path: os.path = None,
|
||||||
ddt_ss_path: os.path = None
|
ddt_ss_path: os.path = None
|
||||||
):
|
):
|
||||||
self.defs = None
|
self.defs = None
|
||||||
self.dataBlock_stylesheet = db_ss_path
|
# self.dataBlock_stylesheet = db_ss_path
|
||||||
self.definition_stylesheet = ddt_ss_path
|
# self.definition_stylesheet = ddt_ss_path
|
||||||
self.builtins = builtins_path
|
# self.builtins = builtins_path
|
||||||
self.definition_paths = def_paths
|
self.definition_paths = def_paths
|
||||||
|
|
||||||
@property
|
# @property
|
||||||
def builtins(self) -> os.path[str]:
|
# def builtins(self) -> os.path[str]:
|
||||||
return self._builtins
|
# return self._builtins
|
||||||
|
|
||||||
@builtins.setter
|
# @builtins.setter
|
||||||
def builtins(
|
# def builtins(
|
||||||
self,
|
# self,
|
||||||
path: os.PathLike = None
|
# path: os.PathLike = None
|
||||||
):
|
# ):
|
||||||
if path is None:
|
# if path is None:
|
||||||
path = DefaultPath.BUILTINS
|
# path = DEFAULT_BUILTINS_PATH
|
||||||
self._builtins = resources.files(DefaultPath.PACKAGE).joinpath(path)
|
# # self._builtins = resources.files(DefaultPath.PACKAGE).joinpath(path)
|
||||||
|
# self._builtins = os.path.join(os.path.dirname(__file__), 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()
|
|
||||||
|
|
||||||
def read(
|
def read(
|
||||||
self,
|
self,
|
||||||
xsy_path: os.path,
|
xsy_path: os.path,
|
||||||
expand: bool = True
|
expand: bool = True
|
||||||
) -> tuple[DataFrame, DataFrame]:
|
) -> tuple[DataFrame, DataFrame]:
|
||||||
"""Read the dataBlock of an XSY file into a dataframe."""
|
"""Read the dataBlock and definitions of an XSY file into a dataframe.
|
||||||
with (
|
"""
|
||||||
open(xsy_path) as xsy_file,
|
self.datablock = DataBlock(xsy_path)
|
||||||
open(self.dataBlock_stylesheet) as stylesheet
|
self.definitions = Definitions(xsy_path)
|
||||||
):
|
# if not expand:
|
||||||
datablock = pd.read_xml(xsy_file, stylesheet=stylesheet)
|
return (self.datablock, self.definitions)
|
||||||
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)
|
|
||||||
|
|
||||||
def write(self):
|
def write(self):
|
||||||
"""Write dataframe to XSY file."""
|
"""Write dataframe to XSY file."""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError(
|
||||||
|
"Writing to XSY is not currently (or probably ever) implemented."
|
||||||
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
|
|
||||||
|
|
||||||
def _calculate_offsets(
|
# def read_datablock(self, xsy_path: os.path) -> DataFrame:
|
||||||
self,
|
# """Read the dataBlock of an XSY file into a DataFrame."""
|
||||||
definitions: DataFrame
|
# with (
|
||||||
) -> DataFrame:
|
# open(xsy_path) as xsy_file,
|
||||||
unique_parents = definitions.parent.unique()
|
# open(self.dataBlock_stylesheet) as stylesheet
|
||||||
for parent in unique_parents:
|
# ):
|
||||||
definitions[definitions.parent == parent]
|
# return pd.read_xml(xsy_file, stylesheet=stylesheet)
|
||||||
return definitions
|
|
||||||
|
|
||||||
|
# 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__":
|
# def _iterate_parents(self) -> DataFrame:
|
||||||
start = timer()
|
# unique_parents = definitions.parent.unique()
|
||||||
xsy = XSY()
|
# definitions_list: list[dict] = []
|
||||||
(datablock, definitions) = xsy.read("tests/assets/rowlett2.xsy", expand=False)
|
# for parent in unique_parents:
|
||||||
out_path = "tests/output"
|
# children = definitions[definitions.parent == parent]
|
||||||
datablock.to_csv(
|
# definitions_list.extend(self._calculate_offsets(children))
|
||||||
os.path.join(out_path, "datablock.csv"),
|
|
||||||
index=False
|
# def _calculate_offsets(
|
||||||
)
|
# self,
|
||||||
definitions.to_csv(
|
# children: DataFrame
|
||||||
os.path.join(out_path, "defs.csv"),
|
# ) -> list[dict]:
|
||||||
index=False
|
# offset: float = 0
|
||||||
)
|
# for child in children.itertuples():
|
||||||
xsy.definitions.to_csv(
|
# if offset < 0:
|
||||||
os.path.join(out_path, "builtins.csv"),
|
# raise ValueError("Offset is less than zero!")
|
||||||
index=False
|
# length = DataTypes[child.type]
|
||||||
)
|
# if length > WORD_LENGTH / 2 and offset > floor(offset):
|
||||||
xsy.expand_datablock(datablock, definitions).to_csv(
|
# offset = ceil(offset)
|
||||||
os.path.join(out_path, "expanded.csv"),
|
# child_dict = child._asdict()
|
||||||
index=False
|
# child_dict["address"] = offset
|
||||||
)
|
# definitions_list.append(child_dict)
|
||||||
end = timer()
|
# if "BOOLS" in child.name:
|
||||||
print(end - start)
|
# 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)
|
||||||
0
src/AuDaGe/formats/ftae/xml.py
Normal file
0
src/AuDaGe/formats/ftae/xml.py
Normal file
0
src/AuDaGe/formats/kepserver/__init__.py
Normal file
0
src/AuDaGe/formats/kepserver/__init__.py
Normal file
0
src/AuDaGe/formats/kepserver/csv.py
Normal file
0
src/AuDaGe/formats/kepserver/csv.py
Normal file
0
src/AuDaGe/formats/pi_historian/__init__.py
Normal file
0
src/AuDaGe/formats/pi_historian/__init__.py
Normal file
0
src/AuDaGe/formats/pi_historian/xlsx.py
Normal file
0
src/AuDaGe/formats/pi_historian/xlsx.py
Normal file
Reference in New Issue
Block a user