/
csv-export-requirements
executable file
·93 lines (74 loc) · 2.37 KB
/
csv-export-requirements
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
#!/usr/bin/python
# -*- Python -*-
#
# Copyright (C) 2012-2013 Codethink Limited
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# This is an exceedingly dodgy script to do a requirements export to a CSV
# suitable for import into libreoffice calc or excel. It works from the
# UNCOMMITTED state so please don't be surprised if it doesn't match a commit.
import sys
try:
import unicodecsv
csv = unicodecsv
except:
import csv
try:
import html2text
def descfilter(desc):
return html2text.html2text(desc)
except:
def descfilter(desc):
return desc
import mustard
class Thingy(object):
pass
state_id = "UNCOMMITTED"
raw_tree_cache = mustard.rawtree.Cache()
element_tree_cache = mustard.elementtree.Cache(raw_tree_cache)
thingy = Thingy()
thingy.base_url = "http://localhost:8080/"
repository = mustard.repository.Repository(thingy, ".")
state_cache = mustard.state.Cache(thingy, repository)
state = state_cache.get(state_id)
raw_tree = mustard.rawtree.Tree(state)
element_tree = mustard.elementtree.Tree(raw_tree)
elt = element_tree
tlreq = elt.find_all(kind='requirement', top_level=True)
fields = ['ID','Title','Description','Tags','Path']
w = csv.DictWriter(sys.stdout, fields)
def writeel(p, el, v):
tagl = el.tags.keys()
tagl.sort()
tagstr = ("\n").join(tagl)
d = {
'ID': v,
'Path': p,
'Title': el.title,
'Description': descfilter(el.description if el.description else "").strip(),
'Tags': tagstr,
}
w.writerow(d)
writeels(el.subrequirements, v)
def writeels(els, pfx):
eld = dict(els)
ks = eld.keys()
ks.sort()
n = 1
for k in ks:
v = ("%s.%d" % (pfx, n)).strip('.')
writeel(k, eld[k], v)
n = n + 1
w.writeheader()
writeels(tlreq, "")