Refactor and buxfix (paths with funky characters would raise)
This commit is contained in:
parent
2ad95914e0
commit
d5efc7db1c
@ -7,45 +7,58 @@ from argparse import ArgumentParser
|
|||||||
from sys import exit
|
from sys import exit
|
||||||
|
|
||||||
|
|
||||||
# parse arguments
|
class normalisename:
|
||||||
parser = ArgumentParser(description='Eliminate funky stuff from filenames.')
|
@property
|
||||||
parser.add_argument('--separator', '-s', type=str, nargs='?', default='_',
|
def separator(self):
|
||||||
help='Set separator to use (to replace spaces with).')
|
return self._separator
|
||||||
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).')
|
|
||||||
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
|
@property
|
||||||
if args.disallow:
|
def whitelist(self):
|
||||||
if args.separator in args.disallow: exit('Disallowing your chosen separator makes no sense!')
|
return self._whitelist
|
||||||
|
|
||||||
# declare special characters that will not be removed (spaces are handled elsewhere)
|
def __init__(self, separator, whitelist):
|
||||||
whitelist = {' ', '.', '-'}
|
self._separator = separator
|
||||||
if args.whitelist: whitelist = set(args.whitelist.split())
|
self._whitelist = set(whitelist)
|
||||||
|
|
||||||
# modify whitelist based on arguments
|
def normalise(self, files):
|
||||||
whitelist.add(args.separator)
|
for path in files:
|
||||||
if args.allow: whitelist = whitelist.union(set(args.allow))
|
dir = dirname(path)
|
||||||
if args.disallow: whitelist = whitelist.difference(set(args.disallow))
|
file = basename(path)
|
||||||
|
rename(path, joinpath(dir, self.normalname(file)))
|
||||||
|
|
||||||
|
def normalname(self, filename):
|
||||||
|
return unidecode(''.join(ch for ch in filename.replace(' ', self.separator)
|
||||||
|
if ch.isalnum()
|
||||||
|
or ch in self.whitelist))
|
||||||
|
|
||||||
|
|
||||||
for path in args.files:
|
if __name__ == '__main__':
|
||||||
dir = dirname(path)
|
# parse arguments
|
||||||
file = basename(path)
|
parser = ArgumentParser(description='Eliminate funky stuff from filenames.')
|
||||||
rename(path,
|
parser.add_argument('--separator', '-s', type=str, nargs='?', default='_',
|
||||||
unidecode
|
help='Set separator to use (to replace spaces with).')
|
||||||
(
|
parser.add_argument('--allow', '-a', type=str, nargs='?', action='append',
|
||||||
joinpath
|
help='Specify a special character to allow (it will not be removed).')
|
||||||
(
|
parser.add_argument('--disallow', '-d', type=str, nargs='?', action='append',
|
||||||
dir,
|
help='Specify a special character to disallow (it will removed).')
|
||||||
''.join(ch for ch in file.replace(' ', args.separator)
|
parser.add_argument('--whitelist', '-w', type=str,
|
||||||
if ch.isalnum()
|
help='Overwrite default whitelist (format: whitespace separated string).')
|
||||||
or ch in whitelist)
|
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: exit('Disallowing your chosen separator makes no sense!')
|
||||||
|
|
||||||
|
# declare special characters that will not be removed (spaces are handled elsewhere)
|
||||||
|
whitelist = {' ', '.', '-'}
|
||||||
|
if args.whitelist: whitelist = set(args.whitelist.split())
|
||||||
|
|
||||||
|
# modify whitelist based on arguments
|
||||||
|
whitelist.add(args.separator)
|
||||||
|
if args.allow: whitelist = whitelist.union(set(args.allow))
|
||||||
|
if args.disallow: whitelist = whitelist.difference(set(args.disallow))
|
||||||
|
|
||||||
|
|
||||||
|
normalisename(args.separator, whitelist).normalise(args.files)
|
||||||
|
Loading…
Reference in New Issue
Block a user