diff --git a/src/criteriaSorter/modules/fileops.py b/src/criteriaSorter/modules/fileops.py index bcc89ea..41bd71a 100644 --- a/src/criteriaSorter/modules/fileops.py +++ b/src/criteriaSorter/modules/fileops.py @@ -16,6 +16,12 @@ EXTENTION_BY_TYPE = { "tex", "latex", "bib", "bibtex"] } +_guess_artist_regex = [ + re.compile(r"(?P.+)\s+by\s+(?P[^.]+)\s*\..*$"), + re.compile(r"(?P[^-]+)\s*-\s*(?P[^.]+)\s*\..*"), + # re.compile(r"(?P.+)_(?P.+)$"), +] + TYPE_BY_EXTENTION = {} for i, j in EXTENTION_BY_TYPE.items(): for k in j: @@ -148,9 +154,20 @@ class FileHandler: def sort(self, list_of_condition_dicts, default=None): for condition in list_of_condition_dicts: - if self.fills_all_conditions(condition['conditions'].split('\n')[:-1]): - self.future_name = condition['destination'] - return self.future_name + try: + if not condition: + raise KeyError + list_conditions = condition['conditions'].split('\n')[:-1] + if self.fills_all_conditions(list_conditions): + self.future_name = condition['destination'] + return self.future_name + except KeyError: + logging.error('No conditions found in condition dict') + logging.error(condition) + logging.info("", exc_info=True) + self.future_name = default + return default + logging.warning("The file {} doesn't meet any criteria, reverting to default".format(self.file_name)) self.future_name = default return default @@ -167,39 +184,37 @@ class FileHandler: destination = os.path.join(destination, self.future_name.format(obj=self)) destination_dir = os.path.dirname(destination) if not os.path.exists(destination_dir): - logging.info(dry_run_message+'Creating directory: ' + destination_dir) + logging.info('{}Creating directory: {}'.format(dry_run_message, destination_dir)) if not dry_run: os.makedirs(destination_dir) - logging.info(dry_run_message+'Moving file: ' + self.name + ' to: ' + destination) + logging.info('{}Moving file: {} to {}'.format(dry_run_message, self.name, destination)) if not dry_run: os.rename(self.file_path, destination) return self.file_path, destination class ArtistHandler(FileHandler): - def __init__(self, file_path): + def __init__(self, file_path, regex=None): super().__init__(file_path) + if regex is None: + regex = _guess_artist_regex.copy() + self.regex_list = regex self.artist, self.file_name = self.guess_artist_and_file_name() def guess_artist_and_file_name(self): """Try to guess artist from file name""" - regex = [ - r"(?P.+) by (?P[^\.]+)\..*$", - r"(?P[^-]+)-(?P.+)$", - # r"(?P.+)_(?P.+)$", - ] try: - for r in regex: + for r in self.regex_list: m = re.match(r, self.file_name) if m: - return m.group("artist").rstrip(" "), m.group("file_name").lstrip(" ") - return None, self.file_name + return m.group("artist").strip(" "), m.group("file_name").strip(" ") + return None, self.base_name except re.error as e: logging.error("An error occured while processing regex on " + self.file_name) logging.error(e) logging.info("", exc_info=True) - return None, self.file_name + return None, self.base_name def get_artist(self): return self.artist diff --git a/tests/modules/test_fileops.py b/tests/modules/test_fileops.py index 53998e2..36a5966 100644 --- a/tests/modules/test_fileops.py +++ b/tests/modules/test_fileops.py @@ -1,9 +1,15 @@ # Test file for fileops.py - +import logging from criteriaSorter.modules import fileops import pathlib import os import pytest +import io +import yaml + + +class SUPERNONE: + pass _BASE_PATH = pathlib.Path("/Test/Path") @@ -19,6 +25,38 @@ _LIST_BASE_FILE_PATH = [ (_BASE_PATH / "TestFile.mpg", "video", "is_video"), (_BASE_PATH / "TestFile.unknown_type", None, "is_unknown"), ] +_TEST_ARTIST_FILE_PATH = [ + (_BASE_PATH / "TestArtist - Testpicture.jpg", "TestArtist", "Testpicture", 'Artists/{obj.artist}/{obj.name}'), + (_BASE_PATH / "Testpicture by TestArtist.jpg", "TestArtist", "Testpicture", 'Artists/{obj.artist}/{obj.name}'), + (_BASE_PATH / "Testpicture by TestArtist.jpg", "TestArtist", "Testpicture", 'Artists/{obj.artist}/{obj.name}'), + (_BASE_PATH / "Testpicture by TestArtist.jpg", "TestArtist", "Testpicture", 'Artists/{obj.artist}/{obj.name}'), + (_BASE_PATH / "Testpicture made with TestArtist.jpg", None, "Testpicture made with TestArtist", SUPERNONE), +] + +_TEST_MOVE_FILE_PATH = [ + (_BASE_PATH / "TestFile.txt", True, "", "TestFile.txt"), + (_BASE_PATH / "TestFile.txt", False, "", "TestFile.txt"), +] + +_FALSE_CONFIG = """ + operation1: + conditions : | + has_artist + is_image + destination : Artists/{obj.artist}/{obj.name} + operation2: + conditions : | + is_video + destination : vids/{obj.name} + operation3: + conditions : | + is_audio + destination : audios/{obj.name} + operation4: + conditions : | + is_document + destination : others/{obj.name} +""" class MockOS: @@ -61,6 +99,35 @@ class MockOS: else: return MockOS.getsize(path) + @staticmethod + def rename(src, dst): + if src != dst: + return True + return False + + @staticmethod + def remove(path): + if "__nonexistent__" in str(path): + return False + return True + + @staticmethod + def dirname(path): + return pathlib.Path(path).parent + + @staticmethod + def exists(path): + if "__nonexistent__" in str(path): + return False + return True + + @staticmethod + def mkdir(path): + if "__nonexistent__" in str(path): + return True + else: + return True + @pytest.fixture def handler(monkeypatch): @@ -81,6 +148,22 @@ def f_handler(monkeypatch): monkeypatch.setattr(os.path, 'getsize', MockOS.getsize) monkeypatch.setattr(os.path, 'isfile', MockOS.isfile) monkeypatch.setattr(os.path, 'stat', MockOS.stat) + monkeypatch.setattr(os.path, 'join', MockOS.join) + monkeypatch.setattr(os.path, 'dirname', MockOS.dirname) + monkeypatch.setattr(os, 'rename', MockOS.rename) + monkeypatch.setattr(os.path, 'exists', MockOS.exists) + + +@pytest.fixture +def a_handler(monkeypatch): + monkeypatch.setattr(os.path, 'getsize', MockOS.getsize) + monkeypatch.setattr(os.path, 'isfile', MockOS.isfile) + monkeypatch.setattr(os.path, 'stat', MockOS.stat) + monkeypatch.setattr(os.path, 'dirname', MockOS.dirname) + monkeypatch.setattr(os, 'rename', MockOS.rename) + monkeypatch.setattr(os.path, 'exists', MockOS.exists) + monkeypatch.setattr(os, 'mkdir', MockOS.mkdir) + monkeypatch.setattr(os, 'makedirs', MockOS.mkdir) def test_DirectoryHandler(handler): @@ -113,12 +196,73 @@ def test_FileHandler(f_handler, path, expected, method): assert handler.fills_all_conditions( [method, method] ) - assert handler.fills_all_conditions(["is_document","is_picture"]) is False - assert handler.fills_all_conditions(["is_bigger_than,10","is_bigger_than_mb,0","is_smaller_than,100000000","is_smaller_than_mb,1000"]) is True + assert handler.fills_all_conditions(["is_document", "is_picture"]) is False + assert handler.fills_all_conditions(["is_bigger_than,10", + "is_bigger_than_mb,0", + "is_smaller_than,100000000", + "is_smaller_than_mb,1000"]) is True -# def test_FileHandler_exception(f_handler): -# handler = fileops.FileHandler(str(path)) -# assert f_handler.guess_file_type() is None -# def test_ArticleHandler(): -# handler = fileops.ArtistHandler(".") +@pytest.mark.parametrize("path, artist, picture, return_pathlike", _TEST_ARTIST_FILE_PATH) +def test_ArtistHandler(caplog, a_handler, path, artist, picture, return_pathlike): + handler = fileops.ArtistHandler(str(path)) + assert handler.get_artist() == artist + assert handler.get_file_name_without_artist() == picture + if artist: + assert handler.has_artist() + assert handler.get_file_name_with_artist() == artist + " - " + picture + else: + assert not handler.has_artist() + assert handler.get_file_name_with_artist() == path.name.split(".")[0] + + YAML_FALSE_CONFIG = yaml.load(io.StringIO(_FALSE_CONFIG), Loader=yaml.FullLoader) + list_conds = [YAML_FALSE_CONFIG[cond] for cond in YAML_FALSE_CONFIG] + + with caplog.at_level(logging.WARNING): + assert handler.sort(list_conds, default=SUPERNONE) == return_pathlike + assert handler.future_name == return_pathlike + if return_pathlike is SUPERNONE: + assert "default" in caplog.text + assert handler.future_name is return_pathlike + else: + assert "default" not in caplog.text + + +@pytest.mark.parametrize("path, dry_run, destination, return_pathlike", _TEST_MOVE_FILE_PATH) +def test_FileHandler_move(f_handler, path, dry_run, destination, return_pathlike): + # Test the move method + handler = fileops.FileHandler(str(_BASE_PATH / "a")) + handler.future_name = "{obj.base_name}/{obj.type} - {obj.base_name}.{obj.extension}" + handler.move(str(_BASE_PATH / "b"), dry_run=dry_run) + # assert pathlib.Path(handler.get_file_path()) == _BASE_PATH / "b" + + +def test_ArtistHandler_exception(caplog, a_handler): + handler = fileops.ArtistHandler(str(_BASE_PATH / "a"), regex=["(?<=\\()[^)*(?=\\)"]) + caplog.set_level(logging.ERROR) + with caplog.at_level(logging.ERROR): + handler.guess_artist_and_file_name() + assert "An error occured while processing regex on" in caplog.text + + YAML_BAD_CONFIG = yaml.load(io.StringIO("""test: no"""), Loader=yaml.FullLoader) + list_conds = [YAML_BAD_CONFIG[cond] for cond in YAML_BAD_CONFIG] + with caplog.at_level(logging.ERROR): + handler.sort(list_conds, default=SUPERNONE) + assert "No conditions found in condition" in caplog.text + assert handler.future_name is SUPERNONE + + +def test_ArtistHandler_exception_2(caplog, a_handler): + handler = fileops.ArtistHandler(str(_BASE_PATH / "a"), regex=["(?<=\\()[^)*(?=\\)"]) + handler.future_name = None + with caplog.at_level(logging.DEBUG): + assert handler.move(str(_BASE_PATH / "b"), dry_run=False) is None + assert "No future_name found" in caplog.text + + +def test_ArtistHandler_exception_3(caplog, a_handler): + handler = fileops.ArtistHandler(str(_BASE_PATH / "a")) + handler.future_name = "{obj.base_name}/{obj.type} - {obj.base_name}{obj.extension}" + with caplog.at_level(logging.INFO): + assert handler.move("__nonexistent__", dry_run=False) + assert "Creating directory" in caplog.text