diff --git a/release/scripts/mgear/anim_picker/handlers/file_handlers.py b/release/scripts/mgear/anim_picker/handlers/file_handlers.py index be8de55b..9a3485bf 100644 --- a/release/scripts/mgear/anim_picker/handlers/file_handlers.py +++ b/release/scripts/mgear/anim_picker/handlers/file_handlers.py @@ -9,6 +9,7 @@ import functools from mgear.core import pyqt +from mgear.core import string from mgear.vendor.Qt import QtWidgets from mgear.anim_picker.widgets import basic @@ -35,47 +36,60 @@ def _exportData(data, file_path): print(e) -def _convert_path_token(file_path): - """convert ANIM_PICKER_PATH env var to a full path +def replace_token_with_path(file_path): + """ + Replaces the token [ANIM_PICKER_PATH] in the string with the value of + the environment variable ANIM_PICKER_PATH. Args: - file_path (str): file path + file_path (str): String containing the token to be replaced. Returns: - str: file path with updated token to path + str: The string with the token replaced by the full path. + """ - path_token = "[{}]".format(ANIM_PICKER_VAR) - if file_path and path_token in file_path: - if os.environ.get(ANIM_PICKER_VAR, ""): - file_path = file_path.replace( - path_token, os.environ.get(ANIM_PICKER_VAR, "") - ) - return file_path + env_path = os.getenv(ANIM_PICKER_VAR) + + if not env_path: + return file_path + return file_path.replace( + f"[{ANIM_PICKER_VAR}]", string.normalize_path(env_path) + ) -def convert_path_token(f): - @functools.wraps(f) - def wrap(*args, **kwargs): - if args: - list(args)[0] = _convert_path_token(args[0]) - else: - kwargs["file_path"] = _convert_path_token(kwargs["file_path"]) - x = f(*args, **kwargs) - return x - return wrap +def replace_path_with_token(file_path): + """ + Replaces the full path in the string with the token [ANIM_PICKER_PATH], + if the path starts with the value of the environment variable + ANIM_PICKER_PATH. + + Args: + file_path (str): String containing the full path to be replaced. + + Returns: + str: The string with the full path replaced by the token. + + """ + env_path = os.getenv(ANIM_PICKER_VAR) + + if env_path and file_path.startswith(env_path): + return file_path.replace( + string.normalize_path(env_path), f"[{ANIM_PICKER_VAR}]" + ) + + return file_path -@convert_path_token def read_data_file(file_path): """Read data from file""" + file_path = replace_token_with_path(file_path) msg = "{} does not seem to be a file".format(file_path) assert os.path.isfile(file_path), msg pkr_data = _importData(file_path) or {} return pkr_data -@convert_path_token def write_data_file(file_path, data={}, force=False): """Write data to file @@ -85,6 +99,7 @@ def write_data_file(file_path, data={}, force=False): f (bool): force write mode, if false, will ask for confirmation when overwriting existing files """ + file_path = replace_token_with_path(file_path) # Ask for confirmation on existing file if os.path.exists(file_path) and not force: diff --git a/release/scripts/mgear/anim_picker/picker_node.py b/release/scripts/mgear/anim_picker/picker_node.py index 0fb87c7e..351ab2f3 100644 --- a/release/scripts/mgear/anim_picker/picker_node.py +++ b/release/scripts/mgear/anim_picker/picker_node.py @@ -227,7 +227,7 @@ def write_data( # Write data to file if to_file: - file_handlers.write_data_file(file_path=file_path, data=data) + file_handlers.write_data_file(file_path, data=data) self._set_str_attr(self.__FILE_ATTR__, value=file_path) # Write data to node attribute @@ -251,7 +251,9 @@ def read_data_from_file(self): file_path = self.get_file_path() if not file_path: return - if not os.path.exists(file_path): + if not os.path.exists( + file_handlers.replace_token_with_path(file_path) + ): return return file_handlers.read_data_file(file_path) diff --git a/release/scripts/mgear/anim_picker/widgets/overlay_widgets.py b/release/scripts/mgear/anim_picker/widgets/overlay_widgets.py index 8590a272..9fe76e79 100644 --- a/release/scripts/mgear/anim_picker/widgets/overlay_widgets.py +++ b/release/scripts/mgear/anim_picker/widgets/overlay_widgets.py @@ -13,6 +13,7 @@ from mgear.vendor.Qt import QtGui from mgear.vendor.Qt import QtCore from mgear.vendor.Qt import QtWidgets +from mgear.core import string # debugging # from PySide2 import QtGui @@ -176,12 +177,19 @@ def select_file_event(self): return global _LAST_USED_DIRECTORY _LAST_USED_DIRECTORY = os.path.dirname(file_path) - self.file_path_le.setText(file_path) + self.file_path_le.setText( + file_handlers.replace_path_with_token(file_path) + ) def select_file_dialog(self): """Get file dialog window starting in default folder""" picker_msg = "Picker Datas (*.pkr)" - path_module = _LAST_USED_DIRECTORY or basic.get_module_path() + if os.environ.get(file_handlers.ANIM_PICKER_VAR, ""): + path_module = string.normalize_path( + os.environ.get(file_handlers.ANIM_PICKER_VAR, "") + ) + else: + path_module = _LAST_USED_DIRECTORY or basic.get_module_path() file_path = QtWidgets.QFileDialog.getSaveFileName( self, "Choose file", path_module, picker_msg ) @@ -198,6 +206,7 @@ def select_file_dialog(self): def _get_file_path(self): """Return line edit file path""" file_path = self.file_path_le.text() + if file_path: return str(file_path) return None