cmd/contentreplace.py

91 lines
2.7 KiB
Python
Raw Normal View History

2019-06-12 05:41:31 +00:00
import argparse
import codecs
import os
2019-06-12 05:41:31 +00:00
import pyperclip
2020-01-15 07:47:11 +00:00
import re
import sys
2019-06-12 05:41:31 +00:00
from voussoirkit import interactive
2020-12-01 06:05:54 +00:00
from voussoirkit import pipeable
from voussoirkit import spinal
from voussoirkit import winglob
2019-06-12 05:41:31 +00:00
2020-01-15 07:47:11 +00:00
def contentreplace(filename, replace_from, replace_to, autoyes=False, do_regex=False):
2019-06-12 05:41:31 +00:00
f = open(filename, 'r', encoding='utf-8')
with f:
content = f.read()
2020-01-15 07:47:11 +00:00
if do_regex:
occurances = len(re.findall(replace_from, content))
else:
occurances = content.count(replace_from)
2019-06-12 05:41:31 +00:00
print(f'{filename}: Found {occurances} occurences.')
if occurances == 0:
return
if not (autoyes or interactive.getpermission('Replace?')):
2019-06-12 05:41:31 +00:00
return
2020-01-15 07:47:11 +00:00
if do_regex:
content = re.sub(replace_from, replace_to, content)
else:
content = content.replace(replace_from, replace_to)
2019-06-12 05:41:31 +00:00
f = open(filename, 'w', encoding='utf-8')
with f:
f.write(content)
2020-12-01 06:05:54 +00:00
@pipeable.ctrlc_return1
2019-06-12 05:41:31 +00:00
def contentreplace_argparse(args):
if args.recurse:
files = spinal.walk_generator('.')
files = (f for f in files if winglob.fnmatch(f.basename, args.filename_glob))
filenames = (f.absolute_path for f in files)
else:
filenames = winglob.glob(args.filename_glob)
filenames = [f for f in filenames if os.path.isfile(f)]
2019-06-12 05:41:31 +00:00
if args.clip_prompt:
replace_from = input('Ready from')
if not replace_from:
replace_from = pyperclip.paste()
replace_to = input('Ready to')
if not replace_to:
replace_to = pyperclip.paste()
else:
replace_from = codecs.decode(args.replace_from, 'unicode_escape')
if args.do_regex:
replace_to = args.replace_to
else:
replace_to = codecs.decode(args.replace_to, 'unicode_escape')
2019-06-12 05:41:31 +00:00
for filename in filenames:
print(filename)
2019-06-12 05:41:31 +00:00
contentreplace(
filename,
replace_from,
replace_to,
autoyes=args.autoyes,
2020-01-15 07:47:11 +00:00
do_regex=args.do_regex,
2019-06-12 05:41:31 +00:00
)
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('filename_glob')
parser.add_argument('replace_from')
parser.add_argument('replace_to')
parser.add_argument('-y', '--yes', dest='autoyes', action='store_true', help='accept results without confirming')
parser.add_argument('--recurse', dest='recurse', action='store_true')
2020-01-15 07:47:11 +00:00
parser.add_argument('--regex', dest='do_regex', action='store_true')
parser.add_argument('--clip_prompt', '--clip-prompt', dest='clip_prompt', action='store_true')
2019-06-12 05:41:31 +00:00
parser.set_defaults(func=contentreplace_argparse)
args = parser.parse_args(argv)
return args.func(args)
2019-06-12 05:41:31 +00:00
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))