-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Monkey patch GUI elements #151
Merged
Merged
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
db2f84d
Monkey patch GUI elements
niklassiemer 40641cd
Update pyiron_gui/monkey_patching.py
niklassiemer bda62d8
Secure reload of monkey patch
niklassiemer bc317da
Format black
pyiron-runner 71a0e42
monkey patch order: specific first
niklassiemer e0cbb51
Merge branch 'master' into monkey
niklassiemer 01fae61
Enable testing of _update_project, again.
niklassiemer 8b94998
Secure and test monkey patching
niklassiemer d46571c
Format black
pyiron-runner 1c00559
Apply suggestions from code review
niklassiemer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# coding: utf-8 | ||
# Copyright (c) Max-Planck-Institut für Eisenforschung GmbH - Computational Materials Design (CM) Department | ||
# Distributed under the terms of "New BSD License", see the LICENSE file. | ||
|
||
"""Adding gui functionality to pyiron classes via monkey patching on import of pyiron_gui. """ | ||
import warnings | ||
|
||
from pyiron_gui.project.project_browser import ( | ||
ProjectBrowser, | ||
HasGroupsBrowser, | ||
DataContainerGUI, | ||
) | ||
from pyiron_base.interfaces.has_groups import HasGroups | ||
from pyiron_base import Project | ||
from pyiron_base import DataContainer | ||
|
||
__author__ = "Niklas Siemer" | ||
__copyright__ = ( | ||
"Copyright 2021, Max-Planck-Institut für Eisenforschung GmbH - " | ||
"Computational Materials Design (CM) Department" | ||
) | ||
__version__ = "0.1" | ||
__maintainer__ = "Niklas Siemer" | ||
__email__ = "[email protected]" | ||
__status__ = "development" | ||
__date__ = "July 06, 2022" | ||
|
||
|
||
def safe_monkey_patch(cls, attr_name, value): | ||
if hasattr(cls, attr_name): | ||
warnings.warn( | ||
f"Class {cls.__name__} already has attribute {attr_name} - Aborting monkey path of gui elements." | ||
) | ||
else: | ||
setattr(cls, attr_name, value) | ||
niklassiemer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def _has_groups_gui(self, box=None, refresh=False): | ||
if self._has_groups_browser is None or refresh: | ||
self._has_groups_browser = HasGroupsBrowser(self, box=box) | ||
return self._has_groups_browser | ||
|
||
|
||
safe_monkey_patch(HasGroups, "_has_groups_browser", None) | ||
safe_monkey_patch(HasGroups, "gui", _has_groups_gui) | ||
|
||
|
||
def _datacontainer_gui(self, box=None, refresh=False): | ||
if self._datacontainer_gui is None or refresh: | ||
self._datacontainer_gui = DataContainerGUI(self, box=box) | ||
return self._datacontainer_browser | ||
|
||
|
||
safe_monkey_patch(DataContainer, "_datacontainer_gui", _datacontainer_gui) | ||
niklassiemer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
safe_monkey_patch(DataContainer, "gui", _datacontainer_gui) | ||
|
||
|
||
def _pyiron_base_project_browser(self): | ||
if self._project_browser is None: | ||
self._project_browser = ProjectBrowser( | ||
project=self, show_files=False, Vbox=None | ||
) | ||
return self._project_browser | ||
|
||
|
||
safe_monkey_patch(Project, "_project_browser", None) | ||
safe_monkey_patch(Project, "browser", property(_pyiron_base_project_browser)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might break in a similar way under reloading module has @jan-janssen pointed out in pyiron_base#808. I discussed a potential fix there also.