Support normalising str and Iterable[str] as well

This commit is contained in:
Kristóf Tóth 2018-09-04 21:08:48 +02:00
parent 0d696114b2
commit baa444f25e
1 changed files with 18 additions and 6 deletions

View File

@ -1,6 +1,7 @@
from os.path import basename, dirname
from os.path import join as joinpath
from re import sub
from collections.abc import Iterable
from unidecode import unidecode
@ -29,13 +30,24 @@ class Normalisename:
def whitelist(self):
return self._whitelist.union({self.separator})
def __call__(self, paths):
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]!')
def normalise_all(self, paths):
for path in paths:
directory = dirname(path)
filename = basename(path)
normalpath = joinpath(directory, self.normalname(filename))
if path != normalpath:
self.operation(path, normalpath) # pylint: disable=not-callable
self.normalise(path)
def normalise(self, path):
directory = dirname(path)
filename = basename(path)
normalpath = joinpath(directory, self.normalname(filename))
if path != normalpath:
self.operation(path, normalpath) # pylint: disable=not-callable
def check_normal(self, path):
filename = basename(path)