offlineimap rev 504

"Automatic Subversion Change Mailer" <[email protected]> Wed, 16 Jul 2003 16:56:26 -0500 (CDT)
Newsgroups gmane.mail.imap.offlineimap.subversion
Message-ID <[email protected]>
You are receiving this message because
all commits get sent to this address.

Author: jgoerzen
Date: 2003-07-16 16:56:06 -0500 (Wed, 16 Jul 2003)
New Revision: 504

Modified:
  imaplib/head/imap2/parser/Objects.py
  imaplib/head/imap2/parser/grammar.g
  imaplib/head/imap2/parser/grammar.py
  imaplib/head/imap2/parser/grammarTest.py

Log:
Responses are much better objectified now.  Just have the Message classes to
do yet.


Diff:
Modified: imaplib/head/imap2/parser/Objects.py
==============================================================================
--- imaplib/head/imap2/parser/Objects.py	2003-07-16 21:03:53 UTC (rev 503)
+++ imaplib/head/imap2/parser/Objects.py	2003-07-16 21:56:06 UTC (rev 504)
@@ -19,7 +19,9 @@
 #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 # END OF COPYRIGHT #
 
-__all__ = ['Response', 'ResponseCode', 'Capabilities']
+__all__ = ['Response', 'ResponseCode', 'Capabilities', 'Resp_Cond_Auth',
+           'Resp_Cond_Bye', 'Resp_Cond_State', 'Mailbox_Data_Tok', 'Mailbox_Data_List',
+           'Mailbox_Data_Flags', 'Mailbox_Data_Search', 'Mailbox_Data_Status']
 
 from UserDict import UserDict
 from UserList import UserList
@@ -30,15 +32,18 @@
     def __init__(self, response_done, data):
         self.result = response_done['result']
         self.donetype = response_done['donetype']
-        detail = response_done['detail']
-        self.code = None
-        if detail.has_key('code'):
-            self.code = detail['code']
-        self.text = detail['text']
+
         self.tag = None
         if response_done.has_key('tag'):
             self.tag = response_done['tag']
 
+        self.data = {}
+        for item in data:
+            key = item.getname()
+            if self.data.has_key(key):
+                raise Exceptions.StreamError, "Got two entries of type %s" % key
+            self.data[key] = item
+
         self.errorcheck()
 
     def errorcheck(self):
@@ -48,20 +53,102 @@
             raise Exceptions.StreamError, "Expected a fatal or tagged result but got %s" % self.donetype
 
     def __repr__(self):
-        return "<Response: [result=%s] [donetype=%s] [code=%s] [text=%s] [tag=%s]>" %\
-               (self.result, self.donetype, self.code, self.text, self.tag)
+        return "<Response: [result=%s] [donetype=%s] [tag=%s] [data=%s]>" %\
+               (self.result, self.donetype, self.tag, self.data)
 
 class ResponseCode:
     def __init__(self, codelist):
         self.codename = codelist[0]
         self.codeinfo = codelist[1:]
 
+    def getname(self):
+        return self.codename
+
     def __repr__(self):
         return "<ResponseCode: [codename=%s] [codeinfo=%s]>" % \
                (self.codename, self.codeinfo)
 
-class Capabilities(UserList):
+class Capabilities:
     """Stores capabilities.  You may access this as a list of strings."""
     def __init__(self, capabilities):
         self.data = capabilities
 
