From 347affa0c5f434d300d404183cf31f42ad1c225d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Wed, 23 Aug 2017 22:34:02 +0200 Subject: [PATCH] Add support for different operations, called only when needed --- normalisename.py | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/normalisename.py b/normalisename.py index a47c454..e232cd2 100755 --- a/normalisename.py +++ b/normalisename.py @@ -8,6 +8,16 @@ from sys import exit 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 def separator(self): return self._separator @@ -16,15 +26,23 @@ class normalisename: def whitelist(self): return self._whitelist - def __init__(self, separator, whitelist): + def __init__(self, operation, separator, whitelist): + self._operation = None + self.operation = operation 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))) + if not self.check_normal(path): + dir = dirname(path) + 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): 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)) - normalisename(args.separator, whitelist).normalise(args.files) + normalisename(rename, args.separator, whitelist).normalise(args.files)