Rewrite XSY module.
This commit is contained in:
@@ -1,16 +1,21 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
import typing
|
|
||||||
from collections.abc import Sequence
|
from collections.abc import Sequence
|
||||||
from importlib import resources
|
from importlib import resources
|
||||||
|
from enum import StrEnum
|
||||||
|
from timeit import default_timer as timer
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
from pandas import DataFrame
|
from pandas import DataFrame
|
||||||
|
|
||||||
import AuDaGe
|
import AuDaGe.iec
|
||||||
from AuDaGe.iec import DataTypes, ElementaryVariable, UDT
|
|
||||||
from AuDaGe.format import Format
|
from AuDaGe.format import Format
|
||||||
|
|
||||||
|
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
|
||||||
(XSY).
|
(XSY).
|
||||||
@@ -36,118 +41,165 @@ class XSY(Format):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
dir_path: os.path = "",
|
|
||||||
def_paths: Sequence[os.path] = [],
|
def_paths: Sequence[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.dir_path = ""
|
|
||||||
self.def_paths = []
|
|
||||||
self.defs = None
|
self.defs = None
|
||||||
self._set_fields(
|
self.dataBlock_stylesheet = db_ss_path
|
||||||
dir_path=dir_path,
|
self.definition_stylesheet = ddt_ss_path
|
||||||
def_paths=def_paths,
|
self.builtins = builtins_path
|
||||||
builtins_path=builtins_path,
|
self.definition_paths = def_paths
|
||||||
db_ss_path=db_ss_path,
|
|
||||||
ddt_ss_path=ddt_ss_path
|
@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()
|
||||||
|
|
||||||
def read(
|
def read(
|
||||||
self,
|
self,
|
||||||
xsy_path: os.path,
|
xsy_path: os.path,
|
||||||
expand: bool = True,
|
expand: bool = True
|
||||||
dir_path: os.path = "",
|
) -> tuple[DataFrame, DataFrame]:
|
||||||
def_paths: Sequence[os.path] = [],
|
|
||||||
db_ss_path: os.path = None,
|
|
||||||
ddt_ss_path: os.path = None,
|
|
||||||
) -> DataFrame:
|
|
||||||
"""Read the dataBlock of an XSY file into a dataframe."""
|
"""Read the dataBlock of an XSY file into a dataframe."""
|
||||||
self._set_fields(
|
|
||||||
dir_path=dir_path,
|
|
||||||
def_paths=def_paths,
|
|
||||||
db_ss_path=db_ss_path,
|
|
||||||
ddt_ss_path=ddt_ss_path
|
|
||||||
)
|
|
||||||
xsy_path = os.path.join(dir_path, xsy_path)
|
|
||||||
with (
|
with (
|
||||||
open(xsy_path) as xsy_file,
|
open(xsy_path) as xsy_file,
|
||||||
open(self.db_ss_path) as ss_file
|
open(self.dataBlock_stylesheet) as stylesheet
|
||||||
):
|
):
|
||||||
datablock = pd.read_xml(xsy_file, stylesheet=ss_file)
|
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:
|
if expand:
|
||||||
return self.expand_datablock(datablock)
|
return (self.expand_datablock(datablock, definitions), definitions)
|
||||||
else:
|
else:
|
||||||
return datablock
|
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()
|
||||||
|
|
||||||
def expand_datablock(self, datablock: DataFrame) -> DataFrame:
|
def expand_datablock(
|
||||||
datablock.set_index("type")
|
|
||||||
datablock_expanded = (
|
|
||||||
datablock
|
|
||||||
.join(
|
|
||||||
self.defs,
|
|
||||||
how="cross",
|
|
||||||
rsuffix="_def"
|
|
||||||
)
|
|
||||||
.query(
|
|
||||||
"type==parent_def"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
return datablock_expanded
|
|
||||||
|
|
||||||
def _read_defs(self, xsy_path: os.path = None) -> DataFrame:
|
|
||||||
"""Read the DDTSource nodes of one or more XSY files into a dataframe.
|
|
||||||
"""
|
|
||||||
if xsy_path is not None:
|
|
||||||
self.def_paths.append(xsy_path)
|
|
||||||
defs: list[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.append(def_df)
|
|
||||||
return pd.concat(defs)
|
|
||||||
|
|
||||||
def _set_fields(
|
|
||||||
self,
|
self,
|
||||||
dir_path: os.path = "",
|
datablock: DataFrame,
|
||||||
def_paths: Sequence[os.path] = [],
|
definitions: DataFrame = None
|
||||||
builtins_path: os.path = None,
|
) -> DataFrame:
|
||||||
db_ss_path: os.path = None,
|
start = timer()
|
||||||
ddt_ss_path: os.path = None
|
if definitions is None:
|
||||||
):
|
definitions = self.definitions
|
||||||
# Directory
|
else:
|
||||||
if dir_path != "":
|
definitions = self._calculate_offsets(
|
||||||
self.dir_path = dir_path
|
pd.concat([definitions, self.definitions])
|
||||||
# Builtins
|
)
|
||||||
if builtins_path is None:
|
print(f"Calculate Offsets: {timer() - start}")
|
||||||
builtins_path = "data/builtins.xsy"
|
datablock_expanded = datablock.join(
|
||||||
self.def_paths.append(resources.files(
|
definitions,
|
||||||
"AuDaGe.formats.controlexpert.xsy"
|
how="cross",
|
||||||
).joinpath(builtins_path))
|
rsuffix="_def"
|
||||||
# Stylesheets
|
)
|
||||||
if db_ss_path is None:
|
print(f"Cross join: {timer() - start}")
|
||||||
db_ss_path = "data/flatten_datablock.xslt"
|
datablock_queried = datablock_expanded[
|
||||||
self.db_ss_path: os.path = resources.files(
|
datablock_expanded.type == datablock_expanded.parent_def
|
||||||
"AuDaGe.formats.controlexpert.xsy"
|
]
|
||||||
).joinpath(db_ss_path)
|
print(f"Query: {timer() - start}")
|
||||||
if ddt_ss_path is None:
|
return datablock_queried
|
||||||
ddt_ss_path = "data/flatten_ddtsource.xslt"
|
|
||||||
self.ddt_ss_path: os.path = resources.files(
|
|
||||||
"AuDaGe.formats.controlexpert.xsy"
|
|
||||||
).joinpath(ddt_ss_path)
|
|
||||||
# Definitions
|
|
||||||
if def_paths != []:
|
|
||||||
self.def_paths.extend([
|
|
||||||
os.path.join(self.dir_path, def_path)
|
|
||||||
for def_path in def_paths
|
|
||||||
])
|
|
||||||
# Update definitions.
|
|
||||||
self.defs = self._read_defs()
|
|
||||||
elif self.defs is None:
|
|
||||||
self.defs = self._read_defs()
|
|
||||||
|
|
||||||
|
def _calculate_offsets(
|
||||||
|
self,
|
||||||
|
definitions: DataFrame
|
||||||
|
) -> DataFrame:
|
||||||
|
unique_parents = definitions.parent.unique()
|
||||||
|
for parent in unique_parents:
|
||||||
|
definitions[definitions.parent == parent]
|
||||||
|
return definitions
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
"""
|
"""
|
||||||
Definitions for IEC 61131 programming paradigms.
|
Definitions for IEC 61131 programming paradigms.
|
||||||
"""
|
"""
|
||||||
|
import dataclasses
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from enum import Enum
|
from enum import Enum, IntEnum
|
||||||
|
|
||||||
|
import pandas as pd
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class DataType:
|
class DataType:
|
||||||
@@ -16,19 +19,49 @@ class DataType:
|
|||||||
self.length_bytes = length_bits // 8
|
self.length_bytes = length_bits // 8
|
||||||
self.length_words = length_bits // word_length
|
self.length_words = length_bits // word_length
|
||||||
|
|
||||||
class DataTypes(DataType, Enum):
|
# DataTypes = {
|
||||||
BOOL = 1
|
# "BOOL": 1,
|
||||||
BYTE = 8
|
# "BYTE": 8,
|
||||||
CHAR = 8
|
# "CHAR": 8,
|
||||||
INT = 16
|
# "INT": 16,
|
||||||
UINT = 16
|
# "UINT": 16,
|
||||||
WORD = 16
|
# "WORD": 16,
|
||||||
DATE = 32
|
# "DATE": 32,
|
||||||
DT = 32
|
# "DT": 32,
|
||||||
DINT = 32
|
# "DINT": 32,
|
||||||
|
# "DWORD": 32,
|
||||||
|
# "REAL": 32,
|
||||||
|
# "TIME": 32,
|
||||||
|
# "UDINT": 32
|
||||||
|
# }
|
||||||
|
# class DataTypes(DataType, Enum):
|
||||||
|
# BOOL = DataType(1)
|
||||||
|
# BYTE = DataType(8)
|
||||||
|
# CHAR = DataType(8)
|
||||||
|
# INT = DataType(16)
|
||||||
|
# UINT = DataType(16)
|
||||||
|
# WORD = DataType(16)
|
||||||
|
# DATE = DataType(32)
|
||||||
|
# DT = DataType(32)
|
||||||
|
# DINT = DataType(32)
|
||||||
|
# DWORD = DataType(32)
|
||||||
|
# REAL = DataType(32)
|
||||||
|
# TIME = DataType(32)
|
||||||
|
# UDINT = DataType(32)
|
||||||
|
|
||||||
|
class DataTypes(int, Enum):
|
||||||
|
BOOL = 1
|
||||||
|
BYTE = 8
|
||||||
|
CHAR = 8
|
||||||
|
INT = 16
|
||||||
|
UINT = 16
|
||||||
|
WORD = 16
|
||||||
|
DATE = 32
|
||||||
|
DT = 32
|
||||||
|
DINT = 32
|
||||||
DWORD = 32
|
DWORD = 32
|
||||||
REAL = 32
|
REAL = 32
|
||||||
TIME = 32
|
TIME = 32
|
||||||
UDINT = 32
|
UDINT = 32
|
||||||
|
|
||||||
class ElementaryVariable:
|
class ElementaryVariable:
|
||||||
@@ -55,4 +88,23 @@ class UDT:
|
|||||||
):
|
):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.comment = comment
|
self.comment = comment
|
||||||
self.variables = variables
|
self.variables = variables
|
||||||
|
|
||||||
|
|
||||||
|
def iec_definitions() -> pd.DataFrame:
|
||||||
|
empty = pd.Series([pd.NA] * len(DataTypes.__members__))
|
||||||
|
return pd.DataFrame(
|
||||||
|
data={
|
||||||
|
"name": empty,
|
||||||
|
"parent": DataTypes.__members__,
|
||||||
|
"type": empty,
|
||||||
|
"address": empty,
|
||||||
|
"bitrank": empty,
|
||||||
|
"save": empty,
|
||||||
|
"comment": empty
|
||||||
|
},
|
||||||
|
# index =
|
||||||
|
)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
iec_definitions().to_csv("tests/output/iec_definitions.csv")
|
||||||
@@ -10,16 +10,17 @@ def test_init():
|
|||||||
def test_export():
|
def test_export():
|
||||||
xsy = XSY()
|
xsy = XSY()
|
||||||
datablock = xsy.read("tests/data/hsps.xsy", expand=False)
|
datablock = xsy.read("tests/data/hsps.xsy", expand=False)
|
||||||
|
print(xsy.def_paths)
|
||||||
out_path = "tests/output"
|
out_path = "tests/output"
|
||||||
datablock.to_csv(
|
datablock.to_csv(
|
||||||
os.path.join(out_path, 'datablock.csv'),
|
os.path.join(out_path, "datablock.csv"),
|
||||||
index=False
|
index=False
|
||||||
)
|
)
|
||||||
xsy.defs.to_csv(
|
xsy.defs.to_csv(
|
||||||
os.path.join(out_path, 'defs.csv'),
|
os.path.join(out_path, "defs.csv"),
|
||||||
index=False
|
index=False
|
||||||
)
|
)
|
||||||
xsy.expand_datablock(datablock).to_csv(
|
xsy.expand_datablock(datablock).to_csv(
|
||||||
os.path.join(out_path, 'expanded.csv'),
|
os.path.join(out_path, "expanded.csv"),
|
||||||
index=False
|
index=False
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user