Skip to content

Commit

Permalink
add option --defer-code-loading
Browse files Browse the repository at this point in the history
allows to defer loading of component source codes; workaround for #175
  • Loading branch information
leon-thomm committed Dec 2, 2023
1 parent 43c0afa commit 7d72013
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 17 deletions.
66 changes: 58 additions & 8 deletions ryven-editor/ryven/gui/code_editor/CodePreviewWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,46 @@
from typing import Type, Optional

from qtpy.QtCore import Qt
from qtpy.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout, QRadioButton, QLabel, QCheckBox, QGridLayout, \
from qtpy.QtWidgets import (
QWidget,
QHBoxLayout,
QVBoxLayout,
QRadioButton,
QLabel,
QCheckBox,
QGridLayout,
QPushButton
)
from ryvencore import Node

from ryven.gui.code_editor.EditSrcCodeInfoDialog import EditSrcCodeInfoDialog
from ryven.gui.code_editor.CodeEditorWidget import CodeEditorWidget
from ryven.gui.code_editor.SourceCodeUpdater import SrcCodeUpdater
from ryven.gui.code_editor.codes_storage import class_codes, mod_codes, modif_codes, NodeTypeCodes, \
Inspectable, NodeInspectable, MainWidgetInspectable, CustomInputWidgetInspectable
from ryven.gui.code_editor.codes_storage import (
class_codes,
mod_codes,
modif_codes,
NodeTypeCodes,
Inspectable,
NodeInspectable,
MainWidgetInspectable,
CustomInputWidgetInspectable,
load_src_code,
)


class LoadSrcCodeButton(QPushButton):
def __init__(self):
super().__init__('load source code')
self._node = None

@property
def node(self):
return self._node

@node.setter
def node(self, node):
self._node = node


class LinkedRadioButton(QRadioButton):
Expand Down Expand Up @@ -40,6 +71,13 @@ def setup_ui(self):

secondary_layout = QHBoxLayout()

# load source code button
self.load_code_button = LoadSrcCodeButton()
self.load_code_button.setProperty('class', 'small_button')
secondary_layout.addWidget(self.load_code_button)
self.load_code_button.hide()
self.load_code_button.clicked.connect(self._load_code_button_clicked)

# class radio buttons widget
self.class_selection_layout = QHBoxLayout() # QGridLayout()

Expand Down Expand Up @@ -96,18 +134,23 @@ def _set_node(self, node: Optional[Node]):
self.reset_code_button.setEnabled(False)
self.text_edit.set_code('')
self._clear_class_layout()
return
else:
if class_codes.get(node.__class__) is None:
# source code not loaded yet
self.load_code_button.node = node
self.load_code_button.show()
self._clear_class_layout()
self._update_code(NodeInspectable(node, 'source not loaded'))
else:
self._process_node_src(node)

def _process_node_src(self, node: Node):
self._rebuild_class_selection(node)

code = class_codes[node.__class__].node_cls

if self.edits_enabled and node in modif_codes:
code = modif_codes[node]

self._update_code(NodeInspectable(node, code))


def _update_code(self, insp: Inspectable):
if self.edits_enabled:
self._disable_editing()
Expand All @@ -122,6 +165,7 @@ def _update_code(self, insp: Inspectable):


def _rebuild_class_selection(self, node: Node):
self.load_code_button.hide()
self._clear_class_layout()
self.radio_buttons.clear()

Expand Down Expand Up @@ -168,6 +212,12 @@ def _clear_class_layout(self):
widget = item.widget()
widget.hide()
self.class_selection_layout.removeItem(item)

def _load_code_button_clicked(self):
node: Node = self.sender().node
load_src_code(node.__class__)
self.load_code_button.hide()
self._process_node_src(node)

def _update_radio_buttons_edit_status(self):
"""Draws radio buttons referring to modified objects bold."""
Expand Down
10 changes: 9 additions & 1 deletion ryven-editor/ryven/gui/code_editor/codes_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,17 @@

def register_node_type(n: Type[Node]):
"""
Inspects and stores source code of a node type.
Registers a node type and loads its source code directly if deferred
source code loading is disabled.
"""

if not instance.defer_code_loading:
load_src_code(n)
else:
class_codes[n] = None


def load_src_code(n: Type[Node]):
has_gui = hasattr(n, 'GUI') # check if node type has custom gui
has_mw = has_gui and n.GUI.main_widget_class is not None

Expand Down
10 changes: 10 additions & 0 deletions ryven-editor/ryven/main/args_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ def parse_sys_args(just_defaults=False) -> Config:
• When enabled, Ryven might consume much more memory than usual
''')

parser.add_argument(
'--defer-code-loading',
action='store_true',
dest='defer_code_loading',
help=f'''
• When using deferred code loading, Ryven will load the source code of\\
nodes only once the user wants to inspect it.\\
• Deferred code loading decreases package loading time.\\
''')

# Project configuration

group = parser.add_argument_group('project configuration')
Expand Down
1 change: 1 addition & 0 deletions ryven-editor/ryven/main/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Config:
window_title: str = 'Ryven'
qt_api: str = 'pyside2'
src_code_edits_enabled: bool = False
defer_code_loading: bool = False

@staticmethod
def get_available_window_themes() -> Set[str]:
Expand Down
8 changes: 0 additions & 8 deletions ryven-editor/ryven/main/packages/gui_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class GuiClassesRegistry:
"""

exported_guis = []
exported_guis_sources: [[str]] = []


class GuiClassesContainer:
Expand All @@ -43,10 +42,6 @@ def export_guis(guis: [Type[NodeGUI]]):
setattr(gcc, w.__name__, w)
GuiClassesRegistry.exported_guis.append(gcc)

# get sources
gui_sources = [inspect.getsource(g) for g in guis]
GuiClassesRegistry.exported_guis_sources.append(gui_sources)


def node_gui(node_cls: Type[Node]):
"""
Expand All @@ -64,9 +59,6 @@ def register_gui(gui_cls: Type[NodeGUI]):
node_cls.GUI = gui_cls
__explicit_nodes.add(node_cls)
InfoMsgs.write(f"Registered node gui: {node_cls} for {gui_cls}")
# legacy
GuiClassesRegistry.exported_guis.append(gui_cls)
GuiClassesRegistry.exported_guis_sources.append(inspect.getsource(gui_cls))
return gui_cls

return register_gui

0 comments on commit 7d72013

Please sign in to comment.