Make operation optional, return results from __call__
This commit is contained in:
parent
baa444f25e
commit
ab24097866
2
cli.py
2
cli.py
@ -84,4 +84,4 @@ if __name__ == '__main__':
|
|||||||
operation = checkname
|
operation = checkname
|
||||||
|
|
||||||
# do what needs to be done
|
# do what needs to be done
|
||||||
Normalisename(operation, args.separator, whitelist)(args.files)
|
Normalisename(args.separator, whitelist, operation)(args.files)
|
||||||
|
@ -7,10 +7,11 @@ from unidecode import unidecode
|
|||||||
|
|
||||||
|
|
||||||
class Normalisename:
|
class Normalisename:
|
||||||
def __init__(self, operation, separator, whitelist):
|
def __init__(self, separator, whitelist, operation=None):
|
||||||
self._operation, self.operation = None, operation
|
|
||||||
self._separator = separator
|
self._separator = separator
|
||||||
self._whitelist = set(whitelist)
|
self._whitelist = set(whitelist)
|
||||||
|
self._operation = None
|
||||||
|
self.operation = operation or self.noop
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def operation(self):
|
def operation(self):
|
||||||
@ -22,6 +23,10 @@ class Normalisename:
|
|||||||
raise ValueError('Operation must be callable!')
|
raise ValueError('Operation must be callable!')
|
||||||
self._operation = value
|
self._operation = value
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def noop(*_):
|
||||||
|
pass
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def separator(self):
|
def separator(self):
|
||||||
return self._separator
|
return self._separator
|
||||||
@ -32,15 +37,13 @@ class Normalisename:
|
|||||||
|
|
||||||
def __call__(self, path_or_paths):
|
def __call__(self, path_or_paths):
|
||||||
if isinstance(path_or_paths, str):
|
if isinstance(path_or_paths, str):
|
||||||
self.normalise(path_or_paths)
|
return self.normalise(path_or_paths)
|
||||||
elif isinstance(path_or_paths, Iterable):
|
if isinstance(path_or_paths, Iterable):
|
||||||
self.normalise_all(path_or_paths)
|
return self.normalise_all(path_or_paths)
|
||||||
else:
|
|
||||||
raise ValueError('Argument must be str Iterable[str]!')
|
raise ValueError('Argument must be str Iterable[str]!')
|
||||||
|
|
||||||
def normalise_all(self, paths):
|
def normalise_all(self, paths):
|
||||||
for path in paths:
|
return [self.normalise(path) for path in paths]
|
||||||
self.normalise(path)
|
|
||||||
|
|
||||||
def normalise(self, path):
|
def normalise(self, path):
|
||||||
directory = dirname(path)
|
directory = dirname(path)
|
||||||
@ -48,6 +51,7 @@ class Normalisename:
|
|||||||
normalpath = joinpath(directory, self.normalname(filename))
|
normalpath = joinpath(directory, self.normalname(filename))
|
||||||
if path != normalpath:
|
if path != normalpath:
|
||||||
self.operation(path, normalpath) # pylint: disable=not-callable
|
self.operation(path, normalpath) # pylint: disable=not-callable
|
||||||
|
return normalpath
|
||||||
|
|
||||||
def check_normal(self, path):
|
def check_normal(self, path):
|
||||||
filename = basename(path)
|
filename = basename(path)
|
||||||
|
Loading…
Reference in New Issue
Block a user