diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c43b8a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +**__pycache__/ +*py[cod] +*$py.class +test/data diff --git a/link_fixer.py b/link_fixer.py index 9f08dee..55c4510 100644 --- a/link_fixer.py +++ b/link_fixer.py @@ -32,6 +32,9 @@ def link_fixer(ln_path, tgt_dir_path): print("Targets dir: \t", tgt_dir) if ln_is_dir: sys.exit("But ln dir version not yet implemented. Sorry!") + +def fix_link(lnk, tgt_dir_path): + return True if __name__ == "__main__": diff --git a/search_file.py b/search_file.py index e69de29..eff199d 100644 --- a/search_file.py +++ b/search_file.py @@ -0,0 +1,45 @@ +# search_file.py +# searching for target file in a given dir +# prompt user if multiple are found + +import os +from fnmatch import fnmatch + +def search_all_match(filename, path): +# search part shamelessly copied from stack overflow... +# real author: Nadia Alramli + results = [] + for root, dirs, files in os.walk(path): + for name in files: + if fnmatch(filename, name): + results.append(os.path.join(root, name)) + return results + +def print_num_list(paths): + n = 1 + for p in paths: + print(f"{n}. {p}") + n += 1 + return 0 + +def select_path(paths): + l = len(paths) + if l == 0 : + raise Exception("Error select_path - empty list") + elif l == 1 : + return paths[0] + c = '0' + while not (c.isdigit() and 0 < int(c) <= l): + print("Multiple matches found.") + print("Please select target:") + print_num_list(paths) + c = input(f"Please select file to link to: (1-{l})") + return paths[int(c)-1] + +def search_file(filename, path): + paths = search_all_match(filename, path) + if len(paths) == 0 : + print(f"No match for \"{filename}\". Link not modified.") + raise Exception(FileNotFoundError) + p = select_path(paths) + return p diff --git a/test/test_link_fixer.py b/test/test_link_fixer.py index 35a7114..321985d 100644 --- a/test/test_link_fixer.py +++ b/test/test_link_fixer.py @@ -3,7 +3,7 @@ import os from pathlib import Path import unittest -class TestFixLinker(unittest.TestCase): +class TestLinkFixer(unittest.TestCase): tgt_dir = Path("test/data/tgt_dir") lnk_dir = Path("test/data/lnk_dir") @@ -69,5 +69,9 @@ class TestFixLinker(unittest.TestCase): link_fixer(self.tgt_file, self.tgt_dir) self.assertEqual(err.exception.code, self.ln_ns_error) + print("Test for inexistent ") + with self.assertRaises(SystemExit) as err: + link + if __name__ == "__main__": unittest.main() \ No newline at end of file diff --git a/test/test_search_file.py b/test/test_search_file.py new file mode 100644 index 0000000..c504ffe --- /dev/null +++ b/test/test_search_file.py @@ -0,0 +1,22 @@ +import unittest +from search_file import search_file + +# TODO: gros TODO icitte. Presque rien de fait! + +class TestSearchFile(unittest.TestCase): + + data_dir = "test/data/search_test" + + def test_print_num_list(self): + p = [""] + + + def test_search_file(self): + print(search_file("match1.txt", self.data_dir)) + with self.assertRaises(FileNotFoundError) as e: + search_file("inexistent.txt", self.data_dir) + self.assertTrue("File does not exist", e.exception) + self.assertEqual(search_file("with space.txt", self.data_dir), "test/data/search_test/subdir2/with space.txt") + print(search_file("match2.txt", self.data_dir)) + print(search_file("subdir2", self.data_dir)) + \ No newline at end of file