]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Halfway through re-working how BODY/BODYSTRUCTURE is handled so the parser doesn...
authorMenno Smits <menno@freshfoo.com>
Fri, 3 Jun 2011 15:30:15 +0000 (16:30 +0100)
committerMenno Smits <menno@freshfoo.com>
Fri, 3 Jun 2011 15:30:15 +0000 (16:30 +0100)
The failing tests light the way.

imapclient/response_lexer.py
imapclient/response_parser.py

index 3005cb000f83ded291cb30981d806de4cd0b4052..7f42594c0a21104f777f0f8decf3d2a246395c84 100644 (file)
@@ -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]
-
         
         
index a21586dc173f86fcf6652729ee9bee555002e8cc..31581609d3036598540fec59a5f8d41f73382175 100644 (file)
@@ -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)