Skip to content

Commit 4e09912

Browse files
committed
#51 Fixed saving and opening dpp sessions.
Enhanced PluginBuilder. Previously the list of config option classes (e.g. String, Integer, ...) needed to managed here as well. Now they are loaded dynamically.
1 parent 625a591 commit 4e09912

File tree

3 files changed

+48
-66
lines changed

3 files changed

+48
-66
lines changed

dpp/core/plugin/builder.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import dpp
2020
from dpp.core import plugin
2121
from dpp.core.plugin import AbstractPlugin, NullPlugin
22-
from dpp.core.plugin.config.options import Integer, String, Boolean, Group
2322

2423

2524
class PluginBuilder:
@@ -38,20 +37,15 @@ def _build_config(self, config):
3837
clazz = value.pop("clazz")
3938
value["label"] = plugin.config.Label(value["label"]["key"], value["label"]["name"])
4039
value.pop("is_initialized")
41-
result[name] = {
42-
"String": String,
43-
"Integer": Integer,
44-
"Boolean": Boolean,
45-
"Group": Group
46-
}.get(clazz)(**value)
40+
mod = __import__('dpp.core.plugins.config.options', fromlist=[clazz])
41+
result[name] = getattr(mod, clazz)(**value)
4742
except:
4843
raise Exception("Error while loading plugin configuration!")
4944

5045
return result
5146

5247
def build(self, config) -> AbstractPlugin:
5348
""" Returns a plugin as specified within configuration item. Returns a NullPlugin on error. """
54-
config = config["config"]
5549
try:
5650
plugin = self._context.getPluginByName(config["name"], config["type"])
5751
plugin.setup(self._build_config(config['config']))

dpp/ui/view/classic/classic_main_window_widget.py

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#
1818
import json
1919

20-
from qtpy.QtWidgets import QWidget
20+
from qtpy.QtWidgets import QFileDialog, QWidget
2121

2222
from dpp.core.plugin import PluginType
2323
from dpp.ui.view.classic import CodecTab
@@ -129,41 +129,47 @@ def _focus_input_text(self, callback):
129129
# Connector functions
130130
#############################################
131131

132-
def _open_file_action(self) -> str:
133-
filename = super()._open_file_action()
134-
if filename:
135-
try:
136-
with open(filename) as f:
137-
save_file = json.loads(f.read())
138-
for tab_config in save_file:
139-
index, tab = self.newTab(title=tab_config["name"])
140-
frame_index = 0
141-
for frame_config in tab_config["frames"]:
142-
if frame_index == 0:
143-
# New tabs already contain one empty frame
144-
frame = tab.frames().getFrames()[0]
145-
frame.setInputText(frame_config["text"])
146-
frame.setStatus(frame_config["status"]["type"], frame_config["status"]["message"])
147-
else:
148-
frame = tab.frames().newFrame(frame_config["text"],
149-
frame_config["title"],
150-
frame_index,
151-
frame_config["status"]["type"],
152-
frame_config["status"]["message"])
153-
frame.fromDict(frame_config)
154-
frame_index = frame_index + 1
155-
self._context.logger.info("Successfully loaded {}!".format(filename))
156-
except Exception as e:
157-
self._context.logger.error("Unexpected error loading file. {}".format(e))
158-
159-
def _save_as_file_action(self) -> str:
160-
filename = super()._save_as_file()
161-
if filename:
162-
try:
163-
self._context.saveAsFile(filename, str(json.dumps(self.toDict(), default=lambda x: x.__dict__)))
164-
self._context.logger.info("Successfully saved session in {}!".format(filename))
165-
except Exception as e:
166-
self._context.logger.error("Unexpected error saving file. {}".format(e))
132+
@menu.register_menu_item(id=MenuItem.OPEN_FILE, text="&Open File...", shortcut_key="Ctrl+O")
133+
def _open_file_action(self):
134+
filename, _ = QFileDialog.getOpenFileName(self, 'Open File')
135+
if not filename:
136+
return
137+
138+
try:
139+
with open(filename) as f:
140+
save_file = json.loads(f.read())
141+
for tab_config in save_file:
142+
index, tab = self.newTab(title=tab_config["name"])
143+
frame_index = 0
144+
for frame_config in tab_config["frames"]:
145+
if frame_index == 0:
146+
# New tabs already contain one empty frame
147+
frame = tab.frames().getFrames()[0]
148+
frame.setInputText(frame_config["text"])
149+
frame.setStatus(frame_config["status"]["type"], frame_config["status"]["message"])
150+
else:
151+
frame = tab.frames().newFrame(frame_config["text"],
152+
frame_config["title"],
153+
frame_index,
154+
frame_config["status"]["type"],
155+
frame_config["status"]["message"])
156+
frame.fromDict(frame_config)
157+
frame_index = frame_index + 1
158+
self._context.logger.info("Successfully loaded {}!".format(filename))
159+
except Exception as e:
160+
self._context.logger.error("Unexpected error loading file. {}".format(e))
161+
162+
@menu.register_menu_item(id=MenuItem.SAVE_AS_FILE, text="&Save As...", shortcut_key="Ctrl+S")
163+
def _save_as_file_action(self):
164+
filename, _ = QFileDialog.getSaveFileName(self, 'Save As File')
165+
if not filename:
166+
return
167+
168+
try:
169+
self._context.saveAsFile(filename, str(json.dumps(self.toDict(), default=lambda x: x.__dict__)))
170+
self._context.logger.info("Successfully saved session in {}!".format(filename))
171+
except Exception as e:
172+
self._context.logger.error("Unexpected error saving file. {}".format(e))
167173

