pyreqif 0.9__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.
pyreqif/rif.py ADDED
@@ -0,0 +1,702 @@
1
+ #!/usr/bin/env python2
2
+ # -*- coding: utf-8 -*-
3
+ from __future__ import absolute_import
4
+
5
+ import io
6
+ from builtins import *
7
+
8
+ from lxml import etree
9
+ from lxml import objectify
10
+ import json
11
+ import sys
12
+
13
+ sys.path.append('..')
14
+ import pyreqif.pyreqif
15
+ from pprint import pprint
16
+ import re
17
+
18
+
19
+ def pretty(d, indent=0):
20
+ for key, value in d.items():
21
+ print('\t' * indent + str(key))
22
+ if isinstance(value, dict):
23
+ pretty(value, indent + 1)
24
+ else:
25
+ print('\t' * (indent + 1) + str(value))
26
+
27
+
28
+ transLationTable = {"IDENTIFIER": "identifier",
29
+ "COUNTRY-CODE": "countryCode",
30
+ "CREATION-TIME": "creationTime",
31
+ "TITLE": "title",
32
+ "COMMENT": "comment",
33
+ "AUTHOR": "author",
34
+ "LONG-NAME": "longName",
35
+ "VERSION": "version",
36
+ "SOURCE-TOOL-ID": "sourceToolId",
37
+ "LAST-CHANGE": "lastChange",
38
+ "EMBEDDED": "embedded",
39
+ "TYPE": "type",
40
+ "VALUES": "values",
41
+ "CONTENT-REF": "contentref",
42
+ "CONTENT": "content",
43
+ "DESC": "desc"}
44
+
45
+ mapReqifAttributeValue = {"default": "embeddedDoc",
46
+ "ATTRIBUTE-VALUE-EMBEDDED-DOCUMENT": "embeddedDoc",
47
+ "ATTRIBUTE-VALUE-STRING": "string",
48
+ "ATTRIBUTE-VALUE-DATE": "string",
49
+ "ATTRIBUTE-VALUE-SIMPLE": "embeddedDoc",
50
+ "ATTRIBUTE-VALUE-XHTML": "embeddedDoc",
51
+ "ATTRIBUTE-VALUE-BOOLEAN": "embeddedDoc",
52
+ "ATTRIBUTE-VALUE-REAL": "embeddedDoc",
53
+ "ATTRIBUTE-VALUE-INTEGER": "embeddedDoc"}
54
+
55
+ mapReqifAttributeDefinition = {"default": "complex",
56
+ "ATTRIBUTE-DEFINITION-COMPLEX": "complex",
57
+ "ATTRIBUTE-DEFINITION-STRING": "string",
58
+ "ATTRIBUTE-DEFINITION-DATE": "string",
59
+ "ATTRIBUTE-DEFINITION-SIMPLE": "complex",
60
+ "ATTRIBUTE-DEFINITION-XHTML": "complex",
61
+ "ATTRIBUTE-DEFINITION-BOOLEAN": "complex",
62
+ "ATTRIBUTE-DEFINITION-REAL": "complex",
63
+ "ATTRIBUTE-DEFINITION-INTEGER": "complex"}
64
+
65
+ mapReqifDatatypeDefinition = {"default": "document",
66
+ "DATATYPE-DEFINITION-DOCUMENT": "document",
67
+ "DATATYPE-DEFINITION-STRING": "string",
68
+ "DATATYPE-DEFINITION-DATE": "string",
69
+ "DATATYPE-DEFINITION-SIMPLE": "document",
70
+ "DATATYPE-DEFINITION-XHTML": "document",
71
+ "DATATYPE-DEFINITION-BOOLEAN": "document",
72
+ "DATATYPE-DEFINITION-REAL": "document",
73
+ "DATATYPE-DEFINITION-INTEGER": "document"}
74
+
75
+ transLationTableReverse = dict(map(reversed, transLationTable.items()))
76
+
77
+ mapReqifAttributeValueReversed = dict(map(reversed, mapReqifAttributeValue.items()))
78
+ mapReqifAttributeDefinitionReversed = dict(map(reversed, mapReqifAttributeDefinition.items()))
79
+ mapReqifDatatypeDefinitionReversed = dict(map(reversed, mapReqifDatatypeDefinition.items()))
80
+
81
+
82
+ def mapReqifAttributeValue2Py(elem: str):
83
+ if elem in mapReqifAttributeValue:
84
+ return mapReqifAttributeValue[elem]
85
+ else:
86
+ print("Not supported datatype: ")
87
+ print(elem)
88
+ return mapReqifAttributeValue['default']
89
+
90
+
91
+ def mapPy2ReqifAttributeValue(elem: str):
92
+ if elem in mapReqifAttributeValueReversed:
93
+ return mapReqifAttributeValueReversed[elem]
94
+ else:
95
+ print("Not supported datatype: ")
96
+ print(elem)
97
+ return mapReqifAttributeValueReversed['default']
98
+
99
+
100
+ def mapReqifAttributeDefinition2Py(elem: str):
101
+ if elem in mapReqifAttributeDefinition:
102
+ return mapReqifAttributeDefinition[elem]
103
+ else:
104
+ print("Not supported attribute definition: ")
105
+ print(elem)
106
+ return mapReqifAttributeDefinition['default']
107
+
108
+
109
+ def mapPy2ReqifAttributeDefinition(elem: str):
110
+ if elem in mapReqifAttributeDefinitionReversed:
111
+ return mapReqifAttributeDefinitionReversed[elem]
112
+ else:
113
+ print("Not supported attribute definition: ")
114
+ print(elem)
115
+ return mapReqifAttributeDefinitionReversed['default']
116
+
117
+
118
+ def mapReqifDatatypeDefinition2Py(elem: str):
119
+ if elem in mapReqifDatatypeDefinition:
120
+ return mapReqifDatatypeDefinition[elem]
121
+ else:
122
+ print("Not supported datatype definition: ")
123
+ print(elem)
124
+ return mapReqifDatatypeDefinition['default']
125
+
126
+
127
+ def mapPy2ReqifDatatypeDefinition(elem: str):
128
+ if elem in mapReqifDatatypeDefinitionReversed:
129
+ return mapReqifDatatypeDefinitionReversed[elem]
130
+ else:
131
+ print("Not supported datatype datatype: ")
132
+ print(elem)
133
+ return mapReqifDatatypeDefinitionReversed['default']
134
+
135
+
136
+ def py2reqif(myDict):
137
+ MyNewDict = {}
138
+ for pyname in myDict:
139
+ if pyname in transLationTableReverse:
140
+ reqifname = transLationTableReverse[pyname]
141
+ MyNewDict[reqifname] = myDict[pyname]
142
+ else:
143
+ MyNewDict[pyname] = myDict[pyname]
144
+ return MyNewDict
145
+
146
+
147
+ def reqif2py(myDict):
148
+ MyNewDict = {}
149
+ for reqifname in myDict:
150
+ if reqifname in transLationTable:
151
+ pyname = transLationTable[reqifname]
152
+ MyNewDict[pyname] = myDict[reqifname]
153
+ else:
154
+ MyNewDict[reqifname] = myDict[reqifname]
155
+ return MyNewDict
156
+
157
+
158
+ def load(f):
159
+ inputType = "RIF"
160
+ doc = pyreqif.pyreqif.doc()
161
+ tree = etree.parse(f)
162
+ root = tree.getroot()
163
+
164
+ ns = "{" + tree.xpath('namespace-uri(.)') + "}"
165
+ nsp = tree.xpath('namespace-uri(.)')
166
+
167
+ def getSubElementValuesByTitle(xmlElement, tagNameArray=[]):
168
+ defaultsSubElements = ['IDENTIFIER', 'LAST-CHANGE', 'LONG-NAME']
169
+ # ALTERNATIVE-ID ?
170
+ tagNameArray = list(set(defaultsSubElements + tagNameArray))
171
+ returnDict = {}
172
+ for tag in tagNameArray:
173
+ if tag in xmlElement.attrib:
174
+ returnDict[tag] = xmlElement.attrib[tag]
175
+ else:
176
+ temp = xmlElement.find('./' + ns + tag)
177
+ if temp is not None:
178
+ returnDict[tag] = temp.text
179
+ return returnDict
180
+
181
+ if root.tag == ns + "REQ-IF":
182
+ inputType = "REQIF"
183
+ headerRoot = root.find('./' + ns + 'THE-HEADER/' + ns + 'REQ-IF-HEADER')
184
+ contentRoot = root.find('./' + ns + 'CORE-CONTENT/' + ns + 'REQ-IF-CONTENT')
185
+
186
+ else:
187
+ headerRoot = root
188
+ contentRoot = root
189
+
190
+ headerTags = getSubElementValuesByTitle(headerRoot,
191
+ ['AUTHOR', 'COMMENT', 'COUNTRY-CODE', 'CREATION-TIME', 'SOURCE-TOOL-ID',
192
+ 'TITLE', 'VERSION'])
193
+ # header missing:
194
+ # COMMENT, REPOSITORY-ID, REQ-IF-TOOL-ID, REQ-IF-VERSION
195
+ doc.addHeader(reqif2py(headerTags))
196
+
197
+ datatypesXmlElement = contentRoot.find('./' + ns + 'DATATYPES')
198
+ for child in datatypesXmlElement:
199
+ if child.tag == ns + "DATATYPE-DEFINITION-DOCUMENT" or child.tag == ns + 'DATATYPE-DEFINITION-STRING' or child.tag == ns + 'DATATYPE-DEFINITION-XHTML'\
200
+ or child.tag == ns + 'DATATYPE-DEFINITION-BOOLEAN' or child.tag == ns + "DATATYPE-DEFINITION-INTEGER" or child.tag == ns + "DATATYPE-DEFINITION-DATE"\
201
+ or child.tag == ns + 'DATATYPE-DEFINITION-REAL' or child.tag == ns + 'DATATYPE-DEFINITION-SIMPLE':
202
+ datatypeProto = getSubElementValuesByTitle(child, ['EMBEDDED'])
203
+ tagWithoutNamespace = re.sub(r'{[\S]+}', '', child.tag)
204
+ datatypeProto['type'] = mapReqifDatatypeDefinition2Py(tagWithoutNamespace)
205
+ doc.addDatatype(reqif2py(datatypeProto))
206
+
207
+ elif child.tag == ns + "DATATYPE-DEFINITION-ENUMERATION":
208
+ datatypeProto = getSubElementValuesByTitle(child, ['EMBEDDED'])
209
+ datatypeProto['type'] = "enum"
210
+ specifiedValues = child.find('./' + ns + "SPECIFIED-VALUES")
211
+ values = {}
212
+ for valElement in specifiedValues:
213
+ tempDict = getSubElementValuesByTitle(valElement)
214
+ properties = valElement.find('./' + ns + "PROPERTIES")
215
+ if properties is not None:
216
+ embeddedValue = properties.find('./' + ns + "EMBEDDED-VALUE")
217
+ if embeddedValue is not None:
218
+ tempDict['properites'] = reqif2py(getSubElementValuesByTitle(embeddedValue, ['KEY', 'OTHER-CONTENT']))
219
+ tempDict = reqif2py(tempDict)
220
+ values[tempDict['identifier']] = tempDict
221
+ datatypeProto['values'] = values
222
+ doc.addDatatype(reqif2py(datatypeProto))
223
+ else:
224
+ print("Not supported datatype: ", )
225
+ print(child.tag)
226
+
227
+ specTypesXmlElement = contentRoot.find('./' + ns + 'SPEC-TYPES')
228
+ for child in specTypesXmlElement:
229
+ if child.tag == ns + "SPEC-TYPE" or child.tag == ns + "SPEC-OBJECT-TYPE":
230
+ specType = getSubElementValuesByTitle(child, ['DESC'])
231
+ # specType = getSubElementValuesByTitle(child)
232
+ attributesXml = child.find('./' + ns + "SPEC-ATTRIBUTES")
233
+ if attributesXml is not None:
234
+ for attribute in attributesXml:
235
+ if attribute.tag == ns +"ATTRIBUTE-DEFINITION-COMPLEX" or attribute.tag == ns +"ATTRIBUTE-DEFINITION-STRING" or attribute.tag == ns +"ATTRIBUTE-DEFINITION-XHTML"\
236
+ or attribute.tag == ns + "ATTRIBUTE-DEFINITION-BOOLEAN" or attribute.tag == ns + "ATTRIBUTE-DEFINITION-INTEGER"\
237
+ or attribute.tag == ns + "ATTRIBUTE-VALUE-DATE" or attribute.tag == ns + "ATTRIBUTE-DEFINITION-DATE"\
238
+ or attribute.tag == ns + "ATTRIBUTE-DEFINITION-REAL" or attribute.tag == ns + "ATTRIBUTE-DEFINITION-SIMPLE":
239
+ specAttribType = getSubElementValuesByTitle(attribute)
240
+ tagWithoutNamespace = re.sub(r'{[\S]+}', '', attribute.tag)
241
+ specAttribType["type"] = mapReqifAttributeDefinition2Py(tagWithoutNamespace)
242
+ typeTag = attribute.find('./' + ns + 'TYPE')
243
+ if typeTag is not None:
244
+ reference = typeTag.getchildren()[0]
245
+ # reference = typeTag.find('./' + ns + 'DATATYPE-DEFINITION-DOCUMENT-REF')
246
+ reference = typeTag.getchildren()[0]
247
+ if doc.datatypeById(reference.text):
248
+ specAttribType['typeRef'] = reference.text
249
+ else:
250
+ print("BEEP unknown Datatype")
251
+ elif attribute.tag == ns + "ATTRIBUTE-DEFINITION-ENUMERATION":
252
+ specAttribType = getSubElementValuesByTitle(attribute)
253
+ specAttribType["type"] = "enum"
254
+ typeRef = attribute.find('./' + ns + 'TYPE/' + ns + 'DATATYPE-DEFINITION-ENUMERATION-REF')
255
+ if typeRef is not None:
256
+ specAttribType['typeRef'] = typeRef.text
257
+ defaultValue = attribute.find(
258
+ './' + ns + 'DEFAULT-VALUE/' + ns + 'ATTRIBUTE-VALUE-ENUMERATION/' + ns + 'VALUES/' + ns + 'ENUM-VALUE-REF')
259
+ if defaultValue is not None:
260
+ specAttribType['defaultValue'] = defaultValue.text
261
+ if "MULTI-VALUED" in attribute.attrib:
262
+ specAttribType['multiValued'] = attribute.attrib["MULTI-VALUED"] == "true"
263
+ else:
264
+ multiValuedXml = attribute.find('./' + ns + 'MULTI-VALUED')
265
+ if multiValuedXml is not None:
266
+ specAttribType['multiValued'] = multiValuedXml.text == "true"
267
+
268
+ else:
269
+ print("Not supported Attribute: ", )
270
+ print(attribute.tag)
271
+
272
+ specAttribType = reqif2py(specAttribType)
273
+ specType[specAttribType['identifier']] = specAttribType
274
+
275
+ # specType[specAttribType['identifier']].pop('identifier')
276
+ doc.addRequirementType(reqif2py(specType))
277
+
278
+ def remove_namespaces(thedoc):
279
+ # http://wiki.tei-c.org/index.php/Remove-Namespaces.xsl
280
+ xslt = '''<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
281
+ <xsl:output method="xml" indent="no"/>
282
+
283
+ <xsl:template match="/|comment()|processing-instruction()">
284
+ <xsl:copy>
285
+ <xsl:apply-templates/>
286
+ </xsl:copy>
287
+ </xsl:template>
288
+
289
+ <xsl:template match="*">
290
+ <xsl:element name="{local-name()}">
291
+ <xsl:apply-templates select="@*|node()"/>
292
+ </xsl:element>
293
+ </xsl:template>
294
+
295
+ <xsl:template match="@*">
296
+ <xsl:attribute name="{local-name()}">
297
+ <xsl:value-of select="."/>
298
+ </xsl:attribute>
299
+ </xsl:template>
300
+ </xsl:stylesheet>
301
+ '''
302
+
303
+ # xslt_doc = etree.parse(io.BytesIO(xslt))
304
+ xslt_doc = etree.parse(io.BytesIO(bytes(xslt, "utf8")))
305
+ transform = etree.XSLT(xslt_doc)
306
+ ret = transform(thedoc)
307
+ return ret
308
+
309
+ specObjectsXmlElement = contentRoot.find('./' + ns + 'SPEC-OBJECTS')
310
+ for requirementXml in specObjectsXmlElement:
311
+ requirement = None
312
+
313
+ if requirementXml.tag == ns + "SPEC-OBJECT":
314
+ requirement = getSubElementValuesByTitle(requirementXml)
315
+
316
+ typeRefXml = requirementXml.find('./' + ns + 'TYPE/' + ns + 'SPEC-TYPE-REF')
317
+ if typeRefXml is None:
318
+ typeRefXml = requirementXml.find('./' + ns + 'TYPE/' + ns + 'SPEC-OBJECT-TYPE-REF')
319
+ if typeRefXml is not None:
320
+ requirement["typeRef"] = typeRefXml.text
321
+
322
+ valuesXml = requirementXml.find('./' + ns + 'VALUES')
323
+ if valuesXml is None:
324
+ valuesXml = []
325
+ values = {}
326
+ for valueXml in valuesXml:
327
+ value = getSubElementValuesByTitle(valueXml)
328
+ #TODO : Support other types
329
+ value = getSubElementValuesByTitle(valueXml)
330
+ if valueXml.tag == ns + 'ATTRIBUTE-VALUE-EMBEDDED-DOCUMENT' or valueXml.tag == ns + 'ATTRIBUTE-VALUE-STRING' or valueXml.tag == ns + 'ATTRIBUTE-VALUE-XHTML'\
331
+ or valueXml.tag == ns + 'ATTRIBUTE-VALUE-BOOLEAN' or valueXml.tag == ns + 'ATTRIBUTE-VALUE-INTEGER' or valueXml.tag == ns + 'ATTRIBUTE-VALUE-DATE' or valueXml.tag == ns + 'ATTRIBUTE-VALUE-REAL'\
332
+ or valueXml.tag == ns + 'ATTRIBUTE-VALUE-SIMPLE':
333
+ definitionXml = valueXml.find('./' + ns + 'DEFINITION')
334
+ if definitionXml is None or len(definitionXml.getchildren()) == 0:
335
+ print("Warning: no DEFINITION found for value, skipping value:")
336
+ print(valueXml.tag[len(ns):])
337
+ continue
338
+ attributeRefXml = definitionXml.getchildren()[0]
339
+ if attributeRefXml.text is None:
340
+ print("Warning: no attribute definition reference found for value, skipping value:")
341
+ print(valueXml.tag[len(ns):])
342
+ continue
343
+ value['attributeRef'] = attributeRefXml.text
344
+ if 'THE-VALUE' in valueXml.attrib:
345
+ value["content"] = valueXml.attrib['THE-VALUE']
346
+ else:
347
+ contentXml = valueXml.find(
348
+ './' + ns + 'XHTML-CONTENT/{http://automotive-his.de/200706/rif-xhtml}div')
349
+ if contentXml is None:
350
+ contentXml = valueXml.find("./" + ns + 'THE-VALUE/{http://www.w3.org/1999/xhtml}div')
351
+ if contentXml is None:
352
+ contentXml = valueXml.find(
353
+ "./" + ns + 'THE-VALUE'
354
+ )
355
+
356
+ value["content"] = etree.tostring(remove_namespaces(contentXml))
357
+
358
+ # value["content"] = "".join(contentXml.itertext())
359
+
360
+ tagWithoutNamespace = re.sub(r'{[\S]+}', '', valueXml.tag)
361
+ value["type"] = mapReqifAttributeValue2Py(tagWithoutNamespace)
362
+
363
+ elif valueXml.tag == ns + 'ATTRIBUTE-VALUE-ENUMERATION':
364
+ value["type"] = "enum"
365
+ attributeRefXml = valueXml.find(
366
+ './' + ns + 'DEFINITION/' + ns + 'ATTRIBUTE-DEFINITION-ENUMERATION-REF')
367
+ value['attributeRef'] = attributeRefXml.text
368
+ contentXml = valueXml.findall('./' + ns + 'VALUES/' + ns + 'ENUM-VALUE-REF')
369
+ if contentXml is not None:
370
+ value["contentRef"] = []
371
+ for content in contentXml:
372
+ value["contentRef"].append(content.text)
373
+ a = 1
374
+ else:
375
+ value["contentRef"] = None
376
+ else:
377
+ print("valueType not supported yet:", )
378
+ print(valueXml.tag[len(ns):])
379
+
380
+ values[value['attributeRef']] = reqif2py(value)
381
+ requirement["values"] = values
382
+ else:
383
+ print("Unknown spec object tag:", )
384
+ print(requirementXml.tag)
385
+
386
+ if requirement != None:
387
+ doc.addRequirement(reqif2py(requirement))
388
+
389
+ specGroupsXml = contentRoot.find('./' + ns + 'SPEC-GROUPS')
390
+ if specGroupsXml is not None:
391
+ for specGroupXml in specGroupsXml:
392
+ if specGroupXml.tag == ns + "SPEC-GROUP":
393
+ specification = getSubElementValuesByTitle(specGroupXml, ['DESC'])
394
+ spec = pyreqif.pyreqif.specification(**reqif2py(specification))
395
+
396
+ specObjectsXml = specGroupXml.find('./' + ns + 'SPEC-OBJECTS')
397
+ for specObjectRef in specObjectsXml:
398
+ spec.addReq(specObjectRef.text)
399
+ doc.addSpecification(spec)
400
+
401
+ def getHierarchy(hierarchyEle, inputType):
402
+ hierarchyDict = getSubElementValuesByTitle(hierarchyEle)
403
+ typeRef = hierarchyEle.find('./' + ns + 'TYPE/' + ns + 'SPEC-TYPE-REF')
404
+ if typeRef is not None:
405
+ hierarchyDict["typeRef"] = typeRef.text
406
+
407
+ objectRef = hierarchyEle.find('./' + ns + 'OBJECT/' + ns + 'SPEC-OBJECT-REF')
408
+ if objectRef is not None:
409
+ hierarchyDict["objectRef"] = objectRef.text
410
+ hierarchy = pyreqif.pyreqif.hierarchy(**reqif2py(hierarchyDict))
411
+
412
+ children = hierarchyEle.find('./' + ns + 'CHILDREN')
413
+ if children is not None:
414
+ for child in children:
415
+ hierarchy.addChild(getHierarchy(child, inputType))
416
+ return hierarchy
417
+
418
+ if inputType == "RIF":
419
+ hierarchyRoots = contentRoot.find('./' + ns + 'SPEC-HIERARCHY-ROOTS')
420
+ elif inputType == "REQIF":
421
+ hierarchyRoots = contentRoot.find('./' + ns + 'SPECIFICATIONS')
422
+
423
+ for hierarchyRoot in hierarchyRoots:
424
+ doc.hierarchy.append(getHierarchy(hierarchyRoot, inputType))
425
+
426
+ # SPEC-HIERARCHY
427
+ relations = {}
428
+ specRelsXml = contentRoot.find('./' + ns + 'SPEC-RELATIONS')
429
+ if specRelsXml is not None:
430
+ for specRelXml in specRelsXml:
431
+ if specRelXml.tag == ns + "SPEC-RELATION":
432
+ relation = getSubElementValuesByTitle(specRelXml)
433
+ typeRef = specRelXml.find('./' + ns + 'TYPE')
434
+ if typeRef is not None:
435
+ relation["typeRef"] = typeRef.getchildren()[0].text
436
+ sourceRef = specRelXml.find('./' + ns + 'SOURCE/' + ns + 'SPEC-OBJECT-REF')
437
+ if sourceRef is not None:
438
+ relation["sourceRef"] = sourceRef.text
439
+ targetRef = specRelXml.find('./' + ns + 'TARGET/' + ns + 'SPEC-OBJECT-REF')
440
+ if targetRef is not None:
441
+ relation["targetRef"] = targetRef.text
442
+
443
+ doc.addRelation(reqif2py(relation))
444
+ return doc
445
+
446
+
447
+ attributesForElements = ["IDENTIFIER", "LAST-CHANGE", "LONG-NAME", "MAX-LENGTH", "MAX", "MIN", "ACCURACY",
448
+ "OTHER-CONTENT", "KEY", "MULTI-VALUED"]
449
+ notUsedAttributes = ["COUNTRY-CODE", "EMBEDDED", "AUTHOR", "VERSION", "DESC", "contentRef"]
450
+
451
+
452
+ def createSubElements(parent, myDict):
453
+ for key in myDict:
454
+ if key in attributesForElements or key in notUsedAttributes:
455
+ continue
456
+ sn = etree.SubElement(parent, key)
457
+ if myDict[key] is not None:
458
+ sn.text = myDict[key]
459
+ else:
460
+ sn.text = 'None'
461
+
462
+
463
+ def createSubElement(parent, tag, text=None, attributes=None):
464
+ sn = etree.SubElement(parent, tag)
465
+ if text is not None:
466
+ sn.text = text
467
+ if attributes is not None:
468
+ for attributeName in attributesForElements:
469
+ if attributeName in attributes and attributes[
470
+ attributeName] is not None and attributeName not in notUsedAttributes:
471
+ sn.attrib[attributeName] = attributes[attributeName]
472
+ return sn
473
+
474
+
475
+ def dump(doc, f):
476
+ xsi = 'http://www.w3.org/2001/XMLSchema-instance'
477
+ arVersion = "1"
478
+ root = etree.Element(
479
+ 'REQ-IF',
480
+ nsmap={
481
+ None: 'http://www.omg.org/spec/ReqIF/20110401/reqif.xsd',
482
+ 'xhtml': "http://www.w3.org/1999/xhtml",
483
+ 'id': "http://pror.org/presentation/id",
484
+ "configuration": "http://eclipse.org/rmf/pror/toolextensions/1.0",
485
+ })
486
+
487
+ #
488
+ # HEADER
489
+ #
490
+ theheader = createSubElement(root, "THE-HEADER")
491
+ headerXML = createSubElement(theheader, "REQ-IF-HEADER", attributes=py2reqif(doc.header))
492
+ tempDict = py2reqif(doc.header)
493
+ tempDict["REQ-IF-TOOL-ID"] = tempDict["SOURCE-TOOL-ID"]
494
+ tempDict["REQ-IF-VERSION"] = "1.0"
495
+ tempDict["SOURCE-TOOL-ID"] = "pyreqif"
496
+ for tagName in ["COMMENT", "CREATION-TIME", "REQ-IF-TOOL-ID", "REQ-IF-VERSION", "SOURCE-TOOL-ID", "TITLE"]:
497
+ createSubElement(headerXML, tagName, tempDict[tagName])
498
+ coreContent = createSubElement(root, "CORE-CONTENT")
499
+ reqIfContent = createSubElement(coreContent, "REQ-IF-CONTENT")
500
+
501
+ #
502
+ # DATATYPES
503
+ #
504
+ datatypesXml = createSubElement(reqIfContent, "DATATYPES")
505
+ for datatype in doc.datatypeList:
506
+ if datatype.mytype == "document":
507
+ myDict = py2reqif(datatype.toDict())
508
+ datatypeXml = createSubElement(datatypesXml, "DATATYPE-DEFINITION-XHTML", attributes=myDict)
509
+ del myDict["TYPE"]
510
+ createSubElements(datatypeXml, myDict)
511
+ if datatype.mytype == "string":
512
+ myDict = py2reqif(datatype.toDict())
513
+ datatypeXml = createSubElement(datatypesXml, "DATATYPE-DEFINITION-STRING", attributes=myDict)
514
+ del myDict["TYPE"]
515
+ createSubElements(datatypeXml, myDict)
516
+ if datatype.mytype == "enum":
517
+ datatypeXml = createSubElement(datatypesXml, "DATATYPE-DEFINITION-ENUMERATION",
518
+ attributes=py2reqif(datatype.toDict()))
519
+ myDict = py2reqif(datatype.toDict())
520
+ del myDict["TYPE"]
521
+ createSubElements(datatypeXml, myDict)
522
+ specifiedValuesXml = createSubElement(datatypeXml, "SPECIFIED-VALUES")
523
+ for value, label in datatype.valueTable.items():
524
+ valuesXml = createSubElement(specifiedValuesXml, "ENUM-VALUE", attributes=py2reqif(label))
525
+ # createSubElement(valuesXml, "IDENTIFIER", value)
526
+ for element, content in py2reqif(label).items():
527
+ if element == "properites":
528
+ props = createSubElement(valuesXml, "PROPERTIES")
529
+ createSubElement(props, "EMBEDDED-VALUE", attributes=py2reqif(content))
530
+ elif element not in attributesForElements:
531
+ createSubElement(valuesXml, element, content)
532
+
533
+ #
534
+ # SPEC-TYPES
535
+ #
536
+ specTypes = createSubElement(reqIfContent, "SPEC-TYPES")
537
+ for reqType in doc.requirementTypeList:
538
+ specType = createSubElement(specTypes, "SPEC-OBJECT-TYPE", attributes=py2reqif(reqType.toDict()))
539
+ createSubElements(specType, py2reqif(reqType.toDict()))
540
+
541
+ if len(reqType.myTypes) > 0:
542
+ attributesXml = createSubElement(specType, "SPEC-ATTRIBUTES")
543
+
544
+ for mytype, ref in reqType.myTypes.items():
545
+ attribDict = py2reqif(ref.toDict())
546
+ if "TYPE" in attribDict and attribDict["TYPE"] == "enum":
547
+ attribDict.pop("TYPE")
548
+ attribDict["MULTI-VALUED"] = "true" if attribDict.pop("multiValued", False) else "false"
549
+ enumXml = createSubElement(attributesXml, "ATTRIBUTE-DEFINITION-ENUMERATION", attributes=attribDict)
550
+ for value, label in attribDict.items():
551
+ if value == "typeRef":
552
+ typeXml = createSubElement(enumXml, "TYPE")
553
+ createSubElement(typeXml, "DATATYPE-DEFINITION-ENUMERATION-REF", label)
554
+ elif value not in attributesForElements:
555
+ createSubElement(enumXml, value, label)
556
+
557
+ if "TYPE" in attribDict and attribDict["TYPE"] == "complex":
558
+ # attribDict.pop("TYPE")
559
+ enumXml = createSubElement(attributesXml, "ATTRIBUTE-DEFINITION-XHTML", attributes=attribDict)
560
+ attribDict.pop("TYPE")
561
+ for value, label in attribDict.items():
562
+ if value == "typeRef":
563
+ typeXml = createSubElement(enumXml, "TYPE")
564
+ createSubElement(typeXml, "DATATYPE-DEFINITION-XHTML-REF", label)
565
+ elif value not in attributesForElements and value not in notUsedAttributes:
566
+ createSubElement(enumXml, value, label)
567
+
568
+ if "TYPE" in attribDict and attribDict["TYPE"] == "string":
569
+ # attribDict.pop("TYPE")
570
+ enumXml = createSubElement(attributesXml, "ATTRIBUTE-DEFINITION-STRING", attributes=attribDict)
571
+ attribDict.pop("TYPE")
572
+ for value, label in attribDict.items():
573
+ if value == "typeRef":
574
+ typeXml = createSubElement(enumXml, "TYPE")
575
+ createSubElement(typeXml, "DATATYPE-DEFINITION-STRING-REF", label)
576
+ elif value not in attributesForElements and value not in notUsedAttributes:
577
+ createSubElement(enumXml, value, label)
578
+
579
+ #
580
+ # SPEC-OBJECTS
581
+ #
582
+ specsXml = createSubElement(reqIfContent, "SPEC-OBJECTS")
583
+
584
+ for req in doc.requirementList:
585
+ specXml = createSubElement(specsXml, "SPEC-OBJECT", attributes=py2reqif(req.toDict()))
586
+ requirementDict = py2reqif(req.toDict())
587
+ for value, label in requirementDict.items():
588
+ if value == "VALUES":
589
+ valuesXml = createSubElement(specXml, "VALUES")
590
+ for value in label:
591
+ tempDict = py2reqif(value.toDict())
592
+ if "LONG-NAME" in tempDict:
593
+ tempDict.pop("LONG-NAME")
594
+ if "LAST-CHANGE" in tempDict:
595
+ tempDict.pop("LAST-CHANGE")
596
+ if "IDENTIFIER" in tempDict:
597
+ tempDict.pop("IDENTIFIER")
598
+ if value.mytype == "enum":
599
+ valueXml = createSubElement(valuesXml, "ATTRIBUTE-VALUE-ENUMERATION", attributes=tempDict)
600
+ valuesValuesXml = createSubElement(valueXml, "VALUES")
601
+ valuesDefinitionsXml = createSubElement(valueXml, "DEFINITION")
602
+ else:
603
+ valueXml = createSubElement(valuesXml, "ATTRIBUTE-VALUE-XHTML", attributes=tempDict)
604
+ valuesDefinitionsXml = createSubElement(valueXml, "DEFINITION")
605
+ for val, lab in py2reqif(value.toDict()).items():
606
+ if val == "contentRef" and lab is not None:
607
+ createSubElement(valuesValuesXml, "ENUM-VALUE-REF", lab[0])
608
+ elif val == "attributeRef":
609
+ if value.mytype == "enum":
610
+ createSubElement(valuesDefinitionsXml, "ATTRIBUTE-DEFINITION-ENUMERATION-REF", lab)
611
+ elif value.mytype == "embeddedDoc":
612
+ createSubElement(valuesDefinitionsXml, "ATTRIBUTE-DEFINITION-XHTML-REF", lab)
613
+ elif value.mytype == "string":
614
+ createSubElement(valuesDefinitionsXml, "ATTRIBUTE-DEFINITION-STRING-REF", lab)
615
+ else:
616
+ print("Unknown Type " + value.mytype)
617
+
618
+ elif val == "TYPE":
619
+ pass
620
+ elif val == "CONTENT":
621
+ if lab is not None:
622
+ if '<' in str(lab):
623
+ labtree = etree.parse(io.BytesIO(lab))
624
+ labroot = labtree.getroot()
625
+ for el in labroot.iter():
626
+ el.tag = '{http://www.w3.org/1999/xhtml}' + el.tag
627
+ contentXml = createSubElement(valueXml, "THE-VALUE")
628
+ contentXml.append(labroot)
629
+ else:
630
+ createSubElement(valueXml, "THE-VALUE", lab)
631
+ elif val not in attributesForElements and val not in notUsedAttributes:
632
+ createSubElement(valueXml, val, lab)
633
+ elif value == "typeRef":
634
+ typeXml = createSubElement(specXml, "TYPE")
635
+ createSubElement(typeXml, "SPEC-OBJECT-TYPE-REF", label)
636
+ elif value not in attributesForElements:
637
+ createSubElement(specXml, value, label)
638
+
639
+ #
640
+ # SPEC-RELATIONS
641
+ #
642
+ specsRelXml = createSubElement(reqIfContent, "SPEC-RELATIONS")
643
+ for relation in doc.relations:
644
+ specsRel = createSubElement(specsRelXml, "SPEC-RELATION")
645
+ for value, label in py2reqif(relation).items():
646
+ if value == "typeRef":
647
+ typeXml = createSubElement(specsRel, "TYPE")
648
+ createSubElement(typeXml, "SPEC-TYPE-REF", label)
649
+ elif value == "sourceRef":
650
+ sourceXml = createSubElement(specsRel, "SOURCE")
651
+ createSubElement(sourceXml, "SPEC-OBJECT-REF", label)
652
+ elif value == "targetRef":
653
+ targetXml = createSubElement(specsRel, "TARGET")
654
+ createSubElement(targetXml, "SPEC-OBJECT-REF", label)
655
+ else:
656
+ createSubElement(specsRel, value, label)
657
+
658
+ #
659
+ # SPEC-GROUPS
660
+ #
661
+ # specGroupsXml = createSubElement(reqIfContent, "SPEC-GROUPS")
662
+ # for specification in doc.specificationList:
663
+ # specGroupXml = createSubElement(specGroupsXml, "SPEC-GROUP")
664
+ # for value,label in py2reqif(specification.toDict()).iteritems():
665
+ # createSubElement(specGroupXml,value,label)
666
+ # specObjectsXml = createSubElement(specGroupXml,"SPEC-OBJECTS")
667
+ # for req in specification:
668
+ # createSubElement(specObjectsXml ,"SPEC-OBJECT-REF", req)
669
+
670
+ #
671
+ # SPEC-HIERARCHY-ROOTS
672
+ #
673
+ def createChildHirachy(parentXmlTag, childObject):
674
+ childrenXml = createSubElement(parentXmlTag, "CHILDREN")
675
+ hierarchXml = createSubElement(childrenXml, "SPEC-HIERARCHY", attributes=py2reqif(childObject.toDict()))
676
+ for value, label in py2reqif(childObject.toDict()).items():
677
+ if value == "objectRef":
678
+ objectXml = createSubElement(hierarchXml, "OBJECT")
679
+ createSubElement(objectXml, "SPEC-OBJECT-REF", label)
680
+ elif value not in attributesForElements:
681
+ if label is not None:
682
+ createSubElement(hierarchXml, value, label)
683
+
684
+ for child in childObject.children:
685
+ createChildHirachy(hierarchXml, child)
686
+
687
+ specHierarchRootsXml = createSubElement(reqIfContent, "SPECIFICATIONS")
688
+ # SPEC-HIERARCHY-ROOT
689
+ for hierarch in doc.hierarchy:
690
+ specHierarchRootXml = createSubElement(specHierarchRootsXml, "SPECIFICATION",
691
+ attributes=py2reqif(hierarch.toDict()))
692
+ for value, label in py2reqif(hierarch.toDict()).items():
693
+ if value == "typeRef":
694
+ typeXml = createSubElement(specHierarchRootXml, "TYPE")
695
+ createSubElement(typeXml, "SPECIFICATION-TYPE-REF", label)
696
+ elif value not in attributesForElements:
697
+ createSubElement(specHierarchRootXml, value, label)
698
+
699
+ for child in hierarch.children:
700
+ createChildHirachy(specHierarchRootXml, child)
701
+
702
+ f.write(etree.tostring(root, pretty_print=True, xml_declaration=True))