Make better use of passing glob patterns through argparse.

This commit is contained in:
voussoir 2021-08-11 01:04:41 -07:00
parent eeda354a2b
commit 6daa10c0d1
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
3 changed files with 10 additions and 20 deletions

View file

@ -10,7 +10,6 @@ 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 vlogging
from voussoirkit import winglob
log = vlogging.getLogger(__name__, 'contentreplace') log = vlogging.getLogger(__name__, 'contentreplace')
@ -41,12 +40,10 @@ def contentreplace(file, replace_from, replace_to, autoyes=False, do_regex=False
@pipeable.ctrlc_return1 @pipeable.ctrlc_return1
def contentreplace_argparse(args): def contentreplace_argparse(args):
if args.recurse: files = spinal.walk(
files = spinal.walk('.', yield_files=True, yield_directories=False) glob_filenames=args.filename_glob,
files = (f for f in files if winglob.fnmatch(f.basename, args.filename_glob)) recurse=args.recurse,
else: )
files = pathclass.cwd().glob(args.filename_glob)
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')

View file

@ -3,15 +3,11 @@ import sys
from voussoirkit import pathclass from voussoirkit import pathclass
from voussoirkit import pipeable from voussoirkit import pipeable
from voussoirkit import spinal
from voussoirkit import winglob from voussoirkit import winglob
def empty_directories_argparse(args): def empty_directories_argparse(args):
if args.patterns: patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True)
patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True) directories = (pathclass.Path(d) for pattern in patterns for d in winglob.glob(pattern))
directories = (pathclass.Path(d) for pattern in patterns for d in winglob.glob(pattern))
else:
directories = pathclass.cwd().listdir()
directories = (d for d in directories if d.is_dir) directories = (d for d in directories if d.is_dir)
for directory in directories: for directory in directories:
@ -21,7 +17,7 @@ def empty_directories_argparse(args):
def main(argv): def main(argv):
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('patterns', nargs='*') parser.add_argument('patterns', nargs='*', default=['*'])
parser.set_defaults(func=empty_directories_argparse) parser.set_defaults(func=empty_directories_argparse)
args = parser.parse_args(argv) args = parser.parse_args(argv)

View file

@ -6,11 +6,8 @@ from voussoirkit import pipeable
from voussoirkit import winglob from voussoirkit import winglob
def nonempty_directories_argparse(args): def nonempty_directories_argparse(args):
if args.patterns: patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True)
patterns = pipeable.input_many(args.patterns, skip_blank=True, strip=True) directories = (pathclass.Path(d) for pattern in patterns for d in winglob.glob(pattern))
directories = (pathclass.Path(d) for pattern in patterns for d in winglob.glob(pattern))
else:
directories = pathclass.cwd().listdir()
directories = (d for d in directories if d.is_dir) directories = (d for d in directories if d.is_dir)
for directory in directories: for directory in directories:
@ -20,7 +17,7 @@ def nonempty_directories_argparse(args):
def main(argv): def main(argv):
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('patterns', nargs='*') parser.add_argument('patterns', nargs='*', default=['*'])
parser.set_defaults(func=nonempty_directories_argparse) parser.set_defaults(func=nonempty_directories_argparse)
args = parser.parse_args(argv) args = parser.parse_args(argv)