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

View File

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