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 |
6636
|
aaronmk
|
url_comment_re = r'(?:\([^)]*\))'
|
14 |
|
|
|
15 |
6620
|
aaronmk
|
def simplify_url(url): return re.sub(r'\(.*?\)', r'', url)
|
16 |
|
|
|
17 |
6635
|
aaronmk
|
def url_comment_text(comment):
|
18 |
|
|
if not comment: return comment
|
19 |
|
|
|
20 |
6638
|
aaronmk
|
match = re.match(r'^\(\d*:?(.*?)\)$', comment)
|
21 |
6635
|
aaronmk
|
assert match
|
22 |
|
|
text, = match.groups()
|
23 |
|
|
return text
|
24 |
|
|
|
25 |
6620
|
aaronmk
|
def url_term(url):
|
26 |
6637
|
aaronmk
|
'''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 |
6620
|
aaronmk
|
assert match
|
31 |
6637
|
aaronmk
|
provider_str, term = match.groups()
|
32 |
6639
|
aaronmk
|
provider = filter(bool, map(url_comment_text,
|
33 |
|
|
re.findall(url_comment_re, provider_str)))
|
34 |
6637
|
aaronmk
|
|
35 |
|
|
return ':'.join(provider+[term])
|
36 |
6620
|
aaronmk
|
|
37 |
|
|
##### Redmine
|
38 |
|
|
|
39 |
6684
|
aaronmk
|
sp = ' ' # Unicode thin space
|
40 |
|
|
|
41 |
6632
|
aaronmk
|
def redmine_bold(text): return '*'+text+'*'
|
42 |
|
|
|
43 |
6620
|
aaronmk
|
def redmine_url(text, url):
|
44 |
6686
|
aaronmk
|
if not url: return text
|
45 |
|
|
elif url.find('://') >= 0: return '"'+text+'":'+url # external link
|
46 |
|
|
else: return '[['+url+'|'+text+']]' # internal link
|
47 |
6620
|
aaronmk
|
|
48 |
|
|
def source2redmine_url(url):
|
49 |
6623
|
aaronmk
|
if url: return redmine_url(url_term(url), simplify_url(url))
|
50 |
|
|
else: return ''
|
51 |
6620
|
aaronmk
|
|
52 |
6684
|
aaronmk
|
def redmine_add_links(page_name, text):
|
53 |
|
|
# Link citations to entry in sources list
|
54 |
6687
|
aaronmk
|
text = re.sub(r'(?<=\[)(.*?)(?=\])',
|
55 |
|
|
sp+redmine_url('\g<0>', page_name+r'#\g<0>')+sp, text)
|
56 |
|
|
# sp prevents [[[, which Redmine would parse incorrectly
|
57 |
6684
|
aaronmk
|
return text
|
58 |
|
|
|
59 |
6620
|
aaronmk
|
class RedmineTableWriter:
|
60 |
|
|
'''Formats rows as a Redmine table'''
|
61 |
|
|
|
62 |
|
|
def __init__(self, stream):
|
63 |
|
|
self.stream = stream
|
64 |
|
|
|
65 |
|
|
def writerow(self, row):
|
66 |
|
|
self.stream.write(('|'.join(['']+row+['']))+'\n')
|
67 |
|
|
|
68 |
6673
|
aaronmk
|
class RedmineDictWriter:
|
69 |
|
|
'''Formats rows as Redmine sections containing a table'''
|
70 |
|
|
|
71 |
6676
|
aaronmk
|
def __init__(self, term_col, def_col, header, stream):
|
72 |
6673
|
aaronmk
|
self.term_col = term_col
|
73 |
6676
|
aaronmk
|
self.def_col = def_col
|
74 |
6673
|
aaronmk
|
self.header = header
|
75 |
6684
|
aaronmk
|
self.page_name = header[term_col]
|
76 |
6673
|
aaronmk
|
self.stream = stream
|
77 |
|
|
|
78 |
|
|
def writerow(self, row):
|
79 |
6684
|
aaronmk
|
row = map(lambda f: redmine_add_links(self.page_name, f), row)
|
80 |
|
|
|
81 |
6676
|
aaronmk
|
term = row[self.term_col]
|
82 |
|
|
def_ = row[self.def_col]
|
83 |
|
|
self.stream.write('h3. '+term+'\n\n')
|
84 |
|
|
if def_: self.stream.write(def_+'\n\n')
|
85 |
6673
|
aaronmk
|
|
86 |
|
|
table = RedmineTableWriter(self.stream)
|
87 |
|
|
for i, col_name in enumerate(self.header):
|
88 |
6677
|
aaronmk
|
if not i in set([self.term_col, self.def_col]):
|
89 |
6676
|
aaronmk
|
value = row[i]
|
90 |
|
|
if value: table.writerow([redmine_bold(col_name), value])
|
91 |
|
|
self.stream.write('\n')
|
92 |
6620
|
aaronmk
|
|
93 |
6673
|
aaronmk
|
|
94 |
6620
|
aaronmk
|
def main():
|
95 |
6676
|
aaronmk
|
try: _prog_name, term_col, def_col, sources_col = sys.argv
|
96 |
6626
|
aaronmk
|
except ValueError: raise SystemExit('Usage: '+sys.argv[0]
|
97 |
6676
|
aaronmk
|
+' <spreadsheet term_col# definition_col# sources_col# >redmine')
|
98 |
|
|
term_col, def_col, sources_col = map(int, [term_col, def_col, sources_col])
|
99 |
6626
|
aaronmk
|
|
100 |
6632
|
aaronmk
|
# Translate input
|
101 |
6620
|
aaronmk
|
reader = csv.reader(sys.stdin)
|
102 |
6673
|
aaronmk
|
header = reader.next()
|
103 |
6676
|
aaronmk
|
writer = RedmineDictWriter(term_col, def_col, header, sys.stdout)
|
104 |
6620
|
aaronmk
|
for row in reader:
|
105 |
|
|
term = row[term_col]
|
106 |
6634
|
aaronmk
|
sources = row[sources_col].split(source_sep)
|
107 |
6620
|
aaronmk
|
|
108 |
6634
|
aaronmk
|
row[term_col] = redmine_url(term, simplify_url(sources[0]))
|
109 |
6632
|
aaronmk
|
row[sources_col] = source_sep.join(map(source2redmine_url, sources))
|
110 |
6628
|
aaronmk
|
|
111 |
6632
|
aaronmk
|
writer.writerow(row)
|
112 |
6620
|
aaronmk
|
|
113 |
|
|
main()
|