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 not re.match(r'[\\(?]', in_): # no special regexp chars
27
            in_ = r'\b'+in_+r'(?=(?:(?i)_id)?\b)' # match whole word
28
        repls.append((in_, out))
29
    stream.close()
30
    def repl_all(str_):
31
        for repl, with_ in repls: str_ = re.sub(repl, with_, str_)
32
        return str_
33
    
34
    # Modify map or file
35
    if col_num != None:
36
        reader = csv.reader(sys.stdin)
37
        writer = csv.writer(sys.stdout)
38
        cols = reader.next()
39
        label, sep, root = cols[col_num].partition(':')
40
        if label != repl_in: raise SystemExit('Map error: Map column '+
41
            str(col_num)+' label "'+label+'" doesn\'t match replacements input '
42
            'column label "'+repl_in+'"')
43
        cols[col_num] = repl_out+sep+repl_all(root)
44
        writer.writerow(cols)
45
        for row in reader:
46
            row[col_num] = repl_all(row[col_num])
47
            writer.writerow(row)
48
    else:
49
        while True:
50
            line = sys.stdin.readline()
51
            if line == '': break
52
            sys.stdout.write(repl_all(line))
53

    
54
main()
(8-8/16)