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/reqif.py ADDED
@@ -0,0 +1,410 @@
1
+ import sys
2
+
3
+ sys.path.append('..')
4
+ import io
5
+ import pyreqif.rif
6
+ from lxml import etree
7
+ import lxml.html
8
+
9
+
10
+ def load(f):
11
+ return pyreqif.rif.load(f)
12
+
13
+
14
+ transLationTable = {"IDENTIFIER": "identifier",
15
+ "COUNTRY-CODE": "countryCode",
16
+ "CREATION-TIME": "creationTime",
17
+ "TITLE": "title",
18
+ "COMMENT": "comment",
19
+ "AUTHOR": "author",
20
+ "LONG-NAME": "longName",
21
+ "VERSION": "version",
22
+ "SOURCE-TOOL-ID": "sourceToolId",
23
+ "LAST-CHANGE": "lastChange",
24
+ "EMBEDDED": "embedded",
25
+ "TYPE": "type",
26
+ "VALUES": "values",
27
+ "CONTENT-REF": "contentref",
28
+ "CONTENT": "content",
29
+ "DESC": "desc"}
30
+
31
+ transLationTableReverse = dict(map(reversed, transLationTable.items()))
32
+
33
+
34
+ def py2reqif(myDict):
35
+ MyNewDict = {}
36
+ for pyname in myDict:
37
+ if pyname in transLationTableReverse:
38
+ reqifname = transLationTableReverse[pyname]
39
+ MyNewDict[reqifname] = myDict[pyname]
40
+ else:
41
+ MyNewDict[pyname] = myDict[pyname]
42
+ return MyNewDict
43
+
44
+
45
+ attributesForElements = ["IDENTIFIER", "LAST-CHANGE", "LONG-NAME", "MAX-LENGTH", "MAX", "MIN", "ACCURACY",
46
+ "OTHER-CONTENT", "KEY", "MULTI-VALUED"]
47
+ notUsedAttributes = ["COUNTRY-CODE", "EMBEDDED", "AUTHOR", "VERSION", "DESC", "contentRef"]
48
+
49
+
50
+ def createSubElements(parent, myDict):
51
+ for key in myDict:
52
+ if key in attributesForElements or key in notUsedAttributes:
53
+ continue
54
+ sn = etree.SubElement(parent, key)
55
+ if myDict[key] is not None:
56
+ sn.text = myDict[key]
57
+ else:
58
+ sn.text = 'None'
59
+
60
+
61
+ def createSubElement(parent, tag, text=None, attributes=None, additionalAttributesForElements=[]):
62
+ sn = etree.SubElement(parent, tag)
63
+ if text is not None:
64
+ sn.text = str(text)
65
+ if attributes is not None:
66
+ for attributeName in attributesForElements:
67
+ if attributeName in attributes and attributes[
68
+ attributeName] is not None and attributeName not in notUsedAttributes:
69
+ sn.attrib[attributeName] = attributes[attributeName]
70
+ for attributeName in additionalAttributesForElements:
71
+ if attributeName in attributes and attributes[
72
+ attributeName] is not None and attributeName not in notUsedAttributes:
73
+ sn.attrib[attributeName] = attributes[attributeName]
74
+ return sn
75
+
76
+
77
+ def dump(doc, f):
78
+ xsi = 'http://www.w3.org/2001/XMLSchema-instance'
79
+ arVersion = "1"
80
+ root = etree.Element(
81
+ 'REQ-IF',
82
+ nsmap={
83
+ None: 'http://www.omg.org/spec/ReqIF/20110401/reqif.xsd',
84
+ 'xhtml': "http://www.w3.org/1999/xhtml",
85
+ 'id': "http://pror.org/presentation/id",
86
+ "configuration": "http://eclipse.org/rmf/pror/toolextensions/1.0",
87
+ })
88
+
89
+ #
90
+ # HEADER
91
+ #
92
+ theheader = createSubElement(root, "THE-HEADER")
93
+ headerXML = createSubElement(theheader, "REQ-IF-HEADER", attributes=py2reqif(doc.header))
94
+ tempDict = py2reqif(doc.header)
95
+ tempDict["REQ-IF-TOOL-ID"] = tempDict["SOURCE-TOOL-ID"]
96
+ tempDict["REQ-IF-VERSION"] = "1.0"
97
+ tempDict["SOURCE-TOOL-ID"] = "pyreqif"
98
+ for tagName in ["COMMENT", "CREATION-TIME", "REQ-IF-TOOL-ID", "REQ-IF-VERSION", "SOURCE-TOOL-ID", "TITLE"]:
99
+ createSubElement(headerXML, tagName, tempDict[tagName])
100
+ coreContent = createSubElement(root, "CORE-CONTENT")
101
+ reqIfContent = createSubElement(coreContent, "REQ-IF-CONTENT")
102
+
103
+ #
104
+ # DATATYPES
105
+ #
106
+ datatypesXml = createSubElement(reqIfContent, "DATATYPES")
107
+ for datatype in doc.datatypeList:
108
+ if datatype.mytype == "document":
109
+ myDict = py2reqif(datatype.toDict())
110
+ datatypeXml = createSubElement(datatypesXml, "DATATYPE-DEFINITION-XHTML", attributes=myDict)
111
+ del myDict["TYPE"]
112
+ createSubElements(datatypeXml, myDict)
113
+ if datatype.mytype == "string":
114
+ myDict = py2reqif(datatype.toDict())
115
+ datatypeXml = createSubElement(datatypesXml, "DATATYPE-DEFINITION-STRING", attributes=myDict)
116
+ del myDict["TYPE"]
117
+ createSubElements(datatypeXml, myDict)
118
+ if datatype.mytype == "enum":
119
+ datatypeXml = createSubElement(datatypesXml, "DATATYPE-DEFINITION-ENUMERATION",
120
+ attributes=py2reqif(datatype.toDict()))
121
+ myDict = py2reqif(datatype.toDict())
122
+ del myDict["TYPE"]
123
+ createSubElements(datatypeXml, myDict)
124
+ specifiedValuesXml = createSubElement(datatypeXml, "SPECIFIED-VALUES")
125
+ # for value,label in datatype.valueTable.iteritems():
126
+ for value, label in datatype.valueTable.items():
127
+ valuesXml = createSubElement(specifiedValuesXml, "ENUM-VALUE", attributes=py2reqif(label))
128
+ # createSubElement(valuesXml, "IDENTIFIER", value)
129
+ # for element,content in py2reqif(label).iteritems():
130
+ for element, content in py2reqif(label).items():
131
+ if element == "properites":
132
+ props = createSubElement(valuesXml, "PROPERTIES")
133
+ createSubElement(props, "EMBEDDED-VALUE", attributes=py2reqif(content))
134
+ elif element not in attributesForElements:
135
+ createSubElement(valuesXml, element, content)
136
+
137
+ #
138
+ # SPEC-TYPES
139
+ #
140
+ specTypes = createSubElement(reqIfContent, "SPEC-TYPES")
141
+ # SPEC-OBJECT-TYPE
142
+ for reqType in doc.requirementTypeList:
143
+ specType = createSubElement(specTypes, "SPEC-OBJECT-TYPE", attributes=py2reqif(reqType.toDict()))
144
+ createSubElements(specType, py2reqif(reqType.toDict()))
145
+
146
+ if len(reqType.myTypes) > 0:
147
+ attributesXml = createSubElement(specType, "SPEC-ATTRIBUTES")
148
+
149
+ # for mytype,ref in reqType.myTypes.iteritems():
150
+ for mytype, ref in reqType.myTypes.items():
151
+ attribDict = py2reqif(ref.toDict())
152
+ if "TYPE" in attribDict and attribDict["TYPE"] == "enum":
153
+ attribDict.pop("TYPE")
154
+ attribDict["MULTI-VALUED"] = "true" if attribDict.pop("multiValued", False) else "false"
155
+ enumXml = createSubElement(attributesXml, "ATTRIBUTE-DEFINITION-ENUMERATION", attributes=attribDict)
156
+ # for value,label in attribDict.iteritems():
157
+ for value, label in attribDict.items():
158
+ if value == "typeRef":
159
+ typeXml = createSubElement(enumXml, "TYPE")
160
+ createSubElement(typeXml, "DATATYPE-DEFINITION-ENUMERATION-REF", label)
161
+ elif value not in attributesForElements:
162
+ createSubElement(enumXml, value, label)
163
+
164
+ if "TYPE" in attribDict and attribDict["TYPE"] == "complex":
165
+ # attribDict.pop("TYPE")
166
+ enumXml = createSubElement(attributesXml, "ATTRIBUTE-DEFINITION-XHTML", attributes=attribDict)
167
+ attribDict.pop("TYPE")
168
+ # for value,label in attribDict.iteritems():
169
+ for value, label in attribDict.items():
170
+ if value == "typeRef":
171
+ typeXml = createSubElement(enumXml, "TYPE")
172
+ createSubElement(typeXml, "DATATYPE-DEFINITION-XHTML-REF", label)
173
+ elif value not in attributesForElements and value not in notUsedAttributes:
174
+ createSubElement(enumXml, value, label)
175
+
176
+ if "TYPE" in attribDict and attribDict["TYPE"] == "string":
177
+ # attribDict.pop("TYPE")
178
+ enumXml = createSubElement(attributesXml, "ATTRIBUTE-DEFINITION-STRING", attributes=attribDict)
179
+ attribDict.pop("TYPE")
180
+ # for value,label in attribDict.iteritems():
181
+ for value, label in attribDict.items():
182
+ if value == "typeRef":
183
+ typeXml = createSubElement(enumXml, "TYPE")
184
+ createSubElement(typeXml, "DATATYPE-DEFINITION-STRING-REF", label)
185
+ elif value not in attributesForElements and value not in notUsedAttributes:
186
+ createSubElement(enumXml, value, label)
187
+
188
+ # SPEC-RELATION-TYPE
189
+ for relationType in doc.specRelationTypeList:
190
+ createSubElement(specTypes, "SPEC-RELATION-TYPE", attributes=py2reqif(relationType.toDict()))
191
+
192
+ for specificationType in doc.specificationTypes:
193
+ print(py2reqif(specificationType))
194
+ createSubElement(specTypes, "SPECIFICATION-TYPE", attributes=py2reqif(specificationType))
195
+ # TODO
196
+ # <SPECIFICATION-TYPE IDENTIFIER="_jgCytgfNEeeAO8RifBaE-g" LAST-CHANGE="2017-03-13T10:15:09.017+01:00" LONG-NAME="Specification Type">
197
+ # <SPEC-ATTRIBUTES>
198
+ # <ATTRIBUTE-DEFINITION-STRING IDENTIFIER="_jgCytwfNEeeAO8RifBaE-g" LAST-CHANGE="2017-03-13T10:15:09.017+01:00" LONG-NAME="Description">
199
+ # <TYPE>
200
+ # <DATATYPE-DEFINITION-STRING-REF>_jgCyswfNEeeAO8RifBaE-g</DATATYPE-DEFINITION-STRING-REF>
201
+ # </TYPE>
202
+ # </ATTRIBUTE-DEFINITION-STRING>
203
+ # </SPEC-ATTRIBUTES>
204
+ # </SPECIFICATION-TYPE>
205
+
206
+ # RELATION-GROUP-TYPE
207
+ for relationGroup in doc.specRelationGroupList:
208
+ attributes = py2reqif(relationGroup.toDict())
209
+ attributes['IDENTIFIER'] += "_type"
210
+ createSubElement(specTypes, "RELATION-GROUP-TYPE", attributes=attributes)
211
+
212
+ #
213
+ # SPEC-OBJECTS
214
+ #
215
+ specsXml = createSubElement(reqIfContent, "SPEC-OBJECTS")
216
+
217
+ for req in doc.requirementList:
218
+ specXml = createSubElement(specsXml, "SPEC-OBJECT", attributes=py2reqif(req.toDict()))
219
+ requirementDict = py2reqif(req.toDict())
220
+ # for value,label in requirementDict.iteritems():
221
+ for value, label in requirementDict.items():
222
+ if value == "VALUES":
223
+ valuesXml = createSubElement(specXml, "VALUES")
224
+ for value in label:
225
+ tempDict = py2reqif(value.toDict())
226
+ if "LONG-NAME" in tempDict:
227
+ tempDict.pop("LONG-NAME")
228
+ if "LAST-CHANGE" in tempDict:
229
+ tempDict.pop("LAST-CHANGE")
230
+ if "IDENTIFIER" in tempDict:
231
+ tempDict.pop("IDENTIFIER")
232
+ if value.mytype == "enum":
233
+ valueXml = createSubElement(valuesXml, "ATTRIBUTE-VALUE-ENUMERATION", attributes=tempDict)
234
+ valuesValuesXml = createSubElement(valueXml, "VALUES")
235
+ valuesDefinitionsXml = createSubElement(valueXml, "DEFINITION")
236
+ elif value.mytype == "string":
237
+ tempDict["THE-VALUE"] = tempDict['CONTENT']
238
+ tempDict.pop('CONTENT', None)
239
+ value._content = None
240
+ valueXml = createSubElement(valuesXml, "ATTRIBUTE-VALUE-STRING", attributes=tempDict,
241
+ additionalAttributesForElements=['THE-VALUE'])
242
+ valuesDefinitionsXml = createSubElement(valueXml, "DEFINITION")
243
+ elif value.mytype == "embeddedDoc":
244
+ valueXml = createSubElement(valuesXml, "ATTRIBUTE-VALUE-XHTML", attributes=tempDict)
245
+ valuesDefinitionsXml = createSubElement(valueXml, "DEFINITION")
246
+ else:
247
+ print("Unknown Type " + value.mytype)
248
+
249
+ # for val,lab in py2reqif(value.toDict()).iteritems():
250
+ for val, lab in py2reqif(value.toDict()).items():
251
+ if val == "contentRef" and lab is not None:
252
+ for elem in lab:
253
+ createSubElement(valuesValuesXml, "ENUM-VALUE-REF", elem)
254
+ elif val == "attributeRef":
255
+ if value.mytype == "enum":
256
+ createSubElement(valuesDefinitionsXml, "ATTRIBUTE-DEFINITION-ENUMERATION-REF", lab)
257
+ elif value.mytype == "embeddedDoc":
258
+ createSubElement(valuesDefinitionsXml, "ATTRIBUTE-DEFINITION-XHTML-REF", lab)
259
+ elif value.mytype == "string":
260
+ createSubElement(valuesDefinitionsXml, "ATTRIBUTE-DEFINITION-STRING-REF", lab)
261
+ else:
262
+ print("Unknown Type " + value.mytype)
263
+
264
+ elif val == "TYPE":
265
+ pass
266
+ elif val == "CONTENT":
267
+ if lab is not None:
268
+ if "<" in str(lab):
269
+ labtree = lxml.html.fromstring(lab)
270
+ # labtree = etree.parse(io.BytesIO(lab))
271
+ labroot = labtree # .getroot()
272
+ for el in labroot.iter():
273
+ if el.tag == "font" or el.tag == "height" or el.tag == "width":
274
+ el.drop_tag()
275
+ # for key in el.keys:
276
+ for attr in ["height", "width", "align"]:
277
+ if attr in el.keys():
278
+ del (el.attrib[attr])
279
+ if el.tag == "a":
280
+ el.tag = "object"
281
+ el.attrib["data"] = el.attrib["href"].replace(
282
+ 'mks:///item/field?fieldid=Text Attachments&attachmentname=',
283
+ 'attachments/')
284
+ del (el.attrib["href"])
285
+ if el.attrib["data"].endswith(".png"):
286
+ el.attrib["type"] = "image/png"
287
+ elif el.attrib["data"].endswith(".rtf"):
288
+ el.attrib["type"] = "text/x-rtf"
289
+
290
+ if el.tag == "img":
291
+ el.tag = "object"
292
+ el.attrib["data"] = el.attrib["src"].replace(
293
+ 'mks:///item/field?fieldid=Text Attachments&attachmentname=',
294
+ 'attachments/')
295
+
296
+ del (el.attrib["src"])
297
+ if "alt" in el.keys():
298
+ el.text = el.attrib["alt"]
299
+ del (el.attrib["alt"])
300
+ if el.attrib["data"].endswith(".png"):
301
+ el.attrib["type"] = "image/png"
302
+ elif el.attrib["data"].endswith(".rtf"):
303
+ el.attrib["type"] = "text/x-rtf"
304
+ try:
305
+ el.tag = '{http://www.w3.org/1999/xhtml}' + el.tag
306
+ except:
307
+ print("probably XML Comment found - ignoring")
308
+ contentXml = createSubElement(valueXml, "THE-VALUE")
309
+ contentXml.append(labroot)
310
+ else:
311
+ createSubElement(valueXml, "THE-VALUE", lab)
312
+ elif val not in attributesForElements and val not in notUsedAttributes:
313
+ createSubElement(valueXml, val, lab)
314
+ elif value == "typeRef":
315
+ typeXml = createSubElement(specXml, "TYPE")
316
+ createSubElement(typeXml, "SPEC-OBJECT-TYPE-REF", label)
317
+ elif value not in attributesForElements:
318
+ createSubElement(specXml, value, label)
319
+
320
+ #
321
+ # SPEC-RELATIONS
322
+ #
323
+ specsRelXml = createSubElement(reqIfContent, "SPEC-RELATIONS")
324
+ for relation in doc.relations:
325
+ specsRel = createSubElement(specsRelXml, "SPEC-RELATION", attributes=py2reqif(relation))
326
+ for value, label in py2reqif(relation).items():
327
+ if value == "typeRef":
328
+ typeXml = createSubElement(specsRel, "TYPE")
329
+ createSubElement(typeXml, "SPEC-RELATION-TYPE-REF", label)
330
+ elif value == "sourceRef":
331
+ sourceXml = createSubElement(specsRel, "SOURCE")
332
+ createSubElement(sourceXml, "SPEC-OBJECT-REF", label)
333
+ elif value == "targetRef":
334
+ targetXml = createSubElement(specsRel, "TARGET")
335
+ createSubElement(targetXml, "SPEC-OBJECT-REF", label)
336
+
337
+ # else:
338
+ # createSubElement(specsRel , value, label)
339
+ # RELATION-GROUP-TYPE
340
+
341
+ #
342
+ # SPEC-GROUPS
343
+ #
344
+ # specGroupsXml = createSubElement(reqIfContent, "SPEC-GROUPS")
345
+ # for specification in doc.specificationList:
346
+ # specGroupXml = createSubElement(specGroupsXml, "SPEC-GROUP")
347
+ # for value,label in py2reqif(specification.toDict()).iteritems():
348
+ # createSubElement(specGroupXml,value,label)
349
+ # specObjectsXml = createSubElement(specGroupXml,"SPEC-OBJECTS")
350
+ # for req in specification:
351
+ # createSubElement(specObjectsXml ,"SPEC-OBJECT-REF", req)
352
+
353
+ #
354
+ # SPEC-HIERARCHY-ROOTS
355
+ #
356
+ def createChildHirachy(parentXmlTag, childrenList):
357
+ childrenXml = createSubElement(parentXmlTag, 'CHILDREN')
358
+ for childObject in childrenList:
359
+ hierarchXml = createSubElement(childrenXml, 'SPEC-HIERARCHY', attributes=py2reqif(childObject.toDict()))
360
+ # for value, label in py2reqif(childObject.toDict()).iteritems():
361
+ for value, label in py2reqif(childObject.toDict()).items():
362
+ if value == 'objectRef':
363
+ objectXml = createSubElement(hierarchXml, 'OBJECT')
364
+ createSubElement(objectXml, 'SPEC-OBJECT-REF', label)
365
+ elif value not in attributesForElements:
366
+ if label is not None:
367
+ createSubElement(hierarchXml, value, label)
368
+
369
+ if len(childObject.children) > 0:
370
+ createChildHirachy(hierarchXml, childObject.children)
371
+
372
+ return
373
+
374
+ specHierarchRootsXml = createSubElement(reqIfContent, "SPECIFICATIONS")
375
+ # SPEC-HIERARCHY-ROOT
376
+ for hierarch in doc.hierarchy:
377
+ specHierarchRootXml = createSubElement(specHierarchRootsXml, "SPECIFICATION",
378
+ attributes=py2reqif(hierarch.toDict()))
379
+ # for value,label in py2reqif(hierarch.toDict()).iteritems():
380
+ for value, label in py2reqif(hierarch.toDict()).items():
381
+ if value == "typeRef":
382
+ typeXml = createSubElement(specHierarchRootXml, "TYPE")
383
+ createSubElement(typeXml, "SPECIFICATION-TYPE-REF", label)
384
+ elif value not in attributesForElements:
385
+ createSubElement(specHierarchRootXml, value, label)
386
+
387
+ if len(hierarch.children) > 0:
388
+ createChildHirachy(specHierarchRootXml, hierarch.children)
389
+
390
+ #
391
+ # SPEC-RELATION-GROUPS
392
+ #
393
+ specsRelGroupsXml = createSubElement(reqIfContent, "SPEC-RELATION-GROUPS")
394
+ for relationGroup in doc.specRelationGroupList:
395
+ tempAttributes = relationGroup.toDict()
396
+ tempAttributes["longName"] = relationGroup.nameOfType
397
+ specRelGroupXML = createSubElement(specsRelGroupsXml, "RELATION-GROUP", attributes=py2reqif(tempAttributes))
398
+ specRelations = createSubElement(specRelGroupXML, "SPEC-RELATIONS")
399
+ for specRel in relationGroup.specRelationRefs:
400
+ createSubElement(specRelations, "SPEC-RELATION-REF", specRel)
401
+ textXml = createSubElement(specRelGroupXML, "TYPE")
402
+ createSubElement(textXml, "RELATION-GROUP-TYPE-REF", text=relationGroup._identifier + "_type")
403
+
404
+ sourceXML = createSubElement(specRelGroupXML, "SOURCE-SPECIFICATION")
405
+ createSubElement(sourceXML, "SPECIFICATION-REF", text=relationGroup.sourceDoc)
406
+ targetXML = createSubElement(specRelGroupXML, "TARGET-SPECIFICATION")
407
+ createSubElement(targetXML, "SPECIFICATION-REF", text=relationGroup.targetDoc)
408
+
409
+ xmlData = etree.tostring(root, pretty_print=True, xml_declaration=True)
410
+ f.write(xmlData.decode("utf-8"))