cmd/brename.py

178 lines
5.6 KiB
Python
Raw Normal View History

2019-06-12 05:41:31 +00:00
'''
Batch rename files by providing a string to be `eval`ed, using variable `x` as
2021-02-28 00:09:44 +00:00
the current filename. Yes I know this is weird, but for certain tasks it's just
too quick and easy to pass up.
2019-06-12 05:41:31 +00:00
2021-02-28 00:09:44 +00:00
Examples:
2019-06-12 05:41:31 +00:00
Prefix all the files:
2021-02-28 00:09:44 +00:00
brename.py "f'Test_{x}'"
Rename files to their index with 0 padding:
brename.py "f'{index1:>03}{dot_ext}'"
2019-06-12 05:41:31 +00:00
Keep the first word and extension:
2021-02-28 00:09:44 +00:00
brename.py "(x.split(' ')[0] + dot_ext) if ' ' in x else x"
2019-06-12 05:41:31 +00:00
'''
import argparse
import os
import random
import re
import sys
import unicodedata
2019-06-12 05:41:31 +00:00
from voussoirkit import betterhelp
from voussoirkit import interactive
from voussoirkit import pathclass
2019-06-12 05:41:31 +00:00
from voussoirkit import safeprint
from voussoirkit import spinal
from voussoirkit import stringtools
2019-06-12 05:41:31 +00:00
# These constants are provided for use in your eval string
cwd = pathclass.cwd()
# These variables are provided so that if you have any difficulty typing
# literal quotes etc into your command line, you can just use these variable
# names in your eval string.
apostrophe = "'"
2019-06-12 05:41:31 +00:00
dot = '.'
hyphen = '-'
2019-06-12 05:41:31 +00:00
quote = '"'
space = ' '
underscore = '_'
# For backwards compatibility, these names which were once locally defined
# functions are now just references to stringtools.
excise = stringtools.excise
title = stringtools.title_capitalize
def natural_sorter(x):
'''
Thank you Mark Byers
http://stackoverflow.com/a/11150413
'''
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split(r'([0-9]+)', key)]
return alphanum_key(x)
def unicode_normalize(s):
return unicodedata.normalize('NFC', s)
def brename(transformation, autoyes=False, do_naturalsort=False, recurse=False):
if recurse:
2020-02-04 00:42:15 +00:00
walker = spinal.walk_generator('.', yield_files=True, yield_directories=True)
olds = list(walker)
else:
olds = cwd.listdir()
2019-06-12 05:41:31 +00:00
if do_naturalsort:
olds.sort(key=lambda x: natural_sorter(x.absolute_path))
else:
olds.sort()
2020-09-30 20:16:27 +00:00
pairs = []
for (index, old) in enumerate(olds):
# These variables are assigned so that you can use them in your
# transformation string.
x = old.basename
parent = old.parent
noext = old.replace_extension('').basename
2021-02-28 00:09:44 +00:00
ext = old.extension.no_dot
dot_ext = old.extension.with_dot
index1 = index + 1
2020-09-30 20:16:27 +00:00
new = eval(transformation)
new = parent.with_child(new)
if new.basename == old.basename:
2020-09-30 20:16:27 +00:00
continue
pairs.append((old, new))
if not pairs:
2019-06-12 05:41:31 +00:00
print('Nothing to replace')
return
loop(pairs, dry=True)
2020-12-08 02:02:41 +00:00
if not (autoyes or interactive.getpermission('Is this correct?')):
return
# Sort in reverse so that renaming a file inside a directory always
# occurs before renaming the directory itself. If you rename the
# directory first, then the path to the file is invalid by the time
# you want to rename it.
pairs = sorted(pairs, reverse=True)
loop(pairs, dry=False)
2019-06-12 05:41:31 +00:00
def longest_length(li):
longest = 0
for item in li:
longest = max(longest, len(item))
return longest
def loop(pairs, dry=False):
for (old, new) in pairs:
2019-06-12 05:41:31 +00:00
if dry:
line = f'{old.absolute_path}\n{new.absolute_path}\n'
2019-06-12 05:41:31 +00:00
safeprint.safeprint(line)
else:
os.rename(old.absolute_path, new.absolute_path)
2019-06-12 05:41:31 +00:00
def brename_argparse(args):
return brename(
args.transformation,
autoyes=args.autoyes,
do_naturalsort=args.naturalsort,
recurse=args.recurse,
)
2019-06-12 05:41:31 +00:00
DOCSTRING = '''
brename - batch file renaming
=============================
> brename.py eval_string <flags>
eval_string:
A string which will be evaluated by Python's eval. The name of the file or
folder will be in the variable `x`. In addition, many other variables are
provided for your convenience:
`quote` ("), `apostrophe` (') so you don't have to escape command quotes.
`hyphen` (-) because leading hyphens often cause problems with argparse.
`stringtools` entire stringtools module. See voussoirkit/stringtools.py.
`space` ( ), `dot` (.), `underscore` (_) so you don't have to add quotes to
your command while using these common characters.
`index` the file's index within the loop.
`index1` the file's index+1, in case you want your names to start from 1.
`parent` a pathclass.Path object for the directory containing the file.
`cwd` a pathclass.Path object for the cwd of this program session.
`noext` the name of the file, but without its extension.
`ext` the file's extension, with no dot.
-y | --yes:
Accept the results without confirming.
--recurse:
Recurse into subfolders and rename those files too.
--naturalsort:
Before renaming, the files will be sorted using natural sort instead of the
default lexicographic sort. Natural sort means that "My file 20" will come
before "My file 100" because 20<100. Lexicographic sort means 100 will come
first because 1 is before 2.
The purpose of this flag is so your index and index1 variables are applied
in the order you desire.
'''
2019-06-12 05:41:31 +00:00
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')
parser.add_argument('--recurse', action='store_true')
parser.add_argument('--naturalsort', action='store_true')
2019-06-12 05:41:31 +00:00
parser.set_defaults(func=brename_argparse)
return betterhelp.single_main(argv, parser, DOCSTRING)
2019-06-12 05:41:31 +00:00
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))