From ab24097866b583007e1bebdb2aac6f74a780882e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Tue, 4 Sep 2018 22:45:42 +0200 Subject: [PATCH] Make operation optional, return results from __call__ --- cli.py | 2 +- normalisename.py | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/cli.py b/cli.py index 4740216..5f7fba0 100755 --- a/cli.py +++ b/cli.py @@ -84,4 +84,4 @@ if __name__ == '__main__': operation = checkname # do what needs to be done - Normalisename(operation, args.separator, whitelist)(args.files) + Normalisename(args.separator, whitelist, operation)(args.files) diff --git a/normalisename.py b/normalisename.py index 8f05296..b6d4a6a 100755 --- a/normalisename.py +++ b/normalisename.py @@ -7,10 +7,11 @@ from unidecode import unidecode class Normalisename: - def __init__(self, operation, separator, whitelist): - self._operation, self.operation = None, operation + def __init__(self, separator, whitelist, operation=None): self._separator = separator self._whitelist = set(whitelist) + self._operation = None + self.operation = operation or self.noop @property def operation(self): @@ -22,6 +23,10 @@ class Normalisename: raise ValueError('Operation must be callable!') self._operation = value + @staticmethod + def noop(*_): + pass + @property def separator(self): return self._separator @@ -32,15 +37,13 @@ class Normalisename: def __call__(self, path_or_paths): if isinstance(path_or_paths, str): - self.normalise(path_or_paths) - elif isinstance(path_or_paths, Iterable): - self.normalise_all(path_or_paths) - else: - raise ValueError('Argument must be str Iterable[str]!') + return self.normalise(path_or_paths) + if isinstance(path_or_paths, Iterable): + return self.normalise_all(path_or_paths) + raise ValueError('Argument must be str Iterable[str]!') def normalise_all(self, paths): - for path in paths: - self.normalise(path) + return [self.normalise(path) for path in paths] def normalise(self, path): directory = dirname(path) @@ -48,6 +51,7 @@ class Normalisename: normalpath = joinpath(directory, self.normalname(filename)) if path != normalpath: self.operation(path, normalpath) # pylint: disable=not-callable + return normalpath def check_normal(self, path): filename = basename(path)