From e21caf109fc3405afb36e86ab7f9ff9dbb8b0726 Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Fri, 10 Jun 2022 18:26:05 +0200 Subject: [PATCH] extraction can be called on whole settings --- openpype/tools/settings/settings/base.py | 110 +++++++++++------- .../tools/settings/settings/categories.py | 35 +++++- openpype/tools/settings/settings/constants.py | 13 +++ 3 files changed, 112 insertions(+), 46 deletions(-) diff --git a/openpype/tools/settings/settings/base.py b/openpype/tools/settings/settings/base.py index 3ade5e5ef8..aa2b448ccf 100644 --- a/openpype/tools/settings/settings/base.py +++ b/openpype/tools/settings/settings/base.py @@ -12,28 +12,71 @@ from openpype.tools.settings import CHILD_OFFSET from .widgets import ExpandingWidget from .lib import create_deffered_value_change_timer -from .constants import DEFAULT_PROJECT_LABEL +from .constants import ( + DEFAULT_PROJECT_LABEL, + SETTINGS_PATH_KEY, + ROOT_KEY, + VALUE_KEY, + SAVE_TIME_KEY, + PROJECT_NAME_KEY, +) _MENU_SEPARATOR_REQ = object() -SETTINGS_PATH_KEY = "__settings_path__" -ROOT_KEY = "__root_key__" -VALUE_KEY = "__value__" -SAVE_TIME_KEY = "__extracted__" -PROJECT_NAME_KEY = "__project_name__" + + +class ExtractHelper: + _last_save_dir = os.path.expanduser("~") + + @classmethod + def get_last_save_dir(cls): + return cls._last_save_dir + + @classmethod + def set_last_save_dir(cls, save_dir): + cls._last_save_dir = save_dir + + @classmethod + def ask_for_save_filepath(cls, parent): + dialog = QtWidgets.QFileDialog( + parent, + "Save settings values", + cls.get_last_save_dir(), + "Values (*.json)" + ) + # dialog.setOption(dialog.DontUseNativeDialog) + dialog.setAcceptMode(dialog.AcceptSave) + if dialog.exec() != dialog.Accepted: + return + + selected_urls = dialog.selectedUrls() + if not selected_urls: + return + + filepath = selected_urls[0].toLocalFile() + if not filepath: + return + + if not filepath.lower().endswith(".json"): + filepath += ".json" + return filepath + + @classmethod + def extract_settings_to_json(cls, filepath, settings_data, project_name): + now = datetime.datetime.now() + settings_data[SAVE_TIME_KEY] = now.strftime("%Y-%m-%d %H:%M:%S") + if project_name != 0: + settings_data[PROJECT_NAME_KEY] = project_name + + with open(filepath, "w") as stream: + json.dump(settings_data, stream, indent=4) + + new_dir = os.path.dirname(filepath) + cls.set_last_save_dir(new_dir) class BaseWidget(QtWidgets.QWidget): - _last_save_dir = os.path.expanduser("~") allow_actions = True - @staticmethod - def get_last_save_dir(): - return BaseWidget._last_save_dir - - @staticmethod - def set_last_save_dir(save_dir): - BaseWidget._last_save_dir = save_dir - def __init__(self, category_widget, entity, entity_widget): self.category_widget = category_widget self.entity = entity @@ -266,45 +309,24 @@ class BaseWidget(QtWidgets.QWidget): return [(action, copy_value)] def _extract_to_file(self): - dialog = QtWidgets.QFileDialog( - self, - "Save settings values", - self.get_last_save_dir(), - "Values (*.json)" - ) - # dialog.setOption(dialog.DontUseNativeDialog) - dialog.setAcceptMode(dialog.AcceptSave) - if dialog.exec() != dialog.Accepted: - return - - selected_urls = dialog.selectedUrls() - if not selected_urls: - return - - filepath = selected_urls[0].toLocalFile() + filepath = ExtractHelper.ask_for_save_filepath(self) if not filepath: return - if not filepath.lower().endswith(".json"): - filepath += ".json" - settings_data = self._get_mime_data_from_entity() - now = datetime.datetime.now() - settings_data[SAVE_TIME_KEY] = now.strftime("%Y-%m-%d %H:%M:%S") + project_name = 0 if hasattr(self.category_widget, "project_name"): - settings_data[PROJECT_NAME_KEY] = self.category_widget.project_name + project_name = self.category_widget.project_name - with open(filepath, "w") as stream: - json.dump(settings_data, stream, indent=4) - - new_dir = os.path.dirname(filepath) - self.set_last_save_dir(new_dir) + ExtractHelper.extract_settings_to_json( + filepath, settings_data, project_name + ) def _extract_value_to_file_actions(self, menu): - save_action = QtWidgets.QAction("Extract to file", menu) + extract_action = QtWidgets.QAction("Extract to file", menu) return [ _MENU_SEPARATOR_REQ, - (save_action, self._extract_to_file) + (extract_action, self._extract_to_file) ] def _parse_source_data_for_paste(self, data): diff --git a/openpype/tools/settings/settings/categories.py b/openpype/tools/settings/settings/categories.py index c8ade5fcdb..764f42f1a3 100644 --- a/openpype/tools/settings/settings/categories.py +++ b/openpype/tools/settings/settings/categories.py @@ -45,8 +45,15 @@ from .breadcrumbs_widget import ( SystemSettingsBreadcrumbs, ProjectSettingsBreadcrumbs ) - -from .base import GUIWidget +from .constants import ( + SETTINGS_PATH_KEY, + ROOT_KEY, + VALUE_KEY, +) +from .base import ( + ExtractHelper, + GUIWidget, +) from .list_item_widget import ListWidget from .list_strict_widget import ListStrictWidget from .dict_mutable_widget import DictMutableKeysWidget @@ -627,11 +634,35 @@ class SettingsCategoryWidget(QtWidgets.QWidget): self._on_context_version_trigger ) submenu.addAction(action) + menu.addMenu(submenu) + extract_action = QtWidgets.QAction("Extract to file", menu) + extract_action.triggered.connect(self._on_extract_to_file) + + menu.addAction(extract_action) + def _on_context_version_trigger(self, version): self._on_source_version_change(version) + def _on_extract_to_file(self): + filepath = ExtractHelper.ask_for_save_filepath(self) + if not filepath: + return + + settings_data = { + SETTINGS_PATH_KEY: self.entity.root_key, + ROOT_KEY: self.entity.root_key, + VALUE_KEY: self.entity.value + } + project_name = 0 + if hasattr(self, "project_name"): + project_name = self.project_name + + ExtractHelper.extract_settings_to_json( + filepath, settings_data, project_name + ) + def _on_reset_crash(self): self.save_btn.setEnabled(False) diff --git a/openpype/tools/settings/settings/constants.py b/openpype/tools/settings/settings/constants.py index 9d6d7904d7..d98d18c8bf 100644 --- a/openpype/tools/settings/settings/constants.py +++ b/openpype/tools/settings/settings/constants.py @@ -7,6 +7,12 @@ PROJECT_IS_ACTIVE_ROLE = QtCore.Qt.UserRole + 2 PROJECT_IS_SELECTED_ROLE = QtCore.Qt.UserRole + 3 PROJECT_VERSION_ROLE = QtCore.Qt.UserRole + 4 +# Save/Extract keys +SETTINGS_PATH_KEY = "__settings_path__" +ROOT_KEY = "__root_key__" +VALUE_KEY = "__value__" +SAVE_TIME_KEY = "__extracted__" +PROJECT_NAME_KEY = "__project_name__" __all__ = ( "DEFAULT_PROJECT_LABEL", @@ -15,4 +21,11 @@ __all__ = ( "PROJECT_IS_ACTIVE_ROLE", "PROJECT_IS_SELECTED_ROLE", "PROJECT_VERSION_ROLE", + + "SETTINGS_PATH_KEY", + "ROOT_KEY", + "SETTINGS_PATH_KEY", + "VALUE_KEY", + "SAVE_TIME_KEY", + "PROJECT_NAME_KEY", )