2018-09-02 13:52:52 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import sys
|
|
|
|
from os import rename
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
|
|
|
|
from normalisename import Normalisename
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
# parse arguments
|
|
|
|
parser = ArgumentParser(description='Eliminate funky stuff from filenames.')
|
|
|
|
parser.add_argument(
|
|
|
|
'--separator', '-s', type=str, nargs='?', default='_',
|
|
|
|
help='Set separator to use (to replace spaces with).'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--allow', '-a', type=str, nargs='?', action='append',
|
|
|
|
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,
|
|
|
|
help='Overwrite default whitelist (format: whitespace separated string).'
|
|
|
|
)
|
|
|
|
|
|
|
|
mgroup = parser.add_mutually_exclusive_group()
|
|
|
|
mgroup.add_argument(
|
|
|
|
'--dryrun', '-n', action='store_true',
|
|
|
|
help='Do not touch anything, just tell me what would change!'
|
|
|
|
)
|
|
|
|
mgroup.add_argument(
|
|
|
|
'--funky', '-f', action='store_true',
|
|
|
|
help='List filenames with special characters'
|
|
|
|
)
|
2018-09-02 15:01:05 +00:00
|
|
|
mgroup.add_argument(
|
|
|
|
'--check', '-c', action='store_true',
|
|
|
|
help='Check whether a name is normalised or not'
|
|
|
|
)
|
|
|
|
|
2018-09-02 13:52:52 +00:00
|
|
|
parser.add_argument(
|
|
|
|
'files', type=str, nargs='+',
|
|
|
|
help='File(s) to normalise the name of (relative or absolute path).'
|
|
|
|
)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# verify arguments
|
|
|
|
if args.disallow:
|
|
|
|
if args.separator in args.disallow:
|
|
|
|
sys.exit('Disallowing your chosen separator makes no sense!')
|
|
|
|
|
2018-09-02 14:10:41 +00:00
|
|
|
# declare special characters that will not be removed
|
|
|
|
whitelist = {'.', '-'}
|
2018-09-02 13:52:52 +00:00
|
|
|
if args.whitelist:
|
|
|
|
whitelist = set(args.whitelist.split())
|
|
|
|
|
|
|
|
# modify whitelist based on arguments
|
|
|
|
if args.allow:
|
|
|
|
whitelist = whitelist.union(set(args.allow))
|
|
|
|
if args.disallow:
|
|
|
|
whitelist = whitelist.difference(set(args.disallow))
|
|
|
|
|
|
|
|
# define operations
|
2018-09-02 15:01:05 +00:00
|
|
|
def listchanges(before, after):
|
2018-09-08 22:34:56 +00:00
|
|
|
print(
|
|
|
|
f'⤺ {before}\n'
|
|
|
|
f'⤻ {after}\n'
|
|
|
|
)
|
2018-09-02 15:01:05 +00:00
|
|
|
|
|
|
|
def listfunky(before, _):
|
|
|
|
print(before)
|
|
|
|
|
|
|
|
def checkname(before, after):
|
|
|
|
if before != after:
|
|
|
|
sys.exit(1)
|
|
|
|
sys.exit(0)
|
2018-09-02 13:52:52 +00:00
|
|
|
|
|
|
|
operation = rename
|
|
|
|
if args.dryrun:
|
|
|
|
operation = listchanges
|
|
|
|
elif args.funky:
|
|
|
|
operation = listfunky
|
2018-09-02 15:01:05 +00:00
|
|
|
elif args.check:
|
|
|
|
operation = checkname
|
2018-09-02 13:52:52 +00:00
|
|
|
|
|
|
|
# do what needs to be done
|
2018-09-04 20:45:42 +00:00
|
|
|
Normalisename(args.separator, whitelist, operation)(args.files)
|