Implement smart file resolving

This commit is contained in:
Daniel Rojas 2021-03-27 13:45:07 +01:00
parent b9a5a8d51d
commit 7112adbc34

View File

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-
from typing import List
from pathlib import Path
import re
awg_equiv_table = {
@ -117,3 +118,20 @@ def aspect_ratio(image_src):
except Exception as error:
print(f'aspect_ratio(): {type(error).__name__}: {error}')
return 1 # Assume 1:1 when unable to read actual image size
def smart_file_resolve(filename, possible_paths):
filename = Path(filename)
if filename.is_absolute():
if filename.exists():
return filename
else:
raise Exception(f'{filename} does not exist.')
else: # sarch all possible paths in decreasing order of precedence
possible_paths = [Path(path).resolve() for path in possible_paths]
for possible_path in possible_paths:
resolved_path = (possible_path / filename).resolve()
if (resolved_path).exists():
return resolved_path
else:
raise Exception(f'{filename} was not found in any of the following locations: \n' +
'\n'.join([str(x) for x in possible_paths]))