1 |
205
|
aaronmk
|
#!/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 |
1705
|
aaronmk
|
import os.path
|
7 |
205
|
aaronmk
|
import re
|
8 |
|
|
import sys
|
9 |
|
|
|
10 |
1705
|
aaronmk
|
sys.path.append(os.path.dirname(__file__)+"/../lib")
|
11 |
|
|
|
12 |
|
|
import maps
|
13 |
|
|
|
14 |
205
|
aaronmk
|
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 |
211
|
aaronmk
|
except IndexError: pass
|
22 |
205
|
aaronmk
|
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 |
210
|
aaronmk
|
in_, out = row[:2]
|
31 |
216
|
aaronmk
|
if in_ != '':
|
32 |
564
|
aaronmk
|
if re.match(r'^\w+$', in_):
|
33 |
686
|
aaronmk
|
in_ = r'(?<![^\W_])'+in_+r'(?![^\W_])' # match whole word
|
34 |
1229
|
aaronmk
|
repls.append((r'(?m)'+in_, out))
|
35 |
205
|
aaronmk
|
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 |
1705
|
aaronmk
|
label, root = maps.col_info(cols[col_num])[:2]
|
46 |
205
|
aaronmk
|
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 |
358
|
aaronmk
|
else: sys.stdout.write(repl_all(sys.stdin.read()))
|
55 |
205
|
aaronmk
|
|
56 |
|
|
main()
|