]> khamidou — GitHub mirrors - imapclient.git/commitdiff
Lexer cleanup done. All tests passing.
authorMenno Smits <menno@freshfoo.com>
Sat, 8 May 2010 08:46:22 +0000 (09:46 +0100)
committerMenno Smits <menno@freshfoo.com>
Sat, 8 May 2010 08:46:22 +0000 (09:46 +0100)
imapclient/response_lexer.py
imapclient/response_parser.py

index 26c400cec9ab66be4653ac954a0571c1b70b3308..532ae5c22fb4bf22d67147277e447f2085d21a29 100644 (file)
@@ -1,4 +1,9 @@
-"""A lexical analyzer class for IMAP responses."""
+"""
+A lexical analyzer class for IMAP responses.
+
+Although Lexer does all the work, TokenSource is probably the class to
+use for external callers.
+"""
 
 # This was heavily inspired by (ie, ripped off from) python 2.6's shlex
 # module with further inspiration from the patch in
@@ -8,6 +13,26 @@
 __all__ = ["Lexer"]
 
 
+class TokenSource(object):
+    """
+    A simple iterator for the Lexer class that also provides access to
+    the current IMAP literal.
+    """
+
+    def __init__(self, text):
+        lex = Lexer()
+        lex.sources = (LiteralHandlingIter(lex, chunk) for chunk in text)
+        self.lex = lex
+        self.src = iter(lex)
+
+    @property
+    def current_literal(self):
+        return self.lex.current_source.literal
+
+    def __iter__(self):
+        return self.src
+    
+
 class Lexer(object):
     "A lexical analyzer class for IMAP"
 
@@ -16,10 +41,10 @@ class Lexer(object):
     ALL_CHARS = [chr(ch) for ch in range(256)]
     NON_SPECIALS = [ch for ch in ALL_CHARS if ch not in SPECIALS]
 
-    def __init__(self, sources):
+    def __init__(self):
         self.wordchars = set(self.NON_SPECIALS)
         self.whitespace = set((' \t\r\n'))
-        self.sources = (LiteralHandlingIter(self, chunk) for chunk in sources)
+        self.sources = None
         self.current_source = None
 
     def parse_quote(self, stream_i, quoted, token):
@@ -105,25 +130,6 @@ class Lexer(object):
             for tok in self.read_token_stream(iter(source)):
                 yield tok
 
-    @classmethod
-    def create_token_source(cls, text):
-        lex = cls(text)
-        return TokenIterator(lex)
-
-
-class TokenIterator(object):
-
-    def __init__(self, lex):
-        self.lex = lex
-        self.src = iter(lex)
-
-    @property
-    def current_literal(self):
-        return self.lex.current_source.literal
-
-    def __iter__(self):
-        return self.src
-    
 
 # imaplib has poor handling of 'literals' - it both fails to remove the
 # {size} marker, and fails to keep responses grouped into the same logical
@@ -145,7 +151,7 @@ class LiteralHandlingIter:
             # the literal itself.
             src_text, self.literal = resp_record
             assert src_text.endswith("}"), src_text
-            self.src_text = self.literal
+            self.src_text = src_text
         else:
             # just a line with no literals.
             self.src_text = resp_record
index eea1f9f34c0fbd26a17a5e143668b2d20437c5e5..ebc521348f2f1916e23e40fdd655e137537ae2c9 100644 (file)
@@ -10,7 +10,7 @@ Intially inspired by http://effbot.org/zone/simple-iterator-parser.htm
 import imaplib
 from datetime import datetime
 from fixed_offset import FixedOffset
-from response_lexer import Lexer
+from response_lexer import TokenSource
 
 
 __all__ = ['parse_response', 'ParseError']
@@ -31,7 +31,7 @@ def parse_response(text):
 def gen_parsed_response(text):
     if not text:
         return
-    src = Lexer.create_token_source(text)
+    src = TokenSource(text)
     
     token = None
     try: