-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreqgen.py
170 lines (135 loc) · 5.16 KB
/
reqgen.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python
"""Requirements Tracing Generator.
USAGE: python reqgen.py <in_filename> <out_filename>
Prerequisites: xtwt, xlrd in virtualenv
"""
__author__ = 'Michael Meisinger'
import csv
import datetime
import os
import sys
import xlwt
from xlsparser import XLSParser
REQ_FILE = "Deliverabe-Milestone-Requirement_Mapping_V02.xlsx"
OUT_FILE_PREFIX = "output/reqanalysis"
OUT_TRACE_PREFIX = "output/tracing"
TAB_TRACING = "Example v2"
HTABLE_START = """
<div class="panel" style="border-width: 1px;">
<div class="panelContent">
<h3>%%TITLE%%</h3>
<div class='table-wrap'>
<table class='confluenceTable'>"""
HTABLE_END = """
</table>
</div>
</div>
</div>"""
HTABLE_ROW_START = """
<tr>"""
HTABLE_ROW_END = """
</tr>"""
HTABLE_HEAD_ROW = """
<th class='confluenceTh'>%%TEXT%%</th>"""
HTABLE_ROW = """
<td class='confluenceTd'>%%TEXT%%</td>"""
HTABLE_SEP = """
<p>
<br class="atl-forced-newline" />
</p>"""
class ReqAnalysis(object):
def __init__(self):
self.req = {}
def parse(self, filename):
# Load from load xlsx file
if os.path.exists(filename):
with open(filename, "rb") as f:
print "Opened", filename
doc_str = f.read()
print "Read req file, size=%s" % len(doc_str)
xls_parser = XLSParser()
self.csv_files = xls_parser.extract_csvs(doc_str)
print "Parsed req file OK. Found %s tabs." % len(self.csv_files)
else:
print "ERROR: Requirements file %s does not exist" % filename
sys.exit(1)
PARSE_TABS = [
(TAB_TRACING, "tracing"),
]
for tab, name in PARSE_TABS:
tab_rows = self.csv_files[tab]
parse_func_name = "_parse_%s" % name
if hasattr(self, parse_func_name):
parse_func = getattr(self, parse_func_name)
reader = csv.DictReader(tab_rows, delimiter=',')
print "Parsing tab", tab
self._lnum = 0
self._add_cnt = 0
for row in reader:
res = parse_func(row)
self._lnum += 1
if res:
self._add_cnt += 1
print " ...using %s of %s rows" % (self._add_cnt, self._lnum)
def dump_trace_files(self):
for ms_id in sorted(self.req):
ms_list = self.req[ms_id]
if not os.path.exists(OUT_TRACE_PREFIX):
os.makedirs(OUT_TRACE_PREFIX)
ms_filename = "%s/%s.html" % (OUT_TRACE_PREFIX, ms_id)
with open(ms_filename, "w") as f:
ms_item = ms_list[0]
f.write(HTABLE_START.replace("%%TITLE%%", "Requirements for %s (%s)" % (ms_id, ms_item["subject"])))
f.write(HTABLE_ROW_START)
f.write(HTABLE_HEAD_ROW.replace("%%TEXT%%", "Relationship"))
f.write(HTABLE_HEAD_ROW.replace("%%TEXT%%", "Object"))
f.write(HTABLE_HEAD_ROW.replace("%%TEXT%%", "Sub-Relationship"))
f.write(HTABLE_HEAD_ROW.replace("%%TEXT%%", "Sub-Object"))
f.write(HTABLE_HEAD_ROW.replace("%%TEXT%%", "Description"))
f.write(HTABLE_ROW_END)
for item in ms_list[1:]:
f.write(HTABLE_ROW_START)
f.write(HTABLE_ROW.replace("%%TEXT%%", item["rel1"]))
f.write(HTABLE_ROW.replace("%%TEXT%%", item["object"]))
f.write(HTABLE_ROW.replace("%%TEXT%%", item["rel2"]))
f.write(HTABLE_ROW.replace("%%TEXT%%", item["subobj"]))
f.write(HTABLE_ROW.replace("%%TEXT%%", item["desc"]))
f.write(HTABLE_ROW_END)
f.write(HTABLE_END)
def do_all(self, in_filename=None, out_filename=None):
self.parse(in_filename or REQ_FILE)
self.dump_trace_files()
# -------------------------------------------------------------------------
def _add_req(self, level, req_id, req):
if not level in self.req:
self.req[level] = []
req_dict = self.req[level]
req_dict.append(req)
# -------------------------------------------------------------------------
def _parse_tracing(self, row):
activated = row["Activated"]
sub_dom = row["Subject Domain"]
if str(activated) != "1":
return
if sub_dom != "Milestone":
return
ms_id = row["Subject ID"]
item_id = row["Sort ID"]
ms_dict = dict(
ms_id=ms_id,
item_id=item_id,
subject=row["Subject Title"],
rel1=row["Relationship"],
object=row["Object Title"],
rel2=row["Sub-Relationship"],
subobj=row["Subobject Title"],
desc=row["Description"],
order=self._lnum
)
self._add_req(ms_id, item_id, ms_dict)
return True
if __name__ == '__main__':
in_filename = sys.argv[1] if len(sys.argv) >= 2 else None
out_filename = sys.argv[2] if len(sys.argv) >= 3 else None
ra = ReqAnalysis()
ra.do_all(in_filename, out_filename)