Add config-based database conversion.

This commit is contained in:
Joshua Lucas
2026-10-29 09:09:03 -05:00
parent 36636878bd
commit d5da5c6618
35 changed files with 8450 additions and 209 deletions

View File

@@ -0,0 +1,106 @@
# Leonard WTP Alarm Severity Tag Set Macro
$schema: ../../../src/AuDage/extensions/assets/schema.yaml
format:
name: csv
sep: " "
header: false
index: false
extension: mcr
quoting: 3 # No quoting
quotechar: null
escapechar: null
import:
filter:
columns: tagname
operations:
- operation: match
pattern: '\.AESeverity$'
columns:
command:
type: str
const: "&set"
tagname:
type: str
translate:
columns: tagname
operations:
- operation: radd
arg: $plc$.PLC.
value:
translate:
columns: tagname
operations:
- operation: replace
pattern: '.*?almPLCFault\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almBrokenLoop\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almHigh\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almHighHigh\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almLow\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almLowLow\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almOverRange\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almRateOfChange\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almUnderRange\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almFailToStrt\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almFailToStop\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almMotorFailure\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almSpeedDeviation\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almFailToClose\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almFailToOpen\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almPositionFailure\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almValveFailure\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?almPLCFault\.AESeverity$'
repl: "250"
- operation: replace
pattern: '.*?MY\.Alarm\.AESeverity$'
repl: "1000"
- operation: replace
pattern: '.*?YA\.Alarm\.AESeverity$'
repl: "1000"
- operation: replace
pattern: '.*?\.Alarm\.AESeverity$'
repl: "250"
export:
tagname:
translate:
columns: tagname
type:
const: ""
address:
const: ""
comment:
const: ""

View File

@@ -0,0 +1,139 @@
$schema: ../../../src/AuDage/extensions/assets/schema.yaml
format:
name: csv
sep: " "
header: false
index: false
extension: mcr
quoting: 3 # No quoting
quotechar: null
escapechar: null
import:
columns:
command:
type: str
const: "&set"
tagname:
type: str
translate:
columns: tagname
operations:
- operation: radd
arg: Captions.$plc$.PLC.
value:
type: str
translate:
columns: tagname
operations:
- operation: replace
pattern: '^WA3252_([A-Z]+)_\d+_(\d+[A-Z]?)_?([A-Z]+\d?)?.*'
repl: '"\1 \2 \3"'
- operation: replace
pattern: (AFD|PMP)
repl: Pump
- operation: replace
pattern: MTR
repl: Motor
- operation: replace
pattern: (FCV|FV)
repl: Valve
- operation: replace
pattern: (\wIT|TT)
repl: ""
- operation: replace
pattern: CP
repl: Control Panel
- operation: replace
pattern: FLT
repl: Filter
- operation: replace
pattern: GEN
repl: Generator
- operation: replace
pattern: NET
repl: Network Panel
- operation: replace
pattern: LSH
repl: Level Switch High
- operation: replace
pattern: (?<!U)PS
repl: Pressure Switch
- operation: replace
pattern: REC
repl: Recloser
- operation: replace
pattern: SD
repl: Smoke Detector
- operation: replace
pattern: SPD
repl: Surge Protector
- operation: replace
pattern: "[TX]FR"
repl: Transformer
- operation: replace
pattern: SI
repl: Speed
- operation: replace
pattern: FI
repl: Flow
- operation: replace
pattern: ZI
repl: Position
- operation: replace
pattern: VI
repl: "Vibration "
- operation: replace
pattern: PI
repl: Pressure
- operation: replace
pattern: MY
repl: E-Stop
- operation: replace
pattern: UA
repl: MPR Alarm
- operation: replace
pattern: UY
repl: MPR Trip
- operation: replace
pattern: MF
repl: Motor Fault
- operation: replace
pattern: LH
repl: Level High
- operation: replace
pattern: XA
repl: Alarm
- operation: replace
pattern: ZF
repl: Valve Fault
- operation: replace
pattern: WH
repl: Overtorque
- operation: replace
pattern: LL
repl: Level Low
- operation: replace
pattern: PL(?!C) # Negative lookahead: PL not followed by C
repl: Pressure Low
- operation: replace
pattern: SH
repl: Overspeed
- operation: replace
pattern: TH
repl: Temp High
- operation: replace
pattern: TL
repl: Temp Low
- operation: replace
pattern: J[AF]
repl: Power Fail
export:
tagname:
translate:
columns: tagname
type:
const: ""
address:
const: ""
comment:
const: ""

View File

@@ -0,0 +1,40 @@
$schema: ../../../src/AuDage/extensions/assets/schema.yaml
format:
name: csv
sep: " "
header: false
index: false
extension: mcr
quoting: 3 # No quoting
quotechar: null
escapechar: null
import:
columns:
command:
type: str
const: "&set"
tagname:
type: str
translate:
columns: tagname
operations:
- operation: radd
arg: Descriptions.$plc$.PLC.
value:
type: str
translate:
columns: comment
operations:
- operation: replace
pattern: "^(.*?):.*"
repl: '"\1"'
export:
tagname:
translate:
columns: tagname
type:
const: ""
address:
const: ""
comment:
const: ""

View File

@@ -0,0 +1,49 @@
$schema: ../../../src/AuDage/extensions/assets/schema.yaml
format:
name: csv
sep: " "
header: false
index: false
extension: mcr
quoting: 3 # No quoting
quotechar: null
escapechar: null
import:
columns:
command:
type: str
const: "&set"
tagname:
type: str
translate:
columns: tagname
operations:
- operation: radd
arg: Strings.$plc$.PLC.
value:
type: str
translate:
columns: tagname
operations:
- operation: replace
pattern: '.*?([A-Z])\d?\.On_Message'
repl: '"\1"'
- operation: replace
pattern: '^"H"$'
repl: '"High"'
- operation: replace
pattern: '^"L"$'
repl: '"Low"'
- operation: replace
pattern: '^"[A-Z]"$'
repl: '"Alarm"'
export:
tagname:
translate:
columns: tagname
type:
const: ""
address:
const: ""
comment:
const: ""

View File

@@ -0,0 +1,40 @@
$schema: ../../../src/AuDage/extensions/assets/schema.yaml
format:
name: csv
sep: " "
header: false
index: false
extension: mcr
quoting: 3 # No quoting
quotechar: null
escapechar: null
import:
columns:
command:
type: str
const: "&set"
tagname:
type: str
translate:
columns: tagname
operations:
- operation: radd
arg: Tagnames.$plc$.PLC.
value:
type: str
translate:
columns: tagname
operations:
- operation: replace
pattern: '(.*?)\.Tagname'
repl: '"\1"'
export:
tagname:
translate:
columns: tagname
type:
const: ""
address:
const: ""
comment:
const: ""

View File

@@ -0,0 +1,66 @@
$schema: ../../../src/AuDage/extensions/assets/schema.yaml
format:
name: csv
sep: " "
header: false
index: false
extension: mcr
quoting: 3 # No automatic quoting
quotechar: null
escapechar: null
export:
encoding: cp1252
import:
columns:
command:
type: str
const: "&set"
tagname:
type: str
translate:
columns: tagname
operations:
- operation: radd
arg: Units.$plc$.PLC.
value:
type: str
translate:
columns: tagname
operations:
- operation: replace
pattern: '^WA3252_[A-Z]+_\d+_\d+[A-Z]?_?([A-Z]+)?\d?.*'
repl: '"\1"'
- operation: replace
pattern: '"AI"'
repl: '"PPM"'
- operation: replace
pattern: '"FI"'
repl: '"GPM"'
- operation: replace
pattern: '"LI"'
repl: '"FT"'
- operation: replace
pattern: '"PI"'
repl: '"PSI"'
- operation: replace
pattern: '"SI"'
repl: '"%"'
- operation: replace
pattern: '"TI"'
repl: '"°F"'
- operation: replace
pattern: '"VI"'
repl: '"IN/S"'
- operation: replace
pattern: '("ZI"|"")'
repl: '"%"'
export:
tagname:
translate:
columns: tagname
type:
const: ""
address:
const: ""
comment:
const: ""

View File

@@ -0,0 +1,134 @@
$schema: ../../../src/AuDage/extensions/assets/schema.yaml
format: csv
import:
filter:
columns:
- name
- type
- address
columns:
Tag Name:
type: str
translate:
columns: name
operations:
# Replace any character that is not a letter, underscore, number, or
# square bracket with an underscore.
operation: replace
pattern: '[^\w\d\[\]]'
repl: _
Address:
type: float
parse:
# Add a leading zero to one-digit bit addresses, e.g. 400001.1 ->
# 400001.01.
operation: replace
pattern: '\.(\d\"?,?)$'
repl: '.0\1'
translate:
columns: address
operations:
operation: address_iec_to_modbus
Data Type:
type: str
enum:
- Boolean
- Char
- Byte
- Short
- Word
- Long
- DWord
- LongLong
- QWord
- Float
- Double
- String
translate:
columns: type
operations:
operation: type_iec_to_opcua
Respect Data Type:
type: int
enum:
- 0
- 1
const: 1
Client Access:
type: str
enum:
- R
- W
- R/W
const: R/W
Scan Rate:
type: int
const: 1000
Scaling:
type: str
enum:
- ""
- Linear
- Square Root
const: ""
Raw Low:
type: float
const: nan
Raw High:
type: float
const: nan
Scaled Low:
type: float
const: nan
Scaled High:
type: float
const: nan
Scaled Data Type:
type: str
enum:
- Boolean
- Char
- Byte
- Short
- Word
- Long
- DWord
- LongLong
- QWord
- Float
- Double
- String
const: ""
Clamp Low:
type: float
const: nan
Clamp High:
type: float
const: nan
Eng Units:
type: str
const: ""
Description:
type: str
translate:
columns: comment
Negate Value:
type: str
const: nan
export:
tagname:
translate:
columns: Tag Name
type:
translate:
columns: Data Type
operations:
operation: type_opcua_to_iec
address:
translate:
columns: Address
operations:
operation: address_modbus_to_iec
comment:
translate:
columns: Description

View File

@@ -0,0 +1,134 @@
$schema: ../../../src/AuDage/extensions/assets/schema.yaml
format:
name: csv
export:
index: false
import:
filter:
columns: tagname
operations:
operation: match
pattern: 'almPLCFault\.ALRM$'
columns:
Tag Name:
type: str
translate:
columns: tagname
operations:
# Replace with an underscore any character that is not a letter,
# underscore, number, period, or square bracket.
- operation: replace
pattern: '[^\w\d\.\[\]]'
repl: _
- operation: replace
pattern: 'almPLCFault\.ALRM'
repl: ""
- operation: add
arg: $extension$
Address:
type: str
translate:
columns: name
operations:
- operation: format
arg: 04d
- operation: radd
arg: S
Data Type:
type: str
enum:
- Boolean
- Char
- Byte
- Short
- Word
- Long
- DWord
- LongLong
- QWord
- Float
- Double
- String
const: String
Respect Data Type:
type: int
enum:
- 0
- 1
const: 1
Client Access:
type: str
enum:
- R
- W
- R/W
const: R/W
Scan Rate:
type: int
const: 100000
Scaling:
type: str
enum:
- ""
- Linear
- Square Root
const: ""
Raw Low:
type: float
const: ""
Raw High:
type: float
const: ""
Scaled Low:
type: float
const: ""
Scaled High:
type: float
const: ""
Scaled Data Type:
type: str
enum:
- Boolean
- Char
- Byte
- Short
- Word
- Long
- DWord
- LongLong
- QWord
- Float
- Double
- String
const: ""
Clamp Low:
type: float
const: ""
Clamp High:
type: float
const: ""
Eng Units:
type: str
const: ""
Description:
type: str
translate:
columns: comment
Negate Value:
type: str
const: ""
export:
tagname:
translate:
columns: Tag Name
type:
translate:
columns: Data Type
operations:
operation: type_opcua_to_iec
address:
translate:
columns: Address
comment:
translate:
columns: Description

View File

