Skip to content

Commit

Permalink
MNT: refactor to use pcdsutils.qt.callbacks.WeakPartialMethodSlot ins…
Browse files Browse the repository at this point in the history
…tead of vendoring
  • Loading branch information
tangkong committed Oct 19, 2023
1 parent aa4e4f3 commit 6831864
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 92 deletions.
4 changes: 2 additions & 2 deletions atef/widgets/config/find_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import happi
import qtawesome as qta
from apischema import ValidationError, serialize
from pcdsutils.qt.callbacks import WeakPartialMethodSlot
from qtpy import QtCore, QtWidgets

from atef.cache import DataCache, get_signal_cache
Expand All @@ -26,8 +27,7 @@
from atef.util import get_happi_client
from atef.widgets.config.utils import TableWidgetWithAddRow
from atef.widgets.core import DesignerDisplay
from atef.widgets.utils import (BusyCursorThread, WeakPartialMethodSlot,
insert_widget)
from atef.widgets.utils import BusyCursorThread, insert_widget

if TYPE_CHECKING:
from .window import Window
Expand Down
2 changes: 1 addition & 1 deletion atef/widgets/config/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import numpy as np
import qtawesome as qta
from ophyd import EpicsSignal, EpicsSignalRO
from pcdsutils.qt.callbacks import WeakPartialMethodSlot
from qtpy import QtCore, QtWidgets
from qtpy.QtCore import (QPoint, QPointF, QRect, QRectF, QRegularExpression,
QSize, Qt, QTimer)
Expand Down Expand Up @@ -45,7 +46,6 @@
from atef.widgets.happi import HappiDeviceComponentWidget
from atef.widgets.ophyd import OphydAttributeData, OphydAttributeDataSummary
from atef.widgets.utils import (BusyCursorThread, PV_validator,
WeakPartialMethodSlot,
match_line_edit_text_width)

logger = logging.getLogger(__name__)
Expand Down
4 changes: 2 additions & 2 deletions atef/widgets/config/window.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import qtawesome
from apischema import ValidationError, deserialize, serialize
from pcdsutils.qt.callbacks import WeakPartialMethodSlot
from qtpy import QtWidgets
from qtpy.QtCore import Qt, QTimer
from qtpy.QtCore import Signal as QSignal
Expand All @@ -30,8 +31,7 @@
from atef.report import ActiveAtefReport, PassiveAtefReport
from atef.widgets.config.find_replace import (FillTemplatePage,
FindReplaceWidget)
from atef.widgets.utils import (WeakPartialMethodSlot, reset_cursor,
set_wait_cursor)
from atef.widgets.utils import reset_cursor, set_wait_cursor

from ..archive_viewer import get_archive_viewer
from ..core import DesignerDisplay
Expand Down
87 changes: 0 additions & 87 deletions atef/widgets/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
"""
Non-core utilities. Primarily dynamic styling tools.
"""
import weakref
from types import MethodType
from typing import ClassVar, Generator, Optional

from qtpy import QtCore, QtGui, QtWidgets
Expand Down Expand Up @@ -308,88 +306,3 @@ def on_toggle(self):

# self.setMinimumHeight(min_height)
self.updateGeometry()


class WeakPartialMethodSlot:
"""
A PyQt-compatible slot for a partial method.
This utility handles deleting the connection when the method class instance
gets garbage collected. This avoids cycles in the garbage collector
that would prevent the instance from being garbage collected prior to the
program exiting.
Parameters
----------
signal_owner : QtCore.QObject
The owner of the signal.
signal : QtCore.Signal
The signal instance itself.
method : instance method
The method slot to call when the signal fires.
*args :
Arguments to pass to the method.
**kwargs :
Keyword arguments to pass to the method.
"""
def __init__(
self,
signal_owner: QtCore.QObject,
signal: QtCore.Signal,
method: MethodType,
*args,
**kwargs
):
self.signal = signal
self.signal.connect(self._call, QtCore.Qt.QueuedConnection)
self.method = weakref.WeakMethod(method)
self._method_finalizer = weakref.finalize(
method.__self__, self._method_destroyed
)
self._signal_finalizer = weakref.finalize(
signal_owner, self._signal_destroyed
)
self.partial_args = args
self.partial_kwargs = kwargs

def _signal_destroyed(self):
"""Callback: the owner of the signal was destroyed; clean up."""
if self.signal is None:
return

self.method = None
self.partial_args = []
self.partial_kwargs = {}
self.signal = None

def _method_destroyed(self):
"""Callback: the owner of the method was destroyed; clean up."""
if self.signal is None:
return

self.method = None
self.partial_args = []
self.partial_kwargs = {}
try:
self.signal.disconnect(self._call)
except Exception:
...
self.signal = None

def _call(self, *new_args, **new_kwargs):
"""
PyQt callback slot which handles the internal WeakMethod.
This method currently throws away arguments passed in from the signal.
This is for backward-compatibility to how the previous
`partial()`-using implementation worked.
If reused beyond the TyphosSuite, this class may need revisiting in the
future.
"""
method = self.method()
if method is None:
self._method_destroyed()
return
return method(*self.partial_args, *new_args,
**{**self.partial_kwargs, **new_kwargs})
1 change: 1 addition & 0 deletions conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ requirements:
- ipython
- numpy
- ophyd
- pcdsutils >=0.14.0
- pydm
- pyyaml
- qtpy
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ happi
ipython
numpy
ophyd
pcdsutils>=0.14.0
pydm
PyQt5
pyyaml
Expand Down

0 comments on commit 6831864

Please sign in to comment.