+    def getname(self):
+        return "Capabilities"
+
+    def __repr__(self):
+        return "<%s %s>" % (self.__class__.__name__,
+                            self.data)
+
+class Resp_Cond_Generic:
+    def __init__(self, result, detail):
+        self.result = result
+        self.detail = detail
+
+        self.code = None
+        if detail.has_key('code'):
+            self.code = detail['code']
+        self.text = detail['text']
+
+    def getcode(self):
+        return self.code
+
+    def getname(self):
+        return "Resp." + self.code.getname()
+
+    def __repr__(self):
+        return "<%s: [result=%s] [code=%s] [text=%s]>" % \
+               (self.__class__.__name__, self.result, self.code, self.text)
+
+class Resp_Cond_State(Resp_Cond_Generic):
+    pass
+
+class Resp_Cond_Bye(Resp_Cond_Generic):
+    pass
+
+class Resp_Cond_Auth(Resp_Cond_Generic):
+    pass
+
+class Mailbox_Data_Generic:
+    def __init__(self, data):
+        self.data = data
+
+    def getname(self):
+        return self.__class__.__name__
+
+    def __repr__(self):
+        return "<%s: %s>" % (self.__class__.__name__, self.data)
+
+
+class Mailbox_Data_Flags(Mailbox_Data_Generic):
+    pass
+
+
+class Mailbox_Data_List(Mailbox_Data_Generic):
+    pass
+
+class Mailbox_Data_Search(Mailbox_Data_Generic):
+    pass
+
+class Mailbox_Data_Status(Mailbox_Data_Generic):
+    def __init__(self, mailbox, status_att_list):
+        self.mailbox = mailbox
+        self.status_att_list = status_att_list
+
+    def __repr__(self):
+        return "<%s: [mailbox=%s] [status_att_list=%s]>" % \
+               (self.__class__.__name__, self.mailbox, self.status_att_list)
+
+class Mailbox_Data_Tok(Mailbox_Data_Generic):
+    def __init__(self, tok, num):
+        self.num = num
+        self.tok = tok.upper()
+
+    def getname(self):
+        return "Mailbox_Data." + self.tok
+
+    def __repr__(self):
+        return "<%s: [infotype=%s] [number=%s]>" % \
+               (self.__class__.__name__, self.tok, self.num)
+    
+

Modified: imaplib/head/imap2/parser/grammar.g
==============================================================================
--- imaplib/head/imap2/parser/grammar.g	2003-07-16 21:03:53 UTC (rev 503)
+++ imaplib/head/imap2/parser/grammar.g	2003-07-16 21:56:06 UTC (rev 504)
@@ -324,15 +324,17 @@
 
     rule mailbox: ("INBOX" {{ v = "INBOX" }} | astring {{ v = astring }} )
                   {{ return v }}
