-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathutils.py
More file actions
46 lines (34 loc) · 1.34 KB
/
utils.py
File metadata and controls
46 lines (34 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from contextlib import contextmanager
from typing import Callable, Iterable
from ._base import Graphic
@contextmanager
def pause_events(*graphics: Graphic, event_handlers: Iterable[Callable] = None):
"""
Context manager for pausing Graphic events.
Optionally pass in only specific event handlers which are blocked. Other events for the graphic will not be blocked.
Examples
--------
.. code-block::
# pass in any number of graphics
with fpl.pause_events(graphic1, graphic2, graphic3):
# enter context manager
# all events are blocked from graphic1, graphic2, graphic3
# context manager exited, event states restored.
"""
if not all([isinstance(g, Graphic) for g in graphics]):
raise TypeError(
f"`pause_events` only takes Graphic instances as arguments, "
f"you have passed the following types:\n{[type(g) for g in graphics]}"
)
original_vals = [g.block_events for g in graphics]
for g in graphics:
if event_handlers is not None:
g.block_handlers.extend([e for e in event_handlers])
else:
g.block_events = True
yield
for g, value in zip(graphics, original_vals):
if event_handlers is not None:
g.block_handlers.clear()
else:
g.block_events = value