# Path: lib/gruvi/callbacks.py
# def add_callback(obj, callback, args=()):
#     """Add a callback to an object."""
#     callbacks = obj._callbacks
#     node = Node(callback, args)
#     # Store a single callback directly in _callbacks
#     if callbacks is None:
#         obj._callbacks = node
#         return node
#     # Otherwise use a dllist.
#     if not isinstance(callbacks, dllist):
#         obj._callbacks = dllist()
#         obj._callbacks.insert(callbacks)
#         callbacks = obj._callbacks
#     callbacks.insert(node)
#     return node
# 
# def remove_callback(obj, handle):
#     """Remove a callback from an object."""
#     callbacks = obj._callbacks
#     if callbacks is handle:
#         obj._callbacks = None
#     elif isinstance(callbacks, dllist):
#         callbacks.remove(handle)
#         if not callbacks:
#             obj._callbacks = None
# 
# def walk_callbacks(obj, func, log=None):
#     """Call func(callback, args) for all callbacks and keep only those
#     callbacks for which the function returns True."""
#     callbacks = obj._callbacks
#     if isinstance(callbacks, Node):
#         node = callbacks
#         try:
#             if not func(node.data, node.extra):
#                 obj._callbacks = None
#         except Exception:
#             if log is None:
#                 log = logging.get_logger()
#             log.exception('uncaught exception in callback')
#     elif isinstance(callbacks, dllist):
#         for node in callbacks:
#             try:
#                 if func(node.data, node.extra):
#                     continue
#                 callbacks.remove(node)
#             except Exception:
#                 if log is None:
#                     log = logging.get_logger()
#                 log.exception('uncaught exception in callback')
#         if not callbacks:
#             obj._callbacks = None
# 
# Path: lib/gruvi/callbacks.py
# def has_callback(obj, handle):
#     """Return whether a callback is currently registered for an object."""
#     callbacks = obj._callbacks
#     if not callbacks:
#         return False
#     if isinstance(callbacks, Node):
#         return handle is callbacks
#     else:
#         return handle in callbacks
# 
# def clear_callbacks(obj):
#     """Remove all callbacks from an object."""
#     callbacks = obj._callbacks
#     if isinstance(callbacks, dllist):
#         # Help the garbage collector by clearing all links.
#         callbacks.clear()
#     obj._callbacks = None


import pyuv
from .callbacks import add_callback, remove_callback, walk_callbacks
from .callbacks import has_callback, clear_callbacks
# Copyright (c) 2012-2014 the Gruvi authors. See the file "AUTHORS" for a
# complete list.

from __future__ import absolute_import, print_function


__all__ = ['READABLE', 'WRITABLE']


READABLE = pyuv.UV_READABLE
WRITABLE = pyuv.UV_WRITABLE


if __debug__:

    def dump(mp):
        print('== Dumping MultiPoll {!r}'.format(mp))
        # [total, nreaders, nwriters, nreadwrite, disabled]
        stats = [0, 0, 0, 0, 0]
        def walker(callback, args):
            stats[0] += 1
            if args == READABLE:
                stats[1] += 1
            elif args == WRITABLE:
                stats[2] += 1
            elif args == READABLE|WRITABLE:
                stats[3] += 1
            elif not args:
                stats[4] += 1
            return True

# Path: lib/gruvi/callbacks.py
# def add_callback(obj, callback, args=()):
#     """Add a callback to an object."""
#     callbacks = obj._callbacks
#     node = Node(callback, args)
#     # Store a single callback directly in _callbacks
#     if callbacks is None:
#         obj._callbacks = node
#         return node
#     # Otherwise use a dllist.
#     if not isinstance(callbacks, dllist):
#         obj._callbacks = dllist()
#         obj._callbacks.insert(callbacks)
#         callbacks = obj._callbacks
#     callbacks.insert(node)
#     return node
# 
# def remove_callback(obj, handle):
#     """Remove a callback from an object."""
#     callbacks = obj._callbacks
#     if callbacks is handle:
#         obj._callbacks = None
#     elif isinstance(callbacks, dllist):
#         callbacks.remove(handle)
#         if not callbacks:
#             obj._callbacks = None
# 
# def walk_callbacks(obj, func, log=None):
#     """Call func(callback, args) for all callbacks and keep only those
#     callbacks for which the function returns True."""
#     callbacks = obj._callbacks
#     if isinstance(callbacks, Node):
#         node = callbacks
#         try:
#             if not func(node.data, node.extra):
#                 obj._callbacks = None
#         except Exception:
#             if log is None:
#                 log = logging.get_logger()
#             log.exception('uncaught exception in callback')
#     elif isinstance(callbacks, dllist):
#         for node in callbacks:
#             try:
#                 if func(node.data, node.extra):
#                     continue
#                 callbacks.remove(node)
#             except Exception:
#                 if log is None:
#                     log = logging.get_logger()
#                 log.exception('uncaught exception in callback')
#         if not callbacks:
#             obj._callbacks = None
# 
# Path: lib/gruvi/callbacks.py
# def has_callback(obj, handle):
#     """Return whether a callback is currently registered for an object."""
#     callbacks = obj._callbacks
#     if not callbacks:
#         return False
#     if isinstance(callbacks, Node):
#         return handle is callbacks
#     else:
#         return handle in callbacks
# 
# def clear_callbacks(obj):
#     """Remove all callbacks from an object."""
#     callbacks = obj._callbacks
#     if isinstance(callbacks, dllist):
#         # Help the garbage collector by clearing all links.
#         callbacks.clear()
#     obj._callbacks = None


# Path: lib/gruvi/poll.py
import pyuv
from .callbacks import add_callback, remove_callback, walk_callbacks
from .callbacks import has_callback, clear_callbacks

# Copyright (c) 2012-2014 the Gruvi authors. See the file "AUTHORS" for a
# complete list.

from __future__ import absolute_import, print_function


__all__ = ['READABLE', 'WRITABLE']


READABLE = pyuv.UV_READABLE
WRITABLE = pyuv.UV_WRITABLE


if __debug__:

    def dump(mp):
        print('== Dumping MultiPoll {!r}'.format(mp))
        # [total, nreaders, nwriters, nreadwrite, disabled]
        stats = [0, 0, 0, 0, 0]
        def walker(callback, args):
            stats[0] += 1
            if args == READABLE:
                stats[1] += 1
            elif args == WRITABLE:
                stats[2] += 1
            elif args == READABLE|WRITABLE:
                stats[3] += 1
            elif not args:
                stats[4] += 1
            return True

        walk_callbacks(mp, walker)