1 |
6620
|
aaronmk
|
#!/usr/bin/env python
|
2 |
|
|
# Translates a data dictionary spreadsheet to Redmine formatting
|
3 |
|
|
|
4 |
|
|
import csv
|
5 |
|
|
import re
|
6 |
|
|
import sys
|
7 |
|
|
|
8 |
|
|
# Spreadsheet format
|
9 |
|
|
source_sep = ', '
|
10 |
|
|
|
11 |
|
|
##### URLs
|
12 |
|
|
|
13 |
|
|
def simplify_url(url): return re.sub(r'\(.*?\)', r'', url)
|
14 |
|
|
|
15 |
|
|
def url_term(url):
|
16 |
6625
|
aaronmk
|
match = re.search(r'[\w:-]+$', url)
|
17 |
6620
|
aaronmk
|
assert match
|
18 |
6622
|
aaronmk
|
return match.group(0)
|
19 |
6620
|
aaronmk
|
|
20 |
|
|
##### Redmine
|
21 |
|
|
|
22 |
|
|
def redmine_url(text, url):
|
23 |
|
|
if url: return '"'+text+'":'+url
|
24 |
|
|
else: return text
|
25 |
|
|
|
26 |
|
|
def source2redmine_url(url):
|
27 |
6623
|
aaronmk
|
if url: return redmine_url(url_term(url), simplify_url(url))
|
28 |
|
|
else: return ''
|
29 |
6620
|
aaronmk
|
|
30 |
|
|
class RedmineTableWriter:
|
31 |
|
|
'''Formats rows as a Redmine table'''
|
32 |
|
|
|
33 |
|
|
def __init__(self, stream):
|
34 |
|
|
self.stream = stream
|
35 |
|
|
|
36 |
|
|
def writerow(self, row):
|
37 |
|
|
self.stream.write(('|'.join(['']+row+['']))+'\n')
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
def main():
|
41 |
6626
|
aaronmk
|
try: _prog_name, term_col, sources_col = sys.argv
|
42 |
|
|
except ValueError: raise SystemExit('Usage: '+sys.argv[0]
|
43 |
|
|
+' <spreadsheet term_col# sources_col# >redmine')
|
44 |
|
|
term_col, sources_col = map(int, [term_col, sources_col])
|
45 |
|
|
|
46 |
6620
|
aaronmk
|
# Determine column order
|
47 |
|
|
reader = csv.reader(sys.stdin)
|
48 |
|
|
header = reader.next()
|
49 |
|
|
|
50 |
|
|
# Translate input
|
51 |
|
|
writer = RedmineTableWriter(sys.stdout)
|
52 |
|
|
writer.writerow(['*Term*', '*Sources*'])
|
53 |
|
|
for row in reader:
|
54 |
|
|
term = row[term_col]
|
55 |
|
|
sources = map(simplify_url, row[sources_col].split(source_sep))
|
56 |
6624
|
aaronmk
|
sources_str = source_sep.join(map(source2redmine_url, sources))
|
57 |
6620
|
aaronmk
|
|
58 |
|
|
writer.writerow([redmine_url(term, sources[0]), sources_str])
|
59 |
|
|
|
60 |
|
|
main()
|