Project

General

Profile

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 6632 aaronmk
def redmine_bold(text): return '*'+text+'*'
23
24 6620 aaronmk
def redmine_url(text, url):
25
    if url: return '"'+text+'":'+url
26
    else: return text
27
28
def source2redmine_url(url):
29 6623 aaronmk
    if url: return redmine_url(url_term(url), simplify_url(url))
30
    else: return ''
31 6620 aaronmk
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 6633 aaronmk
    try: _prog_name, term_col, sources_col = sys.argv
44 6626 aaronmk
    except ValueError: raise SystemExit('Usage: '+sys.argv[0]
45 6633 aaronmk
        +' <spreadsheet term_col# sources_col# >redmine')
46
    term_col, sources_col = map(int, [term_col, sources_col])
47 6626 aaronmk
48 6632 aaronmk
    # Translate input
49 6620 aaronmk
    reader = csv.reader(sys.stdin)
50
    writer = RedmineTableWriter(sys.stdout)
51 6632 aaronmk
    writer.writerow(map(redmine_bold, reader.next())) # header
52 6620 aaronmk
    for row in reader:
53
        term = row[term_col]
54 6634 aaronmk
        sources = row[sources_col].split(source_sep)
55 6620 aaronmk
56 6634 aaronmk
        row[term_col] = redmine_url(term, simplify_url(sources[0]))
57 6632 aaronmk
        row[sources_col] = source_sep.join(map(source2redmine_url, sources))
58 6628 aaronmk
59 6632 aaronmk
        writer.writerow(row)
60 6620 aaronmk
61
main()