Skip to content

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
class 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.
    """

    _data = {}

    def __init__(self, topic, data=None, source=None, event_system=None):
        self._id = str(uuid4())
        self._topic = topic
        if data is None:
            data = {}
        self._data = data
        self._source = source
        self._event_system = event_system

    def __getitem__(self, key):
        return self._data[key]

    def get(self, key, *args, **kwargs):
        return self._data.get(key, *args, **kwargs)

    @property
    def id(self):
        return self._id

    @property
    def source(self):
        """Event's source used for triggering callbacks.

        Returns:
            Union[str, None]: Source string or None. Source is optional.
        """

        return self._source

    @property
    def data(self):
        return self._data

    @property
    def topic(self):
        """Event's topic used for triggering callbacks.

        Returns:
            str: Topic string.
        """

        return self._topic

    def emit(self):
        """Emit event and trigger callbacks."""
        if self._event_system is None:
            raise MissingEventSystem(
                "Can't emit event {}. Does not have set event system.".format(
                    str(repr(self))
                )
            )
        self._event_system.emit_event(self)

    def to_data(self):
        """Convert Event object to data.

        Returns:
            Dict[str, Any]: Event data.
        """

        return {
            "id": self.id,
            "topic": self.topic,
            "source": self.source,
            "data": copy.deepcopy(self.data)
        }

    @classmethod
    def from_data(cls, event_data, event_system=None):
        """Create event from data.

        Args:
            event_data (Dict[str, Any]): Event data with defined keys. Can be
                created using 'to_data' method.
            event_system (EventSystem): System to which the event belongs.

        Returns:
            Event: Event with attributes from passed data.
        """

        obj = cls(
            event_data["topic"],
            event_data["data"],
            event_data["source"],
            event_system
        )
        obj._id = event_data["id"]
        return obj

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
def emit(self):
    """Emit event and trigger callbacks."""
    if self._event_system is None:
        raise MissingEventSystem(
            "Can't emit event {}. Does not have set event system.".format(
                str(repr(self))
            )
        )
    self._event_system.emit_event(self)

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
@classmethod
def from_data(cls, event_data, event_system=None):
    """Create event from data.

    Args:
        event_data (Dict[str, Any]): Event data with defined keys. Can be
            created using 'to_data' method.
        event_system (EventSystem): System to which the event belongs.

    Returns:
        Event: Event with attributes from passed data.
    """

    obj = cls(
        event_data["topic"],
        event_data["data"],
        event_data["source"],
        event_system
    )
    obj._id = event_data["id"]
    return obj

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
def to_data(self):
    """Convert Event object to data.

    Returns:
        Dict[str, Any]: Event data.
    """

    return {
        "id": self.id,
        "topic": self.topic,
        "source": self.source,
        "data": copy.deepcopy(self.data)
    }

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
class 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.

    Args:
        topic (str): Topic which will be listened.
        func (Callable): Callback to a topic.
        order (Union[int, None]): Order of callback. Lower number means higher
            priority.

    Raises:
        TypeError: When passed function is not a callable object.
    """

    def __init__(self, topic, func, order):
        if not callable(func):
            raise TypeError((
                "Registered callback is not callable. \"{}\""
            ).format(str(func)))

        self._validate_order(order)

        self._log = None
        self._topic = topic
        self._order = order
        self._enabled = True
        # Replace '*' with any character regex and escape rest of text
        #   - when callback is registered for '*' topic it will receive all
        #       events
        #   - it is possible to register to a partial topis 'my.event.*'
        #       - it will receive all matching event topics
        #           e.g. 'my.event.start' and 'my.event.end'
        topic_regex_str = "^{}$".format(
            ".+".join(
                re.escape(part)
                for part in topic.split("*")
            )
        )
        topic_regex = re.compile(topic_regex_str)
        self._topic_regex = topic_regex

        # Callback function prep
        if isinstance(func, weakref_partial):
            partial_func = func
            (name, path) = _get_func_info(func.get_func())
            func_ref = None
            expect_args = partial_func.validate_signature("fake")
            expect_kwargs = partial_func.validate_signature(event="fake")

        else:
            partial_func = None
            (name, path) = _get_func_info(func)
            # Convert callback into references
            #   - deleted functions won't cause crashes
            func_ref = _get_func_ref(func)

            # Get expected arguments from function spec
            # - positional arguments are always preferred
            expect_args = is_func_signature_supported(func, "fake")
            expect_kwargs = is_func_signature_supported(func, event="fake")

        self._func_ref = func_ref
        self._partial_func = partial_func
        self._ref_is_valid = True
        self._expect_args = expect_args
        self._expect_kwargs = expect_kwargs

        self._name = name
        self._path = path

    def __repr__(self):
        return "< {} - {} > {}".format(
            self.__class__.__name__, self._name, self._path
        )

    @property
    def log(self):
        if self._log is None:
            self._log = logging.getLogger(self.__class__.__name__)
        return self._log

    @property
    def is_ref_valid(self):
        """

        Returns:
            bool: Is reference to callback valid.
        """

        self._validate_ref()
        return self._ref_is_valid

    def validate_ref(self):
        """Validate if reference to callback is valid.

        Deprecated:
            Reference is always live checkd with 'is_ref_valid'.
        """

        # Trigger validate by getting 'is_valid'
        _ = self.is_ref_valid

    @property
    def enabled(self):
        """Is callback enabled.

        Returns:
            bool: Is callback enabled.
        """

        return self._enabled

    def set_enabled(self, enabled):
        """Change if callback is enabled.

        Args:
            enabled (bool): Change enabled state of the callback.
        """

        self._enabled = enabled

    def deregister(self):
        """Calling this function will cause that callback will be removed."""

        self._ref_is_valid = False
        self._partial_func = None
        self._func_ref = None

    def get_order(self):
        """Get callback order.

        Returns:
            Union[int, None]: Callback order.
        """

        return self._order

    def set_order(self, order):
        """Change callback order.

        Args:
            order (Union[int, None]): Order of callback. Lower number means
                higher priority.
        """

        self._validate_order(order)
        self._order = order

    order = property(get_order, set_order)

    def topic_matches(self, topic):
        """Check if event topic matches callback's topic.

        Args:
            topic (str): Topic name.

        Returns:
            bool: Topic matches callback's topic.
        """

        return self._topic_regex.match(topic)

    def process_event(self, event):
        """Process event.

        Args:
            event(Event): Event that was triggered.
        """

        # Skip if callback is not enabled
        if not self._enabled:
            return

        # Get reference and skip if is not available
        callback = self._get_callback()
        if callback is None:
            return

        if not self.topic_matches(event.topic):
            return

        # Try to execute callback
        try:
            if self._expect_args:
                callback(event)

            elif self._expect_kwargs:
                callback(event=event)

            else:
                callback()

        except Exception:
            self.log.warning(
                "Failed to execute event callback {}".format(
                    str(repr(self))
                ),
                exc_info=True
            )

    def _validate_order(self, order):
        if isinstance(order, int):
            return

        raise TypeError(
            "Expected type 'int' got '{}'.".format(str(type(order)))
        )

    def _get_callback(self):
        if self._partial_func is not None:
            return self._partial_func

        if self._func_ref is not None:
            return self._func_ref()
        return None

    def _validate_ref(self):
        if self._ref_is_valid is False:
            return

        if self._func_ref is not None:
            self._ref_is_valid = self._func_ref() is not None

        elif self._partial_func is not None:
            self._ref_is_valid = self._partial_func.is_valid()

        else:
            self._ref_is_valid = False

        if not self._ref_is_valid:
            self._func_ref = None
            self._partial_func = None

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
def deregister(self):
    """Calling this function will cause that callback will be removed."""

    self._ref_is_valid = False
    self._partial_func = None
    self._func_ref = None

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
def get_order(self):
    """Get callback order.

    Returns:
        Union[int, None]: Callback order.
    """

    return self._order

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
def process_event(self, event):
    """Process event.

    Args:
        event(Event): Event that was triggered.
    """

    # Skip if callback is not enabled
    if not self._enabled:
        return

    # Get reference and skip if is not available
    callback = self._get_callback()
    if callback is None:
        return

    if not self.topic_matches(event.topic):
        return

    # Try to execute callback
    try:
        if self._expect_args:
            callback(event)

        elif self._expect_kwargs:
            callback(event=event)

        else:
            callback()

    except Exception:
        self.log.warning(
            "Failed to execute event callback {}".format(
                str(repr(self))
            ),
            exc_info=True
        )

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
def set_enabled(self, enabled):
    """Change if callback is enabled.

    Args:
        enabled (bool): Change enabled state of the callback.
    """

    self._enabled = enabled

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
def set_order(self, order):
    """Change callback order.

    Args:
        order (Union[int, None]): Order of callback. Lower number means
            higher priority.
    """

    self._validate_order(order)
    self._order = order

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
def topic_matches(self, topic):
    """Check if event topic matches callback's topic.

    Args:
        topic (str): Topic name.

    Returns:
        bool: Topic matches callback's topic.
    """

    return self._topic_regex.match(topic)

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
def validate_ref(self):
    """Validate if reference to callback is valid.

    Deprecated:
        Reference is always live checkd with 'is_ref_valid'.
    """

    # Trigger validate by getting 'is_valid'
    _ = self.is_ref_valid

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
class 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'.
    """

    default_order = 100

    def __init__(self):
        self._registered_callbacks = []

    def add_callback(self, topic, callback, order=None):
        """Register callback in event system.

        Args:
            topic (str): Topic for EventCallback.
            callback (Union[Callable, weakref_partial]): Function or method
                that will be called when topic is triggered.
            order (Optional[int]): Order of callback. Lower number means
                higher priority.

        Returns:
            EventCallback: Created callback object which can be used to
                stop listening.
        """

        if order is None:
            order = self.default_order

        callback = EventCallback(topic, callback, order)
        self._registered_callbacks.append(callback)
        return callback

    def create_event(self, topic, data, source):
        """Create new event which is bound to event system.

        Args:
            topic (str): Event topic.
            data (dict): Data related to event.
            source (str): Source of event.

        Returns:
            Event: Object of event.
        """

        return Event(topic, data, source, self)

    def emit(self, topic, data, source):
        """Create event based on passed data and emit it.

        This is easiest way how to trigger event in an event system.

        Args:
            topic (str): Event topic.
            data (dict): Data related to event.
            source (str): Source of event.

        Returns:
            Event: Created and emitted event.
        """

        event = self.create_event(topic, data, source)
        event.emit()
        return event

    def emit_event(self, event):
        """Emit event object.

        Args:
            event (Event): Prepared event with topic and data.
        """

        self._process_event(event)

    def clear_callbacks(self):
        """Clear all registered callbacks."""
        self._registered_callbacks = []

    def _process_event(self, event):
        """Process event topic and trigger callbacks.

        Args:
            event (Event): Prepared event with topic and data.
        """

        callbacks = tuple(sorted(
            self._registered_callbacks, key=lambda x: x.order
        ))
        for callback in callbacks:
            callback.process_event(event)
            if not callback.is_ref_valid:
                self._registered_callbacks.remove(callback)

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
def add_callback(self, topic, callback, order=None):
    """Register callback in event system.

    Args:
        topic (str): Topic for EventCallback.
        callback (Union[Callable, weakref_partial]): Function or method
            that will be called when topic is triggered.
        order (Optional[int]): Order of callback. Lower number means
            higher priority.

    Returns:
        EventCallback: Created callback object which can be used to
            stop listening.
    """

    if order is None:
        order = self.default_order

    callback = EventCallback(topic, callback, order)
    self._registered_callbacks.append(callback)
    return callback

clear_callbacks()

Clear all registered callbacks.

Source code in client/ayon_core/lib/events.py
569
570
571
def clear_callbacks(self):
    """Clear all registered callbacks."""
    self._registered_callbacks = []

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
def create_event(self, topic, data, source):
    """Create new event which is bound to event system.

    Args:
        topic (str): Event topic.
        data (dict): Data related to event.
        source (str): Source of event.

    Returns:
        Event: Object of event.
    """

    return Event(topic, data, source, self)

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
def emit(self, topic, data, source):
    """Create event based on passed data and emit it.

    This is easiest way how to trigger event in an event system.

    Args:
        topic (str): Event topic.
        data (dict): Data related to event.
        source (str): Source of event.

    Returns:
        Event: Created and emitted event.
    """

    event = self.create_event(topic, data, source)
    event.emit()
    return event

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
def emit_event(self, event):
    """Emit event object.

    Args:
        event (Event): Prepared event with topic and data.
    """

    self._process_event(event)

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
class 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.
    """

    _global_event_system = None

    @classmethod
    def get_global_event_system(cls):
        if cls._global_event_system is None:
            cls._global_event_system = EventSystem()
        return cls._global_event_system

    @classmethod
    def add_callback(cls, topic, callback):
        event_system = cls.get_global_event_system()
        return event_system.add_callback(topic, callback)

    @classmethod
    def emit(cls, topic, data, source):
        event_system = cls.get_global_event_system()
        return event_system.emit(topic, data, source)

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
class QueuedEventSystem(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.

    Args:
        auto_execute (Optional[bool]): If 'True', events are processed
            automatically. Custom loop calling 'process_next_event'
            must be implemented when set to 'False'.
    """

    def __init__(self, auto_execute=True):
        super(QueuedEventSystem, self).__init__()
        self._event_queue = collections.deque()
        self._current_event = None
        self._auto_execute = auto_execute

    def __len__(self):
        return self.count()

    def count(self):
        """Get number of events in queue.

        Returns:
            int: Number of events in queue.
        """

        return len(self._event_queue)

    def process_next_event(self):
        """Process next event in queue.

        Should be used only if 'auto_execute' is set to 'False'. Only single
            event is processed.

        Returns:
            Union[Event, None]: Processed event.
        """

        if self._current_event is not None:
            raise ValueError("An event is already in progress.")

        if not self._event_queue:
            return None
        event = self._event_queue.popleft()
        self._current_event = event
        self._process_event(event)
        self._current_event = None
        return event

    def emit_event(self, event):
        """Emit event object.

        Args:
           event (Event): Prepared event with topic and data.
        """

        if not self._auto_execute or self._current_event is not None:
            self._event_queue.append(event)
            return

        self._event_queue.append(event)
        while self._event_queue:
            event = self._event_queue.popleft()
            self._current_event = event
            self._process_event(event)
        self._current_event = None

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
def count(self):
    """Get number of events in queue.

    Returns:
        int: Number of events in queue.
    """

    return len(self._event_queue)

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
def emit_event(self, event):
    """Emit event object.

    Args:
       event (Event): Prepared event with topic and data.
    """

    if not self._auto_execute or self._current_event is not None:
        self._event_queue.append(event)
        return

    self._event_queue.append(event)
    while self._event_queue:
        event = self._event_queue.popleft()
        self._current_event = event
        self._process_event(event)
    self._current_event = None

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
def process_next_event(self):
    """Process next event in queue.

    Should be used only if 'auto_execute' is set to 'False'. Only single
        event is processed.

    Returns:
        Union[Event, None]: Processed event.
    """

    if self._current_event is not None:
        raise ValueError("An event is already in progress.")

    if not self._event_queue:
        return None
    event = self._event_queue.popleft()
    self._current_event = event
    self._process_event(event)
    self._current_event = None
    return event

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
class 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.

    Warnings:
        Values passed as *args and **kwargs are stored strongly in memory.
            That may "keep alive" objects that should be already destroyed.
            It is recommended to pass only immutable objects like 'str',
            'bool', 'int' etc.

    Args:
        func (Callable): Function to wrap.
        *args: Arguments passed to the wrapped function.
        **kwargs: Keyword arguments passed to the wrapped function.
    """

    def __init__(self, func, *args, **kwargs):
        self._func_ref = _get_func_ref(func)
        self._args = args
        self._kwargs = kwargs

    def __call__(self, *args, **kwargs):
        func = self._func_ref()
        if func is None:
            return

        new_args = tuple(list(self._args) + list(args))
        new_kwargs = dict(self._kwargs)
        new_kwargs.update(kwargs)
        return func(*new_args, **new_kwargs)

    def get_func(self):
        """Get wrapped function.

        Returns:
            Union[Callable, None]: Wrapped function or None if it was
                destroyed.
        """

        return self._func_ref()

    def is_valid(self):
        """Check if wrapped function is still valid.

        Returns:
            bool: Is wrapped function still valid.
        """

        return self._func_ref() is not None

    def validate_signature(self, *args, **kwargs):
        """Validate if passed arguments are supported by wrapped function.

        Returns:
            bool: Are passed arguments supported by wrapped function.
        """

        func = self._func_ref()
        if func is None:
            return False

        new_args = tuple(list(self._args) + list(args))
        new_kwargs = dict(self._kwargs)
        new_kwargs.update(kwargs)
        return is_func_signature_supported(
            func, *new_args, **new_kwargs
        )

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
def get_func(self):
    """Get wrapped function.

    Returns:
        Union[Callable, None]: Wrapped function or None if it was
            destroyed.
    """

    return self._func_ref()

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
def is_valid(self):
    """Check if wrapped function is still valid.

    Returns:
        bool: Is wrapped function still valid.
    """

    return self._func_ref() is not None

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
def validate_signature(self, *args, **kwargs):
    """Validate if passed arguments are supported by wrapped function.

    Returns:
        bool: Are passed arguments supported by wrapped function.
    """

    func = self._func_ref()
    if func is None:
        return False

    new_args = tuple(list(self._args) + list(args))
    new_kwargs = dict(self._kwargs)
    new_kwargs.update(kwargs)
    return is_func_signature_supported(
        func, *new_args, **new_kwargs
    )

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
def 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:
        Event: Object of event that was emitted.
    """

    return GlobalEventSystem.emit(topic, data, source)

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 Event object is passed.

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
def register_event_callback(topic, callback):
    """Add callback that will be executed on specific topic.

    Args:
        topic(str): Topic on which will callback be triggered.
        callback(function): Callback that will be triggered when a topic
            is triggered. Callback should expect none or 1 argument where
            `Event` object is passed.

    Returns:
        EventCallback: Object wrapping the callback. It can be used to
            enable/disable listening to a topic or remove the callback from
            the topic completely.
    """

    return GlobalEventSystem.add_callback(topic, callback)