traits_listener Module

Defines classes used to implement and manage various trait listener patterns.

traits.traits_listener.indent(text, first_line=True, n=1, width=4)[source]

Indent lines of text.

Parameters:
  • text (str) – The text to indent.
  • first_line (bool, optional) – If False, then the first line will not be indented (default: True).
  • n (int, optional) – The level of indentation (default: 1).
  • width (int, optional) – The number of spaces in each level of indentation (default: 4).
Returns:

indented (str)

traits.traits_listener.is_not_none(value)[source]
traits.traits_listener.is_none(value)[source]
traits.traits_listener.not_event(value)[source]
class traits.traits_listener.ListenerBase[source]

Bases: traits.has_traits.HasPrivateTraits

register(new)[source]

Registers new listeners.

unregister(old)[source]

Unregisters any existing listeners.

handle(object, name, old, new)[source]

Handles a trait change for a simple trait.

handle_list(object, name, old, new)[source]

Handles a trait change for a list trait.

handle_list_items(object, name, old, new)[source]

Handles a trait change for a list traits items.

handle_dict(object, name, old, new)[source]

Handles a trait change for a dictionary trait.

handle_dict_items(object, name, old, new)[source]

Handles a trait change for a dictionary traits items.

class traits.traits_listener.ListenerItem[source]

Bases: traits.traits_listener.ListenerBase

name = Str

The name of the trait to listen to:

metadata_name = Str

The name of any metadata that must be present (or not present):

metadata_defined = Bool( True )

Does the specified metadata need to be defined (True) or not defined (False)?

handler = Any

The handler to be called when any listened-to trait is changed:

wrapped_handler_ref = Any

A weakref ‘wrapped’ version of ‘handler’:

dispatch = Str

The dispatch mechanism to use when invoking the handler:

priority = Bool( False )

Does the handler go at the beginning (True) or end (False) of the notification handlers list?

next = Instance( ListenerBase )

The next level (if any) of ListenerBase object to be called when any of this object’s listened-to traits is changed:

type = Enum( ANY_LISTENER, SRC_LISTENER, DST_LISTENER )

The type of handler being used:

notify = Bool( True )

Should changes to this item generate a notification to the handler?

deferred = Bool( False )

Should registering listeners for items reachable from this listener item be deferred until the associated trait is first read or set?

is_any_trait = Bool( False )

Is this an ‘any_trait’ change listener, or does it create explicit listeners for each individual trait?

is_list_handler = Bool( False )

Is the associated handler a special list handler that handles both ‘foo’ and ‘foo_items’ events by receiving a list of ‘deleted’ and ‘added’ items as the ‘old’ and ‘new’ arguments?

active = Instance( WeakKeyDictionary, () )

A dictionary mapping objects to a list of all current active (name, type) listener pairs, where type defines the type of listener, one of: (SIMPLE_LISTENER, LIST_LISTENER, DICT_LISTENER).

register(new)[source]

Registers new listeners.

unregister(old)[source]

Unregisters any existing listeners.

handle_simple(object, name, old, new)[source]

Handles a trait change for an intermediate link trait.

handle_dst(object, name, old, new)[source]

Handles a trait change for an intermediate link trait when the notification is for the final destination trait.

handle_list(object, name, old, new)[source]

Handles a trait change for a list (or set) trait.

handle_list_items(object, name, old, new)[source]

Handles a trait change for items of a list (or set) trait.

handle_list_items_special(object, name, old, new)[source]

Handles a trait change for items of a list (or set) trait with notification.

handle_dict(object, name, old, new)[source]

Handles a trait change for a dictionary trait.

handle_dict_items(object, name, old, new)[source]

Handles a trait change for items of a dictionary trait.

handle_error(obj, name, old, new)[source]

Handles an invalid intermediate trait change to a handler that must be applied to the final destination object.trait.

class traits.traits_listener.ListenerGroup[source]

Bases: traits.traits_listener.ListenerBase

handler = Property

The handler to be called when any listened-to trait is changed

wrapped_handler_ref = Property

A weakref ‘wrapped’ version of ‘handler’:

dispatch = Property

The dispatch mechanism to use when invoking the handler:

priority = ListProperty

Does the handler go at the beginning (True) or end (False) of the notification handlers list?

next = ListProperty

The next level (if any) of ListenerBase object to be called when any of this object’s listened-to traits is changed

type = ListProperty

The type of handler being used:

notify = ListProperty

Should changes to this item generate a notification to the handler?

deferred = ListProperty

Should registering listeners for items reachable from this listener item be deferred until the associated trait is first read or set?

register(new)[source]

Registers new listeners.

unregister(old)[source]

Unregisters any existing listeners.

class traits.traits_listener.ListenerParser(text='', **traits)[source]

Bases: traits.has_traits.HasPrivateTraits

len_text = Int

The length of the string being parsed.

index = Int

The current parse index within the string

next = Property

The next character from the string being parsed

name = Property

The next Python attribute name within the string:

skip_ws = Property

The next non-whitespace character

backspace = Property

Backspaces to the last character processed

listener = Instance( ListenerBase )

The ListenerBase object resulting from parsing text

text = Str

The string being parsed

parse()[source]

Parses the text and returns the appropriate collection of ListenerBase objects described by the text.

parse_group(terminator=']')[source]

Parses the contents of a group.

parse_item(terminator)[source]

Parses a single, complete listener item or group string.

parse_metadata(item)[source]

Parses the metadata portion of a listener item.

error(msg)[source]

Raises a syntax error.

class traits.traits_listener.ListenerNotifyWrapper(handler, owner, id, listener, target=None)[source]

Bases: traits.trait_notifiers.TraitChangeNotifyWrapper

listener_deleted(ref)[source]
owner_deleted(ref)[source]
class traits.traits_listener.ListenerHandler(handler)[source]

Bases: object

listener_deleted(ref)[source]