def _put_response(self, resp):

        if self._expecting_data:
            rlen = len(resp)
            dlen = min(self._expecting_data_len, rlen)
            if __debug__: self._log(5, '_put_response expecting data len %s, got %s' % (self._expecting_data_len, rlen))
            self._expecting_data_len -= dlen
            self._expecting_data = (self._expecting_data_len != 0)
            if rlen <= dlen:
                self._accumulated_data.append(resp)
                return
            self._accumulated_data.append(resp[:dlen])
            resp = resp[dlen:]

        if self._accumulated_data:
            typ, dat = self._literal_expected
            self._append_untagged(typ, (dat, b''.join(self._accumulated_data)))
            self._accumulated_data = []

        # Protocol mandates all lines terminated by CRLF
        resp = resp[:-2]
        if __debug__: self._log(5, '_put_response(%r)' % resp)

        if 'continuation' in self.tagged_commands:
            continuation_expected = True
        else:
            continuation_expected = False

        if self._literal_expected is not None:
            dat = resp
            if self._match(self.literal_cre, dat):
                self._literal_expected[1] = dat
                self._expecting_data = True
                self._expecting_data_len = int(self.mo.group('size'))
                if __debug__: self._log(4, 'expecting literal size %s' % self._expecting_data_len)
                return
            typ = self._literal_expected[0]
            self._literal_expected = None
            if dat:
                self._append_untagged(typ, dat)  # Tail
            if __debug__: self._log(4, 'literal completed')
        else:
            # Command completion response?
            if self._match(self.tagre, resp):
                tag = self.mo.group('tag')
                typ = str(self.mo.group('type'), 'ASCII')
                dat = self.mo.group('data')
                if typ in ('OK', 'NO', 'BAD') and self._match(self.response_code_cre, dat):
                    self._append_untagged(str(self.mo.group('type'), 'ASCII'), self.mo.group('data'))
                if not tag in self.tagged_commands:
                    if __debug__: self._log(1, 'unexpected tagged response: %r' % resp)
                else:
                    self._request_pop(tag, (typ, [dat]))
            else:
                dat2 = None

                # '*' (untagged) responses?

                if not self._match(self.untagged_response_cre, resp):
                    if self._match(self.untagged_status_cre, resp):
                        dat2 = self.mo.group('data2')

                if self.mo is None:
                    # Only other possibility is '+' (continuation) response...

                    if self._match(self.continuation_cre, resp):
                        if not continuation_expected:
                            if __debug__: self._log(1, "unexpected continuation response: '%r'" % resp)
                            return
                        self._request_pop('continuation', (True, self.mo.group('data')))
                        return

                    if __debug__: self._log(1, "unexpected response: '%r'" % resp)
                    return

                typ = str(self.mo.group('type'), 'ASCII')
                dat = self.mo.group('data')
                if dat is None: dat = b''        # Null untagged response
                if dat2: dat = dat + b' ' + dat2

                # Is there a literal to come?

                if self._match(self.literal_cre, dat):
                    self._expecting_data = True
                    self._expecting_data_len = int(self.mo.group('size'))
                    if __debug__: self._log(4, 'read literal size %s' % self._expecting_data_len)
                    self._literal_expected = [typ, dat]
                    return

                self._append_untagged(typ, dat)
                if typ in ('OK', 'NO', 'BAD') and self._match(self.response_code_cre, dat):
                    self._append_untagged(str(self.mo.group('type'), 'ASCII'), self.mo.group('data'))

                if typ != 'OK':                 # NO, BYE, IDLE
                    self._end_idle()

        # Command waiting for aborted continuation response?

        if continuation_expected:
            self._request_pop('continuation', (False, resp))

        # Bad news?

        if typ in ('NO', 'BAD', 'BYE'):
            if typ == 'BYE':
                self.Terminate = True
            if __debug__: self._log(1, '%s response: %r' % (typ, dat))
dataset/ETHPy150Open OfflineIMAP/imapfw/imapfw/imap/imaplib3/imaplib2.py/IMAP4._put_response/original