pywry.callbacks¶
Callback registry for handling widget events.
Callback Types¶
pywry.callbacks.CallbackFunc
module-attribute
¶
pywry.callbacks.WidgetType
¶
Bases: str, Enum
Widget types for event routing.
Callback Registry¶
pywry.callbacks.get_registry
¶
get_registry() -> CallbackRegistry
Get the global callback registry instance.
| RETURNS | DESCRIPTION |
|---|---|
CallbackRegistry
|
The callback registry singleton. |
pywry.callbacks.CallbackRegistry
¶
Registry for managing event callbacks.
Supports both sync and async callbacks with namespace:event-name pattern. Thread-safe singleton pattern.
Initialize the registry.
Attributes¶
_scoped_callbacks
instance-attribute
¶
_scoped_callbacks: dict[str, dict[str, list[tuple[str, str, CallbackFunc]]]] = {}
Functions¶
_matches
staticmethod
¶
Check if a pattern matches a source string.
Supports wildcards (*) for flexible matching.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
Pattern to match (can contain * wildcards).
TYPE:
|
source
|
Source string to match against.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if pattern matches source. |
Examples:
register
¶
register(label: str, event_type: str, handler: CallbackFunc, widget_type: str = '*', widget_id: str = '*') -> bool
Register an event handler.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
event_type
|
The event type (namespace:event-name or * for wildcard).
TYPE:
|
handler
|
The callback function.
TYPE:
|
widget_type
|
Widget type filter ("grid", "chart", "toolbar", "html", "window", or "*").
TYPE:
|
widget_id
|
Widget ID filter (specific ID or "*" for all).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if registered successfully, False otherwise. |
unregister
¶
unregister(label: str, event_type: str | None = None, handler: CallbackFunc | None = None) -> bool
Unregister event handler(s).
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
event_type
|
The event type (None to unregister all for this label).
TYPE:
|
handler
|
Specific handler to remove (None to remove all for event_type).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if any handlers were removed, False otherwise. |
_collect_simple_handlers
¶
_collect_simple_handlers(label: str, event_type: str) -> list[CallbackFunc]
Collect handlers from simple (non-scoped) callback structure.
_collect_scoped_handlers
¶
_collect_scoped_handlers(label: str, event_type: str, widget_type: str, widget_id: str) -> list[CallbackFunc]
Collect handlers from scoped callback structure with pattern matching.
_invoke_handler
¶
_invoke_handler(handler: CallbackFunc, data: Any, event_type: str, label: str) -> bool
Invoke a single handler with appropriate arguments.
Sync handlers are executed directly on the current thread. Async handlers are scheduled via the BlockingPortal for proper async runtime integration.
_invoke_async_handler
¶
_invoke_async_handler(handler: CallbackFunc, data: Any, event_type: str, label: str, num_params: int) -> bool
Invoke an async handler via the BlockingPortal.
Uses portal.start_task_soon() to schedule the coroutine without blocking the current thread. Errors are logged via log_callback_error.
dispatch
¶
Dispatch an event to registered handlers.
Matches handlers based on event type and optional widget_type/widget_id from the event data. Scoped handlers are matched using wildcard patterns.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
event_type
|
The event type.
TYPE:
|
data
|
The event data. May contain widget_type and widget_id/gridId/chartId.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if any handlers were called, False otherwise. |
destroy
¶
Completely destroy all resources for a window label.
This removes all callbacks and marks the label as destroyed to prevent future registrations.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label to destroy.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the label was destroyed, False if it didn't exist. |
recover_label
¶
Recover a destroyed label to allow new registrations.
This is useful when restarting the application or reusing a label that was previously destroyed.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label to recover.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the label was recovered (was in destroyed set). |
is_destroyed
¶
Check if a window label has been destroyed.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if destroyed, False otherwise. |
has_handlers
¶
Check if a window has any registered handlers.
| PARAMETER | DESCRIPTION |
|---|---|
label
|
The window label.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if handlers exist, False otherwise. |
get_labels
¶
Get all window labels with registered handlers.
| RETURNS | DESCRIPTION |
|---|---|
List of window labels.
|
|
clear
¶
Clear all callbacks and destroyed labels.
Use with caution - primarily for testing.