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