Add recurse option and more format variables to brename/breplace.

This commit is contained in:
voussoir 2019-12-10 12:59:20 -08:00
parent 8114e39f7e
commit e99cbe3e39
2 changed files with 30 additions and 23 deletions

View file

@ -18,29 +18,39 @@ import re
import sys import sys
from voussoirkit import safeprint from voussoirkit import safeprint
from voussoirkit import spinal
dot = '.'
quote = '"'
apostrophe = "'" apostrophe = "'"
dot = '.'
hyphen = '-'
quote = '"'
space = ' ' space = ' '
underscore = '_'
def brename(transformation, autoyes=False): def brename(transformation, autoyes=False, recurse=False):
old = os.listdir() if recurse:
new = [] olds = [x.absolute_path for x in spinal.walk_generator('.')]
for (index, x) in enumerate(old): 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) (noext, ext) = os.path.splitext(x)
directory = os.path.dirname(x)
basename = os.path.basename(x)
x = basename
x = eval(transformation) x = eval(transformation)
new.append(x) x = os.path.join(directory, x)
pairs = [] news.append(x)
for (x, y) in zip(old, new):
if x == y:
continue
pairs.append((x, y))
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') print('Nothing to replace')
return return
loop(pairs, dry=True)
ok = autoyes ok = autoyes
if not ok: if not ok:
print('Is this correct? y/n') print('Is this correct? y/n')
@ -66,17 +76,12 @@ def longest_length(li):
return longest return longest
def loop(pairs, dry=False): def loop(pairs, dry=False):
has_content = False for (old, new) in pairs:
for (x, y) in pairs:
if dry: if dry:
line = '{old}\n{new}\n' line = f'{old}\n{new}\n'
line = line.format(old=x, new=y)
#print(line.encode('utf-8'))
safeprint.safeprint(line) safeprint.safeprint(line)
has_content = True
else: else:
os.rename(x, y) os.rename(old, new)
return has_content
def title(text): def title(text):
(text, extension) = os.path.splitext(text) (text, extension) = os.path.splitext(text)
@ -96,13 +101,14 @@ def title(text):
return text return text
def brename_argparse(args): def brename_argparse(args):
brename(args.transformation, autoyes=args.autoyes) brename(args.transformation, autoyes=args.autoyes, recurse=args.recurse)
def main(argv): def main(argv):
parser = argparse.ArgumentParser(description=__doc__) parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('transformation', help='python command using x as variable name') 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('-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) parser.set_defaults(func=brename_argparse)
args = parser.parse_args(argv) args = parser.parse_args(argv)

View file

@ -7,7 +7,7 @@ import sys
def breplace_argparse(args): def breplace_argparse(args):
command = f'x.replace("{args.replace_from}", "{args.replace_to}")' 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): def main(argv):
parser = argparse.ArgumentParser(__doc__) parser = argparse.ArgumentParser(__doc__)
@ -15,6 +15,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('--recurse', dest='recurse', action='store_true', help='operate on subdirectories also')
parser.set_defaults(func=breplace_argparse) parser.set_defaults(func=breplace_argparse)
args = parser.parse_args(argv) args = parser.parse_args(argv)