168174
#############################################
169175
# Public functions

dpp/ui/view/main_window_widget.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,20 @@ def __init__(self, parent: QMainWindow, context: 'core.context.Context', input_t
4444
self._plugins = context.plugins()
4545
self._parent = parent
4646

47-
#############################################
48-
# Initialize docks
49-
#############################################
47+
# Initialize docks
5048
self._log_dock_widget = LogDock(self.docksWidget(), context.logger)
5149
self.docksWidget().registerDockWidget(Context.DockWidget.LOG_DOCK_WIDGET, self._log_dock_widget)
5250
self.docksWidget().registerDockWidget(Context.DockWidget.HEX_DOCK_WIDGET, HexDock(context, parent))
5351

54-
#############################################
55-
# Initialize status bar
56-
#############################################
52+
# Initialize status bar
5753
parent.statusBar().addWidget(self._log_dock_widget.logMessageWidget())
5854
parent.statusBar().addPermanentWidget(self._init_hidden_dialog())
5955

60-
#############################################
61-
# Initialize shortcuts
62-
#############################################
56+
# Initialize shortcuts
6357
self._menu_bar = MenuBar(self._parent.menuBar())
6458
self._init_menu_items()
6559

66-
#############################################
67-
# Initialize tabs
68-
#############################################
60+
# Initialize tabs
6961
self.tabsWidget().onTabAddButtonClick.connect(self.newTab)
7062
self.tabsWidget().onTabDuplicateButtonClick.connect(self.duplicateTab)
7163
self.newTab(input_text=input_text)
@@ -153,16 +145,6 @@ def _previous_tab_action(self):
153145
def _close_tab_action(self):
154146
self.tabsWidget().closeTab()
155147

156-
@menu.register_menu_item(id=MenuItem.OPEN_FILE, text="&Open File...", shortcut_key="Ctrl+O")
157-
def _open_file_action(self):
158-
filename, _ = QFileDialog.getOpenFileName(self, 'Open File')
159-
return filename
160-
161-
@menu.register_menu_item(id=MenuItem.SAVE_AS_FILE, text="&Save As...", shortcut_key="Ctrl+S")
162-
def _save_as_file_action(self):
163-
filename, _ = QFileDialog.getSaveFileName(self, 'Save As File')
164-
return filename
165-
166148
@menu.register_menu_item(id=MenuItem.SHOW_PLUGINS, text="&Plugins...", shortcut_key="Ctrl+Shift+P")
167149
def _show_plugins_dialog_action(self):
168150
self._show_hidden_dialog("Plugins")

0 commit comments

Comments
 (0)