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:
| Type | 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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Pattern to match (can contain * wildcards). |
required |
source
|
str
|
Source string to match against. |
required |
Returns:
| Type | 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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The window label. |
required |
event_type
|
str
|
The event type (namespace:event-name or * for wildcard). |
required |
handler
|
CallbackFunc
|
The callback function. |
required |
widget_type
|
str
|
Widget type filter ("grid", "chart", "toolbar", "html", "window", or "*"). |
'*'
|
widget_id
|
str
|
Widget ID filter (specific ID or "*" for all). |
'*'
|
Returns:
| Type | 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).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The window label. |
required |
event_type
|
str or None
|
The event type (None to unregister all for this label). |
None
|
handler
|
CallbackFunc or None
|
Specific handler to remove (None to remove all for event_type). |
None
|
Returns:
| Type | 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.
_drain
¶
Wait for all pending sync handler futures to complete.
_ensure_executor
¶
Lazily create the thread pool executor for sync handlers.
_invoke_handler
¶
_invoke_handler(handler: CallbackFunc, data: Any, event_type: str, label: str) -> bool
Invoke a single handler with appropriate arguments.
Sync handlers are submitted to a thread pool so they never block the reader 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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The window label. |
required |
event_type
|
str
|
The event type. |
required |
data
|
Any
|
The event data. May contain widget_type and widget_id/gridId/chartId. |
required |
Returns:
| Type | 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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The window label to destroy. |
required |
Returns:
| Type | 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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The window label to recover. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the label was recovered (was in destroyed set). |
is_destroyed
¶
Check if a window label has been destroyed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The window label. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if destroyed, False otherwise. |
has_handlers
¶
Check if a window has any registered handlers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label
|
str
|
The window label. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if handlers exist, False otherwise. |
get_labels
¶
Get all window labels with registered handlers.
Returns:
| Type | Description |
|---|---|
List of window labels.
|
|
clear
¶
Clear all callbacks and destroyed labels.
Use with caution - primarily for testing.