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/pyreqif.py ADDED
@@ -0,0 +1,729 @@
1
+ #!/usr/bin/env python2
2
+ # -*- coding: utf-8 -*-
3
+ import io
4
+ from lxml import etree
5
+ import collections
6
+
7
+
8
+ class reqIfObject(object):
9
+ def __init__(self):
10
+ self._identifier = None
11
+ self._lastChanged = 0
12
+ self._logName = ""
13
+
14
+ def setValues(self, **kwargs):
15
+ self._reqIfObjargs = [
16
+ ('identifier', '_identifier', str, None),
17
+ ('lastChange', '_lastChanged', str, None),
18
+ ('longName', '_longname', str, None),
19
+ ]
20
+ for arg_name, destination, function, default in self._reqIfObjargs:
21
+ try:
22
+ value = kwargs[arg_name]
23
+ except KeyError:
24
+ value = default
25
+ else:
26
+ kwargs.pop(arg_name)
27
+ # if value is not None:
28
+ setattr(self, destination, value)
29
+ return kwargs
30
+
31
+ def toDict(self):
32
+ myDict = {}
33
+ for arg_name, attrib, function, default in self._reqIfObjargs:
34
+ myDict[arg_name] = getattr(self, attrib)
35
+ return myDict
36
+
37
+
38
+ class header(reqIfObject):
39
+ def __init__(self, **kwargs):
40
+ self._args = [
41
+ ('author', '_author', str, None),
42
+ ('countryCode', '_countrycode', str, None),
43
+ ('creationTime', '_creationtime', str, None),
44
+ ('sourceToolId', '_sourceToolId', str, None),
45
+ ('title', '_title', None, None),
46
+ ('version', '_version', str, None),
47
+ ('comment', '_comment', str, None),
48
+ ]
49
+
50
+ for arg_name, destination, function, default in self._args:
51
+ try:
52
+ value = kwargs[arg_name]
53
+ except KeyError:
54
+ value = default
55
+ else:
56
+ kwargs.pop(arg_name)
57
+ if function is not None and value is not None:
58
+ value = function(value)
59
+ setattr(self, destination, value)
60
+
61
+ kwargs = reqIfObject.setValues(self, **kwargs)
62
+
63
+ if len(kwargs) > 0:
64
+ raise TypeError('{}() got unexpected argument{} {}'.format(
65
+ self.__class__.__name__,
66
+ 's' if len(kwargs) > 1 else '',
67
+ ', '.join(kwargs.keys())
68
+ ))
69
+
70
+ def toDict(self):
71
+ myDict = reqIfObject.toDict(self)
72
+ for arg_name, attrib, function, default in self._args:
73
+ myDict[arg_name] = getattr(self, attrib)
74
+ return myDict
75
+
76
+
77
+ class datatype(reqIfObject):
78
+ def __init__(self, **kwargs):
79
+ self.datatypeArgs = [
80
+ ('type', '_type', str, None),
81
+ ('embedded', '_embedded', str, None),
82
+ ]
83
+
84
+ for arg_name, destination, function, default in self.datatypeArgs:
85
+ try:
86
+ value = kwargs[arg_name]
87
+ except KeyError:
88
+ value = default
89
+ else:
90
+ kwargs.pop(arg_name)
91
+ if function is not None and value is not None:
92
+ value = function(value)
93
+ setattr(self, destination, value)
94
+
95
+ kwargs = reqIfObject.setValues(self, **kwargs)
96
+
97
+ if "values" in kwargs:
98
+ self._isValueTable = True
99
+ self._valueTable = {}
100
+ # for valId, value in kwargs["values"].iteritems():
101
+ for valId, value in kwargs["values"].items():
102
+ self._valueTable[valId] = value
103
+ kwargs.pop("values")
104
+
105
+ if len(kwargs) > 0:
106
+ raise TypeError('{}() got unexpected argument{} {}'.format(
107
+ self.__class__.__name__,
108
+ 's' if len(kwargs) > 1 else '',
109
+ ', '.join(kwargs.keys())
110
+ ))
111
+
112
+ @property
113
+ def mytype(self):
114
+ return self._type
115
+
116
+ @property
117
+ def valueTable(self):
118
+ return self._valueTable
119
+
120
+ def toDict(self):
121
+ myDict = reqIfObject.toDict(self)
122
+ for arg_name, attrib, function, default in self.datatypeArgs:
123
+ myDict[arg_name] = getattr(self, attrib)
124
+ return myDict
125
+
126
+
127
+ class datatypeList(reqIfObject):
128
+ def __init__(self):
129
+ self._list = []
130
+
131
+ def add(self, myDatatypeDict):
132
+ self._list.append(datatype(**myDatatypeDict))
133
+
134
+ def byId(self, datatypeId):
135
+ for dt in self._list:
136
+ if datatypeId == dt._identifier:
137
+ return dt
138
+ return None
139
+
140
+ def __iter__(self):
141
+ return iter(self._list)
142
+
143
+
144
+ class reqTypeRefs(reqIfObject):
145
+ def __init__(self, **kwargs):
146
+ kwargs = reqIfObject.setValues(self, **kwargs)
147
+ if "typeRef" in kwargs:
148
+ self._typeref = kwargs["typeRef"]
149
+ kwargs.pop("typeRef")
150
+ else:
151
+ self._typeref = None
152
+
153
+ if "defaultValue" in kwargs:
154
+ self._defaultValue = kwargs["defaultValue"]
155
+ kwargs.pop("defaultValue")
156
+ else:
157
+ self._defaultValue = None
158
+
159
+ if "type" in kwargs:
160
+ self._type = kwargs["type"]
161
+ kwargs.pop("type")
162
+ else:
163
+ self._typeref = None
164
+
165
+ if "multiValued" in kwargs:
166
+ self._multiValued = kwargs["multiValued"]
167
+ kwargs.pop("multiValued")
168
+ else:
169
+ self._multiValued = False
170
+
171
+ def toDict(self):
172
+ myDict = reqIfObject.toDict(self)
173
+ if self._typeref is not None:
174
+ myDict["typeRef"] = self._typeref
175
+ if self._type is not None:
176
+ myDict["type"] = self._type
177
+ if self._multiValued:
178
+ myDict["multiValued"] = self._multiValued
179
+
180
+ return myDict
181
+
182
+
183
+ class specRelationGroup(reqIfObject):
184
+ def __init__(self, **kwargs):
185
+ self.sourceDoc = None
186
+ self.targetDoc = None
187
+ kwargs = reqIfObject.setValues(self, **kwargs)
188
+ if "desc" in kwargs:
189
+ self._desc = kwargs["desc"]
190
+ kwargs.pop("desc")
191
+ else:
192
+ self._desc = None
193
+ self.sourceDoc = kwargs["sourceDoc"]
194
+ self.targetDoc = kwargs["targetDoc"]
195
+ self.nameOfType = kwargs["longNameOfType"]
196
+ self.specRelationRefs = kwargs["specRelationRefs"]
197
+
198
+
199
+ class specRelationGroupList(reqIfObject):
200
+ def __init__(self):
201
+ self._list = []
202
+
203
+ def add(self, myReqTypeDict):
204
+ reqType = specRelationGroup(**myReqTypeDict)
205
+ temp = self.byId(reqType._identifier)
206
+ if temp is None:
207
+ self._list.append(reqType)
208
+ else:
209
+ for id in reqType.myTypes.keys():
210
+ temp.myTypes[id] = reqType.myTypes[id]
211
+
212
+ def byId(self, identifier):
213
+ for relType in self._list:
214
+ if relType._identifier == identifier:
215
+ return relType
216
+
217
+ def __iter__(self):
218
+ return iter(self._list)
219
+
220
+
221
+ class specRelationType(reqIfObject):
222
+ def __init__(self, **kwargs):
223
+ kwargs = reqIfObject.setValues(self, **kwargs)
224
+ if "desc" in kwargs:
225
+ self._desc = kwargs["desc"]
226
+ kwargs.pop("desc")
227
+ else:
228
+ self._desc = None
229
+
230
+
231
+ class specRelationTypeList(reqIfObject):
232
+ def __init__(self):
233
+ self._list = []
234
+
235
+ def add(self, myReqTypeDict):
236
+ reqType = specRelationType(**myReqTypeDict)
237
+ temp = self.byId(reqType._identifier)
238
+ if temp is None:
239
+ self._list.append(reqType)
240
+ else:
241
+ for id in reqType.myTypes.keys():
242
+ temp.myTypes[id] = reqType.myTypes[id]
243
+
244
+ def byId(self, identifier):
245
+ for relType in self._list:
246
+ if relType._identifier == identifier:
247
+ return relType
248
+
249
+ def __iter__(self):
250
+ return iter(self._list)
251
+
252
+
253
+ class requirementType(reqIfObject):
254
+ def __init__(self, **kwargs):
255
+ self._myTypes = {}
256
+ kwargs = reqIfObject.setValues(self, **kwargs)
257
+ if "desc" in kwargs:
258
+ self._desc = kwargs["desc"]
259
+ kwargs.pop("desc")
260
+ else:
261
+ self._desc = None
262
+ for myType in kwargs:
263
+ self._myTypes[myType] = reqTypeRefs(**kwargs[myType])
264
+
265
+ @property
266
+ def myTypes(self):
267
+ return self._myTypes
268
+
269
+ @property
270
+ def fields(self):
271
+ fields = []
272
+ # for type,typeObj in self._myTypes.iteritems():
273
+ for type, typeObj in self._myTypes.items():
274
+ fields.append(typeObj._longname)
275
+ return fields
276
+
277
+ def attribById(self, attribId):
278
+ for attrib in self._myTypes:
279
+ if attrib == attribId:
280
+ return self._myTypes[attrib]
281
+
282
+ def toDict(self):
283
+ myDict = reqIfObject.toDict(self)
284
+ if self._desc:
285
+ myDict["desc"] = self._desc
286
+ return myDict
287
+
288
+
289
+ class requirementTypeList(reqIfObject):
290
+ def __init__(self):
291
+ self._list = []
292
+
293
+ def add(self, myReqTypeDict):
294
+ reqType = requirementType(**myReqTypeDict)
295
+ temp = self.byId(reqType._identifier)
296
+ if temp is None:
297
+ self._list.append(reqType)
298
+ else:
299
+ for id in reqType.myTypes.keys():
300
+ temp.myTypes[id] = reqType.myTypes[id]
301
+
302
+ def byId(self, identifier):
303
+ for reqType in self._list:
304
+ if reqType._identifier == identifier:
305
+ return reqType
306
+
307
+ @property
308
+ def fields(self):
309
+ fields = []
310
+ for reqType in self._list:
311
+ fields += reqType.fields
312
+ return fields
313
+
314
+ def __iter__(self):
315
+ return iter(self._list)
316
+
317
+
318
+ class requirementItem(reqIfObject):
319
+ def __init__(self, **kwargs):
320
+ kwargs = reqIfObject.setValues(self, **kwargs)
321
+ self.reqItem_args = [
322
+ ('contentRef', '_contentref', None, None),
323
+ ('content', '_content', None, None),
324
+ ('attributeRef', '_attributeref', str, None),
325
+ ('type', '_type', str, None),
326
+ ]
327
+
328
+ for arg_name, destination, function, default in self.reqItem_args:
329
+ try:
330
+ value = kwargs[arg_name]
331
+ except KeyError:
332
+ value = default
333
+ else:
334
+ kwargs.pop(arg_name)
335
+ if function is not None and value is not None:
336
+ value = function(value)
337
+ setattr(self, destination, value)
338
+
339
+ if len(kwargs) > 0:
340
+ raise TypeError('{}() got unexpected argument{} {}'.format(
341
+ self.__class__.__name__,
342
+ 's' if len(kwargs) > 1 else '',
343
+ ', '.join(kwargs.keys())
344
+ ))
345
+
346
+ @property
347
+ def mytype(self):
348
+ return self._type
349
+
350
+ def toDict(self):
351
+ myDict = reqIfObject.toDict(self)
352
+
353
+ for arg_name, attrib, function, default in self.reqItem_args:
354
+ myDict[arg_name] = getattr(self, attrib)
355
+
356
+ return myDict
357
+
358
+
359
+ class requirement(reqIfObject):
360
+ def __init__(self, **kwargs):
361
+ self._values = []
362
+ kwargs = reqIfObject.setValues(self, **kwargs)
363
+ if "typeRef" in kwargs:
364
+ self._typeref = kwargs["typeRef"]
365
+ kwargs.pop("typeRef")
366
+ else:
367
+ self._typeref = None
368
+ # for ident, value in kwargs["values"].iteritems():
369
+ for ident, value in kwargs["values"].items():
370
+ self._values.append(requirementItem(**value))
371
+
372
+ @property
373
+ def values(self):
374
+ return self._values
375
+
376
+ @property
377
+ def values_text(self):
378
+ textValues = []
379
+ for value in self._values:
380
+ newVal = value
381
+ # print value._content
382
+ if value._contentref is None and value._content is not None and len(value._content) > 0 and value._content[
383
+ 0] == "<":
384
+ try:
385
+ tree = etree.parse(io.BytesIO(value._content))
386
+ root = tree.getroot()
387
+ test = "".join(root.itertext())
388
+ newVal._content = test
389
+ except:
390
+ pass
391
+ textValues.append(newVal)
392
+ return textValues
393
+
394
+ def toDict(self):
395
+ myDict = reqIfObject.toDict(self)
396
+ if self._typeref is not None:
397
+ myDict["typeRef"] = self._typeref
398
+ myDict["values"] = self._values
399
+ return myDict
400
+
401
+
402
+ class requirementList(reqIfObject):
403
+ def __init__(self):
404
+ self._list = []
405
+
406
+ def add(self, myReqDict):
407
+ myReq = requirement(**myReqDict)
408
+ temp = self.byId(myReq._identifier)
409
+ if temp is None:
410
+ self._list.append(myReq)
411
+ else:
412
+ for id in myReq.values:
413
+ temp.values.append(id)
414
+
415
+ def byId(self, reqId):
416
+ for req in self._list:
417
+ if req._identifier == reqId:
418
+ return req
419
+
420
+ def __iter__(self):
421
+ return iter(self._list)
422
+
423
+
424
+ class specification(reqIfObject):
425
+ def __init__(self, **mySpecDict):
426
+ mySpecDict = reqIfObject.setValues(self, **mySpecDict)
427
+ if "desc" in mySpecDict:
428
+ self._desc = mySpecDict["desc"]
429
+ mySpecDict.pop("desc")
430
+ else:
431
+ self._desc = None
432
+ self._list = []
433
+ if len(mySpecDict) > 0:
434
+ raise TypeError('{}() got unexpected argument{} {}'.format(
435
+ self.__class__.__name__,
436
+ 's' if len(mySpecDict) > 1 else '',
437
+ ', '.join(mySpecDict.keys())
438
+ ))
439
+
440
+ def __iter__(self):
441
+ return iter(self._list)
442
+
443
+ @property
444
+ def name(self):
445
+ return self._longname
446
+
447
+ @property
448
+ def desc(self):
449
+ return self._desc
450
+
451
+ def toDict(self):
452
+ myDict = reqIfObject.toDict(self)
453
+ if self._desc is not None:
454
+ myDict["desc"] = self._desc
455
+ return myDict
456
+
457
+ def addReq(self, reqId):
458
+ self._list.append(reqId)
459
+
460
+
461
+ class specificationList(reqIfObject):
462
+ def __init__(self):
463
+ self._list = []
464
+
465
+ def add(self, mySpec):
466
+ self._list.append(mySpec)
467
+
468
+ def __iter__(self):
469
+ return iter(self._list)
470
+
471
+ def __len__(self):
472
+ return len(self._list)
473
+
474
+
475
+ class hierarchy(reqIfObject):
476
+ def __init__(self, **kwargs):
477
+ kwargs = reqIfObject.setValues(self, **kwargs)
478
+ if "typeRef" in kwargs:
479
+ self._typeref = kwargs["typeRef"]
480
+ kwargs.pop("typeRef")
481
+ else:
482
+ self._typeref = None
483
+ if "objectRef" in kwargs:
484
+ self._objectref = kwargs["objectRef"]
485
+ kwargs.pop("objectRef")
486
+ else:
487
+ self._objectref = None
488
+
489
+ self._children = []
490
+
491
+ @property
492
+ def children(self):
493
+ return self._children
494
+
495
+ def toDict(self):
496
+ myDict = reqIfObject.toDict(self)
497
+ if self._typeref is not None:
498
+ myDict["typeRef"] = self._typeref
499
+ if self._objectref is not None:
500
+ myDict["objectRef"] = self._objectref
501
+ return myDict
502
+
503
+ def addChild(self, child):
504
+ self._children.append(child)
505
+
506
+
507
+ class relation(reqIfObject):
508
+ def __init__(self, **kwargs):
509
+ kwargs = reqIfObject.setValues(self, **kwargs)
510
+ args = [
511
+ ('typeRef', '_typeref', str, None),
512
+ ('desc', '_desc', None, None),
513
+ ('sourceRef', '_sourceref', str, None),
514
+ ('targetRef', '_targetref', str, None),
515
+ ]
516
+
517
+ for arg_name, destination, function, default in args:
518
+ try:
519
+ value = kwargs[arg_name]
520
+ except KeyError:
521
+ value = default
522
+ else:
523
+ kwargs.pop(arg_name)
524
+ if function is not None and value is not None:
525
+ value = function(value)
526
+ setattr(self, destination, value)
527
+
528
+ if len(kwargs) > 0:
529
+ raise TypeError('{}() got unexpected argument{} {}'.format(
530
+ self.__class__.__name__,
531
+ 's' if len(kwargs) > 1 else '',
532
+ ', '.join(kwargs.keys())
533
+ ))
534
+
535
+
536
+ class relationList(reqIfObject):
537
+ def __init__(self):
538
+ self._list = []
539
+
540
+ def add(self, relation):
541
+ self._list.append(relation)
542
+
543
+ def __iter__(self):
544
+ return iter(self._list)
545
+
546
+
547
+ class specificationTypes(reqIfObject):
548
+ def __init__(self):
549
+ self._list = []
550
+
551
+ def add(self, relation):
552
+ self._list.append(relation)
553
+
554
+ def __iter__(self):
555
+ return iter(self._list)
556
+
557
+
558
+ class doc(reqIfObject):
559
+ def __init__(self):
560
+ self._header = None
561
+ self._datatypeList = datatypeList()
562
+ self._requirementTypeList = requirementTypeList()
563
+ self._requirementList = requirementList()
564
+ self._specificationList = specificationList()
565
+ self._hierarchy = []
566
+ self._specRelationTypeList = specRelationTypeList()
567
+ self._specRelationGroupList = specRelationGroupList()
568
+ self._relations = relationList()
569
+ self.specificationTypes = specificationTypes()
570
+
571
+ @property
572
+ def header(self):
573
+ return self._header.toDict()
574
+
575
+ @property
576
+ def datatypeList(self):
577
+ return self._datatypeList
578
+
579
+ @property
580
+ def requirementTypeList(self):
581
+ return self._requirementTypeList
582
+
583
+ @property
584
+ def requirementList(self):
585
+ return self._requirementList
586
+
587
+ @property
588
+ def relations(self):
589
+ return self._relations
590
+
591
+ @property
592
+ def hierarchy(self):
593
+ return self._hierarchy
594
+
595
+ @property
596
+ def specificationList(self):
597
+ if len(self._specificationList) == 0:
598
+ def createFlatSpec(asd, spec):
599
+ spec.addReq(asd["reqifId"])
600
+ for id, child in asd["children"].items():
601
+ createFlatSpec(child, spec)
602
+
603
+ hierarchSpec = self.asHierarchDict()
604
+ spec = specification()
605
+ for id, topElement in hierarchSpec[0].items():
606
+ createFlatSpec(topElement, spec)
607
+ self.addSpecification(spec)
608
+ return self._specificationList
609
+
610
+ @property
611
+ def fields(self):
612
+ return self._requirementTypeList.fields
613
+
614
+ @property
615
+ def specRelationTypeList(self):
616
+ return self._specRelationTypeList
617
+
618
+ @property
619
+ def specRelationGroupList(self):
620
+ return self._specRelationGroupList
621
+
622
+ def addHeader(self, myHeader):
623
+ self._header = header(**myHeader)
624
+
625
+ def addDatatype(self, myDatatypeDict):
626
+ self._datatypeList.add(myDatatypeDict)
627
+
628
+ def addSpecRelationType(self, myDatatypeDict):
629
+ self._specRelationTypeList.add(myDatatypeDict)
630
+
631
+ def addSpecRelationGroup(self, myDatatypeDict):
632
+ self._specRelationGroupList.add(myDatatypeDict)
633
+
634
+ def datatypeById(self, datatypeId):
635
+ return self._datatypeList.byId(datatypeId)
636
+
637
+ def addRequirementType(self, myReqTypeDict):
638
+ self._requirementTypeList.add(myReqTypeDict)
639
+
640
+ def addRequirement(self, myReqDict):
641
+ self._requirementList.add(myReqDict)
642
+
643
+ def addSpecification(self, mySpec):
644
+ self._specificationList.add(mySpec)
645
+
646
+ def getReqById(self, reqId):
647
+ for req in self._requirementList._list:
648
+ if req._identifier == reqId:
649
+ return req
650
+
651
+ def getReqByLongName(self, reqLongName):
652
+ for req in self._requirementList._list:
653
+ if req._longname == reqLongName:
654
+ return req
655
+
656
+ def flatReq(self, requirement, **kwargs):
657
+ reqDict = {}
658
+ reqType = self.requirementTypeList.byId(requirement._typeref)
659
+
660
+ # for myTypeId,myType in reqType.myTypes.iteritems():
661
+ for myTypeId, myType in reqType.myTypes.items():
662
+ if myType._defaultValue is not None:
663
+ dataType = self.datatypeById(myType._typeref)
664
+ reqDict[myType._longname] = dataType._valueTable[myType._defaultValue]["longName"]
665
+
666
+ if "html" in kwargs and kwargs["html"]:
667
+ valueArray = requirement.values
668
+ else:
669
+ valueArray = requirement.values_text
670
+ for value in valueArray:
671
+ valueType = reqType.attribById(value._attributeref)
672
+ dataType = self.datatypeById(valueType._typeref)
673
+
674
+ if value._contentref is not None:
675
+ reqDict[valueType._longname] = ""
676
+ for contentref in value._contentref:
677
+ if "longName" in dataType._valueTable[contentref]:
678
+ reqDict[valueType._longname] += dataType._valueTable[contentref]["longName"] + ", "
679
+ # remove trailing comma
680
+ reqDict[valueType._longname] = reqDict[valueType._longname][:-2]
681
+ else:
682
+ reqDict[valueType._longname] = value._content
683
+ return reqDict
684
+
685
+ def addRelation(self, relation):
686
+ self._relations.add(relation)
687
+
688
+ def asDict(self):
689
+ cols = self.fields
690
+ specs = []
691
+
692
+ for specification in self.specificationList:
693
+ spec = []
694
+ for req in specification:
695
+ tempReq = self.req2dict(req, cols)
696
+ spec.append(tempReq)
697
+ specs.append(spec)
698
+ return specs
699
+
700
+ def req2dict(self, req, cols):
701
+ reqObj = self.getReqById(req)
702
+ tempReq = collections.OrderedDict()
703
+ for col in cols:
704
+ tempReq[col] = ""
705
+ for col, value in self.flatReq(reqObj, html=True).items():
706
+ tempReq[col] = value
707
+ tempReq["reqifId"] = req
708
+ return tempReq
709
+
710
+ def asHierarchDict(self):
711
+ cols = self.fields
712
+ spec = []
713
+
714
+ for child in self.hierarchy:
715
+ spec.append(self._hierarchDict(child, cols))
716
+ return spec
717
+
718
+ def _hierarchDict(self, element, cols):
719
+ retDict = collections.OrderedDict()
720
+ for child in element.children:
721
+ retDict[child._objectref] = self.req2dict(child._objectref, cols)
722
+ retDict[child._objectref]["children"] = self._hierarchDict(child, cols)
723
+ return retDict
724
+
725
+ def hierach_iterator(self, element, cols, depth=0):
726
+ for child in element.children:
727
+ yield [self.req2dict(child._objectref, cols), depth]
728
+ for child in self.hierach_iterator(child, cols, depth + 1):
729
+ yield child