Add option to disallow special characters. closes #2

This commit is contained in:
Kjistóf 2017-05-28 20:07:20 +02:00 committed by Kristóf Tóth
parent e20f442016
commit 2ad95914e0

View File

@ -4,26 +4,35 @@ from os import rename
from os.path import basename, dirname from os.path import basename, dirname
from os.path import join as joinpath from os.path import join as joinpath
from argparse import ArgumentParser from argparse import ArgumentParser
from sys import exit
# parse arguments
parser = ArgumentParser(description='Eliminate funky stuff from filenames.') parser = ArgumentParser(description='Eliminate funky stuff from filenames.')
parser.add_argument('--separator', '-s', type=str, nargs='?', default='_', parser.add_argument('--separator', '-s', type=str, nargs='?', default='_',
help='Set separator to use (to replace spaces with).') help='Set separator to use (to replace spaces with).')
parser.add_argument('--allow', '-a', type=str, nargs='?', action='append', parser.add_argument('--allow', '-a', type=str, nargs='?', action='append',
help='Specify a special character to allow (it will not be removed).') help='Specify a special character to allow (it will not be removed).')
parser.add_argument('--disallow', '-d', type=str, nargs='?', action='append',
help='Specify a special character to disallow (it will removed).')
parser.add_argument('--whitelist', '-w', type=str, parser.add_argument('--whitelist', '-w', type=str,
help='Overwrite default whitelist (format: whitespace separated string).') help='Overwrite default whitelist (format: whitespace separated string).')
parser.add_argument('files', type=str, nargs='+', parser.add_argument('files', type=str, nargs='+',
help='File(s) to normalise the name of (relative or absolute path).') help='File(s) to normalise the name of (relative or absolute path).')
args = parser.parse_args() args = parser.parse_args()
# verify arguments
if args.disallow:
if args.separator in args.disallow: exit('Disallowing your chosen separator makes no sense!')
# special characters that will not be removed # declare special characters that will not be removed (spaces are handled elsewhere)
whitelist = {' ', '.', '-'} whitelist = {' ', '.', '-'}
if args.whitelist: whitelist = set(args.whitelist.split()) if args.whitelist: whitelist = set(args.whitelist.split())
# modify whitelist based on arguments
whitelist.add(args.separator) whitelist.add(args.separator)
if args.allow: whitelist = whitelist.union(set(args.allow)) if args.allow: whitelist = whitelist.union(set(args.allow))
if args.disallow: whitelist = whitelist.difference(set(args.disallow))
for path in args.files: for path in args.files: