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