<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">"""Event-loop debugging tools."""

from kombu.utils.eventio import ERR, READ, WRITE
from kombu.utils.functional import reprcall


def repr_flag(flag):
    """Return description of event loop flag."""
    return '{}{}{}'.format('R' if flag &amp; READ else '',
                           'W' if flag &amp; WRITE else '',
                           '!' if flag &amp; ERR else '')


def _rcb(obj):
    if obj is None:
        return '&lt;missing&gt;'
    if isinstance(obj, str):
        return obj
    if isinstance(obj, tuple):
        cb, args = obj
        return reprcall(cb.__name__, args=args)
    return obj.__name__


def repr_active(h):
    """Return description of active readers and writers."""
    return ', '.join(repr_readers(h) + repr_writers(h))


def repr_events(h, events):
    """Return description of events returned by poll."""
    return ', '.join(
        '{}({})-&gt;{}'.format(
            _rcb(callback_for(h, fd, fl, '(GONE)')), fd,
            repr_flag(fl),
        )
        for fd, fl in events
    )


def repr_readers(h):
    """Return description of pending readers."""
    return [f'({fd}){_rcb(cb)}-&gt;{repr_flag(READ | ERR)}'
            for fd, cb in h.readers.items()]


def repr_writers(h):
    """Return description of pending writers."""
    return [f'({fd}){_rcb(cb)}-&gt;{repr_flag(WRITE)}'
            for fd, cb in h.writers.items()]


def callback_for(h, fd, flag, *default):
    """Return the callback used for hub+fd+flag."""
    try:
        if flag &amp; READ:
            return h.readers[fd]
        if flag &amp; WRITE:
            if fd in h.consolidate:
                return h.consolidate_callback
            return h.writers[fd]
    except KeyError:
        if default:
            return default[0]
        raise
</pre></body></html>