Upgrade brename and breplace to use real argparse.
This commit is contained in:
parent
0dbc0728c0
commit
71670f3c99
2 changed files with 44 additions and 11 deletions
|
@ -19,7 +19,7 @@ import sys
|
||||||
from voussoirkit import safeprint
|
from voussoirkit import safeprint
|
||||||
|
|
||||||
|
|
||||||
def brename(transformation):
|
def brename(transformation, autoyes=False):
|
||||||
old = os.listdir()
|
old = os.listdir()
|
||||||
new = [eval(transformation) for x in old]
|
new = [eval(transformation) for x in old]
|
||||||
pairs = []
|
pairs = []
|
||||||
|
@ -27,12 +27,17 @@ def brename(transformation):
|
||||||
if x == y:
|
if x == y:
|
||||||
continue
|
continue
|
||||||
pairs.append((x, y))
|
pairs.append((x, y))
|
||||||
|
|
||||||
if not loop(pairs, dry=True):
|
if not loop(pairs, dry=True):
|
||||||
print('Nothing to replace')
|
print('Nothing to replace')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
ok = autoyes
|
||||||
|
if not ok:
|
||||||
print('Is this correct? y/n')
|
print('Is this correct? y/n')
|
||||||
if input('>').lower() not in ('y', 'yes', 'yeehaw'):
|
ok = input('>').lower() in ('y', 'yes', 'yeehaw')
|
||||||
return
|
|
||||||
|
if ok:
|
||||||
loop(pairs, dry=False)
|
loop(pairs, dry=False)
|
||||||
|
|
||||||
def excise(s, mark_left, mark_right):
|
def excise(s, mark_left, mark_right):
|
||||||
|
@ -81,6 +86,20 @@ def title(text):
|
||||||
text = first + rest + extension
|
text = first + rest + extension
|
||||||
return text
|
return text
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def brename_argparse(args):
|
||||||
|
brename(args.transformation)
|
||||||
|
|
||||||
|
def main(argv):
|
||||||
|
parser = argparse.ArgumentParser(description=__doc__)
|
||||||
|
|
||||||
|
parser.add_argument('transformation', help='python command using x as variable name')
|
||||||
|
parser.set_defaults(func=brename_argparse)
|
||||||
|
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
args.func(args)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
transformation = sys.argv[1]
|
raise SystemExit(main(sys.argv[1:]))
|
||||||
brename(transformation)
|
|
||||||
|
|
|
@ -1,10 +1,24 @@
|
||||||
'''
|
'''
|
||||||
Batch rename files by replacing the first argument with the second.
|
Batch rename files by replacing the first argument with the second.
|
||||||
'''
|
'''
|
||||||
|
import argparse
|
||||||
import brename
|
import brename
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
replace_from = sys.argv[1]
|
def breplace_argparse(args):
|
||||||
replace_to = sys.argv[2]
|
command = f'x.replace("{args.replace_from}", "{args.replace_to}")'
|
||||||
command = 'x.replace("{f}", "{t}")'.format(f=replace_from, t=replace_to)
|
brename.brename(command, autoyes=args.autoyes)
|
||||||
brename.brename(command)
|
|
||||||
|
def main(argv):
|
||||||
|
parser = argparse.ArgumentParser(__doc__)
|
||||||
|
|
||||||
|
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.set_defaults(func=breplace_argparse)
|
||||||
|
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
args.func(args)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
raise SystemExit(main(sys.argv[1:]))
|
||||||
|
|
Loading…
Reference in a new issue