Refactor and buxfix (paths with funky characters would raise)

This commit is contained in:
Kjistóf 2017-08-23 18:48:49 +02:00 committed by Kristóf Tóth
parent 2ad95914e0
commit d5efc7db1c

View File

@ -7,6 +7,32 @@ from argparse import ArgumentParser
from sys import exit from sys import exit
class normalisename:
@property
def separator(self):
return self._separator
@property
def whitelist(self):
return self._whitelist
def __init__(self, separator, whitelist):
self._separator = separator
self._whitelist = set(whitelist)
def normalise(self, files):
for path in files:
dir = dirname(path)
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))
if __name__ == '__main__':
# parse arguments # 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='_',
@ -35,17 +61,4 @@ if args.allow: whitelist = whitelist.union(set(args.allow))
if args.disallow: whitelist = whitelist.difference(set(args.disallow)) if args.disallow: whitelist = whitelist.difference(set(args.disallow))
for path in args.files: normalisename(args.separator, whitelist).normalise(args.files)
dir = dirname(path)
file = basename(path)
rename(path,
unidecode
(
joinpath
(
dir,
''.join(ch for ch in file.replace(' ', args.separator)
if ch.isalnum()
or ch in whitelist)
)
))