Skip to content

Commit

Permalink
anim_picker: Reading data file which uses ANIM_PICKER_PATH token is n…
Browse files Browse the repository at this point in the history
…ot working Fixes #210
  • Loading branch information
miquelcampos committed Dec 12, 2024
1 parent 42dda57 commit 6949050
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
61 changes: 38 additions & 23 deletions release/scripts/mgear/anim_picker/handlers/file_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down
6 changes: 4 additions & 2 deletions release/scripts/mgear/anim_picker/picker_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
13 changes: 11 additions & 2 deletions release/scripts/mgear/anim_picker/widgets/overlay_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand All @@ -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
Expand Down

0 comments on commit 6949050

Please sign in to comment.