Skip to content

Commit

Permalink
Implement smart file resolving
Browse files Browse the repository at this point in the history
  • Loading branch information
formatc1702 committed Mar 27, 2021
1 parent b9a5a8d commit 7112adb
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/wireviz/wv_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# -*- coding: utf-8 -*-

from typing import List
from pathlib import Path
import re

awg_equiv_table = {
Expand Down Expand Up @@ -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]))

0 comments on commit 7112adb

Please sign in to comment.