Skip to content

Commit

Permalink
Fixed bug where errors would ensue if root directory had a '/', and a…
Browse files Browse the repository at this point in the history
…dded tests
  • Loading branch information
h-0-0 committed Sep 28, 2023
1 parent 2070e42 commit 71f4c11
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
3 changes: 2 additions & 1 deletion slune/savers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
55 changes: 31 additions & 24 deletions slune/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_savers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 71f4c11

Please sign in to comment.