# Path: imap_tools/utils.py
# def check_command_status(command_result: tuple, exception: type, expected='OK'):
#     """
#     Check that IMAP command responses status equals <expected> status
#     If not, raise specified <exception>
#     :param command_result: imap command result: tuple(typ, data)
#     :param exception: exception subclass of UnexpectedCommandStatusError, that raises
#     :param expected: expected command status
#     """
#     typ, data = command_result[0], command_result[1]
#     if typ != expected:
#         raise exception(command_result=command_result, expected=expected)
# 
# Path: imap_tools/errors.py
# class MailboxTaggedResponseError(UnexpectedCommandStatusError):
#     pass


import sys
import socket
import select
import imaplib
from typing import Optional, List
from .utils import check_command_status
from .errors import MailboxTaggedResponseError
        poller = select.poll()
        poller.register(sock.fileno(), select.POLLIN)
        timeout = None if timeout is None else timeout * 1000
        return poller.poll(timeout)
    else:
        # select.select fails if your process has more than 1024 file descriptors, needs for windows and some other
        return select.select([sock], [], [], timeout)[0]


class IdleManager:
    """
    Mailbox IDLE logic
    Info about IMAP4 IDLE command at rfc2177
    Workflow examples:
    1.
        mailbox.idle.start()
        resps = mailbox.idle.poll(timeout=60)
        mailbox.idle.stop()
    2.
        resps = mailbox.idle.wait(timeout=60)
    """

    def __init__(self, mailbox):
        self.mailbox = mailbox
        self._idle_tag = None

    def start(self):
        """Switch on mailbox IDLE mode"""
        self._idle_tag = self.mailbox.box._command('IDLE')  # b'KLIG3'
        result = self.mailbox.box._get_response()

# Path: imap_tools/utils.py
# def check_command_status(command_result: tuple, exception: type, expected='OK'):
#     """
#     Check that IMAP command responses status equals <expected> status
#     If not, raise specified <exception>
#     :param command_result: imap command result: tuple(typ, data)
#     :param exception: exception subclass of UnexpectedCommandStatusError, that raises
#     :param expected: expected command status
#     """
#     typ, data = command_result[0], command_result[1]
#     if typ != expected:
#         raise exception(command_result=command_result, expected=expected)
# 
# Path: imap_tools/errors.py
# class MailboxTaggedResponseError(UnexpectedCommandStatusError):
#     pass


# Path: imap_tools/idle.py
import sys
import socket
import select
import imaplib
from typing import Optional, List
from .utils import check_command_status
from .errors import MailboxTaggedResponseError

        poller = select.poll()
        poller.register(sock.fileno(), select.POLLIN)
        timeout = None if timeout is None else timeout * 1000
        return poller.poll(timeout)
    else:
        # select.select fails if your process has more than 1024 file descriptors, needs for windows and some other
        return select.select([sock], [], [], timeout)[0]


class IdleManager:
    """
    Mailbox IDLE logic
    Info about IMAP4 IDLE command at rfc2177
    Workflow examples:
    1.
        mailbox.idle.start()
        resps = mailbox.idle.poll(timeout=60)
        mailbox.idle.stop()
    2.
        resps = mailbox.idle.wait(timeout=60)
    """

    def __init__(self, mailbox):
        self.mailbox = mailbox
        self._idle_tag = None

    def start(self):
        """Switch on mailbox IDLE mode"""
        self._idle_tag = self.mailbox.box._command('IDLE')  # b'KLIG3'
        result = self.mailbox.box._get_response()

        check_command_status((result, 'IDLE start'), MailboxTaggedResponseError, expected=None)