from tags import Tags from pathlib import Path import shutil import logging import os class Library: def __init__(self, dir_root: Path): self.dir_root = dir_root self.dir_orphan = Path(dir_root, 'orphan') self.dir_orphan.mkdir(exist_ok=True, parents=True) def move_to_orphan(self, p: Path): logging.info("%s move to orphan", p) shutil.move(os.fspath(p), os.fspath(self.dir_orphan)) def move(self, p: Path, tags: Tags): new_path = self.__compute_path(tags) new_path.mkdir(exist_ok=True, parents=True) logging.info("%s move to %s", p.name, new_path) shutil.move(os.fspath(p), os.fspath(new_path)) def __compute_path(self, tags: Tags) -> Path: p = self.dir_root if tags.copyrights == 'original': # Originals groups by artist p = p / "_originals" if tags.artists != "": artist = tags.artists.split(" ")[0] p = p / self.__sanitize(artist) return p # Main section copyright = "" if tags.copyrights != "": copyright = tags.copyrights.split(" ")[0] p = p / self.__sanitize(copyright) if tags.characters == "": return p # Characters section characters = tags.characters.split(" ") if len(characters) == 1: character = characters[0] \ .replace(copyright, "") \ .replace("("+copyright+")", "") \ .replace("()", "") \ .strip() p = p / self.__sanitize(character) else: p = p / "_multiple" return p def __sanitize(self, s: str) -> str: s = "".join(x for x in s if x.isalnum() or x in "._-()") return s.replace("_", " ").strip()