From 0124cfc766a4e2ae22734b52603e07a3cb51a124 Mon Sep 17 00:00:00 2001 From: Carlos Sevilla Salcedo Date: Fri, 29 Sep 2023 12:25:55 +0300 Subject: [PATCH] Fixed issue with relative paths --- src/vai_lab/_import_helper.py | 23 ++++++++++++++++++++++- src/vai_lab/utils/plugins/MainPage.py | 6 +++--- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/vai_lab/_import_helper.py b/src/vai_lab/_import_helper.py index e56d2118..3095ddbc 100644 --- a/src/vai_lab/_import_helper.py +++ b/src/vai_lab/_import_helper.py @@ -43,4 +43,25 @@ def rel_to_abs(filename: str) -> str: filename = path.normpath(path.join(get_lib_parent_dir(), filename)) elif filename[0] == "/" or (filename[0].isalpha() and filename[0].isupper()): filename = filename - return filename \ No newline at end of file + return filename + +def abs_to_rel(filename: str) -> str: + """Checks if path is relative or absolute + If absolute, converts path to relative if possible + If relative, returns itself + """ + if filename[0] == ".": + #Relative path + return filename + elif filename[0].isalpha() and filename[0] != get_lib_parent_dir()[0]: + #Different drive -> Absolute path + return filename + else: + #Same drive not relative + _folder = path.relpath(filename, get_lib_parent_dir()) + if _folder[:2] == '..': + # Absolute path + return filename + else: + # Relative path + return path.join('.',_folder) diff --git a/src/vai_lab/utils/plugins/MainPage.py b/src/vai_lab/utils/plugins/MainPage.py index ad30fc60..ca78db04 100644 --- a/src/vai_lab/utils/plugins/MainPage.py +++ b/src/vai_lab/utils/plugins/MainPage.py @@ -6,7 +6,7 @@ import pandas as pd from vai_lab.utils.plugins.dataLoader import dataLoader -from vai_lab._import_helper import get_lib_parent_dir, rel_to_abs +from vai_lab._import_helper import get_lib_parent_dir, abs_to_rel _PLUGIN_READABLE_NAMES = {"main": "default", "main page": "alias", @@ -311,7 +311,7 @@ def start_dataloader(self): # Infers by default, should it be None? data[variable] = pd.read_csv(filename) isVar[i] = 1 - self.controller.xml_handler.append_input_data(variable, rel_to_abs(filename)) + self.controller.xml_handler.append_input_data(variable, abs_to_rel(filename)) if i == 0: self.controller.Data.set(True) if any(isVar[1::2]) and ( @@ -337,7 +337,7 @@ def upload_data_path(self): title='Select a folder', mustexist=True) if folder is not None and len(folder) > 0: - self.controller.xml_handler.append_input_data('X', rel_to_abs(folder)) + self.controller.xml_handler.append_input_data('X', abs_to_rel(folder)) def upload_data_folder(self): """ Stores the directory containing the data that will be later loaded