Use pathclass in contentreplace.
This commit is contained in:
parent
eb39dcf005
commit
7bc152f05a
1 changed files with 24 additions and 19 deletions
|
@ -6,14 +6,17 @@ import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from voussoirkit import interactive
|
from voussoirkit import interactive
|
||||||
|
from voussoirkit import pathclass
|
||||||
from voussoirkit import pipeable
|
from voussoirkit import pipeable
|
||||||
from voussoirkit import spinal
|
from voussoirkit import spinal
|
||||||
|
from voussoirkit import vlogging
|
||||||
from voussoirkit import winglob
|
from voussoirkit import winglob
|
||||||
|
|
||||||
|
log = vlogging.getLogger(__name__, 'contentreplace')
|
||||||
|
|
||||||
def contentreplace(filename, replace_from, replace_to, autoyes=False, do_regex=False):
|
def contentreplace(file, replace_from, replace_to, autoyes=False, do_regex=False):
|
||||||
f = open(filename, 'r', encoding='utf-8')
|
file = pathclass.Path(file)
|
||||||
with f:
|
with file.open('r', encoding='utf-8') as f:
|
||||||
content = f.read()
|
content = f.read()
|
||||||
|
|
||||||
if do_regex:
|
if do_regex:
|
||||||
|
@ -21,7 +24,7 @@ def contentreplace(filename, replace_from, replace_to, autoyes=False, do_regex=F
|
||||||
else:
|
else:
|
||||||
occurances = content.count(replace_from)
|
occurances = content.count(replace_from)
|
||||||
|
|
||||||
print(f'{filename}: Found {occurances} occurences.')
|
print(f'{file.absolute_path}: Found {occurances} occurences.')
|
||||||
if occurances == 0:
|
if occurances == 0:
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -33,19 +36,17 @@ def contentreplace(filename, replace_from, replace_to, autoyes=False, do_regex=F
|
||||||
else:
|
else:
|
||||||
content = content.replace(replace_from, replace_to)
|
content = content.replace(replace_from, replace_to)
|
||||||
|
|
||||||
f = open(filename, 'w', encoding='utf-8')
|
with file.open('w', encoding='utf-8') as f:
|
||||||
with f:
|
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|
||||||
@pipeable.ctrlc_return1
|
@pipeable.ctrlc_return1
|
||||||
def contentreplace_argparse(args):
|
def contentreplace_argparse(args):
|
||||||
if args.recurse:
|
if args.recurse:
|
||||||
files = spinal.walk_generator('.')
|
files = spinal.walk('.', yield_files=True, yield_directories=False)
|
||||||
files = (f for f in files if winglob.fnmatch(f.basename, args.filename_glob))
|
files = (f for f in files if winglob.fnmatch(f.basename, args.filename_glob))
|
||||||
filenames = (f.absolute_path for f in files)
|
|
||||||
else:
|
else:
|
||||||
filenames = winglob.glob(args.filename_glob)
|
files = pathclass.cwd().glob(args.filename_glob)
|
||||||
filenames = [f for f in filenames if os.path.isfile(f)]
|
files = (f for f in files if f.is_file)
|
||||||
|
|
||||||
if args.clip_prompt:
|
if args.clip_prompt:
|
||||||
replace_from = input('Ready from')
|
replace_from = input('Ready from')
|
||||||
|
@ -61,17 +62,21 @@ def contentreplace_argparse(args):
|
||||||
else:
|
else:
|
||||||
replace_to = codecs.decode(args.replace_to, 'unicode_escape')
|
replace_to = codecs.decode(args.replace_to, 'unicode_escape')
|
||||||
|
|
||||||
for filename in filenames:
|
for file in files:
|
||||||
print(filename)
|
try:
|
||||||
contentreplace(
|
contentreplace(
|
||||||
filename,
|
file,
|
||||||
replace_from,
|
replace_from,
|
||||||
replace_to,
|
replace_to,
|
||||||
autoyes=args.autoyes,
|
autoyes=args.autoyes,
|
||||||
do_regex=args.do_regex,
|
do_regex=args.do_regex,
|
||||||
)
|
)
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
log.error('%s encountered unicode decode error.', file.absolute_path)
|
||||||
|
|
||||||
def main(argv):
|
def main(argv):
|
||||||
|
argv = vlogging.main_level_by_argv(argv)
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description=__doc__)
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
|
||||||
parser.add_argument('filename_glob')
|
parser.add_argument('filename_glob')
|
||||||
|
|
Loading…
Reference in a new issue