Make operation optional, return results from __call__

This commit is contained in:
Kristóf Tóth 2018-09-04 22:45:42 +02:00
parent baa444f25e
commit ab24097866
2 changed files with 14 additions and 10 deletions

2
cli.py
View File

@ -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)

View File

@ -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:
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)