Project

General

Profile

1
#!/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.group(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
    if url: return redmine_url(url_term(url), simplify_url(url))
32
    else: return ''
33

    
34
class RedmineTableWriter:
35
    '''Formats rows as a Redmine table'''
36
    
37
    def __init__(self, stream):
38
        self.stream = stream
39
    
40
    def writerow(self, row):
41
        self.stream.write(('|'.join(['']+row+['']))+'\n')
42

    
43

    
44
def main():
45
    # Determine column order
46
    reader = csv.reader(sys.stdin)
47
    header = reader.next()
48
    sources_col = header.index('Sources')
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
        sources_str = source_sep.join(sources)
57
        
58
        writer.writerow([redmine_url(term, sources[0]), sources_str])
59

    
60
main()
(13-13/69)