From d3b3fd1af316e0a1c413b485a78e9023cfb3c62b Mon Sep 17 00:00:00 2001 From: Menno Smits Date: Fri, 3 Jun 2011 16:30:15 +0100 Subject: [PATCH] Halfway through re-working how BODY/BODYSTRUCTURE is handled so the parser doesn't have to treat paren lists without whitespace between them specially. The failing tests light the way. --- imapclient/response_lexer.py | 16 ++-------------- imapclient/response_parser.py | 34 ++++++++++------------------------ 2 files changed, 12 insertions(+), 38 deletions(-) diff --git a/imapclient/response_lexer.py b/imapclient/response_lexer.py index 3005cb0..7f42594 100644 --- a/imapclient/response_lexer.py +++ b/imapclient/response_lexer.py @@ -94,14 +94,10 @@ class Lexer(object): assert not token yield nextchar + read_until(stream_i, nextchar) else: - # Other punctuation, eg. "(" + # Other punctuation, eg. "(". This ends the current token. if token: yield token - if nextchar == ')' and stream_i.peek() == '(': - stream_i.next() # Read the '(' - yield ')(' - else: - yield nextchar # yield the punctuation + yield nextchar break else: if token: @@ -164,13 +160,5 @@ class PushableIterator(object): def push(self, item): self.pushed.append(item) - def peek(self, default=NO_MORE): - if not self.pushed: - try: - self.pushed.append(self.it.next()) - except StopIteration: - return default - return self.pushed[-1] - diff --git a/imapclient/response_parser.py b/imapclient/response_parser.py index a21586d..3158160 100644 --- a/imapclient/response_parser.py +++ b/imapclient/response_parser.py @@ -90,7 +90,7 @@ def parse_fetch_response(text): elif word == 'INTERNALDATE': msg_data[word] = _convert_INTERNALDATE(value) elif word in ('BODY', 'BODYSTRUCTURE'): - msg_data[word] = BodyData(value) + msg_data[word] = BodyData.create(value) else: msg_data[word] = value @@ -108,6 +108,14 @@ def _int_or_error(value, error_text): class BodyData(tuple): + @classmethod + def create(cls, response): + # In case of multipart messages we will see at least 2 tuples + # at the start. Restructure these in to a list so that the + # returned response tuple always has a consistent number of + # items regardless of whether the message is multipart or not. + raise NotImplementedError + @property def is_multipart(self): return isinstance(self[0], list) @@ -164,33 +172,11 @@ def parse_tuple(src): for token in src: if token == ")": return tuple(out) - elif token == ')(': - return parse_juxtaposed_tuples(src, out) - else: - out.append(atom(src, token)) + out.append(atom(src, token)) # no terminator raise ParseError('Tuple incomplete before "(%s"' % _fmt_tuple(out)) -def parse_juxtaposed_tuples(src, init): - out = [tuple(init)] - current = [] - for token in src: - if token in (')', ')('): - out.append(tuple(current)) - if token == ')': - return out - current = [] - else: - current.append(atom(src, token)) - - # no terminator - preceeding = ''.join('(' + _fmt_tuple(t) + ')' for t in out) - if current: - preceeding += '(' + _fmt_tuple(current) - raise ParseError('Juxtaposed tuples incomplete before "%s"' % preceeding) - - def _fmt_tuple(t): return ' '.join(str(item) for item in t) -- 2.43.0