Project

General

Profile

1
#!/usr/bin/env python
2
# Converts a map spreadsheet to human-readable (but machine unusable) form
3
# Usage: self [col_num...] <in_map >out_map
4

    
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 keys except last
21
    xpath = sub_nested(r':?\[[^\[\]]*?\](?=.*\[[^\[\]]*?\].*?)', r'', xpath)
22
    
23
    # Remove lookahead assertions
24
    xpath = sub_nested(r'\((/[^\)]*?)\)(?=/)', r'\1', xpath)
25
    
26
    # Remove pointers
27
    xpath, n = re.subn(r'^.*->', r'', xpath)
28
    if n > 0: truncated = True
29
    
30
    # Remove part of path before first key list, XML function, or path end
31
    # Leave enough to include the table of a user-defined value
32
    xpath, n = re.subn(r'^(?:/(?!_)[\w*]+)*(?=(?:/(?!_)[\w*]+){2}(?:\[|/_|$))',
33
        r'', xpath)
34
    if n > 0: truncated = True
35
    
36
    # Remove XML functions other than unit conversions
37
    xpath = re.sub(r'/_(?!\w+_to_)\w+/\w+', r'', xpath)
38
    
39
    # Remove backward (child-to-parent) pointer's target ID attr
40
    xpath = re.sub(r'\[[\w*]+\]|(?<=\[)[\w*]+,', r'', xpath)
41
    
42
    # Remove negative keys
43
    xpath = re.sub(r',?!(?:[\w*]+/)*@?[\w*]+', r'', xpath)
44
    
45
    # Remove path before key
46
    xpath = re.sub(r'(?:[\w*]+/)*(@?[\w*]+)(?==)', r'\1', xpath)
47
    
48
    # Prepend / to show truncation
49
    if truncated: xpath = '/'+xpath
50
    
51
    return xpath
52

    
53
def main():
54
    col_nums = sys.argv[1:]
55
    if col_nums == []: col_nums = range(2)
56
    col_nums = map(int, col_nums) # 0-based
57
    
58
    # Convert map
59
    reader = csv.reader(sys.stdin)
60
    writer = csv.writer(sys.stdout)
61
    writer.writerow(reader.next())
62
    for row in reader:
63
        for i in col_nums: row[i] = cleanup(row[i])
64
        writer.writerow(row)
65

    
66
main()
(49-49/63)