From 26889bd2e0be742d46f896b24eb2f2345743e8c8 Mon Sep 17 00:00:00 2001 From: Nabin Hait Date: Wed, 14 Mar 2012 11:08:15 +0530 Subject: [PATCH] search and replace code with confirmation --- wnf.py | 32 ++++++++++++++++++++++++++------ 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/wnf.py b/wnf.py index d0a0d5690fa..463ecc41f21 100755 --- a/wnf.py +++ b/wnf.py @@ -29,12 +29,32 @@ def replace_code(start, txt1, txt2, extn): content = f.read() if re.search(txt1, content): - a = raw_input('Change in %s [y/n]?' % fpath) - if a=='y': - with open(fpath, 'w') as f: - f.write(re.sub(txt1, txt2, content)) - - print 'updated in %s' % fpath + search_replace_with_prompt(fpath, txt1, txt2) + + + +def search_replace_with_prompt(fpath, txt1, txt2): + """ Search and replace all txt1 by txt2 in the file with confirmation""" + + from termcolor import colored + with open(fpath, 'r') as f: + content = f.readlines() + + tmp = [] + for c in content: + if c.find(txt1) != -1: + print '\n', fpath + print colored(txt1, 'red').join(c[:-1].split(txt1)) + + a = raw_input('Do you want to Change [y/n]?') + if a=='y': + c = c.replace(txt1, txt2) + tmp.append(c) + + with open(fpath, 'w') as f: + f.write(''.join(tmp)) + print colored('Updated in %s' % fpath, 'green') + def setup_options(): from optparse import OptionParser