diff --git a/src/wireviz/wv_helper.py b/src/wireviz/wv_helper.py index ffc4bd8..6cad64d 100644 --- a/src/wireviz/wv_helper.py +++ b/src/wireviz/wv_helper.py @@ -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]))