2019-06-12 05:41:31 +00:00
|
|
|
'''
|
|
|
|
Batch rename files by providing a string to be `eval`ed, using variable `x` as
|
|
|
|
the current filename.
|
|
|
|
Yes I know this is weird, but for certain tasks it's just too quick and easy to pass up.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
Prefix all the files:
|
|
|
|
brename.py "'Test_' + x"
|
|
|
|
|
|
|
|
Keep the first word and extension:
|
|
|
|
brename.py "(x.split(' ')[0] + '.' + x.split('.')[-1]) if ' ' in x else x"
|
|
|
|
'''
|
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import re
|
|
|
|
import sys
|
2020-03-11 23:46:22 +00:00
|
|
|
import unicodedata
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2020-02-09 20:08:38 +00:00
|
|
|
from voussoirkit import getpermission
|
2019-06-12 05:41:31 +00:00
|
|
|
from voussoirkit import safeprint
|
2019-12-10 20:59:20 +00:00
|
|
|
from voussoirkit import spinal
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2019-12-10 20:59:20 +00:00
|
|
|
apostrophe = "'"
|
2019-06-12 05:41:31 +00:00
|
|
|
dot = '.'
|
2019-12-10 20:59:20 +00:00
|
|
|
hyphen = '-'
|
2019-06-12 05:41:31 +00:00
|
|
|
quote = '"'
|
|
|
|
space = ' '
|
2019-12-10 20:59:20 +00:00
|
|
|
underscore = '_'
|
2020-03-11 23:46:22 +00:00
|
|
|
def unicode_normalize(s):
|
|
|
|
return unicodedata.normalize('NFC', s)
|
2019-12-10 20:59:20 +00:00
|
|
|
|
|
|
|
def brename(transformation, autoyes=False, recurse=False):
|
|
|
|
if recurse:
|
2020-02-04 00:42:15 +00:00
|
|
|
walker = spinal.walk_generator('.', yield_files=True, yield_directories=True)
|
2020-02-02 00:39:18 +00:00
|
|
|
olds = [x.absolute_path for x in walker]
|
2019-12-10 20:59:20 +00:00
|
|
|
else:
|
|
|
|
olds = [os.path.join(os.getcwd(), x) for x in os.listdir('.')]
|
2019-06-12 05:41:31 +00:00
|
|
|
|
2019-12-10 20:59:20 +00:00
|
|
|
news = []
|
|
|
|
for (index, x) in enumerate(olds):
|
|
|
|
directory = os.path.dirname(x)
|
|
|
|
basename = os.path.basename(x)
|
2019-12-12 20:15:57 +00:00
|
|
|
(noext, ext) = os.path.splitext(basename)
|
2019-12-10 20:59:20 +00:00
|
|
|
x = basename
|
2019-06-12 05:41:31 +00:00
|
|
|
x = eval(transformation)
|
2019-12-10 20:59:20 +00:00
|
|
|
x = os.path.join(directory, x)
|
|
|
|
news.append(x)
|
|
|
|
|
|
|
|
pairs = [(x, y) for (x, y) in zip(olds, news) if x != y]
|
|
|
|
|
|
|
|
if not pairs:
|
2019-06-12 05:41:31 +00:00
|
|
|
print('Nothing to replace')
|
|
|
|
return
|
|
|
|
|
2019-12-10 20:59:20 +00:00
|
|
|
loop(pairs, dry=True)
|
|
|
|
|
2020-02-09 20:08:38 +00:00
|
|
|
if autoyes or getpermission.getpermission('Is this correct?'):
|
|
|
|
pairs = reversed(pairs)
|
2019-06-12 05:41:31 +00:00
|
|
|
loop(pairs, dry=False)
|
|
|
|
|
|
|
|
def excise(s, mark_left, mark_right):
|
|
|
|
'''
|
|
|
|
Remove the text between the left and right landmarks, inclusive, returning
|
|
|
|
the rest of the text.
|
|
|
|
|
|
|
|
excise('What a wonderful day [soundtrack].mp3', ' [', ']') ->
|
|
|
|
returns 'What a wonderful day.mp3'
|
|
|
|
'''
|
2020-01-30 20:34:03 +00:00
|
|
|
if mark_left in s and mark_right in s:
|
|
|
|
return s.split(mark_left)[0] + s.split(mark_right)[-1]
|
|
|
|
return s
|
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):
|
2019-12-10 20:59:20 +00:00
|
|
|
for (old, new) in pairs:
|
2019-06-12 05:41:31 +00:00
|
|
|
if dry:
|
2019-12-10 20:59:20 +00:00
|
|
|
line = f'{old}\n{new}\n'
|
2019-06-12 05:41:31 +00:00
|
|
|
safeprint.safeprint(line)
|
|
|
|
else:
|
2019-12-10 20:59:20 +00:00
|
|
|
os.rename(old, new)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
def title(text):
|
|
|
|
(text, extension) = os.path.splitext(text)
|
|
|
|
text = text.title()
|
|
|
|
if ' ' in text:
|
|
|
|
(first, rest) = text.split(' ', 1)
|
|
|
|
else:
|
|
|
|
(first, rest) = (text, '')
|
|
|
|
rest = ' %s ' % rest
|
|
|
|
for article in ['The', 'A', 'An', 'At', 'To', 'In', 'Of', 'From', 'And']:
|
|
|
|
article = ' %s ' % article
|
|
|
|
rest = rest.replace(article, article.lower())
|
|
|
|
rest = rest.strip()
|
|
|
|
if rest != '':
|
|
|
|
rest = ' ' + rest
|
|
|
|
text = first + rest + extension
|
|
|
|
return text
|
|
|
|
|
|
|
|
def brename_argparse(args):
|
2019-12-10 20:59:20 +00:00
|
|
|
brename(args.transformation, autoyes=args.autoyes, recurse=args.recurse)
|
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', help='accept results without confirming')
|
2019-12-10 20:59:20 +00:00
|
|
|
parser.add_argument('--recurse', dest='recurse', action='store_true', help='operate on subdirectories also')
|
2019-06-12 05:41:31 +00:00
|
|
|
parser.set_defaults(func=brename_argparse)
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
2020-02-09 01:18:50 +00:00
|
|
|
return args.func(args)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|