@@ -0,0 +1,134 @@
$schema: ../../../src/AuDage/extensions/assets/schema.yaml
format:
name: csv
export:
index: false
import:
filter:
columns: tagname
operations:
operation: match
pattern: 'almPLCFault\.ALRM$'
columns:
Tag Name:
type: str
translate:
columns: tagname
operations:
# Replace with an underscore any character that is not a letter,
# underscore, number, period, or square bracket.
- operation: replace
pattern: '[^\w\d\.\[\]]'
repl: _
- operation: replace
pattern: 'almPLCFault\.ALRM'
repl: ""
- operation: add
arg: $extension$
Address:
type: str
translate:
columns: name
operations:
- operation: format
arg: 04d
- operation: radd
arg: S
Data Type:
type: str
enum:
- Boolean
- Char
- Byte
- Short
- Word
- Long
- DWord
- LongLong
- QWord
- Float
- Double
- String
const: String
Respect Data Type:
type: int
enum:
- 0
- 1
const: 1
Client Access:
type: str
enum:
- R
- W
- R/W
const: R/W
Scan Rate:
type: int
const: 100000
Scaling:
type: str
enum:
- ""
- Linear
- Square Root
const: ""
Raw Low:
type: float
const: ""
Raw High:
type: float
const: ""
Scaled Low:
type: float
const: ""
Scaled High:
type: float
const: ""
Scaled Data Type:
type: str
enum:
- Boolean
- Char
- Byte
- Short
- Word
- Long
- DWord
- LongLong
- QWord
- Float
- Double
- String
const: ""
Clamp Low:
type: float
const: ""
Clamp High:
type: float
const: ""
Eng Units:
type: str
const: ""
Description:
type: str
translate:
columns: comment
Negate Value:
type: str
const: ""
export:
tagname:
translate:
columns: Tag Name
type:
translate:
columns: Data Type
operations:
operation: type_opcua_to_iec
address:
translate:
columns: Address
comment:
translate:
columns: Description

View File

@@ -0,0 +1,134 @@
$schema: ../../../src/AuDage/extensions/assets/schema.yaml
format:
name: csv
export:
index: false
import:
filter:
columns: tagname
operations:
operation: match
pattern: 'almPLCFault\.ALRM$'
columns:
Tag Name:
type: str
translate:
columns: tagname
operations:
# Replace with an underscore any character that is not a letter,
# underscore, number, period, or square bracket.
- operation: replace
pattern: '[^\w\d\.\[\]]'
repl: _
- operation: replace
pattern: 'almPLCFault\.ALRM'
repl: ""
- operation: add
arg: On_$extension$
Address:
type: str
translate:
columns: name
operations:
- operation: format
arg: 04d
- operation: radd
arg: S
Data Type:
type: str
enum:
- Boolean
- Char
- Byte
- Short
- Word
- Long
- DWord
- LongLong
- QWord
- Float
- Double
- String
const: String
Respect Data Type:
type: int
enum:
- 0
- 1
const: 1
Client Access:
type: str
enum:
- R
- W
- R/W
const: R/W
Scan Rate:
type: int
const: 100000
Scaling:
type: str
enum:
- ""
- Linear
- Square Root
const: ""
Raw Low:
type: float
const: ""
Raw High:
type: float
const: ""
Scaled Low:
type: float
const: ""
Scaled High:
type: float
const: ""
Scaled Data Type:
type: str
enum:
- Boolean
- Char
- Byte
- Short
- Word
- Long
- DWord
- LongLong
- QWord
- Float
- Double
- String
const: ""
Clamp Low:
type: float
const: ""
Clamp High:
type: float
const: ""
Eng Units:
type: str
const: ""
Description:
type: str
translate:
columns: comment
Negate Value:
type: str
const: ""
export:
tagname:
translate:
columns: Tag Name
type:
translate:
columns: Data Type
operations:
operation: type_opcua_to_iec
address:
translate:
columns: Address
comment:
translate:
columns: Description

View File

@@ -0,0 +1,134 @@
$schema: ../../../src/AuDage/extensions/assets/schema.yaml
format:
name: csv
export:
index: false
import:
filter:
columns: tagname
operations:
operation: match
pattern: 'almPLCFault\.ALRM$'
columns:
Tag Name:
type: str
translate:
columns: tagname
operations:
# Replace with an underscore any character that is not a letter,
# underscore, number, period, or square bracket.
- operation: replace
pattern: '[^\w\d\.\[\]]'
repl: _
- operation: replace
pattern: 'almPLCFault\.ALRM'
repl: ""
- operation: add
arg: $extension$
Address:
type: str
translate:
columns: name
operations:
- operation: format
arg: 04d
- operation: radd
arg: S
Data Type:
type: str
enum:
- Boolean
- Char
- Byte
- Short
- Word
- Long
- DWord
- LongLong
- QWord
- Float
- Double
- String
const: String
Respect Data Type:
type: int
enum:
- 0
- 1
const: 1
Client Access:
type: str
enum:
- R
- W
- R/W
const: R/W
Scan Rate:
type: int
const: 100000
Scaling:
type: str
enum:
- ""
- Linear
- Square Root
const: ""
Raw Low:
type: float
const: ""
Raw High:
type: float
const: ""
Scaled Low:
type: float
const: ""
Scaled High:
type: float
const: ""
Scaled Data Type:
type: str
enum:
- Boolean
- Char
- Byte
- Short
- Word
- Long
- DWord
- LongLong
- QWord
- Float
- Double
- String
const: ""
Clamp Low:
type: float
const: ""
Clamp High:
type: float
const: ""
Eng Units:
type: str
const: ""
Description:
type: str
translate:
columns: comment
Negate Value:
type: str
const: ""
export:
tagname:
translate:
columns: Tag Name
type:
translate:
columns: Data Type
operations:
operation: type_opcua_to_iec
address:
translate:
columns: Address
comment:
translate:
columns: Description

View File

@@ -0,0 +1,134 @@
$schema: ../../../src/AuDage/extensions/assets/schema.yaml
format:
name: csv
export:
index: false
import:
filter:
columns: tagname
operations:
operation: match
pattern: '\.(VALU|PO_SP|VFDSP)$'
columns:
Tag Name:
type: str
translate:
columns: tagname
operations:
# Replace with an underscore any character that is not a letter,
# underscore, number, period, or square bracket.
- operation: replace
pattern: '[^\w\d\.\[\]]'
repl: _
- operation: replace
pattern: '\.(VALU|PO_SP|VFDSP)$'
repl: "."
- operation: add
arg: $extension$
Address:
type: str
translate:
columns: name
operations:
- operation: format
arg: 04d
- operation: radd
arg: S
Data Type:
type: str
enum:
- Boolean
- Char
- Byte
- Short
- Word
- Long
- DWord
- LongLong
- QWord
- Float
- Double
- String
const: String
Respect Data Type:
type: int
enum:
- 0
- 1
const: 1
Client Access:
type: str
enum:
- R
- W
- R/W
const: R/W
Scan Rate:
type: int
const: 100000
Scaling:
type: str
enum:
- ""
- Linear
- Square Root
const: ""
Raw Low:
type: float
const: ""
Raw High:
type: float
const: ""
Scaled Low:
type: float
const: ""
Scaled High:
type: float
const: ""
Scaled Data Type:
type: str
enum:
- Boolean
- Char
- Byte
- Short
- Word
- Long
- DWord
- LongLong
- QWord
- Float
- Double
- String
const: ""
Clamp Low:
type: float
const: ""
Clamp High:
type: float
const: ""
Eng Units:
type: str
const: ""
Description:
type: str
translate:
columns: comment
Negate Value:
type: str
const: ""
export:
tagname:
translate:
columns: Tag Name
type:
translate:
columns: Data Type
operations:
operation: type_opcua_to_iec
address:
translate:
columns: Address
comment:
translate:
columns: Description

View File

@@ -0,0 +1,4 @@
safe: bool = True
"""Default to disallowing evaluating Python from YAML files. Can be overridden
when calling AuDaGe from the command line with the argument `--unsafe`.
"""

View File

@@ -5,6 +5,7 @@ import pathlib
import sys
from enum import auto
import AuDaGe
from AuDaGe.better_enum import BetterStrEnum
from AuDaGe.frame import AuDaGeFrame
@@ -25,6 +26,15 @@ FC = FormatChoices
def main():
args = parse_args()
if args["unsafe"]:
AuDaGe.safe = False
print(
"You have chosen the option `unsafe`. Be aware (BEWARE!) that this "
"allows execution of arbitrary, possibly unsafe code from files "
"external to the AuDaGe module. Do not specify this option unless "
"you are familiar with the contents of the specified configuration "
"files."
)
frame = load_input_file(args)
dump_output_file(frame, args)
@@ -44,6 +54,7 @@ def parse_args():
"output_format", choices=FormatChoices.values(), help=""
)
parser.add_argument("output_file", type=pathlib.Path, help="")
parser.add_argument("--unsafe", action="store_true")
return parser.parse_args()

View File

View File

View File

@@ -0,0 +1,334 @@
$schema: https://yaml-schema.net/yaml-schema.yaml
title: AuDaGe Extension Schema
description: Schema for extensions to AuDaGe.
# Definitions
$defs:
exportColumn:
type: object
minProperties: 1
additionalProperties: false
properties:
const:
$ref: "#/$defs/scalar"
translate:
$ref: "#/$defs/translate"
formatNames:
enum:
- csv
- xml
- xslx
formatExtension:
description: >-
The file extension to save files with. The value of `name` is used if
`extension` is not provided.
type: string
operation:
description: >-
An operation to perform on a value to convert it from one format to
another.
oneOf:
# Mapping
- description: >-
Map one set of values to another. Returns the value of the `_default`
property if no other key matches. For `regexmapping`, the mapping keys
are treated as regular expressions.
type: object
required:
- operation
minProperties: 2
additionalProperties:
$ref: "#/$defs/scalar"
properties:
operation:
enum:
- mapping
- regexmapping
_default:
default: null
$ref: "#/$defs/scalar"
# Math, Logic & Predefined Functions
- description: >-
Mathematical operations and pre-defined functions to be applied to the
value.
type: object
required:
- operation
additionalProperties: false
properties:
operation:
enum:
# a = value
# b = arg, args[0], kwargs["b"], or 1/True* in order of priority.
# * Unless specified otherwise.
- not
- and
- or
- xor
- add
- radd
- sub # a - b
- rsub # b - a
- mul
- rmul
- div # a / b
- rdiv # b / a
- mod # a % b
- rmod # b % a
- pow # a ** b, default b = 2
- exp # b ** a, default b = math.e
- log # log_b(a), default b = math.e
- rlog # log_a(b)
- ceil # round up to integer
- floor # round down to integer
- round # round to `b` decimal places, default b = 0
- rround # round `b` to `a` decimal places
- address_iec_to_modbus
- address_modbus_to_iec
- type_iec_to_opcua
- type_opcua_to_iec
- format
- rformat
- index # return the index of the current row
arg:
description: One positional argument.
$ref: "#/$defs/scalar"
args:
description: List of positional arguments.
type: array
items:
$ref: "#/$defs/scalar"
kwargs:
description: Keyword arguments.
type: object
# Python function
- description: >-
A custom Python function that accepts either one scalar or iterable
argument and returns a value. This option allows execution of
arbitrary code and is disabled by default.
type: object
required:
- operation
- code
additionalProperties: false
properties:
operation:
const: python
code:
description: >-
A Python function definition with zero or one arguments, a
docstring, and at least one return statement.
type: string
pattern: 'def \w+\(\w*\)( -> \w+)?:\r?\n """.+?"""\r?\n(( )+.*?\r?\n)*?( )+return .+?\r?\n(( )+.*?\r?\n)*?'
# Regex substitution
- description: A regular expression substitution.
type: object
required:
- operation
- pattern
additionalProperties: false
properties:
operation:
enum:
- match
- rmatch
- replace
- rreplace
pattern:
type: string
repl:
type: string
default: ""
- description: >-
A reverse regular expression substitution (the primary argument is the
replacement rather than the original string).
type: object
required:
- operation
- pattern
additionalProperties: false
properties:
operation:
const: rregex
pattern:
type: string
string:
type: string
scalar:
anyOf:
- type: boolean
- type: integer
- type: number
- type: string
- type: "null"
translate:
type: object
required:
- columns
additionalProperties: false
properties:
columns:
description: >-
One or more column names from the source dataframe to use to
generate the value for this column.
oneOf:
- type: string
- type: array
minItems: 1
items:
type: string
operations:
description: >-
Operations to perform on the raw value(s) to generate the value
for this column.
oneOf:
- type: object
$ref: "#/$defs/operation"
- type: array
minItems: 1
items:
$ref: "#/$defs/operation"
# Schema
type: object
required:
- $schema
- format
- import
- export
additionalProperties: false
properties:
$schema:
type: string
format:
description: >-
The file format used for storing the data handled by this extension.
anyOf:
# Just the format name as a string.
- $ref: "#/$defs/formatNames"
# Just a `name` property.
- type: object
required:
- name
additionalProperties: false
properties:
name:
description: >-
The name of the file format. Must be one of: [csv, xslx, xml].
$ref: "#/$defs/formatNames"
# CSV with options.
- type: object
required:
- name
# additionalProperties: false
properties:
name:
description: >-
The file format used for storing the data handled by this
extension.
const: csv
sep:
description: >-
The delimiter to be used in the CSV/delimited file. Default ",".
type: string
default: ","
extension:
$ref: "#/$defs/formatExtension"
# XML with options.
- type: object
required:
- name
additionalProperties: false
properties:
name:
description: >-
The file format used for storing the data handled by this
extension.
const: xml
extension:
$ref: "#/$defs/formatExtension"
importStylesheets:
description: >-
One or more stylesheets used for importing data from an XML file.
oneOf:
- type: string
- type: array
items:
type: string
exportStylesheets:
description: >-
One or more stylesheets used for exporting data to an XML file.
oneOf:
- type: string
- type: array
items:
type: string
import:
description: >-
Import configuration.
type: object
required:
- columns
additionalProperties: false
properties:
filter:
description: >-
Condition that must evaluate to True for the row to be included in the
new dataframe.
$ref: "#/$defs/translate"
columns:
description: >-
Information for generating and validating the columns of this format.
type: object
minProperties: 1
additionalProperties:
type: object
additionalProperties: false
properties:
type:
description: The data type required by this column.
enum:
- bool
- float
- int
- str
enum:
description: Values allowed in this column.
type: array
minItems: 1
items:
$ref: "#/$defs/scalar"
parse:
description: >-
Operation to process data for this column when reading from a
file.
$ref: "#/$defs/operation"
const:
$ref: "#/$defs/scalar"
translate:
description: >-
Translate object for generating this column from an AuDaGeFrame.
$ref: "#/$defs/translate"
export:
description: >-
Translate objects for converting this format to the standard AuDaGeFrame
columns. Must include translators for the four mandatory columns (name,
type, address, comment). Translators for additional columns are allowed
and optional.
type: object
required:
- tagname
- type
- address
- comment
additionalProperties:
$ref: "#/$defs/exportColumn"
properties:
tagname:
$ref: "#/$defs/exportColumn"
type:
$ref: "#/$defs/exportColumn"
address:
$ref: "#/$defs/exportColumn"
comment:
$ref: "#/$defs/exportColumn"

