diff --git a/src/criteriaSorter/__main__.py b/src/criteriaSorter/__main__.py index 7e32d44..4bebee1 100644 --- a/src/criteriaSorter/__main__.py +++ b/src/criteriaSorter/__main__.py @@ -5,4 +5,4 @@ import sys # pragma: nocover # The main call can't be covered because it will only execute through the CLI if __name__ == "__main__": criteriaSorter.main(sys.argv) - sys.exit(0) \ No newline at end of file + sys.exit(0) diff --git a/src/criteriaSorter/modules/criteriaSorter.py b/src/criteriaSorter/modules/criteriaSorter.py index 43262c1..353dfa1 100644 --- a/src/criteriaSorter/modules/criteriaSorter.py +++ b/src/criteriaSorter/modules/criteriaSorter.py @@ -205,4 +205,3 @@ def main(argvp): # Execute the action _LIST_ACTIONS[args.action](args) - diff --git a/src/criteriaSorter/modules/fileops.py b/src/criteriaSorter/modules/fileops.py index b3a81e4..bcc89ea 100644 --- a/src/criteriaSorter/modules/fileops.py +++ b/src/criteriaSorter/modules/fileops.py @@ -2,12 +2,18 @@ import logging import os import re +# import pathlib + EXTENTION_BY_TYPE = { 'image': ["jpg", "jpeg", "png", "gif", "bmp", "tiff", "tif"], 'video': ["mp4", "avi", "mkv", "mov", "flv", "wmv", "mpg", "mpeg", "m4v", "3gp", "3g2"], - 'music': ["mp3", "wav", "wma", "ogg", "flac", "aac", "m4a"], - 'document': ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf", "txt", "rtf", "odt", "ods", "odp", "odg", "odf", "odc", "odb", "csv", "tsv", "html", "htm", "css", "js", "json", "xml", "yml", "yaml", "java", "py", "c", "cpp", "h", "hpp", "hxx", "h++", "cs", "php", "sql", "log", "md", "markdown", "rst", "tex", "latex", "bib", "bibtex"] + 'music': ["mp3", "wav", "wma", "ogg", "flac", "aac", "m4a", "aiff"], + 'document': ["doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf", "txt", "rtf", "odt", + "ods", "odp", "odg", "odf", "odc", "odb", "csv", "tsv", "html", "htm", + "css", "js", "json", "xml", "yml", "yaml", "java", "py", "c", "cpp", "h", + "hpp", "hxx", "h++", "cs", "php", "sql", "log", "md", "markdown", "rst", + "tex", "latex", "bib", "bibtex"] } TYPE_BY_EXTENTION = {} @@ -80,6 +86,15 @@ class FileHandler: def get_file_name(self): return self.file_name + def get_file_size(self): + return os.path.getsize(self.file_path) + + def get_file_size_in_mb(self): + return self.get_file_size() / (1024 * 1024) + + def get_file_size_in_gb(self): + return self.get_file_size() / (1024 * 1024 * 1024) + def get_extension(self): return os.path.splitext(self.file_name)[1] @@ -107,9 +122,27 @@ class FileHandler: def is_document(self): return self.guess_file_type() == 'document' + def is_unknown(self): + return self.guess_file_type() is None + + def is_bigger_than(self, size): + return self.get_file_size() > int(size) + + def is_smaller_than(self, size): + return self.get_file_size() < int(size) + + def is_bigger_than_mb(self, size): + return self.is_bigger_than(int(size) * 1024 * 1024) + + def is_smaller_than_mb(self, size): + return self.is_smaller_than(int(size) * 1024 * 1024) + def fills_all_conditions(self, list_functions): - for function in list_functions: - if not self.__getattribute__(function)(): + for function_str in list_functions: + function_split = function_str.split(',') + function_name = function_split[0] + function_args = function_split[1:] + if not getattr(self, function_name)(*function_args): return False return True diff --git a/tests/modules/test_fileops.py b/tests/modules/test_fileops.py index 2d50577..53998e2 100644 --- a/tests/modules/test_fileops.py +++ b/tests/modules/test_fileops.py @@ -3,9 +3,23 @@ from criteriaSorter.modules import fileops import pathlib import os +import pytest _BASE_PATH = pathlib.Path("/Test/Path") +_LIST_BASE_FILE_PATH = [ + (_BASE_PATH / "TestFile.txt", "document", "is_document"), + (_BASE_PATH / "TestFile.pdf", "document", "is_document"), + (_BASE_PATH / "TestFile.doc", "document", "is_document"), + (_BASE_PATH / "TestFile.jpg", "image", "is_image"), + (_BASE_PATH / "TestFile.png", "image", "is_picture"), + (_BASE_PATH / "TestFile.mp3", "music", "is_music"), + (_BASE_PATH / "TestFile.aiff", "music", "is_audio"), + (_BASE_PATH / "TestFile.mp4", "video", "is_video"), + (_BASE_PATH / "TestFile.mpg", "video", "is_video"), + (_BASE_PATH / "TestFile.unknown_type", None, "is_unknown"), +] + class MockOS: @staticmethod @@ -22,12 +36,6 @@ class MockOS: else: return ['a', 'b', 'c'] - @staticmethod - def path_exists(path): - if path in [_BASE_PATH / i for i in ['a', 'b', 'c', 'a/b', 'a/c', 'a\\b', 'a\\c']]: - return True - return True - @staticmethod def isdir(path): if pathlib.Path(path) == _BASE_PATH / 'a': @@ -54,20 +62,8 @@ class MockOS: return MockOS.getsize(path) -def test_DirectoryHandler(monkeypatch): - # Regular invocation - handler = fileops.DirectoryHandler(".") - - # Test regular calls, doen't test the return value - handler.get_dir_path() - handler.get_dir_name() - handler.get_dir_size() - handler.get_dir_size_in_mb() - handler.get_dir_size_in_gb() - assert [f for f in handler.get_files()] - assert [f for f in handler.get_directories()] - handler.get_full_list_with_size() - +@pytest.fixture +def handler(monkeypatch): # Using a mock function to test return value handler = fileops.DirectoryHandler("/Test/Path") monkeypatch.setattr(os, 'listdir', MockOS.listdir) @@ -76,7 +72,18 @@ def test_DirectoryHandler(monkeypatch): monkeypatch.setattr(os.path, 'getsize', MockOS.getsize) monkeypatch.setattr(os.path, 'join', MockOS.join) monkeypatch.setattr(os, 'stat', MockOS.stat) + return handler + +@pytest.fixture +def f_handler(monkeypatch): + # Regular invocation + monkeypatch.setattr(os.path, 'getsize', MockOS.getsize) + monkeypatch.setattr(os.path, 'isfile', MockOS.isfile) + monkeypatch.setattr(os.path, 'stat', MockOS.stat) + + +def test_DirectoryHandler(handler): # Test return values assert pathlib.Path(handler.get_dir_path()) == _BASE_PATH assert handler.get_dir_name() == "Path" @@ -88,9 +95,30 @@ def test_DirectoryHandler(monkeypatch): assert handler.get_file_list() == [_BASE_PATH / "b", _BASE_PATH / "c"] assert handler.get_directory_list() == ["a"] -# def test_FileHandler(): -# handler = fileops.FileHandler(".") -# -# + +@pytest.mark.parametrize("path, expected, method", _LIST_BASE_FILE_PATH) +def test_FileHandler(f_handler, path, expected, method): + # Test return values + + handler = fileops.FileHandler(str(path)) + + assert pathlib.Path(handler.get_file_path()) == path + assert handler.get_file_name() == path.name + assert handler.get_file_size() == (1024 * 1024) * 1 + assert handler.get_file_size_in_mb() == 1 + assert handler.get_file_size_in_gb() == 1 / 1024 + assert handler.guess_file_type() == expected + assert handler.get_extension() == path.suffix + assert handler.__getattribute__(method)() is True + 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 + +# 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(".")