Project

General

Profile

1
#!/usr/bin/env python
2
# Modifies a map spreadsheet A->B or any file using a replacements spreadsheet
3
# A->C or B->C
4

    
5
import csv
6
import re
7
import sys
8

    
9
def main():
10
    try: _prog_name, repl_path = sys.argv[:2]
11
    except ValueError:
12
        raise SystemExit('Usage: '+sys.argv[0]+' <map repl [col_num] [| '+
13
            sys.argv[0]+' repl_1 [col_num_1]]... >new_map')
14
    col_num = None
15
    try: col_num = sys.argv[2]
16
    except IndexError: pass
17
    if col_num != None: col_num = int(col_num) # 0-based
18
    
19
    # Get replacements
20
    repls = []
21
    stream = open(repl_path, 'rb')
22
    reader = csv.reader(stream)
23
    repl_in, repl_out = reader.next()[:2]
24
    for row in reader:
25
        in_, out = row[:2]
26
        if in_ != '':
27
            if re.match(r'^\w+$', in_):
28
                in_ = r'(?<![^\W_])'+in_+r'(?![^\W_])' # match whole word
29
            repls.append((r'(?m)'+in_, out))
30
    stream.close()
31
    def repl_all(str_):
32
        for repl, with_ in repls: str_ = re.sub(repl, with_, str_)
33
        return str_
34
    
35
    # Modify map or file
36
    if col_num != None:
37
        reader = csv.reader(sys.stdin)
38
        writer = csv.writer(sys.stdout)
39
        cols = reader.next()
40
        label, sep, root = cols[col_num].partition(':')
41
        if label != repl_in: raise SystemExit('Map error: Map column '+
42
            str(col_num)+' label "'+label+'" doesn\'t match replacements input '
43
            'column label "'+repl_in+'"')
44
        cols[col_num] = repl_out+sep+repl_all(root)
45
        writer.writerow(cols)
46
        for row in reader:
47
            row[col_num] = repl_all(row[col_num])
48
            writer.writerow(row)
49
    else: sys.stdout.write(repl_all(sys.stdin.read()))
50

    
51
main()
(21-21/33)