Add support for different operations, called only when needed
This commit is contained in:
		@@ -8,6 +8,16 @@ from sys import exit
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class normalisename:
 | 
			
		||||
    @property
 | 
			
		||||
    def operation(self):
 | 
			
		||||
        return self._operation
 | 
			
		||||
 | 
			
		||||
    @operation.setter
 | 
			
		||||
    def operation(self, value):
 | 
			
		||||
        if not callable(value):
 | 
			
		||||
            raise ValueError('Operation must be callable!')
 | 
			
		||||
        self._operation = value
 | 
			
		||||
 | 
			
		||||
    @property
 | 
			
		||||
    def separator(self):
 | 
			
		||||
        return self._separator
 | 
			
		||||
@@ -16,15 +26,23 @@ class normalisename:
 | 
			
		||||
    def whitelist(self):
 | 
			
		||||
        return self._whitelist
 | 
			
		||||
 | 
			
		||||
    def __init__(self, separator, whitelist):
 | 
			
		||||
    def __init__(self, operation, separator, whitelist):
 | 
			
		||||
        self._operation = None
 | 
			
		||||
        self.operation = operation
 | 
			
		||||
        self._separator = separator
 | 
			
		||||
        self._whitelist = set(whitelist)
 | 
			
		||||
 | 
			
		||||
    def normalise(self, files):
 | 
			
		||||
        for path in files:
 | 
			
		||||
            dir = dirname(path)
 | 
			
		||||
            file = basename(path)
 | 
			
		||||
            rename(path, joinpath(dir, self.normalname(file)))
 | 
			
		||||
            if not self.check_normal(path):
 | 
			
		||||
                dir = dirname(path)
 | 
			
		||||
                file = basename(path)
 | 
			
		||||
                normalpath = joinpath(dir, self.normalname(file))
 | 
			
		||||
                self.operation(path, normalpath)
 | 
			
		||||
 | 
			
		||||
    def check_normal(self, file):
 | 
			
		||||
        file = basename(file)
 | 
			
		||||
        return file == self.normalname(file)
 | 
			
		||||
 | 
			
		||||
    def normalname(self, filename):
 | 
			
		||||
        return unidecode(''.join(ch for ch in filename.replace(' ', self.separator)
 | 
			
		||||
@@ -61,4 +79,4 @@ if __name__ == '__main__':
 | 
			
		||||
    if args.disallow: whitelist = whitelist.difference(set(args.disallow))
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    normalisename(args.separator, whitelist).normalise(args.files)
 | 
			
		||||
    normalisename(rename, args.separator, whitelist).normalise(args.files)
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user