jcmsuite-data-tree 1.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- jcmsuite_data_tree/__init__.py +91 -0
- jcmsuite_data_tree/_build_info.py +4 -0
- jcmsuite_data_tree/compat_runtime/__init__.py +10 -0
- jcmsuite_data_tree/compat_runtime/tree_writer.py +53 -0
- jcmsuite_data_tree/data_branch.py +366 -0
- jcmsuite_data_tree/data_primitives.py +1060 -0
- jcmsuite_data_tree/data_tree_exceptions.py +196 -0
- jcmsuite_data_tree/decorators.py +40 -0
- jcmsuite_data_tree/facade.py +91 -0
- jcmsuite_data_tree/parser.py +1045 -0
- jcmsuite_data_tree/py.typed +0 -0
- jcmsuite_data_tree/schema_primitives.py +989 -0
- jcmsuite_data_tree/schema_tree.py +369 -0
- jcmsuite_data_tree/system.py +102 -0
- jcmsuite_data_tree/toolbox.py +322 -0
- jcmsuite_data_tree-1.0.0.dist-info/METADATA +61 -0
- jcmsuite_data_tree-1.0.0.dist-info/RECORD +18 -0
- jcmsuite_data_tree-1.0.0.dist-info/WHEEL +4 -0
|
@@ -0,0 +1,1045 @@
|
|
|
1
|
+
# SPDX-License-Identifier: MIT
|
|
2
|
+
# Copyright (c) 2001-2026 JCMwave GmbH
|
|
3
|
+
|
|
4
|
+
"""Tokenization and parsing helpers for text-based data-tree files."""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import math
|
|
9
|
+
import os
|
|
10
|
+
import re
|
|
11
|
+
import sys
|
|
12
|
+
import warnings
|
|
13
|
+
from typing import cast
|
|
14
|
+
|
|
15
|
+
from . import data_branch, data_primitives, schema_primitives, schema_tree
|
|
16
|
+
from .compat_runtime import write_tree_dir
|
|
17
|
+
from .data_tree_exceptions import (
|
|
18
|
+
DataRedefinition,
|
|
19
|
+
ExclusionError,
|
|
20
|
+
InclusionError,
|
|
21
|
+
InvalidPrimitive,
|
|
22
|
+
MissingDefault,
|
|
23
|
+
MissingDefaultSonDir,
|
|
24
|
+
MissingDirOpenBracket,
|
|
25
|
+
MissingEqualSign,
|
|
26
|
+
MissingStringCloseQuotMark,
|
|
27
|
+
MissingStringOpenQuotMark,
|
|
28
|
+
ParseTreeError,
|
|
29
|
+
PrimitiveOutOfRange,
|
|
30
|
+
UnknownKeyword,
|
|
31
|
+
)
|
|
32
|
+
from .decorators import accepts
|
|
33
|
+
from .system import FilePath
|
|
34
|
+
from .toolbox import AsciiVector
|
|
35
|
+
|
|
36
|
+
FLOAT_TOKEN = re.compile(r"[-+]?(\d+(\.\d*)?|\.\d+)([eE][-+]?\d+)?")
|
|
37
|
+
|
|
38
|
+
MAX_BUFFER_SIZE = 4096
|
|
39
|
+
|
|
40
|
+
NUMBER_STOP_TOKENS = {",", ";", ":", "]", ")", "i"}
|
|
41
|
+
EMPTY_STOP_TOKENS: set[str] = set()
|
|
42
|
+
type ScalarTokenType = type[int | float | complex | str]
|
|
43
|
+
type NumericScalar = int | float | complex
|
|
44
|
+
type NumericTokenType = type[int | float | complex]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class TreeReader:
|
|
48
|
+
"""Read scalar tokens and sections from a data-tree file."""
|
|
49
|
+
|
|
50
|
+
@accepts(object, FilePath, fileName=FilePath)
|
|
51
|
+
def __init__(self, fileName: FilePath) -> None:
|
|
52
|
+
"""Initialize a reader for the given file path."""
|
|
53
|
+
self.fileName: str = fileName.Path()
|
|
54
|
+
self.data: str = ""
|
|
55
|
+
self.pos: int = 0
|
|
56
|
+
self.buffer: str = ""
|
|
57
|
+
|
|
58
|
+
def Open(self, string: str | None = None) -> None:
|
|
59
|
+
"""Load the file content and skip any leading block header."""
|
|
60
|
+
if string is None:
|
|
61
|
+
with open(self.fileName, "r") as f:
|
|
62
|
+
self.data = f.read()
|
|
63
|
+
else:
|
|
64
|
+
self.data = string
|
|
65
|
+
self.data += " "
|
|
66
|
+
self.pos = 0
|
|
67
|
+
# skip header
|
|
68
|
+
if self.TestToken("/*"):
|
|
69
|
+
self.Skip(2)
|
|
70
|
+
while self.pos < len(self.data):
|
|
71
|
+
self.pos += 1
|
|
72
|
+
if self.data[(self.pos - 2) : (self.pos - 1)] == "*/":
|
|
73
|
+
break
|
|
74
|
+
|
|
75
|
+
def FileName(self) -> str:
|
|
76
|
+
"""Return the current file name."""
|
|
77
|
+
return self.fileName
|
|
78
|
+
|
|
79
|
+
@accepts(object, type, bool, dtype=type, moveForward=bool)
|
|
80
|
+
def Get(
|
|
81
|
+
self,
|
|
82
|
+
dtype: ScalarTokenType,
|
|
83
|
+
moveForward: bool = True,
|
|
84
|
+
stopTokens: set[str] = EMPTY_STOP_TOKENS,
|
|
85
|
+
ignoreSpaces: bool = False,
|
|
86
|
+
) -> tuple[object, bool]:
|
|
87
|
+
"""Parse the next token as the requested scalar type."""
|
|
88
|
+
success = True
|
|
89
|
+
pos_old = self.TellG()
|
|
90
|
+
movedToToken = True
|
|
91
|
+
rc: object
|
|
92
|
+
if moveForward:
|
|
93
|
+
movedToToken = self.FindToken()
|
|
94
|
+
if dtype is str:
|
|
95
|
+
self.buffer, gotToken = self.__GetToken(
|
|
96
|
+
stopTokens, ignoreSpaces=ignoreSpaces
|
|
97
|
+
)
|
|
98
|
+
else:
|
|
99
|
+
self.buffer, gotToken = self.__GetToken(
|
|
100
|
+
NUMBER_STOP_TOKENS, ignoreSpaces=ignoreSpaces
|
|
101
|
+
)
|
|
102
|
+
try:
|
|
103
|
+
if moveForward and not movedToToken or not gotToken:
|
|
104
|
+
raise ParseTreeError("Unable to locate token.", self)
|
|
105
|
+
elif dtype is str:
|
|
106
|
+
rc = self.buffer
|
|
107
|
+
elif dtype is int:
|
|
108
|
+
try:
|
|
109
|
+
dat = self.buffer.strip()
|
|
110
|
+
dat.replace("Inf", "inf")
|
|
111
|
+
rc = int(float(dat))
|
|
112
|
+
except (TypeError, ValueError):
|
|
113
|
+
raise ParseTreeError("Unable to convert to integer.", self)
|
|
114
|
+
elif dtype is float:
|
|
115
|
+
dat = self.buffer.strip()
|
|
116
|
+
dat.replace("Inf", "inf")
|
|
117
|
+
try:
|
|
118
|
+
rc = float(self.buffer)
|
|
119
|
+
except (TypeError, ValueError):
|
|
120
|
+
raise ParseTreeError("Unable to convert to float.", self)
|
|
121
|
+
if self.TestToken("i", False):
|
|
122
|
+
raise ParseTreeError(
|
|
123
|
+
'Found "i" directly behind float number.', self
|
|
124
|
+
)
|
|
125
|
+
elif dtype is complex:
|
|
126
|
+
rc_r = float(0)
|
|
127
|
+
rc_i = float(0)
|
|
128
|
+
if self.buffer[0] == ("("):
|
|
129
|
+
success_r = True
|
|
130
|
+
try:
|
|
131
|
+
rc_r = float(self.buffer[1:])
|
|
132
|
+
except (TypeError, ValueError):
|
|
133
|
+
success_r = False
|
|
134
|
+
if not success_r:
|
|
135
|
+
raise ParseTreeError(
|
|
136
|
+
"Real part (float) of complex number not found.", self
|
|
137
|
+
)
|
|
138
|
+
if not self.SkipToken(","):
|
|
139
|
+
raise ParseTreeError(
|
|
140
|
+
'Part "," of braced complex number not found.', self
|
|
141
|
+
)
|
|
142
|
+
rc_i_value, success_i1 = self.Get(float)
|
|
143
|
+
success_i2 = self.SkipToken(")")
|
|
144
|
+
rc_i = cast(float, rc_i_value)
|
|
145
|
+
if not success_i1 and success_i2:
|
|
146
|
+
rc_i = float(0)
|
|
147
|
+
if not success_i2:
|
|
148
|
+
raise ParseTreeError(
|
|
149
|
+
"Closing bracket in (*,*)-notation of complex number not found.",
|
|
150
|
+
self,
|
|
151
|
+
)
|
|
152
|
+
else:
|
|
153
|
+
try:
|
|
154
|
+
flt = FLOAT_TOKEN.search(self.buffer)
|
|
155
|
+
if flt is None:
|
|
156
|
+
raise ValueError("missing real component")
|
|
157
|
+
rc_r = float(self.buffer[flt.start() : flt.end()])
|
|
158
|
+
remaining_buffer = self.buffer[flt.end() :]
|
|
159
|
+
flt = FLOAT_TOKEN.search(remaining_buffer)
|
|
160
|
+
if flt is not None:
|
|
161
|
+
rc_i = float(remaining_buffer[flt.start() : flt.end()])
|
|
162
|
+
if not self.SkipToken("i", False):
|
|
163
|
+
raise ValueError("missing imaginary component")
|
|
164
|
+
else:
|
|
165
|
+
rc_i = float(0)
|
|
166
|
+
except ValueError:
|
|
167
|
+
raise ParseTreeError(
|
|
168
|
+
"Unable to parse complex number in <x>+<y>i format.", self
|
|
169
|
+
)
|
|
170
|
+
if self.SkipToken("i", False):
|
|
171
|
+
rc_i = rc_r
|
|
172
|
+
rc_r = float(0)
|
|
173
|
+
rc = complex(rc_r, rc_i)
|
|
174
|
+
else:
|
|
175
|
+
raise TypeError(f'Unsupported type: "{dtype!s}"')
|
|
176
|
+
except ParseTreeError:
|
|
177
|
+
self.SeekG(pos_old)
|
|
178
|
+
rc = dtype()
|
|
179
|
+
success = False
|
|
180
|
+
return rc, success
|
|
181
|
+
|
|
182
|
+
@accepts(object, (str), bool, token=(str), lookAhead=bool)
|
|
183
|
+
def TestToken(self, token: str, lookAhead: bool = True) -> bool:
|
|
184
|
+
"""Return whether the next token matches the expected text."""
|
|
185
|
+
old_pos = self.TellG()
|
|
186
|
+
if lookAhead and not self.FindToken():
|
|
187
|
+
self.SeekG(old_pos)
|
|
188
|
+
return False
|
|
189
|
+
if self.data[self.pos : (self.pos + len(token))] != token:
|
|
190
|
+
self.SeekG(old_pos)
|
|
191
|
+
return False
|
|
192
|
+
else:
|
|
193
|
+
self.SeekG(old_pos)
|
|
194
|
+
return True
|
|
195
|
+
|
|
196
|
+
@accepts(object, (str), bool, token=(str), moveForward=bool)
|
|
197
|
+
def SkipToken(self, token: str, moveForward: bool = True) -> bool:
|
|
198
|
+
"""Skip an expected token and report whether it was found."""
|
|
199
|
+
old_pos = self.pos
|
|
200
|
+
try:
|
|
201
|
+
if moveForward and not self.FindToken():
|
|
202
|
+
raise ParseTreeError("No token found.", self)
|
|
203
|
+
if not self.TestToken(token, False):
|
|
204
|
+
raise ParseTreeError("Token not found.", self)
|
|
205
|
+
except ParseTreeError:
|
|
206
|
+
self.pos = old_pos
|
|
207
|
+
return False
|
|
208
|
+
self.Skip(len(token))
|
|
209
|
+
return True
|
|
210
|
+
|
|
211
|
+
def TellG(self) -> int:
|
|
212
|
+
"""Return the current reader offset."""
|
|
213
|
+
return self.pos
|
|
214
|
+
|
|
215
|
+
def SeekG(self, pos: int) -> bool:
|
|
216
|
+
"""Move the reader to an absolute offset."""
|
|
217
|
+
if pos > len(self.data) - 1:
|
|
218
|
+
return False
|
|
219
|
+
self.pos = pos
|
|
220
|
+
return True
|
|
221
|
+
|
|
222
|
+
def Skip(self, nBytes: int) -> bool:
|
|
223
|
+
"""Advance the reader by a fixed number of bytes."""
|
|
224
|
+
return self.SeekG(self.pos + nBytes)
|
|
225
|
+
|
|
226
|
+
def ReportPosition(self) -> tuple[str, int, int]:
|
|
227
|
+
"""Return the current line content, line number, and column number."""
|
|
228
|
+
lineNumber = self.data[: self.pos].count("\n") + 1
|
|
229
|
+
lastNewLine = self.data[: self.pos].rfind("\n")
|
|
230
|
+
columnNumber = self.pos - lastNewLine
|
|
231
|
+
lineBegin = lastNewLine + 1
|
|
232
|
+
nextNewLine = self.data[self.pos :].find("\n")
|
|
233
|
+
if nextNewLine == -1:
|
|
234
|
+
nextNewLine = len(self.data) - self.pos
|
|
235
|
+
lineEnd = self.pos + nextNewLine
|
|
236
|
+
lineContent = self.data[lineBegin:lineEnd]
|
|
237
|
+
return lineContent, lineNumber, columnNumber
|
|
238
|
+
|
|
239
|
+
def FindToken(self) -> bool:
|
|
240
|
+
"""Advance to the next non-whitespace, non-comment token."""
|
|
241
|
+
old_pos = self.pos
|
|
242
|
+
while self.pos < len(self.data):
|
|
243
|
+
if self.data[self.pos] == "#":
|
|
244
|
+
while self.pos < len(self.data) and self.data[self.pos] != "\n":
|
|
245
|
+
self.pos += 1
|
|
246
|
+
if self.pos == len(self.data):
|
|
247
|
+
break
|
|
248
|
+
currentChar = self.data[self.pos]
|
|
249
|
+
if currentChar > " ":
|
|
250
|
+
return True
|
|
251
|
+
self.pos += 1
|
|
252
|
+
self.pos = old_pos
|
|
253
|
+
return False
|
|
254
|
+
|
|
255
|
+
@accepts(object, set, bool, stopTokens=set, ignoreSpaces=bool)
|
|
256
|
+
def __GetToken(
|
|
257
|
+
self, stopTokens: set[str] | None = None, ignoreSpaces: bool = False
|
|
258
|
+
) -> tuple[str, bool]:
|
|
259
|
+
"""Read the next token fragment until a stop condition is reached.
|
|
260
|
+
|
|
261
|
+
Parameters
|
|
262
|
+
----------
|
|
263
|
+
stopTokens : None
|
|
264
|
+
Value passed via ``stopTokens``.
|
|
265
|
+
ignoreSpaces : None
|
|
266
|
+
Value passed via ``ignoreSpaces``.
|
|
267
|
+
|
|
268
|
+
Returns
|
|
269
|
+
-------
|
|
270
|
+
None
|
|
271
|
+
No value is returned.
|
|
272
|
+
|
|
273
|
+
Notes
|
|
274
|
+
-----
|
|
275
|
+
Internal API: this helper is an implementation detail and may change
|
|
276
|
+
without notice.
|
|
277
|
+
"""
|
|
278
|
+
|
|
279
|
+
if stopTokens is None:
|
|
280
|
+
stopTokens = set()
|
|
281
|
+
whites = {"\t", "\r", "\n", "\f", "\v"}
|
|
282
|
+
if self.pos >= len(self.data):
|
|
283
|
+
return "", False
|
|
284
|
+
pos_offset = 0
|
|
285
|
+
while pos_offset < MAX_BUFFER_SIZE and self.pos + pos_offset < len(self.data):
|
|
286
|
+
currentChar = self.data[self.pos + pos_offset]
|
|
287
|
+
if (
|
|
288
|
+
(currentChar == " " and not ignoreSpaces)
|
|
289
|
+
or (currentChar in whites)
|
|
290
|
+
or (currentChar in stopTokens)
|
|
291
|
+
):
|
|
292
|
+
break
|
|
293
|
+
pos_offset += 1
|
|
294
|
+
out = self.data[self.pos : (self.pos + pos_offset)]
|
|
295
|
+
self.pos += pos_offset
|
|
296
|
+
if len(out) == 0:
|
|
297
|
+
success = False
|
|
298
|
+
else:
|
|
299
|
+
success = pos_offset < MAX_BUFFER_SIZE
|
|
300
|
+
return out, success
|
|
301
|
+
|
|
302
|
+
def Copy(self) -> TreeReader:
|
|
303
|
+
"""Reject copying of readers."""
|
|
304
|
+
raise RuntimeError("TreeReader cannot be copied.")
|
|
305
|
+
|
|
306
|
+
|
|
307
|
+
@accepts(TreeReader, (str), reader=TreeReader, sectionName=(str))
|
|
308
|
+
def SkipSection(reader: TreeReader, sectionName: str | None = None) -> bool:
|
|
309
|
+
"""Skip one directory section, optionally matching a specific name."""
|
|
310
|
+
old_pos = reader.TellG()
|
|
311
|
+
if isinstance(sectionName, str) and not reader.TestToken(sectionName):
|
|
312
|
+
return True
|
|
313
|
+
stopTokens = {"{", "}"}
|
|
314
|
+
_dummy_token, success = reader.Get(str, stopTokens=stopTokens)
|
|
315
|
+
if not success:
|
|
316
|
+
return False
|
|
317
|
+
reader.SkipToken("=")
|
|
318
|
+
if not reader.SkipToken("{"):
|
|
319
|
+
reader.SeekG(old_pos)
|
|
320
|
+
return True
|
|
321
|
+
nOpenBrackets = 1
|
|
322
|
+
while nOpenBrackets != 0:
|
|
323
|
+
if reader.SkipToken("{"):
|
|
324
|
+
nOpenBrackets += 1
|
|
325
|
+
elif reader.SkipToken("}"):
|
|
326
|
+
nOpenBrackets -= 1
|
|
327
|
+
else:
|
|
328
|
+
_dummy_token, success = reader.Get(str, stopTokens=stopTokens)
|
|
329
|
+
if not success:
|
|
330
|
+
reader.SeekG(old_pos)
|
|
331
|
+
return False
|
|
332
|
+
return True
|
|
333
|
+
|
|
334
|
+
|
|
335
|
+
def WriteTreeDir(treeDir: data_branch.TreeDir, indentation: int = 0) -> str:
|
|
336
|
+
"""Serialize a tree directory to text."""
|
|
337
|
+
return write_tree_dir(treeDir, indentation=indentation)
|
|
338
|
+
|
|
339
|
+
|
|
340
|
+
# @accepts(schema_tree.DirSchema, data_branch.TreeDir, TreeReader, set, set,
|
|
341
|
+
# prototypeTreeDir=schema_tree.DirSchema,
|
|
342
|
+
# father=data_branch.TreeDir,
|
|
343
|
+
# reader=TreeReader,
|
|
344
|
+
# rejectedItems=set,
|
|
345
|
+
# ignoredItems=set)
|
|
346
|
+
def ParseTreeDir(
|
|
347
|
+
prototypeTreeDir: schema_tree.DirSchema,
|
|
348
|
+
father: data_branch.TreeDir,
|
|
349
|
+
reader: TreeReader | None,
|
|
350
|
+
rejectedItems: set[str] | None = None,
|
|
351
|
+
ignoredItems: set[str] | None = None,
|
|
352
|
+
) -> None:
|
|
353
|
+
"""Parse one schema-guided directory subtree into `father`."""
|
|
354
|
+
rejected_items: set[str] = set() if rejectedItems is None else rejectedItems
|
|
355
|
+
ignored_items: set[str] = set() if ignoredItems is None else ignoredItems
|
|
356
|
+
stopTokens: set[str] = {"{", "}", "="}
|
|
357
|
+
readData: set[str] = set()
|
|
358
|
+
exclusions: dict[str, str] = {}
|
|
359
|
+
inclusions: dict[str, str] = {}
|
|
360
|
+
hasSon = len(prototypeTreeDir.GetChoiceDirs()) > 0
|
|
361
|
+
prototypeTreeDirName = prototypeTreeDir.GetName()
|
|
362
|
+
tokenNameDefault: dict[int, str] = {}
|
|
363
|
+
if len(prototypeTreeDirName) > 3:
|
|
364
|
+
stringLength = len(prototypeTreeDirName)
|
|
365
|
+
bagPart = prototypeTreeDirName[stringLength - 3 :]
|
|
366
|
+
if bagPart == "Bag":
|
|
367
|
+
tokenNameDefault[0] = prototypeTreeDirName[:-3]
|
|
368
|
+
# downward compatibility: skip nonexist. directories XYBag
|
|
369
|
+
skipLevel = 0
|
|
370
|
+
# read tree from file
|
|
371
|
+
while reader is not None and reader.FindToken():
|
|
372
|
+
if reader.SkipToken("}"):
|
|
373
|
+
if skipLevel == 0:
|
|
374
|
+
break
|
|
375
|
+
skipLevel -= 1
|
|
376
|
+
continue
|
|
377
|
+
tokenName = ""
|
|
378
|
+
pos_beforeToken = reader.TellG()
|
|
379
|
+
token_name_value, _success = reader.Get(str, stopTokens=stopTokens)
|
|
380
|
+
tokenName = cast(str, token_name_value)
|
|
381
|
+
if len(tokenName) == 0:
|
|
382
|
+
if len(tokenNameDefault[skipLevel]) != 0:
|
|
383
|
+
tokenName = tokenNameDefault[skipLevel]
|
|
384
|
+
else:
|
|
385
|
+
raise ParseTreeError("Parser error:", reader)
|
|
386
|
+
tokenNameTrue = prototypeTreeDir.GetTrueName(tokenName)
|
|
387
|
+
isMultiple = prototypeTreeDir.IsMultiple(tokenNameTrue)
|
|
388
|
+
if tokenNameTrue in rejected_items:
|
|
389
|
+
raise UnknownKeyword(reader, tokenName)
|
|
390
|
+
if tokenNameTrue in exclusions:
|
|
391
|
+
raise ExclusionError(reader, tokenName, exclusions[tokenNameTrue])
|
|
392
|
+
excludedItems = list(prototypeTreeDir.GetExclusions(tokenNameTrue))
|
|
393
|
+
for excludedItem in excludedItems:
|
|
394
|
+
if excludedItem in readData:
|
|
395
|
+
raise ExclusionError(reader, excludedItem, tokenName)
|
|
396
|
+
exclusions[excludedItem] = tokenNameTrue
|
|
397
|
+
includedItems = list(prototypeTreeDir.GetInclusions(tokenNameTrue))
|
|
398
|
+
for includedItem in includedItems:
|
|
399
|
+
inclusions[includedItem] = tokenNameTrue
|
|
400
|
+
prototypePrimitive = prototypeTreeDir.GetPrimitive(tokenNameTrue)
|
|
401
|
+
prototypeTreeItem = prototypeTreeDir.GetDir(tokenNameTrue)
|
|
402
|
+
adaptor = prototypeTreeDir.GetAdaptor(tokenNameTrue)
|
|
403
|
+
if prototypePrimitive is None and prototypeTreeItem is not None:
|
|
404
|
+
prototypePrimitive = prototypeTreeItem.GetShortCut()
|
|
405
|
+
equalSignRead = reader.SkipToken("=")
|
|
406
|
+
if prototypePrimitive is not None and prototypeTreeItem is not None:
|
|
407
|
+
if reader.TestToken("{", True):
|
|
408
|
+
prototypePrimitive = None
|
|
409
|
+
else:
|
|
410
|
+
prototypeTreeItem = None
|
|
411
|
+
if prototypePrimitive is not None and not equalSignRead:
|
|
412
|
+
raise MissingEqualSign(reader)
|
|
413
|
+
if (
|
|
414
|
+
prototypeTreeDirName == "Constant"
|
|
415
|
+
and len(tokenName) > 4
|
|
416
|
+
and tokenName[:5] == "Entry"
|
|
417
|
+
and prototypeTreeDir.GetPrimitive("Tensor") is not None
|
|
418
|
+
):
|
|
419
|
+
reader.SeekG(pos_beforeToken)
|
|
420
|
+
prototypePrimitive = prototypeTreeDir.GetPrimitive("Tensor")
|
|
421
|
+
tokenName = "Tensor"
|
|
422
|
+
tokenNameTrue = tokenName
|
|
423
|
+
if prototypePrimitive is not None:
|
|
424
|
+
if not isMultiple and tokenNameTrue in readData:
|
|
425
|
+
raise DataRedefinition(reader, tokenName)
|
|
426
|
+
primitive = cast(
|
|
427
|
+
data_primitives.TreePrimitive, prototypePrimitive.CreateTreePrimitive()
|
|
428
|
+
)
|
|
429
|
+
if not primitive.Read(reader):
|
|
430
|
+
raise InvalidPrimitive(reader, prototypePrimitive.TypeName())
|
|
431
|
+
if not prototypePrimitive.CheckRange(primitive):
|
|
432
|
+
raise PrimitiveOutOfRange(reader)
|
|
433
|
+
if prototypeTreeDir.IsObsolete(tokenNameTrue):
|
|
434
|
+
dummyException = ParseTreeError("", reader)
|
|
435
|
+
warnings.warn(f'{dummyException!s} Tag "{tokenName}" is obsolete.')
|
|
436
|
+
else:
|
|
437
|
+
father.AddPrimitive(tokenNameTrue, primitive)
|
|
438
|
+
readData.add(tokenNameTrue)
|
|
439
|
+
continue
|
|
440
|
+
if prototypeTreeItem is not None:
|
|
441
|
+
if not isMultiple and tokenNameTrue in readData:
|
|
442
|
+
raise DataRedefinition(reader, tokenName)
|
|
443
|
+
if tokenNameTrue in ignored_items:
|
|
444
|
+
reader.SeekG(pos_beforeToken)
|
|
445
|
+
if not SkipSection(reader, tokenName):
|
|
446
|
+
raise ParseTreeError("Unable to skip ignored section.", reader)
|
|
447
|
+
continue
|
|
448
|
+
subDir = father.Create(tokenNameTrue)
|
|
449
|
+
if tokenName != tokenNameTrue:
|
|
450
|
+
subDir.SetAlias(tokenName)
|
|
451
|
+
if not reader.SkipToken("{"):
|
|
452
|
+
raise MissingDirOpenBracket(reader)
|
|
453
|
+
ParseTreeDir(prototypeTreeItem, subDir, reader)
|
|
454
|
+
father.AddTreeDir(subDir)
|
|
455
|
+
readData.add(tokenNameTrue)
|
|
456
|
+
continue
|
|
457
|
+
if adaptor is not None:
|
|
458
|
+
dummyException = ParseTreeError("", reader)
|
|
459
|
+
warnings.warn(f'{dummyException!s} Tag "{tokenName}" is obsolete.')
|
|
460
|
+
if not isMultiple and tokenNameTrue in readData:
|
|
461
|
+
raise DataRedefinition(reader, tokenName)
|
|
462
|
+
if tokenNameTrue in ignored_items:
|
|
463
|
+
reader.SeekG(pos_beforeToken)
|
|
464
|
+
if not SkipSection(reader, tokenName):
|
|
465
|
+
raise ParseTreeError(
|
|
466
|
+
"Unable to skip ignored adaptor section.", reader
|
|
467
|
+
)
|
|
468
|
+
continue
|
|
469
|
+
if not reader.SkipToken("{"):
|
|
470
|
+
raise MissingDirOpenBracket(reader)
|
|
471
|
+
originalTree = father.Create(tokenNameTrue)
|
|
472
|
+
if adaptor.schema is None:
|
|
473
|
+
continue
|
|
474
|
+
ParseTreeDir(adaptor.schema, originalTree, reader)
|
|
475
|
+
path = AsciiVector()
|
|
476
|
+
path.Import(originalTree.FinalDir().GetFullName())
|
|
477
|
+
while path.Size() > 0:
|
|
478
|
+
if path in adaptor.treeAdaptors:
|
|
479
|
+
break
|
|
480
|
+
path.PopBack()
|
|
481
|
+
if path.Size() == 0:
|
|
482
|
+
continue
|
|
483
|
+
tree_adaptor = cast(data_branch.TreeAdaptor, adaptor.treeAdaptors[path])
|
|
484
|
+
transformedTree = tree_adaptor.Transform(originalTree)
|
|
485
|
+
primitives = transformedTree.GetPrimitives()
|
|
486
|
+
for primIter in primitives.items():
|
|
487
|
+
itemName = primIter[0][0]
|
|
488
|
+
if not prototypeTreeDir.IsMultiple(itemName) and itemName in readData:
|
|
489
|
+
raise DataRedefinition(reader, itemName)
|
|
490
|
+
father.Add(itemName, primIter[1].Copy())
|
|
491
|
+
readData.add(itemName)
|
|
492
|
+
dirs = transformedTree.GetDirs()
|
|
493
|
+
for dirIter in dirs.items():
|
|
494
|
+
itemName = dirIter[0][0]
|
|
495
|
+
if not prototypeTreeDir.IsMultiple(itemName) and itemName in readData:
|
|
496
|
+
raise DataRedefinition(reader, itemName)
|
|
497
|
+
father.AddTreeDir(dirIter[1].Copy())
|
|
498
|
+
readData.add(itemName)
|
|
499
|
+
father_trunk_dir = father.GetTrunkDir()
|
|
500
|
+
transformed_trunk_dir = transformedTree.GetTrunkDir()
|
|
501
|
+
if father_trunk_dir is not None and transformed_trunk_dir is not None:
|
|
502
|
+
raise ExclusionError(
|
|
503
|
+
reader,
|
|
504
|
+
father_trunk_dir.GetName(),
|
|
505
|
+
transformed_trunk_dir.GetName(),
|
|
506
|
+
)
|
|
507
|
+
if transformed_trunk_dir is not None:
|
|
508
|
+
father.SetTrunkDir(transformed_trunk_dir.GetName())
|
|
509
|
+
readData.add(tokenNameTrue)
|
|
510
|
+
continue
|
|
511
|
+
if tokenName.find(".") != -1:
|
|
512
|
+
raise UnknownKeyword(reader, tokenName)
|
|
513
|
+
son = prototypeTreeDir.GetChoiceDir(tokenNameTrue)
|
|
514
|
+
if son is None:
|
|
515
|
+
if prototypeTreeDir.IsDummyDir(tokenNameTrue):
|
|
516
|
+
skipLevel += 1
|
|
517
|
+
if not reader.SkipToken("{"):
|
|
518
|
+
raise MissingDirOpenBracket(reader)
|
|
519
|
+
father.AddDummyDir(tokenNameTrue)
|
|
520
|
+
dummyException = ParseTreeError("", reader)
|
|
521
|
+
warnings.warn(f'{dummyException!s} Tag "{tokenName}" is obsolete.')
|
|
522
|
+
continue
|
|
523
|
+
# downward compatibility: non-existing XYBags are ignored
|
|
524
|
+
stringLength = len(tokenNameTrue)
|
|
525
|
+
if stringLength > 3 and tokenNameTrue[-3:] == "Bag":
|
|
526
|
+
skipLevel += 1
|
|
527
|
+
tokenNameDefault[skipLevel] = tokenNameTrue[:-3]
|
|
528
|
+
if not reader.SkipToken("{"):
|
|
529
|
+
raise MissingDirOpenBracket(reader)
|
|
530
|
+
continue
|
|
531
|
+
else:
|
|
532
|
+
# downward compatibility: parse vector V = [VX VY VZ]
|
|
533
|
+
if ParseLegacy3Vector(tokenNameTrue, prototypeTreeDir, father, reader):
|
|
534
|
+
actualTokenName = tokenNameTrue[:-1]
|
|
535
|
+
readData.add(actualTokenName)
|
|
536
|
+
continue
|
|
537
|
+
else:
|
|
538
|
+
raise UnknownKeyword(reader, tokenName)
|
|
539
|
+
if not isMultiple and tokenNameTrue in readData:
|
|
540
|
+
raise DataRedefinition(reader, tokenName)
|
|
541
|
+
trunk_dir = father.GetTrunkDir()
|
|
542
|
+
if trunk_dir is not None:
|
|
543
|
+
raise ExclusionError(reader, trunk_dir.GetName(), tokenName)
|
|
544
|
+
if not reader.SkipToken("{"):
|
|
545
|
+
raise MissingDirOpenBracket(reader)
|
|
546
|
+
son_ = father.Create(son.GetName(), father)
|
|
547
|
+
ParseTreeDir(son, son_, reader)
|
|
548
|
+
father.SetTrunkDir(son.GetName())
|
|
549
|
+
# end while reader!=None and reader.FindToken()
|
|
550
|
+
# fill default values into tree
|
|
551
|
+
prototypeTrees = prototypeTreeDir.GetDirs()
|
|
552
|
+
for treeIter in prototypeTrees.items():
|
|
553
|
+
if prototypeTreeDir.IsOptional(treeIter[0]):
|
|
554
|
+
continue
|
|
555
|
+
if prototypeTreeDir.IsObsolete(treeIter[0]):
|
|
556
|
+
continue
|
|
557
|
+
if (
|
|
558
|
+
treeIter[0] not in readData
|
|
559
|
+
and treeIter[0] not in rejected_items
|
|
560
|
+
and treeIter[0] not in ignored_items
|
|
561
|
+
and treeIter[0] not in exclusions
|
|
562
|
+
and not prototypeTreeDir.IsMultiple(treeIter[0])
|
|
563
|
+
):
|
|
564
|
+
try:
|
|
565
|
+
subDir = father.Create(treeIter[0])
|
|
566
|
+
ParseTreeDir(treeIter[1], subDir, None)
|
|
567
|
+
father.AddTreeDir(subDir)
|
|
568
|
+
readData.add(treeIter[0])
|
|
569
|
+
except MissingDefault:
|
|
570
|
+
raise MissingDefault(reader, treeIter[0])
|
|
571
|
+
except MissingDefaultSonDir:
|
|
572
|
+
raise MissingDefault(reader, treeIter[0])
|
|
573
|
+
prototypePrimitives = prototypeTreeDir.GetPrimitives()
|
|
574
|
+
for primitiveIter in prototypePrimitives.items():
|
|
575
|
+
if primitiveIter[0][1] > 0:
|
|
576
|
+
continue
|
|
577
|
+
if prototypeTreeDir.IsOptional(primitiveIter[0][0]):
|
|
578
|
+
continue
|
|
579
|
+
if prototypeTreeDir.IsObsolete(primitiveIter[0][0]):
|
|
580
|
+
continue
|
|
581
|
+
if (
|
|
582
|
+
primitiveIter[0][0] not in readData
|
|
583
|
+
and primitiveIter[0][0] not in rejected_items
|
|
584
|
+
and primitiveIter[0][0] not in ignored_items
|
|
585
|
+
and primitiveIter[0][0] not in exclusions
|
|
586
|
+
and not prototypeTreeDir.IsMultiple(primitiveIter[0][0])
|
|
587
|
+
):
|
|
588
|
+
if not primitiveIter[1].HasDefault():
|
|
589
|
+
raise MissingDefault(reader, primitiveIter[0][0])
|
|
590
|
+
primitive = cast(
|
|
591
|
+
data_primitives.TreePrimitive,
|
|
592
|
+
primitiveIter[1].CreateTreePrimitive(),
|
|
593
|
+
)
|
|
594
|
+
father.Add(primitiveIter[0][0], primitive)
|
|
595
|
+
readData.add(primitiveIter[0][0])
|
|
596
|
+
for iter in inclusions.items():
|
|
597
|
+
if iter[0] not in readData:
|
|
598
|
+
raise InclusionError(reader, iter[0], iter[1])
|
|
599
|
+
if hasSon and father.GetTrunkDir() is None:
|
|
600
|
+
son = prototypeTreeDir.DefaultChoiceDir()
|
|
601
|
+
if son is None:
|
|
602
|
+
raise MissingDefaultSonDir(reader)
|
|
603
|
+
son_ = father.Create(son.GetName(), father)
|
|
604
|
+
ParseTreeDir(son, son_, None)
|
|
605
|
+
father.SetTrunkDir(son.GetName())
|
|
606
|
+
|
|
607
|
+
|
|
608
|
+
# additional methods
|
|
609
|
+
def ParseLegacy3VectorComponent(
|
|
610
|
+
dtype: NumericTokenType,
|
|
611
|
+
tokenName: str,
|
|
612
|
+
schema: schema_primitives.PrimitiveSchema | None,
|
|
613
|
+
treeDir: data_branch.TreeDir,
|
|
614
|
+
reader: TreeReader,
|
|
615
|
+
comp: int,
|
|
616
|
+
) -> bool:
|
|
617
|
+
"""Parse one legacy vector component into a three-entry container."""
|
|
618
|
+
if schema is None:
|
|
619
|
+
return False
|
|
620
|
+
value, success = reader.Get(dtype)
|
|
621
|
+
if not success:
|
|
622
|
+
return False
|
|
623
|
+
if treeDir.GetPrimitive(tokenName) is None:
|
|
624
|
+
treeDir.AddPrimitive(
|
|
625
|
+
tokenName,
|
|
626
|
+
cast(data_primitives.TreePrimitive, schema.CreateTreePrimitive()),
|
|
627
|
+
)
|
|
628
|
+
actualVector: list[NumericScalar] | None = None
|
|
629
|
+
if isinstance(schema, schema_primitives.TensorSchema) and schema.rank == 1:
|
|
630
|
+
tensor_primitive = treeDir.GetPrimitive(tokenName)
|
|
631
|
+
if tensor_primitive is None:
|
|
632
|
+
return False
|
|
633
|
+
tensor_value = cast(
|
|
634
|
+
data_primitives.TensorPrimitive, tensor_primitive
|
|
635
|
+
).GetValue()
|
|
636
|
+
actualVector = cast(list[NumericScalar], tensor_value.entries)
|
|
637
|
+
elif isinstance(schema, schema_primitives.VectorSchema):
|
|
638
|
+
vector_primitive = treeDir.GetPrimitive(tokenName)
|
|
639
|
+
if vector_primitive is None:
|
|
640
|
+
return False
|
|
641
|
+
actual_vector_value = vector_primitive.GetValue()
|
|
642
|
+
actualVector = cast(list[NumericScalar], actual_vector_value)
|
|
643
|
+
if len(actualVector) == 0: # just created, w/o default
|
|
644
|
+
for ii in range(3):
|
|
645
|
+
actualVector.append(dtype(0))
|
|
646
|
+
if actualVector is None or len(actualVector) != 3:
|
|
647
|
+
return False
|
|
648
|
+
actualVector[comp] = cast(NumericScalar, value)
|
|
649
|
+
return True
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
def ParseLegacy3Vector(
|
|
653
|
+
tokenName: str,
|
|
654
|
+
prototypeTreeDir: schema_tree.DirSchema,
|
|
655
|
+
treeDir: data_branch.TreeDir,
|
|
656
|
+
reader: TreeReader,
|
|
657
|
+
) -> bool:
|
|
658
|
+
"""Parse one legacy `X/Y/Z` vector component notation."""
|
|
659
|
+
stringLength = len(tokenName)
|
|
660
|
+
if stringLength < 2:
|
|
661
|
+
return False
|
|
662
|
+
comp = -1
|
|
663
|
+
if tokenName[stringLength - 1] == "X":
|
|
664
|
+
comp = 0
|
|
665
|
+
elif tokenName[stringLength - 1] == "Y":
|
|
666
|
+
comp = 1
|
|
667
|
+
elif tokenName[stringLength - 1] == "Z":
|
|
668
|
+
comp = 2
|
|
669
|
+
if comp == -1:
|
|
670
|
+
return False
|
|
671
|
+
actualTokenName = tokenName[:-1]
|
|
672
|
+
prototypePrimitive = prototypeTreeDir.GetPrimitive(actualTokenName)
|
|
673
|
+
if prototypePrimitive is None:
|
|
674
|
+
return False
|
|
675
|
+
if (
|
|
676
|
+
isinstance(prototypePrimitive, schema_primitives.TensorSchema)
|
|
677
|
+
and prototypePrimitive._rank == 1
|
|
678
|
+
) and ParseLegacy3VectorComponent(
|
|
679
|
+
complex, actualTokenName, prototypePrimitive, treeDir, reader, comp
|
|
680
|
+
):
|
|
681
|
+
return True
|
|
682
|
+
return isinstance(
|
|
683
|
+
prototypePrimitive, schema_primitives.VectorSchema
|
|
684
|
+
) and ParseLegacy3VectorComponent(
|
|
685
|
+
cast(NumericTokenType, prototypePrimitive._type),
|
|
686
|
+
actualTokenName,
|
|
687
|
+
prototypePrimitive,
|
|
688
|
+
treeDir,
|
|
689
|
+
reader,
|
|
690
|
+
comp,
|
|
691
|
+
)
|
|
692
|
+
|
|
693
|
+
|
|
694
|
+
def ReadString(
|
|
695
|
+
dtype: type[str], reader: TreeReader, quotMarks: bool = False
|
|
696
|
+
) -> tuple[str, bool]:
|
|
697
|
+
"""Read one string token and expand supported environment substitutions."""
|
|
698
|
+
if quotMarks:
|
|
699
|
+
if not reader.SkipToken('"'):
|
|
700
|
+
raise MissingStringOpenQuotMark(reader)
|
|
701
|
+
stopTokens = {'"'}
|
|
702
|
+
ignoreSpaces = True
|
|
703
|
+
else:
|
|
704
|
+
stopTokens = set()
|
|
705
|
+
ignoreSpaces = False
|
|
706
|
+
readValue, success = reader.Get(
|
|
707
|
+
dtype, stopTokens=stopTokens, ignoreSpaces=ignoreSpaces
|
|
708
|
+
)
|
|
709
|
+
if not success:
|
|
710
|
+
return "", False
|
|
711
|
+
readValue = cast(str, readValue)
|
|
712
|
+
if quotMarks and not reader.SkipToken('"'):
|
|
713
|
+
raise MissingStringCloseQuotMark(reader)
|
|
714
|
+
string_pos = 0
|
|
715
|
+
while string_pos < len(readValue) - 1:
|
|
716
|
+
string_pos = readValue.find("$", string_pos)
|
|
717
|
+
key = ""
|
|
718
|
+
replacedSubValue = ""
|
|
719
|
+
found = False
|
|
720
|
+
string_pos_start = string_pos
|
|
721
|
+
string_pos += 1
|
|
722
|
+
while string_pos < len(readValue) and readValue[string_pos] != "$":
|
|
723
|
+
key_ = key + readValue[string_pos]
|
|
724
|
+
_replacedSubValue = os.getenv(key_, "")
|
|
725
|
+
if len(_replacedSubValue) == 0 and found:
|
|
726
|
+
break
|
|
727
|
+
key = key_
|
|
728
|
+
if len(_replacedSubValue) != 0:
|
|
729
|
+
replacedSubValue = _replacedSubValue
|
|
730
|
+
found = True
|
|
731
|
+
if key == "THIS":
|
|
732
|
+
found = True
|
|
733
|
+
break
|
|
734
|
+
string_pos += 1
|
|
735
|
+
if found:
|
|
736
|
+
if key == "THIS":
|
|
737
|
+
filePath = FilePath(reader.FileName())
|
|
738
|
+
replacedSubValue = filePath.BaseName()
|
|
739
|
+
subStringBeforeReplacementLocation = readValue[:string_pos_start]
|
|
740
|
+
subStringAfterReplacementLocation = readValue[string_pos + 1 :]
|
|
741
|
+
readValue = (
|
|
742
|
+
subStringBeforeReplacementLocation
|
|
743
|
+
+ replacedSubValue
|
|
744
|
+
+ subStringAfterReplacementLocation
|
|
745
|
+
)
|
|
746
|
+
string_pos = string_pos_start + len(replacedSubValue)
|
|
747
|
+
return readValue, True
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
def ParseNumberVectorAsList(
|
|
751
|
+
dtype: NumericTokenType, reader: TreeReader
|
|
752
|
+
) -> tuple[list[NumericScalar], bool]:
|
|
753
|
+
"""Parse one scalar or vector literal into a flat value list."""
|
|
754
|
+
entryList: list[NumericScalar] = []
|
|
755
|
+
if not reader.SkipToken("["):
|
|
756
|
+
readEntry, success = reader.Get(dtype)
|
|
757
|
+
if not success:
|
|
758
|
+
return [], False
|
|
759
|
+
entryList.append(cast(NumericScalar, readEntry))
|
|
760
|
+
return entryList, True
|
|
761
|
+
while True:
|
|
762
|
+
while reader.TestToken("["):
|
|
763
|
+
readList, success = ParseNumberVectorAsList(dtype, reader)
|
|
764
|
+
if not success:
|
|
765
|
+
return [], False
|
|
766
|
+
entryList.extend(readList)
|
|
767
|
+
reader.SkipToken(",")
|
|
768
|
+
if reader.SkipToken("]"):
|
|
769
|
+
return entryList, True
|
|
770
|
+
readEntry, success = reader.Get(dtype)
|
|
771
|
+
if not success:
|
|
772
|
+
return [], False
|
|
773
|
+
numeric_entry: NumericScalar = cast(NumericScalar, readEntry)
|
|
774
|
+
entryList.append(numeric_entry)
|
|
775
|
+
if reader.SkipToken(":"):
|
|
776
|
+
if isinstance(numeric_entry, complex):
|
|
777
|
+
if numeric_entry.imag != 0.0:
|
|
778
|
+
return [], False
|
|
779
|
+
else:
|
|
780
|
+
lowerBound = numeric_entry.real
|
|
781
|
+
else:
|
|
782
|
+
try:
|
|
783
|
+
lowerBound = float(numeric_entry)
|
|
784
|
+
except (TypeError, ValueError):
|
|
785
|
+
return [], False
|
|
786
|
+
readEntry, success = reader.Get(dtype)
|
|
787
|
+
if not success:
|
|
788
|
+
return [], False
|
|
789
|
+
step_entry: NumericScalar = cast(NumericScalar, readEntry)
|
|
790
|
+
if isinstance(step_entry, complex):
|
|
791
|
+
if step_entry.imag != 0.0:
|
|
792
|
+
return [], False
|
|
793
|
+
else:
|
|
794
|
+
step = step_entry.real
|
|
795
|
+
else:
|
|
796
|
+
try:
|
|
797
|
+
step = float(step_entry)
|
|
798
|
+
except (TypeError, ValueError):
|
|
799
|
+
return [], False
|
|
800
|
+
if not reader.SkipToken(":"):
|
|
801
|
+
return [], False
|
|
802
|
+
readEntry, success = reader.Get(dtype)
|
|
803
|
+
if not success:
|
|
804
|
+
return [], False
|
|
805
|
+
upper_entry: NumericScalar = cast(NumericScalar, readEntry)
|
|
806
|
+
if isinstance(upper_entry, complex):
|
|
807
|
+
if upper_entry.imag != 0.0:
|
|
808
|
+
return [], False
|
|
809
|
+
else:
|
|
810
|
+
upperBound = upper_entry.real
|
|
811
|
+
else:
|
|
812
|
+
try:
|
|
813
|
+
upperBound = float(upper_entry)
|
|
814
|
+
except (TypeError, ValueError):
|
|
815
|
+
return [], False
|
|
816
|
+
scaling = max([math.fabs(upperBound - lowerBound), math.fabs(step)])
|
|
817
|
+
newEntry = lowerBound + step
|
|
818
|
+
iStep = 1
|
|
819
|
+
if step.real > 0.0:
|
|
820
|
+
while float(upperBound - newEntry) / scaling > -1e-12:
|
|
821
|
+
entryList.append(dtype(newEntry))
|
|
822
|
+
iStep += 1
|
|
823
|
+
newEntry = lowerBound + float(iStep) * step
|
|
824
|
+
elif step.real < 0.0:
|
|
825
|
+
while float(upperBound - newEntry) / scaling > 1e-12:
|
|
826
|
+
entryList.append(dtype(newEntry))
|
|
827
|
+
iStep += 1
|
|
828
|
+
newEntry = lowerBound + float(iStep) * step
|
|
829
|
+
if math.fabs(upperBound - newEntry + step) / scaling < 1e-12:
|
|
830
|
+
entryList.pop(-1)
|
|
831
|
+
entryList.append(upperBound)
|
|
832
|
+
if reader.SkipToken("]"):
|
|
833
|
+
return entryList, True
|
|
834
|
+
else:
|
|
835
|
+
return [], False
|
|
836
|
+
if reader.SkipToken(",") and reader.SkipToken("]"):
|
|
837
|
+
return [], False
|
|
838
|
+
return entryList, True
|
|
839
|
+
|
|
840
|
+
|
|
841
|
+
def FormatXYZ(ii: int) -> str:
|
|
842
|
+
"""Return the axis suffix used by legacy tensor entry names."""
|
|
843
|
+
if ii == 1:
|
|
844
|
+
return "X"
|
|
845
|
+
elif ii == 2:
|
|
846
|
+
return "Y"
|
|
847
|
+
elif ii == 3:
|
|
848
|
+
return "Z"
|
|
849
|
+
else:
|
|
850
|
+
sys.exit(1)
|
|
851
|
+
|
|
852
|
+
|
|
853
|
+
def ParseLegacyTensorFormat(
|
|
854
|
+
dtype: NumericTokenType, rank: int, reader: TreeReader
|
|
855
|
+
) -> tuple[list[NumericScalar], bool]:
|
|
856
|
+
"""Parse the legacy tensor block syntax into a flat entry list."""
|
|
857
|
+
pos_old = reader.TellG()
|
|
858
|
+
entryList: list[NumericScalar] = []
|
|
859
|
+
index: list[int] = [0] * (rank + 1)
|
|
860
|
+
inBrackets = reader.SkipToken("{")
|
|
861
|
+
tensorTree = schema_tree.DirSchema.Create(name="TensorDir")
|
|
862
|
+
for ii in range(1, rank + 1):
|
|
863
|
+
index[ii] = 1
|
|
864
|
+
while index[1] <= 3:
|
|
865
|
+
oStream = "Entry"
|
|
866
|
+
for r in range(1, rank + 1):
|
|
867
|
+
oStream += FormatXYZ(index[r])
|
|
868
|
+
zero = dtype(0)
|
|
869
|
+
entry = schema_primitives.NumberSchema.Create(zero, dtype=dtype)
|
|
870
|
+
tensorTree.AddPrimitive(oStream, entry)
|
|
871
|
+
r = rank
|
|
872
|
+
while r > 0:
|
|
873
|
+
if index[r] < 3 or r == 1:
|
|
874
|
+
index[r] += 1
|
|
875
|
+
break
|
|
876
|
+
else:
|
|
877
|
+
index[r] = 1
|
|
878
|
+
r = r - 1
|
|
879
|
+
tensorDir = data_branch.TreeDir.Create("Tensor")
|
|
880
|
+
try:
|
|
881
|
+
ParseTreeDir(tensorTree, tensorDir, reader)
|
|
882
|
+
except ParseTreeError as err:
|
|
883
|
+
reader.SeekG(pos_old)
|
|
884
|
+
print("\nParse Error: " + err.message + "\n")
|
|
885
|
+
return [], False
|
|
886
|
+
for ii in range(1, rank + 1):
|
|
887
|
+
index[ii] = 1
|
|
888
|
+
while index[rank] <= 3:
|
|
889
|
+
oStream = "Entry"
|
|
890
|
+
for r in range(rank, 0, -1):
|
|
891
|
+
oStream += FormatXYZ(index[r])
|
|
892
|
+
try:
|
|
893
|
+
primitive = tensorDir.GetPrimitive(oStream)
|
|
894
|
+
if primitive is None:
|
|
895
|
+
return [], False
|
|
896
|
+
primitive_value = cast(NumericScalar, primitive.GetValue())
|
|
897
|
+
if dtype is int:
|
|
898
|
+
if isinstance(primitive_value, complex):
|
|
899
|
+
return [], False
|
|
900
|
+
value: NumericScalar = int(primitive_value)
|
|
901
|
+
elif dtype is float:
|
|
902
|
+
if isinstance(primitive_value, complex):
|
|
903
|
+
return [], False
|
|
904
|
+
value = float(primitive_value)
|
|
905
|
+
else:
|
|
906
|
+
value = complex(primitive_value)
|
|
907
|
+
except (AttributeError, TypeError, ValueError):
|
|
908
|
+
return [], False
|
|
909
|
+
entryList.append(value)
|
|
910
|
+
r = 1
|
|
911
|
+
while r <= rank:
|
|
912
|
+
if index[r] < 3 or r == rank:
|
|
913
|
+
index[r] += 1
|
|
914
|
+
break
|
|
915
|
+
else:
|
|
916
|
+
index[r] = 1
|
|
917
|
+
r += 1
|
|
918
|
+
if not inBrackets:
|
|
919
|
+
reader.SeekG(pos_old)
|
|
920
|
+
reader.SkipToken("{")
|
|
921
|
+
stopTokens = set("}")
|
|
922
|
+
while True:
|
|
923
|
+
_dummy, success = reader.Get(str, stopTokens=stopTokens)
|
|
924
|
+
if not success:
|
|
925
|
+
break
|
|
926
|
+
if reader.TestToken("}"):
|
|
927
|
+
break
|
|
928
|
+
return entryList, True
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
def ParseLegacyRotationFormat(reader: TreeReader) -> tuple[list[list[float]], bool]:
|
|
932
|
+
"""Parse the legacy rotation syntax into a 3x3 matrix."""
|
|
933
|
+
entryMatrix: list[list[float]] = [[0.0 for _ in range(3)] for _ in range(3)]
|
|
934
|
+
zero = float(0)
|
|
935
|
+
rotationTree = schema_tree.DirSchema.Create(name="RotationTree")
|
|
936
|
+
eulerAngle = schema_tree.DirSchema.Create(rotationTree, "EulerAngle")
|
|
937
|
+
eulerAngle.AddPrimitive(
|
|
938
|
+
"Phi", schema_primitives.NumberSchema.Create(zero, dtype=float)
|
|
939
|
+
)
|
|
940
|
+
eulerAngle.AddPrimitive(
|
|
941
|
+
"Theta", schema_primitives.NumberSchema.Create(zero, dtype=float)
|
|
942
|
+
)
|
|
943
|
+
eulerAngle.AddPrimitive(
|
|
944
|
+
"Psi", schema_primitives.NumberSchema.Create(zero, dtype=float)
|
|
945
|
+
)
|
|
946
|
+
rotationMatrix = schema_tree.DirSchema.Create(rotationTree, "RotationMatrix")
|
|
947
|
+
one = 1.0
|
|
948
|
+
rotationMatrix.AddPrimitive(
|
|
949
|
+
"EntryXX", schema_primitives.NumberSchema.Create(one, dtype=float)
|
|
950
|
+
)
|
|
951
|
+
rotationMatrix.AddPrimitive(
|
|
952
|
+
"EntryXY", schema_primitives.NumberSchema.Create(zero, dtype=float)
|
|
953
|
+
)
|
|
954
|
+
rotationMatrix.AddPrimitive(
|
|
955
|
+
"EntryXZ", schema_primitives.NumberSchema.Create(zero, dtype=float)
|
|
956
|
+
)
|
|
957
|
+
rotationMatrix.AddPrimitive(
|
|
958
|
+
"EntryYX", schema_primitives.NumberSchema.Create(zero, dtype=float)
|
|
959
|
+
)
|
|
960
|
+
rotationMatrix.AddPrimitive(
|
|
961
|
+
"EntryYY", schema_primitives.NumberSchema.Create(one, dtype=float)
|
|
962
|
+
)
|
|
963
|
+
rotationMatrix.AddPrimitive(
|
|
964
|
+
"EntryYZ", schema_primitives.NumberSchema.Create(zero, dtype=float)
|
|
965
|
+
)
|
|
966
|
+
rotationMatrix.AddPrimitive(
|
|
967
|
+
"EntryZX", schema_primitives.NumberSchema.Create(zero, dtype=float)
|
|
968
|
+
)
|
|
969
|
+
rotationMatrix.AddPrimitive(
|
|
970
|
+
"EntryZY", schema_primitives.NumberSchema.Create(zero, dtype=float)
|
|
971
|
+
)
|
|
972
|
+
rotationMatrix.AddPrimitive(
|
|
973
|
+
"EntryZZ", schema_primitives.NumberSchema.Create(one, dtype=float)
|
|
974
|
+
)
|
|
975
|
+
rotDirRoot = data_branch.TreeDir.Create("Rotation")
|
|
976
|
+
try:
|
|
977
|
+
ParseTreeDir(rotationTree, rotDirRoot, reader)
|
|
978
|
+
except ParseTreeError:
|
|
979
|
+
return [], False
|
|
980
|
+
rotDir = rotDirRoot.GetTrunkDir()
|
|
981
|
+
if rotDir is None:
|
|
982
|
+
return [], False
|
|
983
|
+
phi = float(0)
|
|
984
|
+
theta = float(0)
|
|
985
|
+
psi = float(0)
|
|
986
|
+
if rotDir.GetName() == "EulerAngle":
|
|
987
|
+
phi_primitive = rotDir.GetPrimitive("Phi")
|
|
988
|
+
theta_primitive = rotDir.GetPrimitive("Theta")
|
|
989
|
+
psi_primitive = rotDir.GetPrimitive("Psi")
|
|
990
|
+
if phi_primitive is None or theta_primitive is None or psi_primitive is None:
|
|
991
|
+
return [], False
|
|
992
|
+
phi = float(cast(float, phi_primitive.GetValue())) / 180.0 * math.pi
|
|
993
|
+
theta = float(cast(float, theta_primitive.GetValue())) / 180.0 * math.pi
|
|
994
|
+
psi = float(cast(float, psi_primitive.GetValue())) / 180.0 * math.pi
|
|
995
|
+
cos_phi = math.cos(phi)
|
|
996
|
+
sin_phi = math.sin(phi)
|
|
997
|
+
cos_theta = math.cos(theta)
|
|
998
|
+
sin_theta = math.sin(theta)
|
|
999
|
+
cos_psi = math.cos(psi)
|
|
1000
|
+
sin_psi = math.sin(psi)
|
|
1001
|
+
entryMatrix[0][0] = cos_psi * cos_theta * cos_phi - sin_psi * sin_phi
|
|
1002
|
+
entryMatrix[1][0] = cos_psi * cos_theta * sin_phi + sin_psi * cos_phi
|
|
1003
|
+
entryMatrix[2][0] = -cos_psi * sin_theta
|
|
1004
|
+
entryMatrix[0][1] = -sin_psi * cos_theta * cos_phi - cos_psi * sin_phi
|
|
1005
|
+
entryMatrix[1][1] = -sin_psi * cos_theta * sin_phi + cos_psi * cos_phi
|
|
1006
|
+
entryMatrix[2][1] = sin_psi * sin_theta
|
|
1007
|
+
entryMatrix[0][2] = sin_theta * cos_phi
|
|
1008
|
+
entryMatrix[1][2] = sin_theta * sin_phi
|
|
1009
|
+
entryMatrix[2][2] = cos_theta
|
|
1010
|
+
elif rotDir.GetName() == "RotationMatrix":
|
|
1011
|
+
entry_xx = rotDir.GetPrimitive("EntryXX")
|
|
1012
|
+
entry_yx = rotDir.GetPrimitive("EntryYX")
|
|
1013
|
+
entry_zx = rotDir.GetPrimitive("EntryZX")
|
|
1014
|
+
entry_xy = rotDir.GetPrimitive("EntryXY")
|
|
1015
|
+
entry_yy = rotDir.GetPrimitive("EntryYY")
|
|
1016
|
+
entry_zy = rotDir.GetPrimitive("EntryZY")
|
|
1017
|
+
entry_xz = rotDir.GetPrimitive("EntryXZ")
|
|
1018
|
+
entry_yz = rotDir.GetPrimitive("EntryYZ")
|
|
1019
|
+
entry_zz = rotDir.GetPrimitive("EntryZZ")
|
|
1020
|
+
if (
|
|
1021
|
+
entry_xx is None
|
|
1022
|
+
or entry_yx is None
|
|
1023
|
+
or entry_zx is None
|
|
1024
|
+
or entry_xy is None
|
|
1025
|
+
or entry_yy is None
|
|
1026
|
+
or entry_zy is None
|
|
1027
|
+
or entry_xz is None
|
|
1028
|
+
or entry_yz is None
|
|
1029
|
+
or entry_zz is None
|
|
1030
|
+
):
|
|
1031
|
+
return [], False
|
|
1032
|
+
entryMatrix[0][0] = float(cast(float, entry_xx.GetValue()))
|
|
1033
|
+
entryMatrix[1][0] = float(cast(float, entry_yx.GetValue()))
|
|
1034
|
+
entryMatrix[2][0] = float(cast(float, entry_zx.GetValue()))
|
|
1035
|
+
entryMatrix[0][1] = float(cast(float, entry_xy.GetValue()))
|
|
1036
|
+
entryMatrix[1][1] = float(cast(float, entry_yy.GetValue()))
|
|
1037
|
+
entryMatrix[2][1] = float(cast(float, entry_zy.GetValue()))
|
|
1038
|
+
entryMatrix[0][2] = float(cast(float, entry_xz.GetValue()))
|
|
1039
|
+
entryMatrix[1][2] = float(cast(float, entry_yz.GetValue()))
|
|
1040
|
+
entryMatrix[2][2] = float(cast(float, entry_zz.GetValue()))
|
|
1041
|
+
for ii in range(3):
|
|
1042
|
+
for jj in range(3):
|
|
1043
|
+
if math.fabs(entryMatrix[ii][jj]) < 1e-14:
|
|
1044
|
+
entryMatrix[ii][jj] = float(0)
|
|
1045
|
+
return entryMatrix, True
|