1
|
#!/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
|
match = re.search(r'[\w:-]+$', url)
|
17
|
assert match
|
18
|
return match.group(0)
|
19
|
|
20
|
##### Redmine
|
21
|
|
22
|
def redmine_bold(text): return '*'+text+'*'
|
23
|
|
24
|
def redmine_url(text, url):
|
25
|
if url: return '"'+text+'":'+url
|
26
|
else: return text
|
27
|
|
28
|
def source2redmine_url(url):
|
29
|
if url: return redmine_url(url_term(url), simplify_url(url))
|
30
|
else: return ''
|
31
|
|
32
|
class RedmineTableWriter:
|
33
|
'''Formats rows as a Redmine table'''
|
34
|
|
35
|
def __init__(self, stream):
|
36
|
self.stream = stream
|
37
|
|
38
|
def writerow(self, row):
|
39
|
self.stream.write(('|'.join(['']+row+['']))+'\n')
|
40
|
|
41
|
|
42
|
def main():
|
43
|
try: _prog_name, term_col, sources_col = sys.argv
|
44
|
except ValueError: raise SystemExit('Usage: '+sys.argv[0]
|
45
|
+' <spreadsheet term_col# sources_col# >redmine')
|
46
|
term_col, sources_col = map(int, [term_col, sources_col])
|
47
|
|
48
|
# Translate input
|
49
|
reader = csv.reader(sys.stdin)
|
50
|
writer = RedmineTableWriter(sys.stdout)
|
51
|
writer.writerow(map(redmine_bold, reader.next())) # header
|
52
|
for row in reader:
|
53
|
term = row[term_col]
|
54
|
sources = map(simplify_url, row[sources_col].split(source_sep))
|
55
|
|
56
|
row[term_col] = redmine_url(term, sources[0])
|
57
|
row[sources_col] = source_sep.join(map(source2redmine_url, sources))
|
58
|
|
59
|
writer.writerow(row)
|
60
|
|
61
|
main()
|