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
excluded_prefix_re = (
19
     '(?<! )' # not if it's a word in a sentence
20
    +'(?<!-)' # not if it's part of a '-'-separated identifier
21
    +'(?<!\*)' # don't double leading *
22
    )
23
excluded_suffix_re = (
24
     '(?! )' # not if it's a word in a sentence
25
    +'(?!-)' # not if it's part of a '-'-separated identifier
26
    )
27

    
28
def unescape_html(str_): return HTMLParser.HTMLParser().unescape(str_)
29

    
30
def repl_unescape_html(match): return unescape_html(match.group(0))
31

    
32
def main():
33
    env_names = []
34
    def usage_err():
35
        raise SystemExit('Usage: '+opts.env_usage(env_names, True)+' '
36
            +sys.argv[0]+' <map repl [col_num] [| '+sys.argv[0]
37
            +' repl_1 [col_num_1]]... >new_map')
38
    
39
    text = opts.env_flag('text', False, env_names) # all patterns are plain text
40
    try: _prog_name, repl_path = sys.argv[:2]
41
    except ValueError: usage_err()
42
    col_num = None
43
    try: col_num = sys.argv[2]
44
    except IndexError: pass
45
    if col_num != None: col_num = int(col_num) # 0-based
46
    
47
    # Get replacements
48
    repls = []
49
    stream = open(repl_path, 'rb')
50
    reader = csv.reader(stream)
51
    reader.next() # skip header
52
    for row in reader:
53
        in_, out = row[:2]
54
        if in_ != '':
55
            is_word = re.match(r'^\w+$', in_)
56
            if text or is_word: # match as whole-word text (like SQL identifier)
57
                in_str_re = re.escape(in_)
58
                q = quote_re
59
                in_ = '(?<='+q+')'+in_str_re+'(?='+q+')' # require quotes
60
                if is_word: # also match with quotes optional
61
                    # don't try to match word w/ suffix, because there are cases
62
                    # where a mapping adds a suffix which would cause the same
63
                    # replacement to be performed repeatedly
64
                    in_word_re = r'\b'+in_str_re+r'\b'
65
                    
66
                    # only use excluded_prefix_re/excluded_suffix_re in text
67
                    # mode (used in renaming columns in SQL scripts), to prevent
68
                    # the special coding for column renames from also affecting
69
                    # regular regexp/word replacements
70
                    if text: in_word_re = (excluded_prefix_re+in_word_re
71
                        +excluded_suffix_re)
72
                    
73
                    in_ = '(?:'+in_+'|'+in_word_re+')'
74
            repls.append((r'(?m)'+in_, out))
75
    stream.close()
76
    def repl_all(str_):
77
        str_ = strings.ustr(str_)
78
        for repl, with_ in repls:
79
            if with_ == 'unescape_html()': with_ = repl_unescape_html
80
            str_ = re.sub(repl, with_, str_)
81
        return str_
82
    
83
    # Modify map or file
84
    if col_num != None:
85
        reader = csv.reader(sys.stdin)
86
        writer = csv.writer(sys.stdout)
87
        cols = reader.next()
88
        writer.writerow(cols)
89
        for row in reader:
90
            row[col_num] = repl_all(row[col_num])
91
            writer.writerow(row)
92
    else: sys.stdout.write(strings.to_raw_str(repl_all(sys.stdin.read())))
93

    
94
main()
(65-65/86)