Merged find_source_item() and get_full_path()
* Also fixed regex match (it wasn't matching the whole string)
This commit is contained in:
parent
b6c6fc9aa9
commit
50cb765108
1 changed files with 30 additions and 19 deletions
|
|
@ -6,37 +6,48 @@ from functions.common import *
|
||||||
|
|
||||||
def case_insensitive_search(path, item):
|
def case_insensitive_search(path, item):
|
||||||
"""Search path for item case insensitively, returns str."""
|
"""Search path for item case insensitively, returns str."""
|
||||||
|
regex_match = '^{}$'.format(item)
|
||||||
|
real_path = ''
|
||||||
|
|
||||||
|
# Quick check first
|
||||||
if os.path.exists('{}/{}'.format(path, item)):
|
if os.path.exists('{}/{}'.format(path, item)):
|
||||||
# Easy mode
|
real_path = '{}{}{}'.format(
|
||||||
return '{}/{}'.format(path, item)
|
path,
|
||||||
|
'' if path == '/' else '/',
|
||||||
|
item,
|
||||||
|
)
|
||||||
|
|
||||||
# Check all items in dir
|
# Check all items in dir
|
||||||
for entry in os.scandir(path):
|
for entry in os.scandir(path):
|
||||||
if re.match(entry.name, item, re.IGNORECASE):
|
if re.match(regex_match, entry.name, re.IGNORECASE):
|
||||||
return '{}/{}'.format(path, entry.name)
|
real_path = '{}{}{}'.format(
|
||||||
|
path,
|
||||||
|
'' if path == '/' else '/',
|
||||||
|
entry.name,
|
||||||
|
)
|
||||||
|
|
||||||
# If we get here the item wasn't found
|
# Done
|
||||||
raise FileNotFoundError('{}/{}'.format(path, item))
|
if real_path:
|
||||||
|
return real_path
|
||||||
|
else:
|
||||||
|
raise FileNotFoundError('{}/{}'.format(path, item))
|
||||||
|
|
||||||
|
|
||||||
def find_source_item(source_dir, item):
|
def find_path(path):
|
||||||
"""Find item relative to source dir, returns str."""
|
"""Find path case-insensitively, returns pathlib.Path obj."""
|
||||||
path = source_dir
|
parts = pathlib.Path(path).resolve().relative_to('/').parts
|
||||||
if item.startswith('/'):
|
real_path = '/'
|
||||||
item = item[1:]
|
|
||||||
|
|
||||||
for part in item.split('/'):
|
# Fix case
|
||||||
path = case_insensitive_search(path, part)
|
for part in parts:
|
||||||
|
real_path = case_insensitive_search(real_path, part)
|
||||||
|
|
||||||
return path
|
# Raise error if path doesn't exist
|
||||||
|
path_obj = pathlib.Path(real_path)
|
||||||
|
|
||||||
def get_full_path(item):
|
|
||||||
"""Get full path to item, returns pathlib.Path obj."""
|
|
||||||
path_obj = pathlib.Path(item).resolve()
|
|
||||||
if not path_obj.exists():
|
if not path_obj.exists():
|
||||||
raise FileNotFoundError(path_obj)
|
raise FileNotFoundError(path_obj)
|
||||||
|
|
||||||
|
# Done
|
||||||
return path_obj
|
return path_obj
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue