| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
""" Used to track events in tests. """ |
| 11 |
|
| 12 |
|
| 13 |
# Enthought library imports. |
| 14 |
from traits.api import HasTraits, List, Str, Tuple |
| 15 |
from traits.has_traits import observe |
| 16 |
|
| 17 |
|
| 18 |
class EventTracker(HasTraits): |
| 19 |
"""Used to track traits events.""" |
| 20 |
|
| 21 |
# The traits events that have fired. |
| 22 |
# |
| 23 |
# This is a list of tuples in the form:- |
| 24 |
# |
| 25 |
# (obj, trait_name, old, new) |
| 26 |
events = List(Tuple) |
| 27 |
|
| 28 |
# The names of the traits events that have fired. |
| 29 |
# |
| 30 |
# This is useful if you just care about the order of the events, not the |
| 31 |
# contents. |
| 32 |
event_names = List(Str) |
| 33 |
|
| 34 |
# The trait event subscriptions used by the tracker. |
| 35 |
# |
| 36 |
# This is a list of tuples in the form:- |
| 37 |
# |
| 38 |
# (obj, trait_name) |
| 39 |
# |
| 40 |
# Where 'obj' is the object to listen to, and 'trait_name' is the name of |
| 41 |
# the trait to listen to, or None to listen for all trait events. |
| 42 |
subscriptions = List(Tuple) |
| 43 |
|
| 44 |
########################################################################### |
| 45 |
# Private interface. |
| 46 |
########################################################################### |
| 47 |
|
| 48 |
#### Trait change handlers ################################################ |
| 49 |
|
| 50 |
@observe("subscriptions") |
| 51 |
def _update_listeners_on_all_subscriptions(self, event): |
| 52 |
"""Static trait change handler.""" |
| 53 |
old, new = event.old, event.new |
| 54 |
for subscription in old: |
| 55 |
self._remove_subscription(subscription) |
| 56 |
|
| 57 |
for subscription in new: |
| 58 |
self._add_subscription(subscription) |
| 59 |
|
| 60 |
@observe("subscriptions:items") |
| 61 |
def _update_listeners_on_changed_subscriptions(self, event): |
| 62 |
"""Static trait change handler.""" |
| 63 |
|
| 64 |
for subscription in event.removed: |
| 65 |
self._remove_subscription(subscription) |
| 66 |
|
| 67 |
for subscription in event.added: |
| 68 |
self._add_subscription(subscription) |
| 69 |
|
| 70 |
def _listener(self, obj, trait_name, old, new): |
| 71 |
"""Dynamic trait change listener.""" |
| 72 |
|
| 73 |
self.events.append((obj, trait_name, old, new)) |
| 74 |
self.event_names.append(trait_name) |
| 75 |
|
| 76 |
#### Methods ############################################################## |
| 77 |
|
| 78 |
def _add_subscription(self, subscription): |
| 79 |
"""Add a subscription.""" |
| 80 |
|
| 81 |
obj, trait_name = subscription |
| 82 |
|
| 83 |
if trait_name is not None: |
| 84 |
obj.on_trait_change(self._listener, trait_name) |
| 85 |
|
| 86 |
else: |
| 87 |
obj.on_trait_change(self._listener) |
| 88 |
|
| 89 |
def _remove_subscription(self, subscription): |
| 90 |
"""Remove a subscription.""" |
| 91 |
|
| 92 |
obj, trait_name = subscription |
| 93 |
|
| 94 |
if trait_name is not None: |
| 95 |
obj.on_trait_change(self._listener, trait_name, remove=True) |
| 96 |
|
| 97 |
else: |
| 98 |
obj.on_trait_change(self._listener, remove=True) |