EventLinker
class¶
A base class that orchestrates the linkage of events and their inherent logic.
Notes:
-
This class provides a centralized mechanism for managing the connections between events and their corresponding logic.
-
The
EventLinker
can be subclassed to create specific namespaces or contexts for managing events within different scopes. Subclassing also allows users to configure the settings of theEventLinker
to suit their specific use cases. -
The
EventLinker
is designed with thread safety in mind. All methods synchronize access to prevent race conditions when managing mutable properties across multiple threads.
Source code in pyventus/events/linkers/event_linker.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 123 124 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 380 381 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 487 488 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 587 588 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 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 |
|
Classes¶
EventLinkerSubCtx
¶
Bases: Generic[_SubCtxE]
, SubscriptionContext[_SubCtxE, EventSubscriber]
A context manager for event linker subscriptions.
Notes:
-
This class establishes a context block for a step-by-step definition of event responses prior to the actual subscription, which occurs immediately upon exiting the context block.
-
This class can be used as either a decorator or a context manager. When used as a decorator, it automatically subscribes the decorated callback to the specified events. When used as a context manager with the
with
statement, it allows multiple callbacks to be associated with the specified events within the context block. -
This subscription context can be
stateful
, retaining references to theevent linker
andsubscriber
, orstateless
, which clears the context upon exiting the subscription block. -
This class is not intended to be subclassed or manually instantiated.
Source code in pyventus/events/linkers/event_linker.py
42 43 44 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 123 124 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 |
|
Functions¶
unpack
¶
Unpack and retrieve the source object and its associated subscriber.
This method returns a tuple containing the source object and its subscriber, while also handling the cleanup of associated resources to prevent memory leaks. After retrieving the objects, it deletes internal references to the source and subscriber to ensure they are no longer retained.
RETURNS | DESCRIPTION |
---|---|
tuple[_SourceType | None, _SubscriberType | None]
|
A tuple of the form (source, subscriber). Both may be |
RAISES | DESCRIPTION |
---|---|
PyventusException
|
If this method is called before or during the subscription context, indicating that the resources are not yet available for unpacking. |
Source code in pyventus/core/subscriptions/subscription_context.py
__enter__
¶
Enter the subscription context block.
This method facilitates the progressive definition of an object that will later be subscribed to the specified source.
RETURNS | DESCRIPTION |
---|---|
Self
|
The subscription context manager. |
Source code in pyventus/core/subscriptions/subscription_context.py
__exit__
¶
__exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None) -> None
Exit the subscription context block.
This method subscribes the defined object to the specified source, and performs any necessary cleanup.
PARAMETER | DESCRIPTION |
---|---|
exc_type
|
The type of the raised exception, if any.
TYPE:
|
exc_val
|
The raised exception object, if any.
TYPE:
|
exc_tb
|
The traceback information, if any.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
Source code in pyventus/core/subscriptions/subscription_context.py
__init__
¶
__init__(events: tuple[SubscribableEventType, ...], event_linker: _SubCtxE, force_async: bool, once: bool, is_stateful: bool) -> None
Initialize an instance of EventLinkerSubCtx
.
PARAMETER | DESCRIPTION |
---|---|
events
|
The events to subscribe/link to.
TYPE:
|
event_linker
|
The type of event linker used for the actual subscription.
TYPE:
|
force_async
|
Determines whether to force all callbacks to run asynchronously.
TYPE:
|
once
|
Specifies if the event subscriber will be a one-time subscription.
TYPE:
|
is_stateful
|
A flag indicating whether the context preserves its state (stateful) or not (stateless) after exiting the subscription context. If
TYPE:
|
Source code in pyventus/events/linkers/event_linker.py
on_event
¶
Set the main callback for the event.
PARAMETER | DESCRIPTION |
---|---|
callback
|
The callback to be executed when the event occurs.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
EventCallbackType
|
The decorated callback. |
Source code in pyventus/events/linkers/event_linker.py
on_success
¶
Set the success callback for the event.
PARAMETER | DESCRIPTION |
---|---|
callback
|
The callback to be executed when the event response completes successfully.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
SuccessCallbackType
|
The decorated callback. |
Source code in pyventus/events/linkers/event_linker.py
on_failure
¶
Set the failure callback for the event.
PARAMETER | DESCRIPTION |
---|---|
callback
|
The callback to be executed when the event response fails.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
FailureCallbackType
|
The decorated callback. |
Source code in pyventus/events/linkers/event_linker.py
__call__
¶
__call__(callback: EventCallbackType) -> tuple[EventCallbackType, EventLinkerSubCtx[_SubCtxE]] | EventCallbackType
Subscribe the decorated callback to the specified events.
PARAMETER | DESCRIPTION |
---|---|
callback
|
The callback to be executed when the event occurs.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[EventCallbackType, EventLinkerSubCtx[_SubCtxE]] | EventCallbackType
|
A tuple containing the decorated callback and its subscription context if the context is stateful; otherwise, returns the decorated callback alone. |
Source code in pyventus/events/linkers/event_linker.py
Functions¶
__init_subclass__
¶
__init_subclass__(max_subscribers: int | None = None, default_success_callback: SuccessCallbackType | None = None, default_failure_callback: FailureCallbackType | None = None, debug: bool | None = None) -> None
Initialize a subclass of EventLinker
.
By default, this method sets up the main registry and the thread lock object, but
it can also be used to configure specific settings of the EventLinker
subclass.
PARAMETER | DESCRIPTION |
---|---|
max_subscribers
|
The maximum number of subscribers allowed per event, or
TYPE:
|
default_success_callback
|
The default callback to assign as the success callback in the subscribers when no specific success callback is provided.
TYPE:
|
default_failure_callback
|
The default callback to assign as the failure callback in the subscribers when no specific failure callback is provided.
TYPE:
|
debug
|
Specifies the debug mode for the subclass logger. If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None. |
RAISES | DESCRIPTION |
---|---|
PyventusException
|
If |
Source code in pyventus/events/linkers/event_linker.py
get_valid_event_name
classmethod
¶
Determine the name of the event and performs validation.
PARAMETER | DESCRIPTION |
---|---|
event
|
The event to obtain the name for.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
A string that represents the event name. |
RAISES | DESCRIPTION |
---|---|
PyventusException
|
If the |
Source code in pyventus/events/linkers/event_linker.py
get_valid_subscriber
classmethod
¶
get_valid_subscriber(subscriber: EventSubscriber) -> EventSubscriber
Validate and return the specified subscriber.
PARAMETER | DESCRIPTION |
---|---|
subscriber
|
The subscriber to validate.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
EventSubscriber
|
The validated subscriber. |
RAISES | DESCRIPTION |
---|---|
PyventusException
|
If the subscriber is not an instance of |
Source code in pyventus/events/linkers/event_linker.py
get_max_subscribers
classmethod
¶
Retrieve the maximum number of subscribers allowed per event.
RETURNS | DESCRIPTION |
---|---|
int | None
|
The maximum number of subscribers or |
Source code in pyventus/events/linkers/event_linker.py
get_default_success_callback
classmethod
¶
Retrieve the default success callback to assign to subscribers when no specific callback is provided.
RETURNS | DESCRIPTION |
---|---|
SuccessCallbackType | None
|
The default success callback or |
Source code in pyventus/events/linkers/event_linker.py
get_default_failure_callback
classmethod
¶
Retrieve the default failure callback to assign to subscribers when no specific callback is provided.
RETURNS | DESCRIPTION |
---|---|
FailureCallbackType | None
|
The default failure callback or |
Source code in pyventus/events/linkers/event_linker.py
is_empty
classmethod
¶
Determine whether the main registry is empty.
RETURNS | DESCRIPTION |
---|---|
bool
|
|
Source code in pyventus/events/linkers/event_linker.py
get_registry
classmethod
¶
get_registry() -> dict[str, set[EventSubscriber]]
Retrieve a shallow copy of the main registry.
RETURNS | DESCRIPTION |
---|---|
dict[str, set[EventSubscriber]]
|
A shallow copy of the main registry, where each event is mapped to a set of its linked subscribers. |
Source code in pyventus/events/linkers/event_linker.py
get_events
classmethod
¶
Retrieve all registered events.
RETURNS | DESCRIPTION |
---|---|
set[str]
|
A set of all registered event names. |
get_subscribers
classmethod
¶
get_subscribers() -> set[EventSubscriber]
Retrieve all registered subscribers.
RETURNS | DESCRIPTION |
---|---|
set[EventSubscriber]
|
A set of all registered subscribers. |
get_event_count
classmethod
¶
Retrieve the number of registered events.
RETURNS | DESCRIPTION |
---|---|
int
|
The total count of events in the registry. |
get_subscriber_count
classmethod
¶
Retrieve the number of registered subscribers.
RETURNS | DESCRIPTION |
---|---|
int
|
The total count of subscribers in the registry. |
Source code in pyventus/events/linkers/event_linker.py
get_events_from_subscribers
classmethod
¶
get_events_from_subscribers(*subscribers: EventSubscriber) -> set[str]
Retrieve a set of events associated with the specified subscribers.
PARAMETER | DESCRIPTION |
---|---|
subscribers
|
One or more subscribers for which to retrieve associated events.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
set[str]
|
A set of events linked to the specified subscribers. Unregistered subscribers are ignored. |
Source code in pyventus/events/linkers/event_linker.py
get_subscribers_from_events
classmethod
¶
get_subscribers_from_events(*events: SubscribableEventType, pop_onetime_subscribers: bool = False) -> set[EventSubscriber]
Retrieve a set of subscribers associated with the specified events.
PARAMETER | DESCRIPTION |
---|---|
events
|
One or more events for which to retrieve associated subscribers.
TYPE:
|
pop_onetime_subscribers
|
If
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
set[EventSubscriber]
|
A set of subscribers linked to the specified events. Unregistered events are ignored. |
Source code in pyventus/events/linkers/event_linker.py
get_event_count_from_subscriber
classmethod
¶
get_event_count_from_subscriber(subscriber: EventSubscriber) -> int
Retrieve the number of events associated with the given subscriber.
PARAMETER | DESCRIPTION |
---|---|
subscriber
|
The subscriber for which to count the associated events.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
The count of events associated with the specified subscriber, or 0 if the subscriber is not found. |
Source code in pyventus/events/linkers/event_linker.py
get_subscriber_count_from_event
classmethod
¶
Retrieve the number of subscribers associated with a given event.
PARAMETER | DESCRIPTION |
---|---|
event
|
The event for which to count the associated subscribers.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
The count of subscribers associated with the specified event, or 0 if the event is not found. |
Source code in pyventus/events/linkers/event_linker.py
contains_event
classmethod
¶
Determine if the specified event is present in the registry.
PARAMETER | DESCRIPTION |
---|---|
event
|
The event to be checked.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
|
Source code in pyventus/events/linkers/event_linker.py
contains_subscriber
classmethod
¶
contains_subscriber(subscriber: EventSubscriber) -> bool
Determine if the specified subscriber is present in the registry.
PARAMETER | DESCRIPTION |
---|---|
subscriber
|
The subscriber to be checked.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
|
Source code in pyventus/events/linkers/event_linker.py
are_linked
classmethod
¶
are_linked(event: SubscribableEventType, subscriber: EventSubscriber) -> bool
Determine whether the given event is linked with the specified subscriber.
PARAMETER | DESCRIPTION |
---|---|
event
|
The event for which the association is being checked.
TYPE:
|
subscriber
|
The subscriber for which the association is being checked.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
|
Source code in pyventus/events/linkers/event_linker.py
once
classmethod
¶
once(*events: SubscribableEventType, force_async: bool = False, stateful_subctx: bool = False) -> EventLinkerSubCtx[type[Self]]
Subscribe callbacks to the specified events for a single invocation.
This method can be used as either a decorator or a context manager. When used as a
decorator, it automatically subscribes the decorated callback to the provided events.
When used as a context manager with the with
statement, it allows multiple callbacks
to be associated with the provided events within the context block.
PARAMETER | DESCRIPTION |
---|---|
events
|
The events to subscribe to.
TYPE:
|
force_async
|
Determines whether to force all callbacks to run asynchronously. If
TYPE:
|
stateful_subctx
|
A flag indicating whether the subscription context preserves its state (
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
EventLinkerSubCtx[type[Self]]
|
A |
Source code in pyventus/events/linkers/event_linker.py
on
classmethod
¶
on(*events: SubscribableEventType, force_async: bool = False, stateful_subctx: bool = False) -> EventLinkerSubCtx[type[Self]]
Subscribe callbacks to the specified events.
This method can be used as either a decorator or a context manager. When used as a
decorator, it automatically subscribes the decorated callback to the provided events.
When used as a context manager with the with
statement, it allows multiple callbacks
to be associated with the provided events within the context block.
PARAMETER | DESCRIPTION |
---|---|
events
|
The events to subscribe to.
TYPE:
|
force_async
|
Determines whether to force all callbacks to run asynchronously. If
TYPE:
|
stateful_subctx
|
A flag indicating whether the subscription context preserves its state (
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
EventLinkerSubCtx[type[Self]]
|
A |
Source code in pyventus/events/linkers/event_linker.py
subscribe
classmethod
¶
subscribe(*events: SubscribableEventType, event_callback: EventCallbackType, success_callback: SuccessCallbackType | None = None, failure_callback: FailureCallbackType | None = None, force_async: bool = False, once: bool = False) -> EventSubscriber
Subscribe the specified callbacks to the given events.
PARAMETER | DESCRIPTION |
---|---|
events
|
The events to subscribe to.
TYPE:
|
event_callback
|
The callback to be executed when the event occurs.
TYPE:
|
success_callback
|
The callback to be executed when the event response completes successfully.
TYPE:
|
failure_callback
|
The callback to be executed when the event response fails.
TYPE:
|
force_async
|
Determines whether to force all callbacks to run asynchronously. If
TYPE:
|
once
|
Specifies if the subscriber will be a one-time subscriber.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
EventSubscriber
|
The subscriber associated with the given events. |
Source code in pyventus/events/linkers/event_linker.py
remove
classmethod
¶
remove(event: SubscribableEventType, subscriber: EventSubscriber) -> bool
Remove the specified subscriber from the given event.
PARAMETER | DESCRIPTION |
---|---|
event
|
The event from which the subscriber will be removed.
TYPE:
|
subscriber
|
The subscriber to be removed from the event.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
|
Source code in pyventus/events/linkers/event_linker.py
remove_event
classmethod
¶
Remove the specified event from the registry.
PARAMETER | DESCRIPTION |
---|---|
event
|
The event to be removed from the registry.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
|
Source code in pyventus/events/linkers/event_linker.py
remove_subscriber
classmethod
¶
remove_subscriber(subscriber: EventSubscriber) -> bool
Remove the specified subscriber from the registry.
PARAMETER | DESCRIPTION |
---|---|
subscriber
|
The subscriber to be removed from the registry.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
|
Source code in pyventus/events/linkers/event_linker.py
remove_all
classmethod
¶
Remove all events and subscribers from the registry.
RETURNS | DESCRIPTION |
---|---|
bool
|
|