1 |
123
|
aaronmk
|
#!/usr/bin/env python
|
2 |
|
|
# Converts a map spreadsheet to human-readable (but machine unusable) form
|
3 |
3807
|
aaronmk
|
# Usage: self [col_num...] <in_map >out_map
|
4 |
123
|
aaronmk
|
|
5 |
|
|
import csv
|
6 |
|
|
import re
|
7 |
|
|
import sys
|
8 |
|
|
|
9 |
|
|
def sub_nested(regex, repl, str_):
|
10 |
|
|
while True:
|
11 |
|
|
str_, n = re.subn(regex, repl, str_)
|
12 |
|
|
if n == 0: return str_
|
13 |
|
|
|
14 |
|
|
def cleanup(xpath):
|
15 |
|
|
truncated = False
|
16 |
|
|
|
17 |
|
|
# Remove attrs
|
18 |
|
|
xpath = sub_nested(r':\[[^\[\]]*?\]', r'', xpath)
|
19 |
|
|
|
20 |
|
|
# Remove lookahead assertions
|
21 |
|
|
xpath = sub_nested(r'\((/[^\)]*?)\)(?=/)', r'\1', xpath)
|
22 |
|
|
|
23 |
|
|
# Remove pointers
|
24 |
|
|
xpath, n = re.subn(r'^.*->', r'', xpath)
|
25 |
|
|
if n > 0: truncated = True
|
26 |
|
|
|
27 |
|
|
# Remove part of path before first key list, XML function, or path end
|
28 |
|
|
# Leave enough to include the table of a user-defined value
|
29 |
|
|
xpath, n = re.subn(r'^(?:/(?!_)[\w*]+)*(?=(?:/(?!_)[\w*]+){2}(?:\[|/_|$))',
|
30 |
|
|
r'', xpath)
|
31 |
|
|
if n > 0: truncated = True
|
32 |
|
|
|
33 |
3811
|
aaronmk
|
# Remove XML functions
|
34 |
|
|
xpath = re.sub(r'/_\w+/\w+', r'', xpath)
|
35 |
|
|
|
36 |
123
|
aaronmk
|
# Remove backward (child-to-parent) pointer's target ID attr
|
37 |
|
|
xpath = re.sub(r'\[[\w*]+\]|(?<=\[)[\w*]+,', r'', xpath)
|
38 |
|
|
|
39 |
|
|
# Remove negative keys
|
40 |
|
|
xpath = re.sub(r',?!(?:[\w*]+/)*@?[\w*]+', r'', xpath)
|
41 |
|
|
|
42 |
|
|
# Remove path before key
|
43 |
|
|
xpath = re.sub(r'(?:[\w*]+/)*(@?[\w*]+)(?==)', r'\1', xpath)
|
44 |
|
|
|
45 |
|
|
# Prepend / to show truncation
|
46 |
|
|
if truncated: xpath = '/'+xpath
|
47 |
|
|
|
48 |
|
|
return xpath
|
49 |
|
|
|
50 |
|
|
def main():
|
51 |
3807
|
aaronmk
|
col_nums = sys.argv[1:]
|
52 |
|
|
if col_nums == []: col_nums = range(2)
|
53 |
|
|
col_nums = map(int, col_nums) # 0-based
|
54 |
|
|
|
55 |
123
|
aaronmk
|
# Convert map
|
56 |
|
|
reader = csv.reader(sys.stdin)
|
57 |
|
|
writer = csv.writer(sys.stdout)
|
58 |
|
|
writer.writerow(reader.next())
|
59 |
125
|
aaronmk
|
for row in reader:
|
60 |
3807
|
aaronmk
|
for i in col_nums: row[i] = cleanup(row[i])
|
61 |
125
|
aaronmk
|
writer.writerow(row)
|
62 |
123
|
aaronmk
|
|
63 |
|
|
main()
|