From: msmits Date: Mon, 15 Oct 2007 18:55:48 +0000 (+0000) Subject: Allow for handling of FETCH parts with sections, like "BODY[HEADER.FIELDS X-Git-Tag: 0.7~124 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=54b6f1b155872b58ef79931a8b7803339db3d8f8;p=imapclient.git Allow for handling of FETCH parts with sections, like "BODY[HEADER.FIELDS (FROM)]". Thanks to Brian Jackson (iggy _AT_ theiggy DOT com) for the bug report. --- diff --git a/imapclient/imapclient.py b/imapclient/imapclient.py index 6f19927..8b15e43 100644 --- a/imapclient/imapclient.py +++ b/imapclient/imapclient.py @@ -455,9 +455,9 @@ class FetchTokeniser(object): PAREN_LIST = '(?:\(.*?\))' PAIR_RE = re.compile(( - '([\w\.]+)\s+' + # name - '((?:\d+)' + # bare integer - '|(?:{\d+?})' + # IMAP literal + '([\w\.]+(?:\[[^\]]+\]+)?)\s+' + # name (matches "FOO", "FOO.BAR" & "BODY[SECTION STUFF]") + '((?:\d+)' + # bare integer + '|(?:{\d+?})' + # IMAP literal '|' + QUOTED_STRING + '|' + PAREN_LIST + ')\s*')) @@ -477,7 +477,7 @@ class FetchTokeniser(object): out = [] for m in strict_finditer(self.PAIR_RE, s): name, data = m.groups() - out.append((name, self._convert(data))) + out.append((name, self.nativefy(data))) return out def process_list(self, s): @@ -490,10 +490,10 @@ class FetchTokeniser(object): return [] out = [] for m in strict_finditer(self.DATA_RE, s): - out.append(self._convert(m.group(1))) + out.append(self.nativefy(m.group(1))) return out - def _convert(self, s): + def nativefy(self, s): if s.startswith('"'): return s[1:-1] # Debracket elif s.startswith('{'): diff --git a/imapclient/test_FetchParser.py b/imapclient/test_FetchParser.py index e802490..8de1ea0 100644 --- a/imapclient/test_FetchParser.py +++ b/imapclient/test_FetchParser.py @@ -178,6 +178,22 @@ class TestFetchParser(unittest.TestCase): } ) + def testLiteralsWithSections(self): + self._parse_test( + [('1 (BODY[TEXT] {11}', 'Hi there.\r\n'), ')'], + { 1: {'BODY[TEXT]': 'Hi there.\r\n',} } + ) + + def testLiteralsWithSectionsAndOtherParts(self): + self._parse_test( + [('1 (FLAGS (\\Seen) UID 2 BODY[HEADER.FIELDS (FROM)] {21}', + 'From: foo@foo.com\r\n'), ')'], + {2: { 'BODY[HEADER.FIELDS (FROM)]': 'From: foo@foo.com\r\n', + 'FLAGS': ['\\Seen'], + } + } + ) + def testUID(self): '''Test UID handling. The UID is returned instead of the given message ID if present.