Add --regex option to contentreplace.
This commit is contained in:
parent
335b7d214a
commit
a05b82d683
1 changed files with 13 additions and 4 deletions
|
@ -1,15 +1,19 @@
|
||||||
import argparse
|
import argparse
|
||||||
import codecs
|
import codecs
|
||||||
import glob
|
import glob
|
||||||
import sys
|
|
||||||
import pyperclip
|
import pyperclip
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def contentreplace(filename, replace_from, replace_to, autoyes=False):
|
def contentreplace(filename, replace_from, replace_to, autoyes=False, do_regex=False):
|
||||||
f = open(filename, 'r', encoding='utf-8')
|
f = open(filename, 'r', encoding='utf-8')
|
||||||
with f:
|
with f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
|
|
||||||
|
if do_regex:
|
||||||
|
occurances = len(re.findall(replace_from, content))
|
||||||
|
else:
|
||||||
occurances = content.count(replace_from)
|
occurances = content.count(replace_from)
|
||||||
|
|
||||||
print(f'{filename}: Found {occurances} occurences.')
|
print(f'{filename}: Found {occurances} occurences.')
|
||||||
|
@ -20,6 +24,9 @@ def contentreplace(filename, replace_from, replace_to, autoyes=False):
|
||||||
if not permission:
|
if not permission:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
if do_regex:
|
||||||
|
content = re.sub(replace_from, replace_to, content)
|
||||||
|
else:
|
||||||
content = content.replace(replace_from, replace_to)
|
content = content.replace(replace_from, replace_to)
|
||||||
|
|
||||||
f = open(filename, 'w', encoding='utf-8')
|
f = open(filename, 'w', encoding='utf-8')
|
||||||
|
@ -46,6 +53,7 @@ def contentreplace_argparse(args):
|
||||||
replace_from,
|
replace_from,
|
||||||
replace_to,
|
replace_to,
|
||||||
autoyes=args.autoyes,
|
autoyes=args.autoyes,
|
||||||
|
do_regex=args.do_regex,
|
||||||
)
|
)
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
|
@ -55,6 +63,7 @@ def main(argv):
|
||||||
parser.add_argument('replace_from')
|
parser.add_argument('replace_from')
|
||||||
parser.add_argument('replace_to')
|
parser.add_argument('replace_to')
|
||||||
parser.add_argument('-y', '--yes', dest='autoyes', action='store_true', help='accept results without confirming')
|
parser.add_argument('-y', '--yes', dest='autoyes', action='store_true', help='accept results without confirming')
|
||||||
|
parser.add_argument('--regex', dest='do_regex', action='store_true')
|
||||||
parser.add_argument('--clip_prompt', dest='clip_prompt', action='store_true')
|
parser.add_argument('--clip_prompt', dest='clip_prompt', action='store_true')
|
||||||
parser.set_defaults(func=contentreplace_argparse)
|
parser.set_defaults(func=contentreplace_argparse)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue