2019-06-12 05:41:31 +00:00
|
|
|
import argparse
|
|
|
|
import codecs
|
|
|
|
import pyperclip
|
2020-01-15 07:47:11 +00:00
|
|
|
import re
|
|
|
|
import sys
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2020-12-07 08:54:00 +00:00
|
|
|
from voussoirkit import interactive
|
2021-05-17 03:56:31 +00:00
|
|
|
from voussoirkit import pathclass
|
2020-12-01 06:05:54 +00:00
|
|
|
from voussoirkit import pipeable
|
2020-06-25 21:16:51 +00:00
|
|
|
from voussoirkit import spinal
|
2021-05-17 03:56:31 +00:00
|
|
|
from voussoirkit import vlogging
|
2020-01-16 06:47:26 +00:00
|
|
|
|
2021-05-17 03:56:31 +00:00
|
|
|
log = vlogging.getLogger(__name__, 'contentreplace')
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2021-05-17 03:56:31 +00:00
|
|
|
def contentreplace(file, replace_from, replace_to, autoyes=False, do_regex=False):
|
|
|
|
file = pathclass.Path(file)
|
|
|
|
with file.open('r', encoding='utf-8') as f:
|
2019-06-12 05:41:31 +00:00
|
|
|
content = f.read()
|
|
|
|
|
2020-01-15 07:47:11 +00:00
|
|
|
if do_regex:
|
2021-03-26 20:28:10 +00:00
|
|
|
occurances = len(re.findall(replace_from, content, flags=re.MULTILINE))
|
2020-01-15 07:47:11 +00:00
|
|
|
else:
|
|
|
|
occurances = content.count(replace_from)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2021-05-17 03:56:31 +00:00
|
|
|
print(f'{file.absolute_path}: Found {occurances} occurences.')
|
2019-06-12 05:41:31 +00:00
|
|
|
if occurances == 0:
|
|
|
|
return
|
|
|
|
|
2020-12-07 08:54:00 +00:00
|
|
|
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:
|
2021-03-26 20:28:10 +00:00
|
|
|
content = re.sub(replace_from, replace_to, content, flags=re.MULTILINE)
|
2020-01-15 07:47:11 +00:00
|
|
|
else:
|
|
|
|
content = content.replace(replace_from, replace_to)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2021-05-17 03:56:31 +00:00
|
|
|
with file.open('w', encoding='utf-8') as f:
|
2019-06-12 05:41:31 +00:00
|
|
|
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):
|
2021-08-11 08:04:41 +00:00
|
|
|
files = spinal.walk(
|
|
|
|
glob_filenames=args.filename_glob,
|
|
|
|
recurse=args.recurse,
|
|
|
|
)
|
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')
|
2020-11-16 01:58:26 +00:00
|
|
|
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
|
|
|
|
2021-05-17 03:56:31 +00:00
|
|
|
for file in files:
|
|
|
|
try:
|
|
|
|
contentreplace(
|
|
|
|
file,
|
|
|
|
replace_from,
|
|
|
|
replace_to,
|
|
|
|
autoyes=args.autoyes,
|
|
|
|
do_regex=args.do_regex,
|
|
|
|
)
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
log.error('%s encountered unicode decode error.', file.absolute_path)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2021-09-24 06:42:34 +00:00
|
|
|
return 0
|
|
|
|
|
2021-06-22 05:11:19 +00:00
|
|
|
@vlogging.main_decorator
|
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')
|
2021-02-21 05:01:55 +00:00
|
|
|
parser.add_argument('--recurse', action='store_true')
|
2020-01-15 07:47:11 +00:00
|
|
|
parser.add_argument('--regex', dest='do_regex', action='store_true')
|
2021-02-21 05:01:55 +00:00
|
|
|
parser.add_argument('--clip_prompt', '--clip-prompt', action='store_true')
|
2019-06-12 05:41:31 +00:00
|
|
|
parser.set_defaults(func=contentreplace_argparse)
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
2020-02-09 01:18:50 +00:00
|
|
|
return args.func(args)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|