Project

General

Profile

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
url_comment_re = r'(?:\([^)]*\))'
14

    
15
def simplify_url(url): return re.sub(r'\(.*?\)', r'', url)
16

    
17
def url_comment_text(comment):
18
    if not comment: return comment
19
    
20
    match = re.match(r'^\(\d*:?(.*?)\)$', comment)
21
    assert match
22
    text, = match.groups()
23
    return text
24

    
25
def url_term(url):
26
    '''Prefixes any provider in the URL to the term name, to create a namespace.
27
    Each hierarchical component of the provider is stored in a URL comment.
28
    '''
29
    match = re.match(r'^('+url_comment_re+r'*).*?([\w:-]+)$', url)
30
    assert match
31
    provider_str, term = match.groups()
32
    provider = filter(bool, map(url_comment_text,
33
        re.findall(url_comment_re, provider_str)))
34
    
35
    return ':'.join(provider+[term])
36

    
37
##### Redmine
38

    
39
sp = ' ' # Unicode thin space
40

    
41
def redmine_esc(text): return '<notextile>'+text+'</notextile>'
42

    
43
def redmine_bold(text): return '*'+text+'*'
44

    
45
def redmine_pad(text): return sp+text+sp
46

    
47
def redmine_link(text, url):
48
    if not url: return text
49
    elif url.find('://') >= 0: return '"'+text+'":'+url # external link
50
    else: return '[['+url+'|'+text+']]' # internal link
51

    
52
def source2redmine_link(url):
53
    if url: return redmine_link(url_term(url), simplify_url(url))
54
    else: return ''
55

    
56
def redmine_add_links(page_name, text):
57
    # Link citations to entry in sources list
58
    text = re.sub(r'(?<!\[)\[([^\[\]]*)\](?!\])',
59
        '('+redmine_link(r'\1', page_name+r'#\1')+')', text)
60
    return text
61

    
62
redmine_table_sep = '|'
63
redmine_table_sep_esc = redmine_esc(redmine_table_sep)
64

    
65
def redmine_table_esc(text):
66
    text = text.replace(redmine_table_sep, redmine_table_sep_esc)
67
    return text
68

    
69
class RedmineTableWriter:
70
    '''Formats rows as a Redmine table'''
71
    
72
    def __init__(self, stream):
73
        self.stream = stream
74
    
75
    def writerow(self, row):
76
        row = map(redmine_table_esc, row)
77
        self.stream.write(('|'.join(['']+row+['']))+'\n')
78

    
79
class RedmineDictWriter:
80
    '''Formats rows as Redmine sections containing a table'''
81
    
82
    def __init__(self, term_col, def_col, header, stream):
83
        self.term_col = term_col
84
        self.def_col = def_col
85
        self.header = header
86
        self.page_name = header[term_col]
87
        self.stream = stream
88
    
89
    def writerow(self, row):
90
        row = map(lambda f: redmine_add_links(self.page_name, f), row)
91
        
92
        term = row[self.term_col]
93
        def_ = row[self.def_col]
94
        self.stream.write('h2. '+term+'\n\n')
95
        if def_: self.stream.write(def_+'\n\n')
96
        
97
        table = RedmineTableWriter(self.stream)
98
        for i, col_name in enumerate(self.header):
99
            if not i in set([self.term_col, self.def_col]):
100
                value = row[i]
101
                if value: table.writerow([redmine_bold(col_name), value])
102
        self.stream.write('\n')
103

    
104

    
105
def main():
106
    try: _prog_name, term_col, def_col, sources_col = sys.argv
107
    except ValueError: raise SystemExit('Usage: '+sys.argv[0]
108
        +' <spreadsheet term_col# definition_col# sources_col# >redmine')
109
    term_col, def_col, sources_col = map(int, [term_col, def_col, sources_col])
110
    
111
    # Translate input
112
    reader = csv.reader(sys.stdin)
113
    header = reader.next()
114
    page_name = header[term_col]
115
    writer = RedmineDictWriter(term_col, def_col, header, sys.stdout)
116
    for row in reader:
117
        term = row[term_col]
118
        sources = row[sources_col].split(source_sep)
119
        
120
        row[term_col] = redmine_link(term, page_name+'#'+term)
121
        row[sources_col] = source_sep.join(map(source2redmine_link, sources))
122
        
123
        writer.writerow(row)
124

    
125
main()
(14-14/82)