Project

General

Profile

« Previous | Next » 

Revision 4357

repl: Support treating all patterns as plain text (non-regexp)

View differences:

bin/repl
10 10
sys.path.append(os.path.dirname(__file__)+"/../lib")
11 11

  
12 12
import maps
13
import opts
13 14

  
14 15
def main():
16
    env_names = []
17
    def usage_err():
18
        raise SystemExit('Usage: '+opts.env_usage(env_names, True)+' '
19
            +sys.argv[0]+' <map repl [col_num] [| '+sys.argv[0]
20
            +' repl_1 [col_num_1]]... >new_map')
21
    
22
    text = opts.env_flag('text', False, env_names) # all patterns are plain text
15 23
    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')
24
    except ValueError: usage_err()
19 25
    col_num = None
20 26
    try: col_num = sys.argv[2]
21 27
    except IndexError: pass
......
29 35
    for row in reader:
30 36
        in_, out = row[:2]
31 37
        if in_ != '':
32
            if re.match(r'^\w+$', in_):
33
                in_ = r'(?<![^\W_])'+in_+r'(?![^\W_])' # match whole word
38
            if text or re.match(r'^\w+$', in_):
39
                in_ = r'(?<![^\W_])'+re.escape(in_)+r'(?![^\W_])' # match word
34 40
            repls.append((r'(?m)'+in_, out))
35 41
    stream.close()
36 42
    def repl_all(str_):

Also available in: Unified diff