View File

@@ -0,0 +1,392 @@
"""
AuDaGe Converter module
name: converter.py
by: Josh Lucas
date: 2026-10-22
desc: AuDaGe module for parsing extension YAML files and converting databases
using them.
"""
import math
import operator
import re
from collections.abc import Callable, Collection
from copy import deepcopy
from enum import StrEnum, auto
from importlib import resources
from os import PathLike, path
from typing import Any
import jsonschema
import pandas as pd
import yaml
from numpy import nan
import assets
import AuDaGe
from AuDaGe import iec
from AuDaGe.frame import AuDaGeFrame, Frame
def regex_mapping(string: str, **mapping: dict[str, Any]) -> Any:
"""Return the first value from `mapping` whose key matches `string`."""
for key, value in mapping.items():
if re.search(key, string) is not None:
return value
class YAMLNodes(StrEnum):
ARG = auto()
ARGS = auto()
COLUMNS = auto()
CONST = auto()
DEFAULT = auto()
ENUM = auto()
EXPORT = auto()
FILTER = auto()
FORMAT = auto()
IMPORT = auto()
NAME = auto()
OPERATION = auto()
OPERATIONS = auto()
PARSE = auto()
SCHEMA = "$schema"
TRANSLATE = auto()
TYPE = auto()
YN = YAMLNodes
class Operation:
def __init__(
self,
name: str,
args: Collection = (),
kwargs: dict[str, Any] | None = None,
):
self.name = name
self.kwargs = {} if kwargs is None else kwargs
# Mapping of operation names to callables and default second arguments.
operation_map: dict[str, Callable] = {
# Mapping
"mapping": lambda a, b=None: self.kwargs.get(a, b),
"regexmapping": regex_mapping,
# Math & Logic
"not": operator.not_,
"and": lambda a, b=True: a and b,
"or": lambda a, b=True: a or b,
"xor": lambda a, b=True: a ^ b,
"add": lambda a, b=1: a + b,
"radd": lambda a, b=1: b + a, # for concatenating strings
"sub": lambda a, b=1: a - b,
"rsub": lambda a, b=1: b - a,
"mul": lambda a, b=1: a * b,
"rmul": lambda a, b=1: b * a, # for making collections
"div": lambda a, b=1: a / b,
"rdiv": lambda a, b=1: b / a,
"mod": lambda a, b=1: a % b,
"rmod": lambda a, b=1: b % a,
"pow": lambda a, b=2: a**b,
"exp": lambda a, b=math.e: b**a,
"log": lambda a, b=math.e: math.log(a, b),
"rlog": lambda a, b=1: math.log(b, a),
"ceil": math.ceil,
"floor": math.floor,
"round": lambda a, b=0: round(a, b),
"rround": lambda a, b=1: round(b, a),
"match": lambda a, pattern=".*": re.search(pattern, a),
"rmatch": lambda a, string="": re.search(a, string),
# Regular expression substitution
"replace": lambda a, pattern, repl="": re.sub(
pattern, repl, str(a)
),
"rreplace": (
lambda a, pattern, string="": re.sub(pattern, str(a), string)
),
# Predefined function
"address_iec_to_modbus": (
lambda a, b=6: iec.Address.iec_to_modbus(a, b)
),
"address_modbus_to_iec": (
lambda a, b=0: iec.Address.modbus_to_iec(a, b)
),
"type_iec_to_opcua": (
lambda a: iec.IECType.from_string(a).to_opcua()
),
"type_opcua_to_iec": (
lambda a, b=16: iec.IECType.from_opcua(a, b).to_string()
),
"format": lambda a, b="d": format(a, b),
"rformat": lambda a, b=1: format(b, a),
"index": lambda a: a.name,
}
if not AuDaGe.safe:
operation_map["python"] = eval
if AuDaGe.safe and name == "python":
raise ValueError(
"The `python` operation is not allowed unless the `unsafe` "
"flag has been passed to AuDaGe at the command line."
)
self.callable = operation_map[name]
self.args = list(args)
class Translator:
def __init__(self, translate: dict[str, Any]):
columns = translate[YN.COLUMNS]
if isinstance(columns, str):
self.columns: list[str] = [columns]
else:
self.columns: list[str] = list(columns)
self.operations = self._get_operations(translate)
def translate(self, frame: Frame) -> pd.Series:
# Series is a Frame for the first iteration and then is actually a
# series.
for i, operation in enumerate(self.operations):
if i == 0:
series = frame.apply(
lambda row, operation=self.operations[0]: (
operation.callable(
*(
row.__getattr__(column)
for column in self.columns
),
*operation.args,
**operation.kwargs,
)
),
axis=1,
)
continue
series = series.apply(
lambda value, operation=operation: operation.callable(
value,
*operation.args,
**operation.kwargs,
)
)
return pd.Series(series)
def _get_operations(self, translate: dict[str, Any]) -> list[Operation]:
try:
operations = deepcopy(translate[YN.OPERATIONS])
except KeyError:
return []
if isinstance(operations, dict):
operations = [operations]
operation_list: list[Operation] = []
for operation in operations:
name = operation.pop(YN.OPERATION)
if YN.ARGS in operation:
args = operation.pop(YN.ARGS)
elif YN.ARG in operation:
args = [operation.pop(YN.ARG)]
else:
args = []
# All the remaining items are keyword arguments.
kwargs = operation
operation_list.append(Operation(name, args, kwargs))
return operation_list
class Converter:
"""Class for converting databases based on an AuDaGe YAML file."""
SCHEMA_PATH = "schema.yaml"
def __init__(
self,
spec_path: PathLike | str,
substitutions: dict[str, str] | None = None,
):
substitutions = {} if substitutions is None else substitutions
with (
resources.files(assets)
.joinpath(self.SCHEMA_PATH)
.open() as schema_file
):
schema = yaml.safe_load(schema_file.read())
with open(spec_path) as spec_file:
spec = spec_file.read()
for key, value in substitutions.items():
spec = spec.replace(key, value)
self.spec: dict[str, Any] = yaml.safe_load(spec)
jsonschema.validate(self.spec, schema)
def from_AF(self, frame: Frame) -> Frame:
try:
filter = Translator(self.spec[YN.IMPORT][YN.FILTER])
frame_filtered = frame
for column in filter.columns:
for operation in filter.operations:
frame_filtered = frame_filtered.loc[
frame_filtered[column].apply(
lambda value, operation=operation: bool(
operation.callable(
value,
*operation.args,
**operation.kwargs,
)
)
)
]
frame_filtered.index = range(len(frame_filtered))
except KeyError:
frame_filtered = frame
column_dict: dict[str, float | str | pd.Series] = {}
for name, column in self.spec[YN.IMPORT][YN.COLUMNS].items():
column_dict[name] = self._make_column(column, frame_filtered)
return Frame(column_dict)
def from_file(self, file_path: PathLike | str) -> Frame:
read_map: dict[str, Callable] = {
"csv": pd.read_csv,
"xml": pd.read_xml,
"xlsx": pd.read_excel,
}
format = deepcopy(self.spec[YN.FORMAT])
if isinstance(format, str):
format_kwargs = {}
read = read_map[format]
else:
format_name = format.pop(YN.NAME)
read = read_map[format_name]
# Remove export kwargs.
try:
format.pop(YN.EXPORT)
except KeyError:
pass
# Un-nest import kwargs.
try:
import_kwargs = format.pop(YN.IMPORT)
except KeyError:
import_kwargs = {}
format_kwargs = format | import_kwargs
converters: dict[str, Callable] = {}
for column_name, column_spec in self.spec[YN.IMPORT][
YN.COLUMNS
].items():
try:
parse = deepcopy(column_spec[YN.PARSE])
except KeyError:
continue
parse_operation = parse.pop(YN.OPERATION)
parse_kwargs = parse
operation = Operation(parse_operation, kwargs=parse_kwargs)
converters[str(column_name)] = lambda a, operation=operation: (
operation.callable(a, *operation.args, **operation.kwargs)
)
return Frame(read(file_path, converters=converters, **format_kwargs))
def to_AF(self, frame: Frame) -> AuDaGeFrame:
column_dict: dict[str, float | str | pd.Series] = {}
for name, column in self.spec[YN.EXPORT].items():
column_dict[name] = self._make_column(column, frame)
return AuDaGeFrame(column_dict)
def to_file(self, frame: Frame, file_path: PathLike | str):
write_map: dict[str, Callable] = {
"csv": frame.to_csv,
"xml": frame.to_xml,
"xlsx": frame.to_excel,
}
format = deepcopy(self.spec[YN.FORMAT])
if isinstance(format, str):
format_kwargs = {}
write = write_map[self.spec[YN.FORMAT]]
else:
format_name = format.pop(YN.NAME)
try:
format_ext = format.pop("extension")
except KeyError:
format_ext = format_name
try:
format.pop(YN.IMPORT)
except KeyError:
pass
try:
export_kwargs = format.pop(YN.EXPORT)
except KeyError:
export_kwargs = {}
format_kwargs = format | export_kwargs
write = write_map[format_name]
write(
".".join((path.splitext(file_path)[0], format_ext)),
**format_kwargs,
)
def _make_column(
self, column_spec: dict, frame: Frame
) -> float | str | pd.Series:
# If `const` is defined, make the column from that.
try:
return column_spec[YN.CONST]
except KeyError:
pass
try:
translate_spec = column_spec[YN.TRANSLATE]
# If neither `const` nor `translate` are defined, return an empty
# series.
except KeyError:
return (
""
if YN.TYPE in column_spec and column_spec[YN.TYPE] == "str"
else nan
)
translator = Translator(translate_spec)
# If the translator provides no operations...
if len(translator.operations) == 0:
# ...and one column name, return the value of that column.
if isinstance(translator.columns, str):
return pd.Series(frame[translator.columns])
# ...and multiple column names, return the value of the first
# column.
return pd.Series(frame[translator.columns[0]])
# Else, run the operations.
return translator.translate(frame)
if __name__ == "__main__":
plcs = ("WA3252_PLC_001_1",)
tags = ("caption", "desc", "message", "tagname", "unit")
for plc in plcs:
for tag in tags:
substitutions = {"$extension$": tag.title(), "$plc$": plc}
kep_converter = Converter(
f"extensions/kepserver/strings/{tag}_csv.yaml",
substitutions=substitutions,
)
kep = kep_converter.from_file(
f"C:/Users/j.lucas/OneDrive - Prime Controls/Customers/NTMWD/"
f"Engineering/Leonard (North) Water Treatment Plant/Programs/PLC/"
f"WA3252-PLC-001-1 Raw Water PS/{plc}.csv"
)
af = kep_converter.to_AF(kep)
kep2 = kep_converter.from_AF(af)
kep_converter.to_file(kep2, f"tests/output/{plc}_strings_{tag}.csv")
af2 = kep_converter.to_AF(kep2)
macro_converter = Converter(
f"extensions/factorytalk/macro/{tag}_macro.yaml",
substitutions=substitutions,
)
macro = macro_converter.from_AF(af2)
macro_converter.to_file(macro, f"tests/output/{plc}_{tag}.mcr")
kep_converter = Converter("extensions/kepserver/modbus/csv.yaml")
kep = kep_converter.from_file(
f"C:/Users/j.lucas/OneDrive - Prime Controls/Customers/NTMWD/"
f"Engineering/Leonard (North) Water Treatment Plant/Programs/PLC/"
f"WA3252-PLC-001-1 Raw Water PS/{plc}.csv"
)
af = kep_converter.to_AF(kep)
macro_converter = Converter(
"extensions/factorytalk/macro/alarm_severity_macro.yaml",
substitutions={"$plc$": plc},
)
macro = macro_converter.from_AF(af)
macro_converter.to_file(
macro, f"tests/output/{plc}_alarm_severities.mcr"
)

