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 HTMLParser
7
import os.path
8
import re
9
import sys
10

    
11
sys.path.append(os.path.dirname(__file__)+"/../lib")
12

    
13
import maps
14
import opts
15
import strings
16

    
17
quote_re = '[\'"`]'
18

    
19
def unescape_html(str_): return HTMLParser.HTMLParser().unescape(str_)
20

    
21
def repl_unescape_html(match): return unescape_html(match.group(0))
22

    
23
def main():
24
    env_names = []
25
    def usage_err():
26
        raise SystemExit('Usage: '+opts.env_usage(env_names, True)+' '
27
            +sys.argv[0]+' <map repl [col_num] [| '+sys.argv[0]
28
            +' repl_1 [col_num_1]]... >new_map')
29
    
30
    text = opts.env_flag('text', False, env_names) # all patterns are plain text
31
    try: _prog_name, repl_path = sys.argv[:2]
32
    except ValueError: usage_err()
33
    col_num = None
34
    try: col_num = sys.argv[2]
35
    except IndexError: pass
36
    if col_num != None: col_num = int(col_num) # 0-based
37
    
38
    # Get replacements
39
    repls = []
40
    stream = open(repl_path, 'rb')
41
    reader = csv.reader(stream)
42
    reader.next() # skip header
43
    for row in reader:
44
        in_, out = row[:2]
45
        if in_ != '':
46
            is_word = re.match(r'^\w+$', in_)
47
            if text or is_word: # match as whole-word text (like SQL identifier)
48
                in_str_re = re.escape(in_)
49
                q = quote_re
50
                in_ = '(?<='+q+')'+in_str_re+'(?='+q+')' # require quotes
51
                if is_word: # also match without quotes
52
                    in_word_re = '(?<!'+q+r')\b'+in_str_re+r'\b(?!'+q+')'
53
                        # don't match as partial part of quoted string
54
                    in_ = '(?:'+in_+'|'+in_word_re+')'
55
            repls.append((r'(?m)'+in_, out))
56
    stream.close()
57
    def repl_all(str_):
58
        str_ = strings.ustr(str_)
59
        for repl, with_ in repls:
60
            if with_ == 'unescape_html()': with_ = repl_unescape_html
61
            str_ = re.sub(repl, with_, str_)
62
        return str_
63
    
64
    # Modify map or file
65
    if col_num != None:
66
        reader = csv.reader(sys.stdin)
67
        writer = csv.writer(sys.stdout)
68
        cols = reader.next()
69
        writer.writerow(cols)
70
        for row in reader:
71
            row[col_num] = repl_all(row[col_num])
72
            writer.writerow(row)
73
    else: sys.stdout.write(strings.to_raw_str(repl_all(sys.stdin.read())))
74

    
75
main()
(65-65/86)