# Path: zorro/util.py
# class priorityqueue(object):
# 
#     def __init__(self):
#         self.heap = []
#         self.counter = 0
# 
#     def add(self, pri, task):
#         self.counter += 1
#         item = [pri, self.counter, task]
#         heappush(self.heap, item)
#         def remover(*a):
#             item[2] = None
#         return remover
# 
#     def min(self):
#         while self.heap and self.heap[0][2] is None:
#             heappop(self.heap)
#         if self.heap:
#             return self.heap[0][0]
#         return None
# 
#     def pop(self, value):
#         val = self.min()
#         if val is not None and val <= value:
#             return self.heap[0][2]
#         return None
# 
#     def __bool__(self):
#         return bool(self.heap)
# 
# class orderedset(object):
# 
#     def __init__(self):
#         self.deque = deque()
# 
#     def add(self, *val):
#         self.deque.append(val)
#         def remover(*a):
#             self.deque.remove(val)
#         return remover
# 
#     def update(self, value):
#         self.deque.extend(value)
# 
#     def first(self):
#         if self.deque:
#             return self.deque[0]
#         return None
# 
#     def remove(self, value):
#         self.deque.remove(value)
# 
#     def __bool__(self):
#         return bool(self.deque)
# 
# def socket_pair():
#     a, b = socketpair()
#     a.setblocking(0)
#     b.setblocking(0)
#     return a, b
# 
# class marker_object(object):
#     __slots__ = ('name',)
# 
#     def __init__(self, name):
#         self.name = name
# 
#     def __repr__(self):
#         return '<{}>'.format(self.name)


import heapq
import time
import threading
import select
import weakref
import logging
import errno
import greenlet
from collections import deque, defaultdict
from functools import partial
from operator import methodcaller
from math import ceil
from .util import priorityqueue, orderedset, socket_pair, marker_object
    def __init__(self, poll):
        self._poll = poll
        for name in self.delegate_methods:
            setattr(self, name, getattr(poll, name))

    def poll(self, timeout=-1, maxevents=-1):
        if timeout > 0:
            return self._poll.poll(timeout/1000, maxevents)
        else:
            return self._poll.poll(timeout, maxevents)


class Hub(object):
    def __init__(self):
        self._log = logging.getLogger('zorro.hub.{:x}'.format(id(self)))
        self._queue = orderedset()
        try:
            self._poller = EpollWrapper(select.epoll())
            self.POLLIN = select.EPOLLIN
            self.POLLOUT = select.EPOLLOUT
            self.POLLHUP = select.EPOLLHUP
            self.POLLERR = select.EPOLLERR
        except AttributeError:
            self._poller = select.poll()
            self.POLLIN = select.POLLIN
            self.POLLOUT = select.POLLOUT
            self.POLLHUP = select.POLLHUP
            self.POLLERR = select.POLLERR
            self._log.info("Using poller %r", self._poller)
        self._filedes = methodcaller('fileno')

# Path: zorro/util.py
# class priorityqueue(object):
# 
#     def __init__(self):
#         self.heap = []
#         self.counter = 0
# 
#     def add(self, pri, task):
#         self.counter += 1
#         item = [pri, self.counter, task]
#         heappush(self.heap, item)
#         def remover(*a):
#             item[2] = None
#         return remover
# 
#     def min(self):
#         while self.heap and self.heap[0][2] is None:
#             heappop(self.heap)
#         if self.heap:
#             return self.heap[0][0]
#         return None
# 
#     def pop(self, value):
#         val = self.min()
#         if val is not None and val <= value:
#             return self.heap[0][2]
#         return None
# 
#     def __bool__(self):
#         return bool(self.heap)
# 
# class orderedset(object):
# 
#     def __init__(self):
#         self.deque = deque()
# 
#     def add(self, *val):
#         self.deque.append(val)
#         def remover(*a):
#             self.deque.remove(val)
#         return remover
# 
#     def update(self, value):
#         self.deque.extend(value)
# 
#     def first(self):
#         if self.deque:
#             return self.deque[0]
#         return None
# 
#     def remove(self, value):
#         self.deque.remove(value)
# 
#     def __bool__(self):
#         return bool(self.deque)
# 
# def socket_pair():
#     a, b = socketpair()
#     a.setblocking(0)
#     b.setblocking(0)
#     return a, b
# 
# class marker_object(object):
#     __slots__ = ('name',)
# 
#     def __init__(self, name):
#         self.name = name
# 
#     def __repr__(self):
#         return '<{}>'.format(self.name)


# Path: zorro/core.py
import heapq
import time
import threading
import select
import weakref
import logging
import errno
import greenlet
from collections import deque, defaultdict
from functools import partial
from operator import methodcaller
from math import ceil
from .util import priorityqueue, orderedset, socket_pair, marker_object

    def __init__(self, poll):
        self._poll = poll
        for name in self.delegate_methods:
            setattr(self, name, getattr(poll, name))

    def poll(self, timeout=-1, maxevents=-1):
        if timeout > 0:
            return self._poll.poll(timeout/1000, maxevents)
        else:
            return self._poll.poll(timeout, maxevents)


class Hub(object):
    def __init__(self):
        self._log = logging.getLogger('zorro.hub.{:x}'.format(id(self)))
        self._queue = orderedset()
        try:
            self._poller = EpollWrapper(select.epoll())
            self.POLLIN = select.EPOLLIN
            self.POLLOUT = select.EPOLLOUT
            self.POLLHUP = select.EPOLLHUP
            self.POLLERR = select.EPOLLERR
        except AttributeError:
            self._poller = select.poll()
            self.POLLIN = select.POLLIN
            self.POLLOUT = select.POLLOUT
            self.POLLHUP = select.POLLHUP
            self.POLLERR = select.POLLERR
            self._log.info("Using poller %r", self._poller)
        self._filedes = methodcaller('fileno')

        self._timeouts = priorityqueue()