search_file break up and test -- not working yet
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
**__pycache__/
|
||||
*py[cod]
|
||||
*$py.class
|
||||
test/data
|
||||
@@ -33,6 +33,9 @@ def link_fixer(ln_path, tgt_dir_path):
|
||||
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__":
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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()
|
||||
22
test/test_search_file.py
Normal file
22
test/test_search_file.py
Normal file
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user