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:
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]
-
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
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)
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)