View File

@@ -14,6 +14,7 @@ from collections.abc import Iterable
from dataclasses import dataclass
from enum import IntEnum, StrEnum, auto
from importlib import resources
from importlib.resources.abc import Traversable
from itertools import pairwise
import pandas as pd
@@ -34,60 +35,51 @@ class XSYBlock(frame.AuDaGeFrame):
from_file class method.
"""
@classmethod
def from_file(
cls,
xsy_path: os.PathLike,
stylesheet_path: os.PathLike,
name_ext: str = "",
) -> XSYBlock:
"""Load an XSY file into a dataframe using the specified stylesheet."""
cls.stylesheet_path = stylesheet_path
cls.xsy_path = xsy_path
with (
resources.as_file(cls.xsy_path) as xsy_file,
resources.as_file(cls.stylesheet_path) as stylesheet_file,
):
return cls(
pd.read_xml(xsy_file, stylesheet=stylesheet_file),
".".join(
(os.path.splitext(os.path.basename(xsy_path))[0], name_ext)
),
)
def _make_array_definitions(
self, frame: frame.AuDaGeFrame | None = None
) -> frame.AuDaGeFrame | None:
"""Make definitions for the expansion of arrays of UDTs."""
frame = self if frame is None else frame
frame_grouped = frame.groupby("type")
array_definitions_list = []
for name, group in frame_grouped:
data_type = iec.IECType.from_string(name)
if data_type.is_array() and not data_type.is_elementary():
array_definitions_list.append(data_type.array_expansion())
try:
return pd.concat(array_definitions_list)
# Return `None` if there is nothing to concatenate.
except ValueError:
return None
class DataBlock(XSYBlock):
"""Read and parse the dataBlock node of an XSY file."""
DEFAULT_SS_PATH = resources.files(ASSETS_PATH).joinpath(
"flatten_datablock.xslt"
)
@classmethod
def from_file(
cls, xsy_path: os.PathLike, stylesheet_path: os.PathLike | None = None
) -> DataBlock:
"""Read the dataBlock of an XSY file into a DataFrame."""
if stylesheet_path is None:
stylesheet_path = cls.DEFAULT_SS_PATH
return cls(super().from_file(xsy_path, stylesheet_path, "DataBlock"))
cls,
path: Traversable,
stylesheet_path: Traversable | None = None,
name_ext: str = "",
) -> XSYBlock:
"""Load an XSY file into a dataframe using the specified stylesheet."""
cls.stylesheet_path = (
cls.DEFAULT_SS_PATH if stylesheet_path is None else stylesheet_path
)
cls.xsy_path = path
with (
resources.as_file(cls.xsy_path) as xsy_file,
resources.as_file(cls.stylesheet_path) as stylesheet_file,
):
return cls(
pd.read_xml(xsy_file, stylesheet=stylesheet_file),
name=".".join(
(os.path.splitext(os.path.basename(str(path)))[0], name_ext)
),
)
def _make_array_definitions(
self,
expand_elementary: bool = True,
) -> frame.AuDaGeFrame:
"""Make definitions for the expansion of arrays of UDTs."""
frame_grouped = self.groupby("type")
array_definitions_list = []
for name, _ in frame_grouped:
data_type = iec.IECType.from_string(str(name))
if data_type.is_array() and (
expand_elementary or not data_type.is_elementary()
):
array_definitions_list.append(data_type.array_expansion())
try:
return frame.AuDaGeFrame(pd.concat(array_definitions_list))
# Return `None` if there is nothing to concatenate.
except ValueError:
return frame.AuDaGeFrame()
class Definitions(XSYBlock):
@@ -111,9 +103,10 @@ class Definitions(XSYBlock):
@classmethod
def from_file(
cls,
xsy_path: os.path,
stylesheet_path: os.path | None = None,
definition_paths: Iterable[os.path] | None = None,
path: Traversable,
stylesheet_path: Traversable | None = None,
name_ext: str = "Definitions",
definition_paths: Iterable[Traversable] | None = None,
include_builtins: bool = True,
) -> Definitions:
"""Read the DDTSource nodes of an XSY file into a DataFrame."""
@@ -127,21 +120,19 @@ class Definitions(XSYBlock):
cls.definition_paths.append(cls.DEFAULT_BUILTINS_PATH)
# Get the definitions from the main XSY file and put it in the list of
# definitions to be concatenated.
cls.definitions_list = [
XSYBlock.from_file(xsy_path, cls.stylesheet_path)
]
cls.definitions_list = [XSYBlock.from_file(path, cls.stylesheet_path)]
# Read the definitions from the other definition files (including the
# builtins file) and add then to the list.
cls.definitions_list.extend(
XSYBlock.from_file(path, cls.stylesheet_path, "Definitions")
XSYBlock.from_file(path, cls.stylesheet_path, name_ext)
for path in cls.definition_paths
)
_name = ".".join(
(os.path.splitext(os.path.basename(xsy_path))[0], "Definitions")
(os.path.splitext(os.path.basename(str(path)))[0], name_ext)
)
definitions = XSYBlock(pd.concat(cls.definitions_list))
definitions_extended = pd.concat(
(definitions, definitions._make_array_definitions())
definitions = cls(pd.concat(cls.definitions_list))
definitions_extended = cls(
pd.concat((definitions, definitions._make_array_definitions()))
)
time_message = (
"{_name} offset calculation completed in {time} {unit}.".format(
@@ -157,8 +148,8 @@ class Definitions(XSYBlock):
cls,
row,
udt_lengths: dict[str, int],
bool_parents: IntEnum = BoolParents,
) -> int:
bool_parents: type[BetterIntEnum] = BoolParents,
) -> float:
# Special case to fix the BOOLs that are packed into data units other
# than bits or words.
if (
@@ -190,10 +181,10 @@ class Definitions(XSYBlock):
@classmethod
def _calculate_padding(
cls,
address: int | nan,
length: int | nan,
address: float,
length: float,
max_alignment: int = iec.DataUnit.DWORD,
) -> int | nan:
) -> float:
# Return early if missing information.
if pd.isna(address) or pd.isna(length) or length <= 0:
return address
@@ -208,7 +199,7 @@ class Definitions(XSYBlock):
@classmethod
def _calculate_offsets(
cls,
definitions: XSYBlock | None = None,
definitions: Definitions,
udt_lengths: dict[str, int] | None = None,
max_recursion: int = MAX_RECURSION,
_current_recursion: int = 0,
@@ -229,7 +220,6 @@ class Definitions(XSYBlock):
),
):
# Set parameters to default values.
definitions = cls if definitions is None else definitions
udt_lengths = {} if udt_lengths is None else udt_lengths
# We don't care about DFBs so set their length at zero.
udt_lengths["DFB"] = 0
@@ -258,13 +248,18 @@ class Definitions(XSYBlock):
# know its length so we save it to the `udt_lengths` dictionary
# for future reference.
if group_calc["address"].notna().all():
udt_lengths[name] = group_calc["address"].tail(1).values[0]
udt_lengths[str(name)] = (
group_calc["address"].tail(1).values[0]
+ iec.IECType.from_string(
group_calc["type"].tail(1).values[0]
).length
)
else:
# This UDT's offsets aren't finished so set the flag False.
all_offsets_found = False
definitions_calculated_list.append(group_calc)
# Re-combine all the UDT definitions.
definitions_calculated = pd.concat(definitions_calculated_list)
definitions_calculated = cls(pd.concat(definitions_calculated_list))
if all_offsets_found:
# We're done! Log the recursion depth we reached and return our
# results.
@@ -298,7 +293,7 @@ class XSY:
"""Object representing a ControlExpert variable exchange (XSY) file."""
# Dataclass attributes:
datablock: DataBlock
datablock: XSYBlock
definitions: Definitions
even_addresses: bool
name: str
@@ -315,12 +310,13 @@ class XSY:
def expand_datablock(
self,
datablock: DataBlock | None = None,
datablock: XSYBlock | None = None,
definitions: Definitions | None = None,
ignored_data_types: StrEnum = IgnoredDataTypes,
ignored_data_types: type[StrEnum] = IgnoredDataTypes,
expand_elementary_arrays: bool = True,
debug_step: DebugStep | int = DebugStep.COMPLETE,
max_recursion: int = MAX_RECURSION,
) -> DataBlock:
) -> XSYBlock:
"""Recursively expand the UDTs in `datablock` based on the UDT
definitions in `definitions` and return the resulting DataFrame.
@@ -338,7 +334,7 @@ class XSY:
max_recursion : int
The maximum number of times to recursively expand UDTs. Default 5.
"""
datablock_expanded: DataBlock
datablock_expanded: XSYBlock
with MeasureTime(
TimeUnit.MS,
True,
@@ -360,8 +356,11 @@ class XSY:
unit="{unit}",
),
):
definitions_extended = pd.concat(
(definitions, datablock._make_array_definitions())
array_definitions = datablock._make_array_definitions()
definitions_extended = Definitions(
pd.concat(
(definitions, array_definitions), ignore_index=True
)
)
if debug_step == self.DebugStep.MAKE_ARRAY_DEFS:
return definitions_extended
@@ -369,14 +368,15 @@ class XSY:
datablock,
definitions_extended,
ignored_data_types,
expand_elementary_arrays,
debug_step,
max_recursion,
)
return datablock_expanded
def _combine_addresses(self, row: pd.Series) -> iec.Address:
if pd.isna(row["address"]):
return nan
def _combine_addresses(self, row: pd.Series) -> str:
if pd.isna(row["address"]) or row["address"] == "":
return ""
address_object = iec.Address.from_string(row["address"])
if pd.notna(row["address_def"]):
offset = row["address_def"] // 16
@@ -385,9 +385,9 @@ class XSY:
if pd.notna(row["bitrank_def"]):
bit = float(row["bitrank_def"])
if address_object.bit is None:
address_object.bit = bit
address_object.bit = int(bit)
else:
address_object.bit += bit
address_object.bit += int(bit)
offset -= 1
address_object.word += offset
return str(address_object)
@@ -401,7 +401,7 @@ class XSY:
comments_cleaned.append(comment_cleaned)
return ": ".join(comments_cleaned)
def _compile_datablock(self, datablock: DataBlock) -> DataBlock:
def _compile_datablock(self, datablock: XSYBlock) -> XSYBlock:
"""Compile a cross-joined and filtered DataBlock by combining the
columns from the datablock with the columns from the definitions."""
datablock_compiled = pd.DataFrame()
@@ -422,17 +422,18 @@ class XSY:
),
axis=1,
)
return DataBlock(datablock_compiled)
return XSYBlock(datablock_compiled)
def _expand_udts(
self,
datablock: DataBlock,
datablock: XSYBlock,
definitions: Definitions,
ignored_data_types: StrEnum,
debug_step: DebugStep,
ignored_data_types: type[StrEnum],
expand_elementary_arrays: bool,
debug_step: DebugStep | int,
max_recursion: int,
_current_recursion: int = 0,
) -> DataBlock:
) -> XSYBlock:
"""Helper function for expand_datablock that does the recursive
expansion of UDTs.
@@ -451,18 +452,20 @@ class XSY:
Current recursion depth. Not to be used except within this method.
"""
datablock_grouped = datablock.groupby("type")
edts = pd.concat(
group
for (name, group) in datablock_grouped
if iec.IECType.from_string(name).is_elementary()
)
udts = pd.concat(
group
for (name, group) in datablock_grouped
if not iec.IECType.from_string(name).is_elementary()
)
edt_list: list[pd.DataFrame] = []
udt_list: list[pd.DataFrame] = []
for name, group in datablock_grouped:
data_type = iec.IECType.from_string(str(name))
if data_type.is_scalar() or (
data_type.is_elementary() and not expand_elementary_arrays
):
edt_list.append(group)
else:
udt_list.append(group)
edts = pd.concat(edt_list)
udts = pd.concat(udt_list)
if debug_step == self.DebugStep.GROUP:
return pd.concat((edts, udts))
return XSYBlock(pd.concat((edts, udts)))
definitions_grouped = definitions.groupby("parent")
udts_grouped = udts.groupby("type")
group_list = []
@@ -480,20 +483,25 @@ class XSY:
),
):
for name, group in udts_grouped:
if name.lower() not in ignored_data_types:
group_list.append(
group.join(
pd.DataFrame(
definitions_grouped.get_group(name.upper())
),
how="cross",
rsuffix="_def",
if str(name).lower() not in ignored_data_types:
try:
group_list.append(
group.join(
pd.DataFrame(
definitions_grouped.get_group(
str(name).upper()
)
),
how="cross",
rsuffix="_def",
)
)
)
except KeyError:
pass
udts_joined = pd.concat(group_list)
if debug_step == self.DebugStep.JOIN:
return udts_joined
udts_compiled = self._compile_datablock(udts_joined)
return XSYBlock(udts_joined)
udts_compiled = self._compile_datablock(XSYBlock(udts_joined))
if debug_step == self.DebugStep.COMPILE:
return udts_compiled
if all(
@@ -520,14 +528,15 @@ class XSY:
udts_compiled,
definitions,
ignored_data_types,
expand_elementary_arrays,
debug_step,
max_recursion,
_current_recursion + 1,
)
return pd.concat((edts, udts_finished))
return XSYBlock(pd.concat((edts, udts_finished)))
class XSYReader(frame.Reader):
class XSYReader:
"""Class for reading and writing Schneider Electric EcoStruxure Control
Expert variable exchange (XSY) files. Can be re-used to read multiple XSY
files using common stylesheets and UDT definitions.
@@ -535,9 +544,9 @@ class XSYReader(frame.Reader):
def __init__(
self,
definition_paths: Iterable[os.path] | None = None,
datablock_ss_path: os.path | None = None,
definitions_ss_path: os.path | None = None,
definition_paths: Iterable[Traversable] | None = None,
datablock_ss_path: Traversable | None = None,
definitions_ss_path: Traversable | None = None,
even_addresses: bool = True,
):
"""Initialize XSYReader object with default definition files and
@@ -564,19 +573,21 @@ class XSYReader(frame.Reader):
self.definitions_ss_path = definitions_ss_path
self.even_addresses = even_addresses
def read(self, xsy_path: os.path, expand: bool = True) -> XSY:
def read(self, path: Traversable, expand: bool = True) -> XSY:
"""Read the dataBlock and definitions of an XSY file into an XSY
object.
"""
datablock = DataBlock.from_file(xsy_path, self.datablock_ss_path)
datablock = XSYBlock.from_file(path, self.datablock_ss_path)
definitions = Definitions.from_file(
xsy_path, self.definitions_ss_path, self.definition_paths
path,
self.definitions_ss_path,
definition_paths=self.definition_paths,
)
xsy = XSY(
datablock=datablock,
definitions=definitions,
even_addresses=self.even_addresses,
name=os.path.basename(xsy_path),
name=os.path.basename(str(path)),
)
if expand:
xsy.datablock = xsy.expand_datablock()

View File

@@ -158,7 +158,7 @@
<Cell>
<Data ss:Type="String">Remote Suppress Tag</Data>
<NamedCell ss:Name="DigitalRemoteSuppressTag"/>
</Cell>
</Cell>
<Cell>
<Data ss:Type="String">Remote Unsuppress Reset</Data>
<NamedCell ss:Name="DigitalRemoteUnsuppressReset"/>
@@ -174,7 +174,7 @@
<Cell>
<Data ss:Type="String">Remote ShelveAll Tag</Data>
<NamedCell ss:Name="DigitalRemoteShelveAllTag"/>
</Cell>
</Cell>
<Cell>
<Data ss:Type="String">Remote ShelveDuration Tag</Data>
<NamedCell ss:Name="DigitalRemoteShelveDurationTag"/>
@@ -499,7 +499,7 @@
</xsl:attribute>
<xsl:value-of select="fta:DiscreteElement/fta:RemoteShelveAllDataItem"/>
</xsl:element>
</xsl:element>
</xsl:element>
<xsl:element name="Cell">
<xsl:element name="Data">
<xsl:attribute name="ss:Type">
@@ -722,7 +722,7 @@
<Data ss:Type="String">LOLO Acknowledged Tag</Data>
<NamedCell ss:Name="LevelLOLOAcknowledgedTag"/>
</Cell>
<Cell>
<Data ss:Type="String">Remote AcknowledgeAll Reset</Data>
<NamedCell ss:Name="LevelRemoteAcknowledgeAllReset"/>
@@ -936,8 +936,8 @@
<xsl:when test="@latched='false'">
<xsl:text>0</xsl:text>
</xsl:when>
</xsl:choose>
</xsl:element>
</xsl:choose>
</xsl:element>
</xsl:element> -->
<xsl:element name="Cell">
<xsl:element name="Data">
@@ -951,11 +951,11 @@
<xsl:value-of select="fta:MultiLevelElement/fta:Deadband"/>
</xsl:element>
</xsl:element>
<xsl:element name="Cell">
<xsl:attribute name="ss:Index"><xsl:text>6</xsl:text></xsl:attribute>
<xsl:element name="Data">
<xsl:attribute name="ss:Type"><xsl:text>Boolean</xsl:text></xsl:attribute>
<xsl:choose>
<xsl:when test="fta:MultiLevelElement/fta:Levels/fta:Level[fta:ConditionType='HIHI']!=''">
@@ -964,7 +964,7 @@
</xsl:choose>
</xsl:element>
</xsl:element>
<xsl:element name="Cell">
<xsl:attribute name="ss:Index"><xsl:text>7</xsl:text></xsl:attribute>
<xsl:element name="Data">
@@ -990,7 +990,7 @@
<xsl:value-of select="fta:MultiLevelElement/fta:Levels/fta:Level[fta:ConditionType='HIHI']/fta:MessageID"/>
</xsl:element>
</xsl:element>
<!-- HI LEVEL -->
<xsl:element name="Cell">
<xsl:attribute name="ss:Index"><xsl:text>11</xsl:text></xsl:attribute>
@@ -1003,7 +1003,7 @@
</xsl:choose>
</xsl:element>
</xsl:element>
<xsl:element name="Cell">
<xsl:attribute name="ss:Index"><xsl:text>12</xsl:text></xsl:attribute>
<xsl:element name="Data">
@@ -1029,8 +1029,8 @@
<xsl:value-of select="fta:MultiLevelElement/fta:Levels/fta:Level[fta:ConditionType='HI']/fta:MessageID"/>
</xsl:element>
</xsl:element>
<!-- LO LEVEL -->
<xsl:element name="Cell">
<xsl:attribute name="ss:Index"><xsl:text>16</xsl:text></xsl:attribute>
@@ -1068,7 +1068,7 @@
<xsl:value-of select="fta:MultiLevelElement/fta:Levels/fta:Level[fta:ConditionType='LO']/fta:MessageID"/>
</xsl:element>
</xsl:element>
<xsl:element name="Cell">
<xsl:attribute name="ss:Index"><xsl:text>21</xsl:text></xsl:attribute>
<xsl:element name="Data">

View File

@@ -0,0 +1,500 @@
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns="urn://www.factorytalk.net/schema/2003/FTLDDAlarms.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn://www.factorytalk.net/schema/2003/FTLDDAlarms.xsd" elementFormDefault="qualified"
attributeFormDefault="unqualified" version="1.0">
<xs:import namespace="http://www.w3.org/XML/1998/namespace" schemaLocation="FTLLDDXml.xsd" />
<xs:simpleType name="NonEmptyString">
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
<!-- Alarm Operation types -->
<xs:simpleType name="OperationType">
<xs:restriction base="xs:string">
<xs:enumeration value="WriteConfig" />
<xs:enumeration value="WriteMsg" />
<xs:enumeration value="WriteAlarmGroup" />
<xs:enumeration value="SetServerName" />
<xs:enumeration value="SetDAPollGroups" />
<xs:enumeration value="SetLanguages" />
</xs:restriction>
</xs:simpleType>
<!-- Poll Group values -->
<xs:simpleType name="PollGroupValues">
<xs:restriction base="xs:decimal">
<xs:enumeration value="0.10" />
<xs:enumeration value="0.25" />
<xs:enumeration value="0.50" />
<xs:enumeration value="1" />
<xs:enumeration value="2" />
<xs:enumeration value="5" />
<xs:enumeration value="10" />
<xs:enumeration value="20" />
<xs:enumeration value="30" />
<xs:enumeration value="60" />
<xs:enumeration value="120" />
</xs:restriction>
</xs:simpleType>
<!-- alarm styles -->
<xs:simpleType name="AlarmStyle">
<xs:restriction base="xs:string">
<xs:enumeration value="Discrete" />
<xs:enumeration value="MultiLevel" />
<xs:enumeration value="Deviation" />
<xs:enumeration value="Event" />
</xs:restriction>
</xs:simpleType>
<!-- event styles -->
<xs:simpleType name="EventStyle">
<xs:restriction base="xs:string">
<xs:enumeration value="DiscreteTrue" />
<xs:enumeration value="DiscreteFalse" />
</xs:restriction>
</xs:simpleType>
<!-- discrete styles -->
<xs:simpleType name="DiscreteStyle">
<xs:restriction base="xs:string">
<xs:enumeration value="DiscreteTrue" />
<xs:enumeration value="DiscreteFalse" />
</xs:restriction>
</xs:simpleType>
<!-- comparison operators -->
<xs:simpleType name="ComparisonOperator">
<xs:restriction base="xs:string">
<xs:enumeration value="=" />
<xs:enumeration value="&lt;&gt;" />
<xs:enumeration value="&lt;=" />
<xs:enumeration value="&lt;" />
<xs:enumeration value="&gt;" />
<xs:enumeration value="&gt;=" />
</xs:restriction>
</xs:simpleType>
<!-- Remote Ack Reset values -->
<xs:simpleType name="RemoteAckResetValues">
<xs:restriction base="xs:string">
<xs:enumeration value="true" />
<xs:enumeration value="false" />
</xs:restriction>
</xs:simpleType>
<!-- delay intervals -->
<xs:simpleType name="DelayInterval">
<xs:restriction base="xs:integer">
<xs:minInclusive value="0" />
<xs:maxInclusive value="600" />
</xs:restriction>
</xs:simpleType>
<!-- Target types -->
<xs:simpleType name="TargetTypes">
<xs:restriction base="xs:string">
<xs:enumeration value="DataItem" />
<xs:enumeration value="Constant" />
</xs:restriction>
</xs:simpleType>
<!-- severity range -->
<xs:simpleType name="SeverityRange">
<xs:restriction base="xs:string">
</xs:restriction>
</xs:simpleType>
<!-- deadband units -->
<xs:simpleType name="DeadbandUnits">
<xs:restriction base="xs:string">
<xs:enumeration value="Constant" />
<xs:enumeration value="None" />
</xs:restriction>
</xs:simpleType>
<!-- delta types -->
<xs:simpleType name="DeltaTypes">
<xs:restriction base="xs:string">
<xs:enumeration value="Constant" />
<xs:enumeration value="Percent" />
</xs:restriction>
</xs:simpleType>
<!-- deadband -->
<xs:complexType name="Deadband">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="units" type="DeadbandUnits" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<!-- Remote Ack Type-->
<xs:complexType name="RemoteCtrlType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="AutoReset" type="RemoteAckResetValues" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<!-- Param typed -->
<xs:complexType name="ParamType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="key" type="xs:string" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<!-- Params type -->
<xs:complexType name="ParamsType">
<xs:sequence>
<xs:element name="Param" type="ParamType" minOccurs="0" maxOccurs="6" />
</xs:sequence>
</xs:complexType>
<!-- deviation style -->
<xs:simpleType name="DeviationStyle">
<xs:restriction base="xs:string">
<xs:enumeration value="DEVLO" />
<xs:enumeration value="DEVHI" />
<xs:enumeration value="DEVHILO" />
</xs:restriction>
</xs:simpleType>
<!-- condition type -->
<xs:simpleType name="ConditionType">
<xs:restriction base="xs:string">
<xs:enumeration value="LOLO" />
<xs:enumeration value="LO" />
<xs:enumeration value="HIHI" />
<xs:enumeration value="HI" />
</xs:restriction>
</xs:simpleType>
<!-- target-->
<xs:complexType name="Target">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="type" type="TargetTypes" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<!-- delta type -->
<xs:complexType name="DeltaType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="units" type="DeltaTypes" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<!-- message type -->
<xs:complexType name="MessageType">
<xs:sequence>
<xs:element name="Msgs">
<xs:complexType>
<xs:sequence>
<xs:element name="Msg" maxOccurs="unbounded">
<xs:complexType mixed="true">
<xs:attribute ref="xml:lang" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="id" use="required">
<xs:simpleType>
<xs:restriction base="xs:nonNegativeInteger">
<xs:minInclusive value="1"/>
<xs:maxInclusive value="2147483647"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:complexType>
<!-- MessageRef -->
<xs:complexType name="MessageRef">
<xs:sequence>
<xs:element name="Params" type="ParamsType" minOccurs="0" />
</xs:sequence>
<xs:attribute name="id" type="xs:nonNegativeInteger" use="required" />
</xs:complexType>
<!-- GroupType -->
<xs:complexType name="GroupType">
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="id" type="xs:positiveInteger" use="required" />
<xs:attribute name="parentID" type="xs:nonNegativeInteger" use="required" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<!-- handshake tags -->
<xs:complexType name="EventHandshakeType">
<xs:sequence>
<xs:element name="InAlarmDataItem" type="xs:string" />
<xs:element name="DisabledDataItem" type="xs:string" />
<xs:element name="SuppressedDataItem" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="HandshakeType">
<xs:sequence>
<xs:element name="InAlarmDataItem" type="xs:string" />
<xs:element name="DisabledDataItem" type="xs:string" />
<xs:element name="AckedDataItem" type="xs:string" />
<xs:element name="SuppressedDataItem" type="xs:string" />
<xs:element name="ShelvedDataItem" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="DeviationHandshakeType">
<xs:sequence>
<xs:element name="InAlarmHiDataItem" type="xs:string" />
<xs:element name="InAlarmLoDataItem" type="xs:string" />
<xs:element name="DisabledDataItem" type="xs:string" />
<xs:element name="AckedHiDataItem" type="xs:string" />
<xs:element name="AckedLoDataItem" type="xs:string" />
<xs:element name="SuppressedDataItem" type="xs:string" />
<xs:element name="ShelvedHiDataItem" type="xs:string" minOccurs="0" />
<xs:element name="ShelvedLoDataItem" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="MultiLevelsHandshakeType">
<xs:sequence>
<xs:element name="DisabledDataItem" type="xs:string" />
<xs:element name="SuppressedDataItem" type="xs:string" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="MultiLevelHandshakeType">
<xs:sequence>
<xs:element name="InAlarmDataItem" type="xs:string" />
<xs:element name="AckedDataItem" type="xs:string" />
<xs:element name="ShelvedDataItem" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
<xs:simpleType name="MessageIDType">
<xs:restriction base="xs:nonNegativeInteger">
<xs:minInclusive value="0"/>
<xs:maxInclusive value="2147483647"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="GroupIDType">
<xs:restriction base="xs:nonNegativeInteger">
</xs:restriction>
</xs:simpleType>
<!-- FTAeAlarmStore -->
<xs:element name="FTAeAlarmStore">
<xs:complexType>
<xs:sequence>
<xs:element name="DetectorStyle" type="xs:string" minOccurs="0" default="FTAeLiveDataDetector" />
<xs:element name="SequenceNumber" type="xs:unsignedLong" minOccurs="0" />
<xs:element name="Version" type="xs:string" />
<xs:element name="Commands">
<xs:complexType>
<xs:sequence>
<xs:element name="FTAeDetectorCommand" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="Operation" type="OperationType" />
<xs:choice>
<xs:element name="ServerName" type="xs:string" minOccurs="0"></xs:element>
<xs:element name="Language" minOccurs="0" maxOccurs="20">
<xs:complexType mixed="true">
<xs:attribute ref="xml:lang" use="optional" />
</xs:complexType>
</xs:element>
<!-- PollGroups -->
<xs:element name="PollGroups">
<xs:complexType>
<xs:sequence>
<xs:element name="PollGroupTags" minOccurs="0" maxOccurs="20">
<xs:complexType>
<xs:sequence>
<xs:element name="Tag" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string" />
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="rate" type="PollGroupValues" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<!--Messages -->
<xs:element name="Messages">
<xs:complexType>
<xs:sequence>
<xs:element name="Message" type="MessageType" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<!--Groups -->
<xs:element name="Groups">
<xs:complexType>
<xs:sequence>
<xs:element name="Group" type="GroupType" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<!-- FTAlarmElemenets -->
<xs:element name="FTAlarmElements">
<xs:complexType>
<xs:sequence>
<xs:element name="FTAlarmElement" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="EventElement" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="DataItem" type="xs:string" />
<xs:element name="Style" type="EventStyle" />
<xs:element name="Target" type="Target" />
<xs:element name="DelayInterval" type="DelayInterval" />
<xs:element name="Deadband" type="Deadband" />
<xs:element name="UserData" type="xs:string" minOccurs="0" />
<xs:element name="RSVCmd" type="xs:string" minOccurs="0" />
<xs:element name="AlarmClass" type="xs:string" minOccurs="0" />
<xs:element name="GroupID" type="GroupIDType" minOccurs="0" maxOccurs="1" />
<xs:element name="EventHandshakeTags" type="EventHandshakeType" minOccurs="0" />
<xs:element name="RemoteDisableDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteEnableDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteResetDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteSuppressDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteUnSuppressDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="MessageID" type="MessageIDType" minOccurs="1" maxOccurs="unbounded" />
<xs:element name="Params" type="ParamsType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="DiscreteElement" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="DataItem" type="xs:string" />
<xs:element name="Style" type="DiscreteStyle" />
<xs:element name="Severity" type="SeverityRange" />
<xs:element name="DelayInterval" type="DelayInterval" />
<xs:element name="EnableTag" type="xs:boolean" minOccurs="0" />
<xs:element name="UserData" type="xs:string" minOccurs="0" />
<xs:element name="RSVCmd" type="xs:string" minOccurs="0" />
<xs:element name="AlarmClass" type="xs:string" minOccurs="0" />
<xs:element name="GroupID" type="GroupIDType" minOccurs="0" maxOccurs="1" />
<xs:element name="HandshakeTags" type="HandshakeType" minOccurs="0" />
<xs:element name="RemoteAckAllDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteDisableDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteEnableDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteSuppressDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteUnSuppressDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteShelveAllDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteUnShelveDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteShelveDuration" type="xs:string" minOccurs="0" />
<xs:element name="MessageID" type="MessageIDType" minOccurs="1" maxOccurs="unbounded" />
<xs:element name="Params" type="ParamsType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="DeviationElement" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="SourceDataItem" type="xs:string" />
<xs:element name="Style" type="DeviationStyle" />
<xs:element name="Severity" type="SeverityRange" />
<xs:element name="DelayInterval" type="DelayInterval" />
<xs:element name="Target" type="Target" />
<xs:element name="Delta" type="DeltaType" />
<xs:element name="Deadband" type="Deadband" />
<xs:element name="EnableTag" type="xs:boolean" minOccurs="0" />
<xs:element name="UserData" type="xs:string" minOccurs="0" />
<xs:element name="RSVCmd" type="xs:string" minOccurs="0" />
<xs:element name="AlarmClass" type="xs:string" minOccurs="0" />
<xs:element name="GroupID" type="GroupIDType" minOccurs="0" maxOccurs="1" />
<xs:element name="DeviationHandshakeTags" type="DeviationHandshakeType" minOccurs="0" />
<xs:element name="RemoteAckAllDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteAckHiDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteAckLoDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteDisableDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteEnableDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteSuppressDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteUnSuppressDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteShelveAllDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteShelveHiDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteShelveLoDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteUnShelveDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteShelveDuration" type="xs:string" minOccurs="0" />
<xs:element name="Deviations">
<xs:complexType>
<xs:sequence>
<xs:element name="Deviation" maxOccurs="2">
<xs:complexType>
<xs:sequence>
<xs:element name="Comparison" type="ComparisonOperator" />
<xs:element name="MessageID" type="MessageIDType" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Params" type="ParamsType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="MultiLevelElement">
<xs:complexType>
<xs:sequence>
<xs:element name="SourceDataItem" type="xs:string" />
<xs:element name="DelayInterval" type="DelayInterval" />
<xs:element name="Deadband" type="Deadband" />
<xs:element name="EnableTag" type="xs:boolean" minOccurs="0" />
<xs:element name="UserData" type="xs:string" minOccurs="0" />
<xs:element name="RSVCmd" type="xs:string" minOccurs="0" />
<xs:element name="AlarmClass" type="xs:string" minOccurs="0" />
<xs:element name="GroupID" type="GroupIDType" minOccurs="0" maxOccurs="1" />
<xs:element name="MultiLevelHandshakeTags" type="MultiLevelsHandshakeType" minOccurs="0" />
<xs:element name="RemoteAckAllDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteDisableDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteEnableDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteSuppressDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteUnSuppressDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteShelveAllDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteUnShelveDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteShelveDuration" type="xs:string" minOccurs="0" />
<xs:element name="Levels">
<xs:complexType>
<xs:sequence>
<xs:element name="Level" maxOccurs="4">
<xs:complexType>
<xs:sequence>
<xs:element name="Target" type="Target" />
<xs:element name="Severity" type="SeverityRange" />
<xs:element name="ApplyDuration" type="xs:boolean" minOccurs="0" />
<xs:element name="ConditionType" type="ConditionType" />
<xs:element name="Comparison" type="ComparisonOperator" />
<xs:element name="MultiLevelLevelHandshakeTags" type="MultiLevelHandshakeType" minOccurs="0" />
<xs:element name="RemoteAckLevelDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="RemoteShelveLevelDataItem" type="RemoteCtrlType" minOccurs="0" />
<xs:element name="MessageID" type="MessageIDType" minOccurs="1" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Params" type="ParamsType" minOccurs="0" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
<xs:attribute name="name" type="NonEmptyString" />
<xs:attribute name="inuse" type="NonEmptyString" />
<xs:attribute name="latched" type="xs:boolean" />
<xs:attribute name="ackRequired" type="xs:boolean" />
<xs:attribute name="style" type="AlarmStyle" />
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="shelveMaxValue" type="xs:positiveInteger" use="optional" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:sequence>
<xs:attribute name="style" type="xs:string" />
<xs:attribute name="version" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://www.w3.org/XML/1998/namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" xml:lang="en">
<xs:attribute name="lang">
<xs:simpleType>
<xs:union memberTypes="xs:language">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="es-ES_tradnl"/>
</xs:restriction>
</xs:simpleType>
</xs:union>
</xs:simpleType>
</xs:attribute>
</xs:schema>

View File

@@ -0,0 +1,7 @@
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
Convert a pandas dataframe to a FactoryTalk Alarm & Events XML file.
Copyright 2026 Prime Controls. All rights reserved.
-->
</xsl:stylesheet>

View File

@@ -0,0 +1,115 @@
patterns:
DataItem:
pattern: .*
sub: .*
Severity:
pattern: .*
sub: .*
groups:
Lake:
_pattern: .*
Intake:
_pattern: .*
IntakeTower: .*
LakeIntakeOxygen: .*
ServiceSpillway: .*
PumpStation:
_pattern: .*
RWPS: .*
RWPSMeterVault: .*
Plant:
_pattern: .*
PlantInfluent:
_pattern: .*
TSR: .*
InfluentPS: .*
RWChemistry: .*
Pretreatment:
_pattern: .*
RapidMix: .*
FloccBasins: .*
SedBasins:
_pattern: .*
SludgeCollectors: .*
Ozone:
_pattern: .*
OzoneDisinfection:
_pattern: .*
OzoneInjection: .*
OzoneContactors: .*
LOX: .*
OzoneGeneration:
_pattern: .*
OzoneGenerators: .*
CoolingWater: .*
OzoneDestruction: .*
OzoneAmbientAnalyzers: .*
Filters:
_pattern: .*
FilterInfluent: .*
Filter1: .*
Filter2: .*
Filter3: .*
Filter4: .*
Filter5: .*
Filter6: .*
Filter7: .*
Filter8: .*
Filter9: .*
Filter10: .*
Filter11: .*
Filter12: .*
FilterAirCompressors: .*
FilterBlowers: .*
FilterEffluent: .*
FilterBackwash: .*
FinishedWater:
_pattern: .*
BoosterPS: .*
ChlorineContactBasins: .*
Clearwells: .*
HSPS: .*
Reclaim:
_pattern: .*
ProcessDrainPS1: .*
ProcessDrainPS2: .*
ProcessDrainPS3: .*
ReclaimBasins: .*
ReclaimPS: .*
UnderDrainPS: .*
Sludge:
_pattern: .*
UnthickenedSludgePS: .*
GravityThickeners: .*
ThickenedSludgePS: .*
SludgeLagoons: .*
Chemicals:
_pattern: .*
NorthChemical:
_pattern: .*
CationicPolymer: .*
Chlorine: .*
ChlorineScrubbers: .*
SodiumChlorite: .*
EastChemical:
_pattern: .*
AnionicPolymer: .*
FerricSulfate: .*
Lime: .*
SulfuricAcid: .*
WestChemical:
_pattern: .*
AmmoniumSulfate: .*
CalciumThiosulfate: .*
HFSA: .*
NonionicPolymer: .*
PhosphoricAcid: .*
SodiumHydroxide: .*
OperationsBldg: .*
MaintenanceBldg: .*
SCADA:
_pattern: .*
Comms: .*
Generators: .*
Panels: .*
Servers: .*

View File

@@ -0,0 +1,82 @@
"""
AuDaGe FactoryTalk Alarm & Events XML module
name: XML.py
by: Josh Lucas
date: 2026-10-20
desc: AuDaGe module for handling FactoryTalk Alarm & Events XML files.
"""
from importlib import resources
from importlib.resources.abc import Traversable
import pandas as pd
import assets
from AuDaGe import frame, iec
from AuDaGe.better_enum import BetterStrEnum
class Columns(BetterStrEnum):
A_NAME = "@name"
A_LATCHED = "@latched"
A_ACKREQUIRED = "@ackRequired"
A_STYLE = "@style"
DATAITEM = "DataItem"
STYLE = "Style"
SEVERITY = "Severity"
DELAYINTERVAL = "DelayInterval"
ENABLETAG = "EnableTag"
USERDATA = "UserData"
RSVCMD = "RSVCmd"
ALARMCLASS = "AlarmClass"
GROUPID = "GroupID"
INALARM = "InAlarmDataItem"
DISABLED = "DisabledDataItem"
ACKEDDATAITEM = "AckedDataItem"
SUPPRESSED = "SuppressedDataItem"
SHELVED = "ShelvedDataItem"
ACKALL = "RemoteAckAllDataItem"
ACKALL_RESET = "RemoteAckAllDataItem@AutoReset"
DISABLE = "RemoteDisableDataItem"
DISABLE_RESET = "RemoteDisableDataItem@AutoReset"
ENABLE = "RemoteEnableDataItem"
ENABLE_RESET = "RemoteEnableDataItem@AutoReset"
SUPPRESS = "RemoteSuppressDataItem"
SUPPRESS_RESET = "RemoteSuppressDataItem@AutoReset"
UNSUPPRESS = "RemoteUnSuppressDataItem"
UNSUPPRESS_RESET = "RemoteUnSuppressDataItem@AutoReset"
SHELVEALL = "RemoteShelveAllDataItem"
SHELVEALL_RESET = "RemoteShelveAllDataItem@AutoReset"
UNSHELVE = "RemoteShelveDataItem"
UNSHELVE_RESET = "RemoteShelveDataItem@AutoReset"
SHELVEDURATION = "RemoteShelveDuration"
MESSAGEID = "MessageID"
PARAMS = "Params"
class TranslatorsFromAF:
"""Class for organizing static methods for converting from an AuDaGeFrame to
a FactoryTalk Alarm & Events frame.
"""
class TranslatorsToAF:
"""Class for organizing static methods for converting from a FactoryTalk
Alarm & Events frame to an AuDaGeFrame.
"""
class Frame(frame.Frame):
"""DataFrame for FTAE XML."""
class XMLReader(frame.Reader):
"""Class for reading and writing to FactoryTalk Alarm & Events XML files."""
def read(self, path: Traversable):
pass
def write(self, path: Traversable):
pass

View File

@@ -9,6 +9,7 @@ desc: AuDaGe module for handling Kepserver CSV files.
"""
import re
from importlib.resources.abc import Traversable
import pandas as pd
from numpy import nan
@@ -18,6 +19,8 @@ from AuDaGe.better_enum import BetterStrEnum
AC = frame.AuDaGeColumns
REMOVE_BRACKETS = False
class Columns(BetterStrEnum):
TAG_NAME = "Tag Name"
@@ -83,19 +86,29 @@ class TranslatorsFromAF:
"""
@staticmethod
def tag_name(row: pd.Series) -> str:
name = row[AC.NAME]
def tag_name(
row: pd.Series, remove_brackets: bool = REMOVE_BRACKETS
) -> str:
name = row[AC.TAGNAME]
# Remove brackets, then replace characters other than letters +
# underscore (\w), numbers (\d), and period (\.) with "_".
return re.sub(r"[^\w\d\.]", "_", name.replace("[", "").replace("]", ""))
if remove_brackets:
name = name.replace("[", "").replace("]", "")
pattern = r"[^\w\d\.]"
else:
pattern = r"[^\w\d\.\[\]]"
return re.sub(pattern, "_", name)
@staticmethod
def address(row: pd.Series) -> str:
address = row[AC.ADDRESS]
try:
return iec.Address.from_string(address).to_modbus()
# InvalidSpecError is raised if `address` is not an IEC address (most
# likely it's already Modbus), so return it as-is.
except iec.InvalidSpecError:
return address
# ValueError is raised if `address` is blank.
except ValueError:
return ""
@@ -109,13 +122,9 @@ class TranslatorsToAF:
frame to an AuDaGeFrame.
"""
@staticmethod
def name(row: pd.Series) -> str:
return row[KC.TAG_NAME]
@staticmethod
def type(row: pd.Series) -> str:
return str(iec.IECType.from_opcua(row[KC.DATA_TYPE]).value)
return str(iec.IECType.from_opcua(row[KC.DATA_TYPE]))
@staticmethod
def address(row: pd.Series) -> str:
@@ -146,7 +155,7 @@ translators_from_af = {
}
translators_to_af = {
AC.NAME: TranslatorsToAF.name,
AC.TAGNAME: KC.TAG_NAME,
AC.TYPE: TranslatorsToAF.type,
AC.ADDRESS: TranslatorsToAF.address,
AC.COMMENT: KC.DESCRIPTION,
@@ -158,5 +167,32 @@ class Frame(
mandatory_columns=Columns.values(),
translators_from_af=translators_from_af,
translators_to_af=translators_to_af,
dropna_columns=KC.ADDRESS,
):
"""Dataframe for Kepserver CSV."""
@classmethod
def from_file(cls, path: Traversable) -> Frame:
"""Read Kepserver CSV file into a dataframe. Custom processing consists
of adding leading zeroes the decimal part of boolean addresses.
"""
def add_leading_zeroes(a: str) -> str:
"""Add a leading zero the decimal part of bit-packed boolean
addresses. E.g. change ".3" to ".03". This is important mainly to
differentiate ".1" (which is changed to ".01") from ".10" in case
the trailing zero of the latter gets dropped.
"""
PATTERN = ( # Match:
r"\." # Decimal point,
r"(\d\"?,?)" # then capture one digit and an optional comma
r"$" # at the end of the address string
)
SUB = ( # Replace with
r".0" # decimal point, zero,
"\1" # and the captured digit and maybe comma.
)
return re.sub(PATTERN, SUB, a)
converters = {"Address": add_leading_zeroes}
return cls(pd.read_csv(filepath_or_buffer=path, converters=converters))

View File

@@ -8,28 +8,44 @@ date: 2026-09-14
desc: Extensions to DataFrame for AuDaGe purposes.
"""
import os
from abc import ABC, abstractmethod
from collections.abc import Callable, Sequence
from collections.abc import Callable, Iterable
from importlib.resources.abc import Traversable
from typing import Any
import pandas as pd
from AuDaGe.better_enum import BetterStrEnum
from AuDaGe.debug_tools import MeasureTime
type DataFrameLike = pd.DataFrame | Sequence[Sequence]
type ColumnNames = str | tuple[str, ...]
type Translator[T] = Callable[pd.Series, T] | T
"""Type that provides a value for a DataFrame cell by providing either a
Callable that generates a value from a pandas Series (i.e. DataFrame row) or a
type DataFrameLike = pd.DataFrame | Iterable[Iterable]
type Translator[T] = Callable[[pd.Series], T] | T
"""Type that provides a value for a DataFrame cell by providing either a
Callable that generates a value from a pandas Series (i.e. DataFrame row) or a
constant default value.
"""
type Translators = dict[str, Translator[any]]
type Translators = dict[str, Translator[Any]]
"""A mapping of column names to Translators."""
def verb(value: any) -> any:
def wrap_with_tuple(arg) -> tuple:
return (
tuple(arg)
if (isinstance(arg, Iterable) and not isinstance(arg, str))
else (arg,)
)
def tuple_union(*args) -> tuple:
tuples = [wrap_with_tuple(arg) for arg in args]
return tuple(set().union(*tuples))
def verb(value):
"""Returns the input *verb*atim."""
return value
@@ -44,9 +60,10 @@ class MetaFrame(type):
clsname,
bases,
attrs,
mandatory_columns: tuple[str] = (),
mandatory_columns: tuple[str, ...] = (),
translators_from_af: Translators | None = None,
translators_to_af: Translators | None = None,
dropna_columns: ColumnNames = (),
):
attrs["mandatory_columns"] = mandatory_columns
attrs["translators_from_af"] = (
@@ -55,6 +72,7 @@ class MetaFrame(type):
attrs["translators_to_af"] = (
{} if translators_to_af is None else translators_to_af
)
attrs["dropna_columns"] = wrap_with_tuple(dropna_columns)
return super().__new__(cls, clsname, bases, attrs)
@@ -63,13 +81,18 @@ class Frame(pd.DataFrame, metaclass=MetaFrame):
and provides methods for converting to and from AuDaGeFrame and importing
and exporting to files. Base class for all other Frame classes."""
dropna_columns: tuple[str, ...]
translators_from_af: Translators
translators_to_af: Translators
def __init__(
self,
data: DataFrameLike = None,
data: DataFrameLike | None = None,
validate: bool = True,
name: str = "",
translators_from_af: Translators | None = None,
translators_to_af: Translators | None = None,
dropna_columns: ColumnNames = (),
):
# Initialization
if data is None:
@@ -83,6 +106,7 @@ class Frame(pd.DataFrame, metaclass=MetaFrame):
self.translators_to_af |= (
{} if translators_to_af is None else translators_to_af
)
self.dropna_columns = tuple_union(self.dropna_columns, dropna_columns)
# Validation
missing_columns = self.missing_columns()
if validate and missing_columns != []:
@@ -108,9 +132,10 @@ class Frame(pd.DataFrame, metaclass=MetaFrame):
@staticmethod
def translate(
output_format: Callable[DataFrameLike, any],
output_format: Callable[[DataFrameLike], Any],
input_frame: pd.DataFrame,
translators: Translators,
dropna_columns: ColumnNames = (),
):
frame_dict = {}
for key in translators:
@@ -135,44 +160,57 @@ class Frame(pd.DataFrame, metaclass=MetaFrame):
# Translator is a constant.
else:
frame_dict[key] = translators[key]
return output_format(frame_dict)
output = output_format(frame_dict)
if len(dropna_columns) > 0:
output.dropna(subset=dropna_columns, inplace=True)
return output
else:
return output_format() if not filter else output # ty: ignore[missing-argument]
@classmethod
def from_AF(
cls,
frame: AuDaGeFrame,
translators: Translators | None = None,
dropna_columns: ColumnNames = (),
) -> Frame:
if translators is None:
translators = cls.translators_from_af
return cls.translate(cls, frame, translators)
translators = cls.translators_from_af | (
{} if translators is None else translators
)
dropna_columns = tuple_union(cls.dropna_columns, dropna_columns)
return cls.translate(cls, frame, translators, dropna_columns)
@classmethod
def from_file(cls, path: os.PathLike) -> Frame:
def from_file(cls, path: Traversable) -> Frame:
"""Minimal implementation of the `from_file` method. Reads `path` as a
CSV file into a dataframe and returns the latter.
"""
return cls(pd.read_csv(path))
return cls(data=pd.read_csv(filepath_or_buffer=path))
def to_AF(
self,
translators: Translators | None = None,
dropna_columns: ColumnNames = (),
) -> AuDaGeFrame:
if translators is None:
translators = self.translators_to_af
return self.translate(AuDaGeFrame, self, translators)
translators = self.translators_to_af | (
{} if translators is None else translators
)
dropna_columns = tuple_union(self.dropna_columns, dropna_columns)
return self.translate(
AuDaGeFrame, self, translators, AuDaGeFrame.dropna_columns
)
def to_file(self, path: os.PathLike):
def to_file(self, path: Traversable):
"""Minimal implementation of `to_file` method. Writes `frame` to `path`
as a CSV.
"""
self.to_csv(path, index=False)
self.to_csv(path_or_buf=path, index=False)
class AuDaGeColumns(BetterStrEnum):
"""Enum of columns required by AuDaGeFrame."""
NAME = "name"
TAGNAME = "tagname"
TYPE = "type"
ADDRESS = "address"
COMMENT = "comment"
@@ -191,9 +229,9 @@ class Reader(ABC):
"""
@abstractmethod
def read(self, path: os.PathLike) -> Frame:
def read(self, path: Traversable) -> Frame:
"""Read a file into a Frame."""
@abstractmethod
def write(self, path: os.PathLike):
def write(self, path: Traversable):
"""Write a Frame to a file."""

View File

@@ -58,7 +58,7 @@ class Address:
address_type: AddressType | str = AddressType.M,
address_length: AddressLength | str | None = AddressLength.W,
word: float | str = 0,
bit: int | str | None = None,
bit: float | str | None = None,
):
"""Initialize an IEC address object.
@@ -106,6 +106,9 @@ class Address:
else:
self.address_length = address_length
try:
# ty warns about `bit` being None, which is handled by `except
# TypeError`.
# ty: ignore[invalid-argument-type]
bit_int = int(bit)
except TypeError, ValueError:
bit_int = None
@@ -132,7 +135,7 @@ class Address:
def __add__(self, other: Address | float) -> Address:
return self._combine(other, operator.add)
def __eq__(self, other: any) -> bool:
def __eq__(self, other: object) -> bool:
try:
return all(
self.__dict__[name] == other.__dict__[name]
@@ -178,7 +181,6 @@ class Address:
match = re.match(cls.IEC_PATTERN, spec, flags=re.IGNORECASE)
except TypeError:
match = None
print(spec)
if match is None:
raise InvalidSpecError(cls.__class__.__name__, spec)
return cls(*match.groups())
@@ -211,7 +213,7 @@ class Address:
if match is None:
raise InvalidSpecError(class_name, spec)
word_init = match.group(1)
bit_init = match.group(2)
bit_init = str(match.group(2))
else:
word_init = str(spec // 1)
bit_init = None if isinstance(spec, int) else (spec % 1) * 100
@@ -245,7 +247,7 @@ class Address:
case _:
raise InvalidSpecError(
class_name,
spec,
str(spec),
"Modbus addresses must start with 0, 1, 3, or 4.",
)
return cls(address_type, address_length, word, bit)
@@ -261,6 +263,27 @@ class Address:
re.match(cls.MODBUS_PATTERN, spec, flags=re.IGNORECASE) is not None
)
@classmethod
def iec_to_modbus(cls, iec_address: str, digits: int = 6) -> str:
"""Convert an address from IEC to Modbus without raising an error."""
try:
return cls.from_string(iec_address).to_modbus(digits)
# InvalidSpecError is raised if `value` is not an IEC address
# (most likely it's already Modbus), so return it as-is.
except InvalidSpecError:
return iec_address
# ValueError is raised if `value` is blank.
except ValueError:
return ""
@classmethod
def modbus_to_iec(cls, modbus_address: str, digits: int = 0) -> str:
"""Convert an address from Modbus to IEC without raising an error."""
try:
return cls.from_modbus(modbus_address).to_string(digits)
except InvalidSpecError:
return modbus_address
# Instance methods
def is_even(self) -> bool:
return self.word % 2 == 0 and not self.bit
@@ -320,6 +343,6 @@ class Address:
(
first_digit,
f"{self.word:0{digits - 1}d}",
f".{self.bit:02d}" if self.bit else "",
f".{self.bit:02d}" if self.bit is not None else "",
)
)

View File

@@ -42,17 +42,17 @@ desc: Definitions for IEC 61131 data type paradigms.
import itertools
import re
from collections.abc import Callable, Iterable
from enum import Enum, StrEnum, auto
from math import ceil, prod
import pandas as pd
from numpy import nan
from AuDaGe.frame import AuDaGeFrame
from .iec_common import *
type ArrayBounds = Sequence[Sequence[int, int]]
type ArrayBounds = Iterable[Iterable[int]]
DEFAULT_STRING_LENGTH = 16
"""Length of a default IEC string: 16 characters."""
@@ -83,9 +83,9 @@ class IECType:
self,
name: str,
iec_class: IECClass,
length: int,
length: float,
array_bounds: ArrayBounds | None = None,
array_type: str | None = None,
array_type: IECType | None = None,
):
self.name = name
self.iec_class = iec_class
@@ -109,7 +109,7 @@ class IECType:
spec_cleaned = spec.strip().upper()
if spec_cleaned == "STRING":
return cls(spec, IECClass.CHAR, string_length * DataUnit.BYTE)
return OPCUAtoIEC[spec_cleaned]
return OPCUAtoIEC[spec_cleaned].value
@classmethod
def from_string(
@@ -117,7 +117,7 @@ class IECType:
spec: str,
udt_lengths: dict | None = None,
allow_unknown: bool = True,
unknown_length: int = UNKNOWN_LENGTH,
unknown_length: float = UNKNOWN_LENGTH,
) -> IECType:
if not isinstance(spec, str):
raise TypeError("`spec` must be a string.")
@@ -151,7 +151,7 @@ class IECType:
def get_length(
self,
unit: DataUnit = DataUnit.WORD,
round_function: Callable[float, float] = ceil,
round_function: Callable[[float], float] = ceil,
) -> float:
"""Get the length of the data type in the specified unit and if
necessary round the value using the specified function.
@@ -201,22 +201,25 @@ class IECType:
f"The `array_expansion` method can only be called on an array "
f"type, not {self.name}."
)
all_indices = None
all_indices: pd.DataFrame
for i, (lbound, ubound) in enumerate(self.array_bounds):
dim_indices = pd.Series(
range(int(lbound), int(ubound) + 1), name=f"dim{i}"
)
if all_indices is None:
try:
all_indices = all_indices.join(dim_indices, how="cross")
except NameError:
all_indices = pd.DataFrame(dim_indices)
continue
all_indices = all_indices.join(dim_indices, how="cross")
columns = {
"name": all_indices.apply(
lambda row: f"{name}[{','.join(str(dim) for dim in row)}]",
axis=1,
),
"type": str(self.array_type),
"address": nan,
"address": [
self.array_type.length * i for i in range(len(all_indices))
],
"comment": comment,
"parent": self.name.upper(),
}
@@ -307,6 +310,8 @@ class IECType:
STRING_SPEC_PATTERN = r"^\s*str(?:ing)?\s*\[(\d+)\]\s*$"
match = re.match(STRING_SPEC_PATTERN, spec, re.IGNORECASE)
try:
# Unresolved attribute is handled by `except AttributeError` below.
# ty: ignore[unresolved-attribute]
length = int(match.group(1)) * DataUnit.BYTE
if length <= 0:
raise InvalidSpecError(

File diff suppressed because it is too large Load Diff

View File

@@ -19,20 +19,27 @@ try:
from tests import assets, output
except ImportError: # pragma: no cover
import assets # noqa: I001
import output
import output # ty: ignore[unresolved-import]
INPUT_FILES = (
"RTU_Status_List.csv",
"WA3251_PLC_001_1.csv",
)
class TestCSV(unittest.TestCase):
def setUp(self):
self.input_file = resources.files(assets).joinpath(
"WA3251_PLC_001_1.csv"
self.input_files = (
resources.files(assets).joinpath(file) for file in INPUT_FILES
)
self.output_folder = resources.files(output)
with MeasureTime(
do_print=True,
message="Kepserver CSV import completed in {time:.3f} {unit}",
):
self.csv_frame = csv.Frame.from_file(self.input_file)
self.csv_frames = (
csv.Frame.from_file(file) for file in self.input_files
)
with MeasureTime(
do_print=True,
message=(
@@ -40,30 +47,34 @@ class TestCSV(unittest.TestCase):
"{time:.3f} {unit}"
),
):
self.af = self.csv_frame.to_AF()
self.afs = (frame.to_AF() for frame in self.csv_frames)
def test_read(self):
self.assertIsInstance(self.csv_frame, csv.Frame)
for frame in self.csv_frames:
self.assertIsInstance(frame, csv.Frame)
def test_from_af(self):
with MeasureTime(
do_print=True,
message=(
"Kepserver CSV frame translation from AuDaGeFrame completed in "
"{time:.3f} {unit}"
),
):
translate_from_af = csv.Frame.from_AF(self.af)
self.assertIsInstance(translate_from_af, csv.Frame)
translate_from_af.to_file(
self.output_folder.joinpath("kep_from_af.csv")
)
for i, frame in enumerate(self.afs):
with MeasureTime(
do_print=True,
message=(
"Kepserver CSV frame translation from AuDaGeFrame "
"completed in {time:.3f} {unit}"
),
):
translate_from_af = csv.Frame.from_AF(frame)
self.assertIsInstance(translate_from_af, csv.Frame)
translate_from_af.to_file(
self.output_folder.joinpath("kep_from_af{i}.csv")
)
def test_to_af(self):
self.assertIsInstance(self.af, AuDaGeFrame)
for frame in self.afs:
self.assertIsInstance(frame, AuDaGeFrame)
def test_write(self):
self.csv_frame.to_file(self.output_folder.joinpath("kepserver.csv"))
for i, frame in enumerate(self.csv_frames):
frame.to_file(self.output_folder.joinpath(f"kepserver{i}.csv"))
if __name__ == "__main__":

View File

@@ -18,7 +18,7 @@ try:
from tests import assets, output
except ImportError: # pragma: no cover
import assets # noqa: I001
import output
import output # ty: ignore[unresolved-import]
class TestXSY(unittest.TestCase):
@@ -30,13 +30,12 @@ class TestXSY(unittest.TestCase):
self.in_paths = [
file
for file in resources.files(assets).iterdir()
if os.path.splitext(file)[-1].lower() == ".xsy"
if os.path.splitext(str(file))[-1].lower() == ".xsy"
]
def test_DataBlock(self):
for in_path in self.in_paths:
datablock = DataBlock.from_file(in_path)
self.assertIsInstance(datablock, DataBlock)
datablock = XSYBlock.from_file(in_path)
self.assertIsInstance(datablock, XSYBlock)
def test_Definitions(self):
@@ -52,7 +51,7 @@ class TestXSYReader(unittest.TestCase):
self.in_paths = [
file
for file in resources.files(assets).iterdir()
if os.path.splitext(file)[-1].lower() == ".xsy"
if os.path.splitext(str(file))[-1].lower() == ".xsy"
]
self.out_path = resources.files(output)
@@ -66,7 +65,7 @@ class TestXSYReader(unittest.TestCase):
def test_export(self):
for path in self.in_paths:
file_name = os.path.splitext(os.path.split(path)[-1])[0]
file_name = os.path.splitext(os.path.split(str(path))[-1])[0]
xsy = self.xsy_reader.read(path, expand=False)
xsy.datablock.to_csv(
self.out_path.joinpath(f"{file_name}_datablock.csv"),
@@ -78,7 +77,9 @@ class TestXSYReader(unittest.TestCase):
)
debug = False
for debug_step in (
XSY.DebugStep if debug else [XSY.DebugStep.COMPLETE]
XSY.DebugStep
if debug
else [XSY.DebugStep.MAKE_ARRAY_DEFS, XSY.DebugStep.COMPLETE]
):
xsy.expand_datablock(debug_step=debug_step).to_csv(
self.out_path.joinpath(