From 7112adbc34d696883e77957f8e1a539b43606f32 Mon Sep 17 00:00:00 2001 From: Daniel Rojas Date: Sat, 27 Mar 2021 13:45:07 +0100 Subject: [PATCH] Implement smart file resolving --- src/wireviz/wv_helper.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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]))