mirror of
https://github.com/ynput/ayon-core.git
synced 2026-01-01 16:34:53 +01:00
extraction can be called on whole settings
This commit is contained in:
parent
c77f9ce2e5
commit
e21caf109f
3 changed files with 112 additions and 46 deletions
|
|
@ -12,28 +12,71 @@ from openpype.tools.settings import CHILD_OFFSET
|
||||||
|
|
||||||
from .widgets import ExpandingWidget
|
from .widgets import ExpandingWidget
|
||||||
from .lib import create_deffered_value_change_timer
|
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()
|
_MENU_SEPARATOR_REQ = object()
|
||||||
SETTINGS_PATH_KEY = "__settings_path__"
|
|
||||||
ROOT_KEY = "__root_key__"
|
|
||||||
VALUE_KEY = "__value__"
|
class ExtractHelper:
|
||||||
SAVE_TIME_KEY = "__extracted__"
|
_last_save_dir = os.path.expanduser("~")
|
||||||
PROJECT_NAME_KEY = "__project_name__"
|
|
||||||
|
@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):
|
class BaseWidget(QtWidgets.QWidget):
|
||||||
_last_save_dir = os.path.expanduser("~")
|
|
||||||
allow_actions = True
|
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):
|
def __init__(self, category_widget, entity, entity_widget):
|
||||||
self.category_widget = category_widget
|
self.category_widget = category_widget
|
||||||
self.entity = entity
|
self.entity = entity
|
||||||
|
|
@ -266,45 +309,24 @@ class BaseWidget(QtWidgets.QWidget):
|
||||||
return [(action, copy_value)]
|
return [(action, copy_value)]
|
||||||
|
|
||||||
def _extract_to_file(self):
|
def _extract_to_file(self):
|
||||||
dialog = QtWidgets.QFileDialog(
|
filepath = ExtractHelper.ask_for_save_filepath(self)
|
||||||
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()
|
|
||||||
if not filepath:
|
if not filepath:
|
||||||
return
|
return
|
||||||
|
|
||||||
if not filepath.lower().endswith(".json"):
|
|
||||||
filepath += ".json"
|
|
||||||
|
|
||||||
settings_data = self._get_mime_data_from_entity()
|
settings_data = self._get_mime_data_from_entity()
|
||||||
now = datetime.datetime.now()
|
project_name = 0
|
||||||
settings_data[SAVE_TIME_KEY] = now.strftime("%Y-%m-%d %H:%M:%S")
|
|
||||||
if hasattr(self.category_widget, "project_name"):
|
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:
|
ExtractHelper.extract_settings_to_json(
|
||||||
json.dump(settings_data, stream, indent=4)
|
filepath, settings_data, project_name
|
||||||
|
)
|
||||||
new_dir = os.path.dirname(filepath)
|
|
||||||
self.set_last_save_dir(new_dir)
|
|
||||||
|
|
||||||
def _extract_value_to_file_actions(self, menu):
|
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 [
|
return [
|
||||||
_MENU_SEPARATOR_REQ,
|
_MENU_SEPARATOR_REQ,
|
||||||
(save_action, self._extract_to_file)
|
(extract_action, self._extract_to_file)
|
||||||
]
|
]
|
||||||
|
|
||||||
def _parse_source_data_for_paste(self, data):
|
def _parse_source_data_for_paste(self, data):
|
||||||
|
|
|
||||||
|
|
@ -45,8 +45,15 @@ from .breadcrumbs_widget import (
|
||||||
SystemSettingsBreadcrumbs,
|
SystemSettingsBreadcrumbs,
|
||||||
ProjectSettingsBreadcrumbs
|
ProjectSettingsBreadcrumbs
|
||||||
)
|
)
|
||||||
|
from .constants import (
|
||||||
from .base import GUIWidget
|
SETTINGS_PATH_KEY,
|
||||||
|
ROOT_KEY,
|
||||||
|
VALUE_KEY,
|
||||||
|
)
|
||||||
|
from .base import (
|
||||||
|
ExtractHelper,
|
||||||
|
GUIWidget,
|
||||||
|
)
|
||||||
from .list_item_widget import ListWidget
|
from .list_item_widget import ListWidget
|
||||||
from .list_strict_widget import ListStrictWidget
|
from .list_strict_widget import ListStrictWidget
|
||||||
from .dict_mutable_widget import DictMutableKeysWidget
|
from .dict_mutable_widget import DictMutableKeysWidget
|
||||||
|
|
@ -627,11 +634,35 @@ class SettingsCategoryWidget(QtWidgets.QWidget):
|
||||||
self._on_context_version_trigger
|
self._on_context_version_trigger
|
||||||
)
|
)
|
||||||
submenu.addAction(action)
|
submenu.addAction(action)
|
||||||
|
|
||||||
menu.addMenu(submenu)
|
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):
|
def _on_context_version_trigger(self, version):
|
||||||
self._on_source_version_change(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):
|
def _on_reset_crash(self):
|
||||||
self.save_btn.setEnabled(False)
|
self.save_btn.setEnabled(False)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,12 @@ PROJECT_IS_ACTIVE_ROLE = QtCore.Qt.UserRole + 2
|
||||||
PROJECT_IS_SELECTED_ROLE = QtCore.Qt.UserRole + 3
|
PROJECT_IS_SELECTED_ROLE = QtCore.Qt.UserRole + 3
|
||||||
PROJECT_VERSION_ROLE = QtCore.Qt.UserRole + 4
|
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__ = (
|
__all__ = (
|
||||||
"DEFAULT_PROJECT_LABEL",
|
"DEFAULT_PROJECT_LABEL",
|
||||||
|
|
@ -15,4 +21,11 @@ __all__ = (
|
||||||
"PROJECT_IS_ACTIVE_ROLE",
|
"PROJECT_IS_ACTIVE_ROLE",
|
||||||
"PROJECT_IS_SELECTED_ROLE",
|
"PROJECT_IS_SELECTED_ROLE",
|
||||||
"PROJECT_VERSION_ROLE",
|
"PROJECT_VERSION_ROLE",
|
||||||
|
|
||||||
|
"SETTINGS_PATH_KEY",
|
||||||
|
"ROOT_KEY",
|
||||||
|
"SETTINGS_PATH_KEY",
|
||||||
|
"VALUE_KEY",
|
||||||
|
"SAVE_TIME_KEY",
|
||||||
|
"PROJECT_NAME_KEY",
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue