From e99cbe3e3948bda83c8a27e45124915efb829b3a Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 10 Dec 2019 12:59:20 -0800 Subject: [PATCH] Add recurse option and more format variables to brename/breplace. --- brename.py | 50 ++++++++++++++++++++++++++++---------------------- breplace.py | 3 ++- 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/brename.py b/brename.py index 3706a7b..c5e946b 100644 --- a/brename.py +++ b/brename.py @@ -18,29 +18,39 @@ import re import sys from voussoirkit import safeprint +from voussoirkit import spinal -dot = '.' -quote = '"' apostrophe = "'" +dot = '.' +hyphen = '-' +quote = '"' space = ' ' +underscore = '_' -def brename(transformation, autoyes=False): - old = os.listdir() - new = [] - for (index, x) in enumerate(old): +def brename(transformation, autoyes=False, recurse=False): + if recurse: + olds = [x.absolute_path for x in spinal.walk_generator('.')] + else: + olds = [os.path.join(os.getcwd(), x) for x in os.listdir('.')] + + news = [] + for (index, x) in enumerate(olds): (noext, ext) = os.path.splitext(x) + directory = os.path.dirname(x) + basename = os.path.basename(x) + x = basename x = eval(transformation) - new.append(x) - pairs = [] - for (x, y) in zip(old, new): - if x == y: - continue - pairs.append((x, y)) + x = os.path.join(directory, x) + news.append(x) - if not loop(pairs, dry=True): + pairs = [(x, y) for (x, y) in zip(olds, news) if x != y] + + if not pairs: print('Nothing to replace') return + loop(pairs, dry=True) + ok = autoyes if not ok: print('Is this correct? y/n') @@ -66,17 +76,12 @@ def longest_length(li): return longest def loop(pairs, dry=False): - has_content = False - for (x, y) in pairs: + for (old, new) in pairs: if dry: - line = '{old}\n{new}\n' - line = line.format(old=x, new=y) - #print(line.encode('utf-8')) + line = f'{old}\n{new}\n' safeprint.safeprint(line) - has_content = True else: - os.rename(x, y) - return has_content + os.rename(old, new) def title(text): (text, extension) = os.path.splitext(text) @@ -96,13 +101,14 @@ def title(text): return text def brename_argparse(args): - brename(args.transformation, autoyes=args.autoyes) + brename(args.transformation, autoyes=args.autoyes, recurse=args.recurse) def main(argv): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('transformation', help='python command using x as variable name') parser.add_argument('-y', '--yes', dest='autoyes', action='store_true', help='accept results without confirming') + parser.add_argument('--recurse', dest='recurse', action='store_true', help='operate on subdirectories also') parser.set_defaults(func=brename_argparse) args = parser.parse_args(argv) diff --git a/breplace.py b/breplace.py index e65de2d..e355b24 100644 --- a/breplace.py +++ b/breplace.py @@ -7,7 +7,7 @@ import sys def breplace_argparse(args): command = f'x.replace("{args.replace_from}", "{args.replace_to}")' - brename.brename(command, autoyes=args.autoyes) + brename.brename(command, autoyes=args.autoyes, recurse=args.recurse) def main(argv): parser = argparse.ArgumentParser(__doc__) @@ -15,6 +15,7 @@ def main(argv): 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') + parser.add_argument('--recurse', dest='recurse', action='store_true', help='operate on subdirectories also') parser.set_defaults(func=breplace_argparse) args = parser.parse_args(argv)