'Invoke IDLE mode until timeout or self.stop() is invoked.'

def __idle(self):

    def callback(args):
        'IDLE    callback    function    invoked    by    imaplib2.\n\n                                                This    is    invoked    when    a)    The    IMAP    server    tells    us    something\n                                                while    in    IDLE    mode,    b)    we    get    an    Exception    (e.g.    on    dropped\n                                                connections,    or    c)    the    standard    imaplib    IDLE    timeout    of    29\n                                                minutes    kicks    in.'
        (result, cb_arg, exc_data) = args
        if ((exc_data is None) and (not self.stop_sig.isSet())):
            self.needsync = True
        self.stop_sig.set()
    def noop(imapobj):
        'Factorize    the    noop    code.'
        try:
            imapobj.noop()
        except imapobj.abort:
            self.ui.warn(('Attempting    NOOP    on    dropped    connection    %s' % imapobj.identifier))
            self.parent.releaseconnection(imapobj, True)
        else:
            self.parent.releaseconnection(imapobj)
    while (not self.stop_sig.isSet()):
        self.needsync = False
        success = False
        while (not success):
            imapobj = self.parent.acquireconnection()
            try:
                imapobj.select(self.folder)
            except OfflineImapError as e:
                if (e.severity == OfflineImapError.ERROR.FOLDER_RETRY):
                    self.ui.error(e, exc_info()[2])
                    self.parent.releaseconnection(imapobj, True)
                elif (e.severity == OfflineImapError.ERROR.FOLDER):
                    self.ui.error(e, exc_info()[2])
                else:
                    raise
            else:
                success = True
        if ('IDLE' in imapobj.capabilities):
            imapobj.idle(callback=callback)
        else:
            self.ui.warn(("IMAP    IDLE    not    supported    on    server    '%s'.Sleep    until    next    refresh    cycle." % imapobj.identifier))
            noop(imapobj)
        self.stop_sig.wait()
        noop(imapobj)
        if self.needsync:
            self.stop_sig.clear()
            self.__dosync()
