def _command(self, name, *args, **kw):

        if Commands[name][CMD_VAL_ASYNC]:
            cmdtyp = 'async'
        else:
            cmdtyp = 'sync'

        if __debug__: self._log(1, '[%s] %s %s' % (cmdtyp, name, args))

        if __debug__: self._log(3, 'state_change_pending.acquire')
        self.state_change_pending.acquire()

        self._end_idle()

        if cmdtyp == 'async':
            self.state_change_pending.release()
            if __debug__: self._log(3, 'state_change_pending.release')
        else:
            # Need to wait for all async commands to complete
            self._check_bye()
            self.commands_lock.acquire()
            if self.tagged_commands:
                self.state_change_free.clear()
                need_event = True
            else:
                need_event = False
            self.commands_lock.release()
            if need_event:
                if __debug__: self._log(3, 'sync command %s waiting for empty commands Q' % name)
                self.state_change_free.wait(threading.TIMEOUT_MAX)
                if __debug__: self._log(3, 'sync command %s proceeding' % name)

        if self.state not in Commands[name][CMD_VAL_STATES]:
            self.literal = None
            raise self.error('command %s illegal in state %s'
                                % (name, self.state))

        self._check_bye()

        if name in ('EXAMINE', 'SELECT'):
            self.commands_lock.acquire()
            self.untagged_responses = []      # Flush all untagged responses
            self.commands_lock.release()
        else:
            for typ in ('OK', 'NO', 'BAD'):
                while self._get_untagged_response(typ):
                    continue

            if not self.is_readonly and self._get_untagged_response('READ-ONLY', leave=True):
                self.literal = None
                raise self.readonly('mailbox status changed to READ-ONLY')

        if self.Terminate:
            raise self.abort('connection closed')

        rqb = self._request_push(name=name, **kw)

        name = bytes(name, 'ASCII')
        data = rqb.tag + b' ' + name
        for arg in args:
            if arg is None: continue
            if isinstance(arg, str):
                arg = bytes(arg, "ASCII")
            data = data + b' ' + arg

        literal = self.literal
        if literal is not None:
            self.literal = None
            if type(literal) is type(self._command):
                literator = literal
            else:
                literator = None
                data = data + bytes(' {%s}' % len(literal), 'ASCII')

        if __debug__: self._log(4, 'data=%r' % data)

        rqb.data = data + CRLF

        if literal is None:
            self.ouq.put(rqb)
            return rqb

        # Must setup continuation expectancy *before* ouq.put 
        crqb = self._request_push(name=name, tag='continuation')

        self.ouq.put(rqb)

        while True:
            # Wait for continuation response

            ok, data = crqb.get_response('command: %s => %%s' % name)
            if __debug__: self._log(4, 'continuation => %s, %r' % (ok, data))

            # NO/BAD response?

            if not ok:
                break

            # Send literal

            if literator is not None:
                literal = literator(data, rqb)

            if literal is None:
                break

            if literator is not None:
                # Need new request for next continuation response
                crqb = self._request_push(name=name, tag='continuation')

            if __debug__: self._log(4, 'write literal size %s' % len(literal))
            crqb.data = literal + CRLF
            self.ouq.put(crqb)

            if literator is None:
                break

        return rqb
dataset/ETHPy150Open OfflineIMAP/imapfw/imapfw/imap/imaplib3/imaplib2.py/IMAP4._command/original