events
Events holding data about specific event.
Event
Base event object.
Can be used for any event because is not specific. Only required argument is topic which defines why event is happening and may be used for filtering.
Arg
topic (str): Identifier of event. data (Any): Data specific for event. Dictionary is recommended. source (str): Identifier of source. event_system (EventSystem): Event system in which can be event triggered.
Source code in client/ayon_core/lib/events.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
|
source
property
Event's source used for triggering callbacks.
Returns:
Type | Description |
---|---|
Union[str, None]: Source string or None. Source is optional. |
topic
property
Event's topic used for triggering callbacks.
Returns:
Name | Type | Description |
---|---|---|
str | Topic string. |
emit()
Emit event and trigger callbacks.
Source code in client/ayon_core/lib/events.py
442 443 444 445 446 447 448 449 450 |
|
from_data(event_data, event_system=None)
classmethod
Create event from data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event_data | Dict[str, Any] | Event data with defined keys. Can be created using 'to_data' method. | required |
event_system | EventSystem | System to which the event belongs. | None |
Returns:
Name | Type | Description |
---|---|---|
Event | Event with attributes from passed data. |
Source code in client/ayon_core/lib/events.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
|
to_data()
Convert Event object to data.
Returns:
Type | Description |
---|---|
Dict[str, Any]: Event data. |
Source code in client/ayon_core/lib/events.py
452 453 454 455 456 457 458 459 460 461 462 463 464 |
|
EventCallback
Callback registered to a topic.
The callback function is registered to a topic. Topic is a string which may contain '*' that will be handled as "any characters".
Examples:
- "workfile.save" Callback will be triggered if the event topic is exactly "workfile.save" .
- "workfile.*" Callback will be triggered an event topic starts with "workfile." so "workfile.save" and "workfile.open" will trigger the callback.
- "*" Callback will listen to all events.
Callback can be function or method. In both cases it should expect one or none arguments. When 1 argument is expected then the processed 'Event' object is passed in.
The callbacks are validated against their reference counter, that is achieved using 'weakref' module. That means that the callback must be stored in memory somewhere. e.g. lambda functions are not supported as valid callback.
You can use 'weakref_partial' functions. In that case is partial object stored in the callback object and reference counter is checked for the wrapped function.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topic | str | Topic which will be listened. | required |
func | Callable | Callback to a topic. | required |
order | Union[int, None] | Order of callback. Lower number means higher priority. | required |
Raises:
Type | Description |
---|---|
TypeError | When passed function is not a callable object. |
Source code in client/ayon_core/lib/events.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
|
enabled
property
Is callback enabled.
Returns:
Name | Type | Description |
---|---|---|
bool | Is callback enabled. |
is_ref_valid
property
Returns:
Name | Type | Description |
---|---|---|
bool | Is reference to callback valid. |
deregister()
Calling this function will cause that callback will be removed.
Source code in client/ayon_core/lib/events.py
269 270 271 272 273 274 |
|
get_order()
Get callback order.
Returns:
Type | Description |
---|---|
Union[int, None]: Callback order. |
Source code in client/ayon_core/lib/events.py
276 277 278 279 280 281 282 283 |
|
process_event(event)
Process event.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event(Event) | Event that was triggered. | required |
Source code in client/ayon_core/lib/events.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
|
set_enabled(enabled)
Change if callback is enabled.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
enabled | bool | Change enabled state of the callback. | required |
Source code in client/ayon_core/lib/events.py
260 261 262 263 264 265 266 267 |
|
set_order(order)
Change callback order.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
order | Union[int, None] | Order of callback. Lower number means higher priority. | required |
Source code in client/ayon_core/lib/events.py
285 286 287 288 289 290 291 292 293 294 |
|
topic_matches(topic)
Check if event topic matches callback's topic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topic | str | Topic name. | required |
Returns:
Name | Type | Description |
---|---|---|
bool | Topic matches callback's topic. |
Source code in client/ayon_core/lib/events.py
298 299 300 301 302 303 304 305 306 307 308 |
|
validate_ref()
Validate if reference to callback is valid.
Deprecated
Reference is always live checkd with 'is_ref_valid'.
Source code in client/ayon_core/lib/events.py
240 241 242 243 244 245 246 247 248 |
|
EventSystem
Encapsulate event handling into an object.
System wraps registered callbacks and triggered events into single object, so it is possible to create multiple independent systems that have their topics and callbacks.
Callbacks are stored by order of their registration, but it is possible to manually define order of callbacks using 'order' argument within 'add_callback'.
Source code in client/ayon_core/lib/events.py
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 |
|
add_callback(topic, callback, order=None)
Register callback in event system.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topic | str | Topic for EventCallback. | required |
callback | Union[Callable, weakref_partial] | Function or method that will be called when topic is triggered. | required |
order | Optional[int] | Order of callback. Lower number means higher priority. | None |
Returns:
Name | Type | Description |
---|---|---|
EventCallback | Created callback object which can be used to stop listening. |
Source code in client/ayon_core/lib/events.py
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 |
|
clear_callbacks()
Clear all registered callbacks.
Source code in client/ayon_core/lib/events.py
569 570 571 |
|
create_event(topic, data, source)
Create new event which is bound to event system.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topic | str | Event topic. | required |
data | dict | Data related to event. | required |
source | str | Source of event. | required |
Returns:
Name | Type | Description |
---|---|---|
Event | Object of event. |
Source code in client/ayon_core/lib/events.py
528 529 530 531 532 533 534 535 536 537 538 539 540 |
|
emit(topic, data, source)
Create event based on passed data and emit it.
This is easiest way how to trigger event in an event system.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topic | str | Event topic. | required |
data | dict | Data related to event. | required |
source | str | Source of event. | required |
Returns:
Name | Type | Description |
---|---|---|
Event | Created and emitted event. |
Source code in client/ayon_core/lib/events.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
|
emit_event(event)
Emit event object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event | Event | Prepared event with topic and data. | required |
Source code in client/ayon_core/lib/events.py
560 561 562 563 564 565 566 567 |
|
GlobalEventSystem
Event system living in global scope of process.
This is primarily used in host implementation to trigger events related to DCC changes or changes of context in the host implementation.
Source code in client/ayon_core/lib/events.py
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
|
QueuedEventSystem
Bases: EventSystem
Events are automatically processed in queue.
If callback triggers another event, the event is not processed until all callbacks of previous event are processed.
Allows to implement custom event process loop by changing 'auto_execute'.
Note
This probably should be default behavior of 'EventSystem'. Changing it now could cause problems in existing code.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
auto_execute | Optional[bool] | If 'True', events are processed automatically. Custom loop calling 'process_next_event' must be implemented when set to 'False'. | True |
Source code in client/ayon_core/lib/events.py
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 |
|
count()
Get number of events in queue.
Returns:
Name | Type | Description |
---|---|---|
int | Number of events in queue. |
Source code in client/ayon_core/lib/events.py
616 617 618 619 620 621 622 623 |
|
emit_event(event)
Emit event object.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
event | Event | Prepared event with topic and data. | required |
Source code in client/ayon_core/lib/events.py
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 |
|
process_next_event()
Process next event in queue.
Should be used only if 'auto_execute' is set to 'False'. Only single event is processed.
Returns:
Type | Description |
---|---|
Union[Event, None]: Processed event. |
Source code in client/ayon_core/lib/events.py
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 |
|
weakref_partial
Partial function with weak reference to the wrapped function.
Can be used as 'functools.partial' but it will store weak reference to function. That means that the function must be reference counted to avoid garbage collecting the function itself.
When the referenced functions is garbage collected then calling the
weakref partial (no matter the args/kwargs passed) will do nothing.
It will fail silently, returning `None`. The `is_valid()` method can
be used to detect whether the reference is still valid.
Is useful for object methods. In that case the callback is deregistered when object is destroyed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func | Callable | Function to wrap. | required |
*args | Arguments passed to the wrapped function. | () | |
**kwargs | Keyword arguments passed to the wrapped function. | {} |
Source code in client/ayon_core/lib/events.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
get_func()
Get wrapped function.
Returns:
Type | Description |
---|---|
Union[Callable, None]: Wrapped function or None if it was destroyed. |
Source code in client/ayon_core/lib/events.py
87 88 89 90 91 92 93 94 95 |
|
is_valid()
Check if wrapped function is still valid.
Returns:
Name | Type | Description |
---|---|---|
bool | Is wrapped function still valid. |
Source code in client/ayon_core/lib/events.py
97 98 99 100 101 102 103 104 |
|
validate_signature(*args, **kwargs)
Validate if passed arguments are supported by wrapped function.
Returns:
Name | Type | Description |
---|---|---|
bool | Are passed arguments supported by wrapped function. |
Source code in client/ayon_core/lib/events.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
|
emit_event(topic, data=None, source=None)
Emit event with topic and data.
Arg
topic(str): Event's topic. data(dict): Event's additional data. Optional. source(str): Who emitted the topic. Optional.
Returns:
Name | Type | Description |
---|---|---|
Event | Object of event that was emitted. |
Source code in client/ayon_core/lib/events.py
709 710 711 712 713 714 715 716 717 718 719 720 721 |
|
register_event_callback(topic, callback)
Add callback that will be executed on specific topic.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
topic(str) | Topic on which will callback be triggered. | required | |
callback(function) | Callback that will be triggered when a topic is triggered. Callback should expect none or 1 argument where | required |
Returns:
Name | Type | Description |
---|---|---|
EventCallback | Object wrapping the callback. It can be used to enable/disable listening to a topic or remove the callback from the topic completely. |
Source code in client/ayon_core/lib/events.py
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 |
|