Skip to content

Commit cba178f

Browse files
committed
Docs for Globals, handle_close update
handle_close no longer fires when the Puzzle is in debug mode (now consistent with documented behaviour)
1 parent d0f6f2b commit cba178f

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

puzzlepiece/param.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,14 @@ def _input_set_value(self, value):
213213
Set the value of the input box. This should be overriden to implement
214214
custom param display types.
215215
216+
As this is a low-level method used internally, it **should not** emit valueChanged
217+
signals for its input box. To stop this from happening, use Qt's `blockSignals`
218+
method::
219+
220+
self.input.blockSignals(True)
221+
self.input.setText(value)
222+
self.input.blockSignals(False)
223+
216224
:meta public:
217225
"""
218226
self.input.setText(self._format.format(value))

puzzlepiece/puzzle.py

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,38 @@ def __repr__(self):
3535

3636

3737
class Globals:
38+
"""
39+
A dictionary wrapper used for :attr:`puzzlepiece.puzzle.Puzzle.globals`. It behaves like
40+
a dictionary, allowing :class:`puzzlepiece.piece.Piece` objects to share device APIs
41+
with each other.
42+
43+
Additionally, :func:`~puzzlepiece.puzzle.Globals.require` and
44+
:func:`~puzzlepiece.puzzle.Globals.release` can be used to keep track of the Pieces
45+
using a given variable, so that the API can be loaded once and then unloaded once
46+
all the Pieces are done with it.
47+
"""
3848
def __init__(self):
3949
self._dict = {}
4050
self._counts = {}
4151

4252
def require(self, name):
53+
"""
54+
Register that a Piece is using the variable with a given name. This will increase
55+
an internal counter to indicate the Piece having a hold on the variable.
56+
57+
Returns `False` if this is the first time a variable is being registered (and thus
58+
setup is needed) or `True` if the variable has been registered already.
59+
60+
For example, this can be used within :func:`~puzzlepiece.piece.Piece.setup`::
61+
62+
def setup(self):
63+
if not self.puzzle.globals.require('sdk'):
64+
# Load the SDK if not done already by a different Piece
65+
self.puzzle.globals['sdk'] = self.load_sdk()
66+
67+
:param name: a dictionary key for the required variable
68+
:rtype: bool
69+
"""
4370
if name not in self._dict:
4471
self._dict[name] = None
4572
self._counts[name] = 1
@@ -49,6 +76,25 @@ def require(self, name):
4976
return True
5077

5178
def release(self, name):
79+
"""
80+
Indicate that a Piece is done using the variable with a given name.
81+
This will decrease an internal counter to indicate the Piece is releasing
82+
its hold on the variable.
83+
84+
Returns `False` if the counter is non-zero (so different Pieces are still using
85+
this variable) or `True` if all Pieces are done with the variable (in that case
86+
the SDK can be safely shut down for example).
87+
88+
For example, this can be used within :func:`~puzzlepiece.piece.Piece.handle_close`::
89+
90+
def handle_close(self):
91+
if self.puzzle.globals.release('sdk'):
92+
# Unload the SDK if all Pieces are done with it
93+
self.puzzle.globals['sdk'].stop()
94+
95+
:param name: a dictionary key for the variable being released
96+
:rtype: bool
97+
"""
5298
if name not in self._dict:
5399
raise KeyError(f"No global variable with id '{name}' to release")
54100
if name not in self._counts:
@@ -408,8 +454,9 @@ def closeEvent(self, event):
408454
"""
409455
self._shutdown_threads.emit()
410456

411-
for piece_name in self.pieces:
412-
self.pieces[piece_name].handle_close(event)
457+
if not self.debug:
458+
for piece_name in self.pieces:
459+
self.pieces[piece_name].handle_close(event)
413460
super().closeEvent(event)
414461

415462

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "puzzlepiece"
7-
version = "0.4.0"
7+
version = "0.5.0"
88
authors = [
99
{ name="Jakub Dranczewski", email="jakub.dranczewski@gmail.com" },
1010
]

0 commit comments

Comments
 (0)