Add support for different operations, called only when needed

This commit is contained in:
Kjistóf 2017-08-23 22:34:02 +02:00 committed by Kristóf Tóth
parent d5efc7db1c
commit 347affa0c5

View File

@ -8,6 +8,16 @@ from sys import exit
class normalisename: class normalisename:
@property
def operation(self):
return self._operation
@operation.setter
def operation(self, value):
if not callable(value):
raise ValueError('Operation must be callable!')
self._operation = value
@property @property
def separator(self): def separator(self):
return self._separator return self._separator
@ -16,15 +26,23 @@ class normalisename:
def whitelist(self): def whitelist(self):
return self._whitelist return self._whitelist
def __init__(self, separator, whitelist): def __init__(self, operation, separator, whitelist):
self._operation = None
self.operation = operation
self._separator = separator self._separator = separator
self._whitelist = set(whitelist) self._whitelist = set(whitelist)
def normalise(self, files): def normalise(self, files):
for path in files: for path in files:
dir = dirname(path) if not self.check_normal(path):
file = basename(path) dir = dirname(path)
rename(path, joinpath(dir, self.normalname(file))) file = basename(path)
normalpath = joinpath(dir, self.normalname(file))
self.operation(path, normalpath)
def check_normal(self, file):
file = basename(file)
return file == self.normalname(file)
def normalname(self, filename): def normalname(self, filename):
return unidecode(''.join(ch for ch in filename.replace(' ', self.separator) return unidecode(''.join(ch for ch in filename.replace(' ', self.separator)
@ -61,4 +79,4 @@ if __name__ == '__main__':
if args.disallow: whitelist = whitelist.difference(set(args.disallow)) if args.disallow: whitelist = whitelist.difference(set(args.disallow))
normalisename(args.separator, whitelist).normalise(args.files) normalisename(rename, args.separator, whitelist).normalise(args.files)