1
- # mypy: disable-error-code="arg-type,attr-defined,no-untyped-def,type-arg,union-attr,operator"
2
1
import json
3
2
import os .path
4
3
from enum import Enum
5
- from typing import Any , Dict , List , Optional , Tuple
4
+ from typing import Any , Dict , List , Tuple , Union
6
5
7
6
from strictdoc .backend .sdoc .models .document import SDocDocument
8
7
from strictdoc .backend .sdoc .models .document_config import DocumentConfig
9
8
from strictdoc .backend .sdoc .models .document_from_file import DocumentFromFile
10
9
from strictdoc .backend .sdoc .models .document_grammar import DocumentGrammar
10
+ from strictdoc .backend .sdoc .models .model import SDocElementIF
11
11
from strictdoc .backend .sdoc .models .node import SDocNode
12
12
from strictdoc .backend .sdoc .models .reference import (
13
13
ChildReqReference ,
17
17
)
18
18
from strictdoc .backend .sdoc .models .section import SDocSection
19
19
from strictdoc .backend .sdoc .models .type_system import (
20
+ GrammarElementField ,
20
21
RequirementFieldType ,
21
22
)
23
+ from strictdoc .core .document_tree import DocumentTree
22
24
from strictdoc .core .project_config import ProjectConfig
23
25
from strictdoc .core .traceability_index import TraceabilityIndex
26
+ from strictdoc .helpers .cast import assert_cast
24
27
25
28
26
29
class TAG (Enum ):
@@ -42,15 +45,18 @@ def export_tree(
42
45
traceability_index : TraceabilityIndex ,
43
46
project_config : ProjectConfig ,
44
47
output_json_root : str ,
45
- ):
46
- project_tree_dict = {
48
+ ) -> None :
49
+ project_tree_dict : Dict [ str , Any ] = {
47
50
"_COMMENT" : (
48
51
"Fields with _ are metadata. "
49
52
"Fields without _ are the actual document/section/requirement/other content."
50
53
),
51
54
"DOCUMENTS" : [],
52
55
}
53
- for document_ in traceability_index .document_tree .document_list :
56
+ document_tree : DocumentTree = assert_cast (
57
+ traceability_index .document_tree , DocumentTree
58
+ )
59
+ for document_ in document_tree .document_list :
54
60
if not project_config .export_included_documents :
55
61
if document_ .document_is_included ():
56
62
continue
@@ -65,7 +71,7 @@ def export_tree(
65
71
output_json_file .write (project_tree_json )
66
72
67
73
@classmethod
68
- def _write_document (cls , document : SDocDocument ) -> Dict :
74
+ def _write_document (cls , document : SDocDocument ) -> Dict [ str , Any ] :
69
75
document_dict : Dict [str , Any ] = {
70
76
"TITLE" : document .title ,
71
77
"REQ_PREFIX" : None ,
@@ -149,7 +155,7 @@ def _write_document(cls, document: SDocDocument) -> Dict:
149
155
document_grammar : DocumentGrammar = document .grammar
150
156
151
157
for element_ in document_grammar .elements :
152
- element_dict = {
158
+ element_dict : Dict [ str , Any ] = {
153
159
"NODE_TYPE" : element_ .tag ,
154
160
"FIELDS" : [],
155
161
"RELATIONS" : [],
@@ -160,10 +166,9 @@ def _write_document(cls, document: SDocDocument) -> Dict:
160
166
cls ._write_grammar_field_type (grammar_field )
161
167
)
162
168
163
- relations : List = element_ .relations
164
- if len (relations ) > 0 :
165
- for element_relation_ in relations :
166
- relation_dict = {
169
+ if len (element_ .relations ) > 0 :
170
+ for element_relation_ in element_ .relations :
171
+ relation_dict : Dict [str , str ] = {
167
172
"TYPE" : element_relation_ .relation_type ,
168
173
}
169
174
if element_relation_ .relation_role is not None :
@@ -178,8 +183,15 @@ def _write_document(cls, document: SDocDocument) -> Dict:
178
183
return document_dict
179
184
180
185
@classmethod
181
- def _write_node (cls , node , document , level_stack : Optional [Tuple ]) -> Dict :
182
- def get_level_string_ (node_ ) -> str :
186
+ def _write_node (
187
+ cls ,
188
+ node : SDocElementIF ,
189
+ document : SDocDocument ,
190
+ level_stack : Tuple [int , ...],
191
+ ) -> Dict [str , Any ]:
192
+ def get_level_string_ (
193
+ node_ : Union [SDocNode , SDocSection , SDocDocument ],
194
+ ) -> str :
183
195
return (
184
196
""
185
197
if node_ .ng_resolved_custom_level == "None"
@@ -190,8 +202,8 @@ def get_level_string_(node_) -> str:
190
202
)
191
203
)
192
204
193
- if isinstance (node , ( SDocSection , SDocDocument ) ):
194
- section_dict = cls ._write_section (
205
+ if isinstance (node , SDocSection ):
206
+ section_dict : Dict [ str , Any ] = cls ._write_section (
195
207
node , document , get_level_string_ (node )
196
208
)
197
209
@@ -200,10 +212,10 @@ def get_level_string_(node_) -> str:
200
212
if subnode_ .ng_resolved_custom_level is None :
201
213
current_number += 1
202
214
203
- subnode_dict = cls ._write_node (
215
+ section_subnode_dict : Dict [ str , Any ] = cls ._write_node (
204
216
subnode_ , document , level_stack + (current_number ,)
205
217
)
206
- section_dict [JSONKey .NODES ].append (subnode_dict )
218
+ section_dict [JSONKey .NODES ].append (section_subnode_dict )
207
219
208
220
return section_dict
209
221
@@ -218,32 +230,49 @@ def get_level_string_(node_) -> str:
218
230
return subnode_dict
219
231
220
232
elif isinstance (node , SDocDocument ):
221
- node_dict : Dict [str , List [Dict ]] = {JSONKey .NODES : []}
233
+ node_dict : Dict [str , Any ] = cls ._write_included_document (
234
+ node , get_level_string_ (node )
235
+ )
222
236
223
237
current_number = 0
224
238
for subnode_ in node .section_contents :
225
239
if subnode_ .ng_resolved_custom_level is None :
226
240
current_number += 1
227
- subnode_dict = cls ._write_node (
241
+ document_subnode_dict : Dict [ str , Any ] = cls ._write_node (
228
242
subnode_ , document , level_stack + (current_number ,)
229
243
)
230
- node_dict [JSONKey .NODES ].append (subnode_dict )
244
+ node_dict [JSONKey .NODES ].append (document_subnode_dict )
231
245
232
246
return node_dict
233
247
234
248
elif isinstance (node , DocumentFromFile ):
249
+ resolved_document = assert_cast (
250
+ node .resolved_document , SDocDocument
251
+ )
235
252
subnode_dict = cls ._write_node (
236
- node . resolved_document , document , level_stack
253
+ resolved_document , document , level_stack
237
254
)
238
255
return subnode_dict
239
256
240
257
else :
241
258
raise NotImplementedError # pragma: no cover
242
259
260
+ @classmethod
261
+ def _write_included_document (
262
+ cls , node : SDocDocument , level_string : str
263
+ ) -> Dict [str , Any ]:
264
+ node_dict : Dict [str , Any ] = {
265
+ "_TOC" : level_string ,
266
+ "TYPE" : "SECTION" ,
267
+ "TITLE" : node .reserved_title ,
268
+ JSONKey .NODES : [],
269
+ }
270
+ return node_dict
271
+
243
272
@classmethod
244
273
def _write_section (
245
274
cls , section : SDocSection , document : SDocDocument , level_string : str
246
- ) -> Dict :
275
+ ) -> Dict [ str , Any ] :
247
276
assert isinstance (section , (SDocSection , SDocDocument ))
248
277
node_dict : Dict [str , Any ] = {
249
278
"_TOC" : level_string ,
@@ -272,7 +301,7 @@ def _write_section(
272
301
@classmethod
273
302
def _write_requirement (
274
303
cls , node : SDocNode , document : SDocDocument , level_string : str
275
- ) -> Dict :
304
+ ) -> Dict [ str , Any ] :
276
305
node_dict : Dict [str , Any ] = {
277
306
"_TOC" : level_string ,
278
307
"TYPE" : node .node_type ,
@@ -281,7 +310,8 @@ def _write_requirement(
281
310
if node .mid_permanent or document .config .enable_mid :
282
311
node_dict ["MID" ] = node .reserved_mid
283
312
284
- element = document .grammar .elements_by_type [node .node_type ]
313
+ document_grammar = assert_cast (document .grammar , DocumentGrammar )
314
+ element = document_grammar .elements_by_type [node .node_type ]
285
315
286
316
for element_field in element .fields :
287
317
field_name = element_field .title
@@ -297,17 +327,19 @@ def _write_requirement(
297
327
return node_dict
298
328
299
329
@classmethod
300
- def _write_grammar_field_type (cls , grammar_field ) -> Dict :
330
+ def _write_grammar_field_type (
331
+ cls , grammar_field : GrammarElementField
332
+ ) -> Dict [str , str ]:
301
333
grammar_field_dict = {
302
334
"TITLE" : grammar_field .title ,
303
- "REQUIRED" : True if grammar_field .required else False ,
335
+ "REQUIRED" : " True" if grammar_field .required else " False" ,
304
336
# FIXME: Support more grammar types.
305
337
"TYPE" : RequirementFieldType .STRING ,
306
338
}
307
339
return grammar_field_dict
308
340
309
341
@staticmethod
310
- def _write_requirement_relations (node : SDocNode ) -> List :
342
+ def _write_requirement_relations (node : SDocNode ) -> List [ Dict [ str , str ]] :
311
343
relations_list = []
312
344
313
345
reference : Reference
0 commit comments