-    rule mailbox_data: {{ v = {} }}
-      ("(?i)FLAGS" SP flag_list {{ v['flags'] = flag_list}}
-       | "(?i)(LIST|LSUB)" SP mailbox_list {{ v['list'] = mailbox_list}}
-       | "(?i)SEARCH" {{ v['search'] = [] }}
-                (SP nz_number {{ v['search'].append(nz_number) }} )*
-       | "(?i)STATUS" SP mailbox {{ v['status'] = {'mailbox': mailbox} }}
-          SP LPAREN [status_att_list {{v['status']['att'] = status_att_list }}
+    rule mailbox_data: 
+      ("(?i)FLAGS" SP flag_list {{ v = Mailbox_Data_Flags(flag_list)}}
+       | "(?i)(LIST|LSUB)" SP mailbox_list {{ v = Mailbox_Data_List(mailbox_list) }}
+       | "(?i)SEARCH" {{ x = [] }}
+                (SP nz_number {{ x.append(nz_number) }} )*
+                {{ v = Mailbox_Data_Search(x) }}
+       | "(?i)STATUS" SP mailbox {{ x = {} }}
+          SP LPAREN [status_att_list {{x = status_att_list }}
                      ] RPAREN
-       | number SP mailbox_data_tok {{ v[mailbox_data_tok.upper()] = number}}
+                {{ v = Mailbox_Data_Status(mailbox, x) }}
+       | number SP mailbox_data_tok {{ v = Mailbox_Data_Tok(mailbox_data_tok, number) }}
        ) {{ return v }}
     rule mailbox_list: {{ v = {} }}
        LPAREN [mbx_list_flags {{ v['flags'] = mbx_list_flags }} ] RPAREN
@@ -433,19 +435,15 @@
     rule response_done: (response_tagged {{ v = response_tagged}}
                          | response_fatal {{ v = response_fatal}}) {{ return v}}
     rule response_fatal: ASTERISK SP resp_cond_bye CRLF 
-            {{ v = {'donetype': 'fatal'} }}
-            {{ v.update(resp_cond_bye)}}
-            {{ return v }}
+            {{ return {'donetype': 'fatal', 'result': resp_cond_bye} }}
     rule response_tagged: tag SP resp_cond_state CRLF
-       {{ v = {'tag': tag, 'donetype':'tagged'} }}
-       {{ v.update(resp_cond_state) }}
-       {{ return v }}
+       {{ return {'tag': tag, 'donetype':'tagged','result': resp_cond_state} }}
     rule resp_cond_auth: resp_cond_auth_tok SP resp_text
-       {{ return {'result': resp_cond_auth_tok, 'detail': resp_text} }}
+       {{ return Resp_Cond_Auth(resp_cond_auth_tok, resp_text) }}
     rule resp_cond_bye: r'(?i)BYE' SP resp_text
-       {{ return {'result': 'BYE', 'detail': resp_text} }}
+       {{ return Resp_Cond_Bye('BYE', resp_text) }}
     rule resp_cond_state: resp_cond_state_tok SP resp_text
-       {{ return {'result': resp_cond_state_tok, 'detail': resp_text} }}
+       {{ return Resp_Cond_State(resp_cond_state_tok, resp_text) }}
     rule resp_text: {{ v = {} }}
        [LBRACKET resp_text_code RBRACKET SP {{v['code'] = resp_text_code}} ]
        text {{ v['text'] = text}} {{ return v}}

Modified: imaplib/head/imap2/parser/grammar.py
==============================================================================
--- imaplib/head/imap2/parser/grammar.py	2003-07-16 21:03:53 UTC (rev 503)
+++ imaplib/head/imap2/parser/grammar.py	2003-07-16 21:56:06 UTC (rev 504)
@@ -1,5 +1,5 @@
 # Implementation of grammar from RFC3501
-# $Id: grammar.g 499 2003-07-16 20:30:49Z jgoerzen $
+# $Id: grammar.g 502 2003-07-16 21:03:53Z jgoerzen $
 
 # COPYRIGHT #
 # Copyright (C) 2003 John Goerzen
@@ -738,41 +738,42 @@
         return v
 
     def mailbox_data(self):
-        v = {}
         _token_ = self._peek('"(?i)FLAGS"', '"(?i)(LIST|LSUB)"', '"(?i)SEARCH"', '"(?i)STATUS"', 'number_tok')
         if _token_ == '"(?i)FLAGS"':
             self._scan('"(?i)FLAGS"')
             SP = self._scan('SP')
             flag_list = self.flag_list()
-            v['flags'] = flag_list
+            v = Mailbox_Data_Flags(flag_list)
         elif _token_ == '"(?i)(LIST|LSUB)"':
             self._scan('"(?i)(LIST|LSUB)"')
             SP = self._scan('SP')
             mailbox_list = self.mailbox_list()
-            v['list'] = mailbox_list
+            v = Mailbox_Data_List(mailbox_list)
         elif _token_ == '"(?i)SEARCH"':
             self._scan('"(?i)SEARCH"')
-            v['search'] = []
+            x = []
             while self._peek('SP', 'CRLF') == 'SP':
                 SP = self._scan('SP')
                 nz_number = self.nz_number()
-                v['search'].append(nz_number)
+                x.append(nz_number)
+            v = Mailbox_Data_Search(x)
         elif _token_ == '"(?i)STATUS"':
             self._scan('"(?i)STATUS"')
             SP = self._scan('SP')
             mailbox = self.mailbox()
-            v['status'] = {'mailbox': mailbox}
+            x = {}
             SP = self._scan('SP')
             LPAREN = self._scan('LPAREN')
             if self._peek('RPAREN', 'status_att') == 'status_att':
                 status_att_list = self.status_att_list()
-                v['status']['att'] = status_att_list
+                x = status_att_list
             RPAREN = self._scan('RPAREN')
+            v = Mailbox_Data_Status(mailbox, x)
         else: # == 'number_tok'
             number = self.number()
             SP = self._scan('SP')
             mailbox_data_tok = self._scan('mailbox_data_tok')
-            v[mailbox_data_tok.upper()] = number
+            v = Mailbox_Data_Tok(mailbox_data_tok, number)
         return v
 
     def mailbox_list(self):
@@ -1039,36 +1040,32 @@
         SP = self._scan('SP')
         resp_cond_bye = self.resp_cond_bye()
         CRLF = self._scan('CRLF')
-        v = {'donetype': 'fatal'}
-        v.update(resp_cond_bye)
-        return v
+        return {'donetype': 'fatal', 'result': resp_cond_bye}
 
     def response_tagged(self):
         tag = self.tag()
         SP = self._scan('SP')
         resp_cond_state = self.resp_cond_state()
         CRLF = self._scan('CRLF')
-        v = {'tag': tag, 'donetype':'tagged'}
-        v.update(resp_cond_state)
-        return v
+        return {'tag': tag, 'donetype':'tagged','result': resp_cond_state}
 
     def resp_cond_auth(self):
         resp_cond_auth_tok = self._scan('resp_cond_auth_tok')
         SP = self._scan('SP')
         resp_text = self.resp_text()
-        return {'result': resp_cond_auth_tok, 'detail': resp_text}
+        return Resp_Cond_Auth(resp_cond_auth_tok, resp_text)
 
     def resp_cond_bye(self):
         self._scan("r'(?i)BYE'")
         SP = self._scan('SP')
         resp_text = self.resp_text()
-        return {'result': 'BYE', 'detail': resp_text}
+        return Resp_Cond_Bye('BYE', resp_text)
 
     def resp_cond_state(self):
         resp_cond_state_tok = self._scan('resp_cond_state_tok')
         SP = self._scan('SP')
         resp_text = self.resp_text()
-        return {'result': resp_cond_state_tok, 'detail': resp_text}
+        return Resp_Cond_State(resp_cond_state_tok, resp_text)
 
     def resp_text(self):
         v = {}

Modified: imaplib/head/imap2/parser/grammarTest.py
==============================================================================
--- imaplib/head/imap2/parser/grammarTest.py	2003-07-16 21:03:53 UTC (rev 503)
+++ imaplib/head/imap2/parser/grammarTest.py	2003-07-16 21:56:06 UTC (rev 504)
@@ -30,7 +30,7 @@
     def test_ex_Connect(self):
         result = parse("* OK IMAP4rev1 Service Ready\r\n", 'goal_connect')
         self.assertEquals(repr(result),
-                          "{'result': 'OK', 'detail': {'text': 'IMAP4rev1 Service Ready'}}")
+                          '<Resp_Cond_State: [result=OK] [code=None] [text=IMAP4rev1 Service Ready]>')
 
     def test_ex_Login(self):
         result = parse("a001 OK LOGIN completed\r\n")
@@ -50,7 +50,7 @@
     def test_Courier_Connect(self):
         result = parse("* OK [CAPABILITY IMAP4rev1 CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA IDLE AUTH=PLAIN] Courier-IMAP ready. Copyright 1998-2003 Double Precision, Inc.  See COPYING for distribution information.\r\n", "goal_connect")
         self.assertEquals(repr(result),
-                          "{'result': 'OK', 'detail': {'text': 'Courier-IMAP ready. Copyright 1998-2003 Double Precision, Inc.  See COPYING for distribution information.', 'code': <ResponseCode: [codename=['IMAP4rev1', 'CHILDREN', 'NAMESPACE', 'THREAD=ORDEREDSUBJECT', 'THREAD=REFERENCES', 'SORT', 'QUOTA', 'IDLE', 'AUTH=PLAIN']] [codeinfo=[]]>}}")
+                          "<Resp_Cond_State: [result=OK] [code=<ResponseCode: [codename=['IMAP4rev1', 'CHILDREN', 'NAMESPACE', 'THREAD=ORDEREDSUBJECT', 'THREAD=REFERENCES', 'SORT', 'QUOTA', 'IDLE', 'AUTH=PLAIN']] [codeinfo=[]]>] [text=Courier-IMAP ready. Copyright 1998-2003 Double Precision, Inc.  See COPYING for distribution information.]>")
 
     def test_Courier_Capability(self):
         result = parse("* CAPABILITY IMAP4rev1 CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA IDLE\r\n2 OK CAPABILITY completed\r\n")