diff --git a/slune/savers.py b/slune/savers.py index 91fb66c..e4ef7e6 100644 --- a/slune/savers.py +++ b/slune/savers.py @@ -36,7 +36,8 @@ def get_match(self, params): match = '/'.join(match) # If there are missing arguments, add them to the path if len(missing_params) > 0: - match = os.path.join(match, *missing_params) + match = match + missing_params + match = os.path.join(*match) return match def get_path(self, params): diff --git a/slune/utils.py b/slune/utils.py index 6a16fb1..71c0d8d 100644 --- a/slune/utils.py +++ b/slune/utils.py @@ -10,30 +10,37 @@ def find_directory_path(strings, root_directory='.'): - root_directory (string): Path to the root directory to be searched, default is current working directory. # TODO: could probably optimize this function """ - # Get list of directories in root directory - dir_list = os.listdir(root_directory) - # Get substring up to and including '=' for each directory name in dir_list, and strip whitespace - stripped_dir_list = [d.split('=')[0].strip() +"=" for d in dir_list] - # Get rid of duplicates - stripped_dir_list = list(set(stripped_dir_list)) - # Check if any of the strings are in the list of directories - for string in strings: - if string in stripped_dir_list: - # If a string is found it means that at the current root there is a directory starting "--string=" - # we now want to find all directories in the root directory that start with "--string=" and search them recursively - # then we return the path to the deepest directory found - dir_list = [d for d in dir_list if d.startswith(string)] - # Recursively search each directory starting with string - paths = [] - for d in dir_list: - paths.append(find_directory_path(strings, os.path.join(root_directory, d))) - # Return the deepest directory found, ie. most /'s in path - return max(paths, key=lambda x: x.count('/')) - # If no strings are found, return the root directory - dirs = root_directory.split('/') - dirs = [dirs[0]] + [d.split('=')[0].strip() +"=" for d in dirs[1:]] - root_directory = '/'.join(dirs) - return root_directory + def _find_directory_path(strings, curr_root, first_call=False): + # Get list of directories in root directory + dir_list = os.listdir(curr_root) + # Get substring up to and including '=' for each directory name in dir_list, and strip whitespace + stripped_dir_list = [d.split('=')[0].strip() +"=" for d in dir_list] + # Get rid of duplicates + stripped_dir_list = list(set(stripped_dir_list)) + # Check if any of the strings are in the list of directories + for string in strings: + if string in stripped_dir_list: + # If a string is found it means that at the current root there is a directory starting "--string=" + # we now want to find all directories in the root directory that start with "--string=" and search them recursively + # then we return the path to the deepest directory found + dir_list = [d for d in dir_list if d.startswith(string)] + # Recursively search each directory starting with string + paths = [] + for d in dir_list: + paths.append(_find_directory_path(strings, os.path.join(curr_root, d))) + # Return the deepest directory found, ie. most /'s in path + return max(paths, key=lambda x: x.count('/')) + # If no strings are found, return the root directory + if first_call: + pass + else: + curr_root = curr_root[len(root_directory):] + dirs = curr_root[1:].split('/') + dirs = [d.split('=')[0].strip() +"=" for d in dirs] + curr_root = '/'.join(dirs) + curr_root = os.path.join(root_directory, curr_root) + return curr_root + return _find_directory_path(strings, root_directory, first_call=True) def dict_to_strings(d): diff --git a/tests/test_savers.py b/tests/test_savers.py index d4653c3..d4a3809 100644 --- a/tests/test_savers.py +++ b/tests/test_savers.py @@ -60,7 +60,7 @@ def test_get_match_too_deep(self): # Test if get_match finds correct match and builds correct directory path using the parameters matching_dir = saver.get_match(["--folder2=0.2", "--folder3=0.3"]) - self.assertEqual(matching_dir, os.path.join(self.test_dir, "--folder2=0.2/--folder3=0.3")) + self.assertEqual(matching_dir, os.path.join(self.test_dir, '--folder2=0.2', '--folder3=0.3')) def test_get_match_no_match(self): # Create a SaverCsv instance diff --git a/tests/test_utils.py b/tests/test_utils.py index 5896569..6fa427a 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -54,6 +54,16 @@ def test_deepest(self): search_strings = ['--folder1=', '--folder2=', '--folder3=', '--folder4='] result = find_directory_path(search_strings, root_directory=self.test_dir) self.assertEqual(result, os.path.join(self.test_dir, '--folder1=', '--folder2=', '--folder3=', '--folder4=')) + + def test_root_dir_with_forward_slash(self): + search_strings = ['--folder2=', '--folder3='] + result = find_directory_path(search_strings, root_directory=self.test_dir + '/--folder1=0.1') + self.assertEqual(result, os.path.join(self.test_dir + '/--folder1=0.1', '--folder2=', '--folder3=')) + + def test_just_root_dir_forward_slash(self): + search_strings = ['--folder_not_there='] + result = find_directory_path(search_strings, root_directory=self.test_dir + '/--folder1=0.1' + '/--folder2=0.2' + '/another_folder') + self.assertEqual(result, os.path.join(self.test_dir, '--folder1=0.1', '--folder2=0.2', 'another_folder')) class TestDictToStrings(unittest.TestCase):