From 6daa10c0d12446d57ece5ce5ba633778d061fded Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Wed, 11 Aug 2021 01:04:41 -0700 Subject: [PATCH] Make better use of passing glob patterns through argparse. --- contentreplace.py | 11 ++++------- empty_directories.py | 10 +++------- nonempty_directories.py | 9 +++------ 3 files changed, 10 insertions(+), 20 deletions(-) diff --git a/contentreplace.py b/contentreplace.py index 786e968..c4d1856 100644 --- a/contentreplace.py +++ b/contentreplace.py @@ -10,7 +10,6 @@ from voussoirkit import pathclass from voussoirkit import pipeable from voussoirkit import spinal from voussoirkit import vlogging -from voussoirkit import winglob log = vlogging.getLogger(__name__, 'contentreplace') @@ -41,12 +40,10 @@ def contentreplace(file, replace_from, replace_to, autoyes=False, do_regex=False @pipeable.ctrlc_return1 def contentreplace_argparse(args): - if args.recurse: - files = spinal.walk('.', yield_files=True, yield_directories=False) - files = (f for f in files if winglob.fnmatch(f.basename, args.filename_glob)) - else: - files = pathclass.cwd().glob(args.filename_glob) - files = (f for f in files if f.is_file) + files = spinal.walk( + glob_filenames=args.filename_glob, + recurse=args.recurse, + ) if args.clip_prompt: replace_from = input('Ready from') diff --git a/empty_directories.py b/empty_directories.py index b8f4a67..7098d2b 100644 --- a/empty_directories.py +++ b/empty_directories.py @@ -3,15 +3,11 @@ import sys from voussoirkit import pathclass from voussoirkit import pipeable -from voussoirkit import spinal from voussoirkit import winglob def empty_directories_argparse(args): - if args.patterns: - 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)) - else: - directories = pathclass.cwd().listdir() + 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 = (d for d in directories if d.is_dir) for directory in directories: @@ -21,7 +17,7 @@ def empty_directories_argparse(args): def main(argv): parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument('patterns', nargs='*') + parser.add_argument('patterns', nargs='*', default=['*']) parser.set_defaults(func=empty_directories_argparse) args = parser.parse_args(argv) diff --git a/nonempty_directories.py b/nonempty_directories.py index fab45a7..3c178db 100644 --- a/nonempty_directories.py +++ b/nonempty_directories.py @@ -6,11 +6,8 @@ from voussoirkit import pipeable from voussoirkit import winglob def nonempty_directories_argparse(args): - if args.patterns: - 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)) - else: - directories = pathclass.cwd().listdir() + 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 = (d for d in directories if d.is_dir) for directory in directories: @@ -20,7 +17,7 @@ def nonempty_directories_argparse(args): def main(argv): parser = argparse.ArgumentParser(description=__doc__) - parser.add_argument('patterns', nargs='*') + parser.add_argument('patterns', nargs='*', default=['*']) parser.set_defaults(func=nonempty_directories_argparse) args = parser.parse_args(argv)