[PATCH v2 1/3] Don't call open_new functions outside of any class

Sebastian Sebastian at SSpaeth.de
Thu Dec 16 12:43:46 GMT 2010


Move them into the correct classes, overriding the open() function.
This is what we intent to do anyway, so do it in a clean way.

Signed-Off-By: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
Actually split up this topic into 3 related patches now, and improved the
commit message per Nicolas request. Also included a description in
offlineimap.conf.

 offlineimap/imaplibutil.py |   45 ++++++++++++++++++++++---------------------
 offlineimap/imapserver.py  |    9 +-------
 2 files changed, 24 insertions(+), 30 deletions(-)

diff --git a/offlineimap/imaplibutil.py b/offlineimap/imaplibutil.py
index a60242b..bb623a1 100644
--- a/offlineimap/imaplibutil.py
+++ b/offlineimap/imaplibutil.py
@@ -110,25 +110,19 @@ def new_mesg(self, s, secs=None):
 
 class WrappedIMAP4_SSL(IMAP4_SSL):
     def open(self, host = '', port = IMAP4_SSL_PORT):
-        IMAP4_SSL.open(self, host, port)
-        self.sslobj = sslwrapper(self.sslobj)
-
-    def readline(self):
-        return self.sslobj.readline()
-
-def new_open(self, host = '', port = IMAP4_PORT):
-        """Setup connection to remote server on "host:port"
-            (default: localhost:standard IMAP4 port).
+        """Setup connection to remote server on "host:port".
+            (default: localhost:standard IMAP4 SSL port).
         This connection will be used by the routines:
             read, readline, send, shutdown.
         """
         self.host = host
         self.port = port
+        #This connects to the first ip found ipv4/ipv6
+        #Added by Adriaan Peeters <apeeters at lashout.net> based on a socket
+        #example from the python documentation:
+        #http://www.python.org/doc/lib/socket-example.html
         res = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
                                  socket.SOCK_STREAM)
-
-        # Try each address returned by getaddrinfo in turn until we
-        # manage to connect to one.
         # Try all the addresses in turn until we connect()
         last_error = 0
         for remote in res:
@@ -142,22 +136,29 @@ def new_open(self, host = '', port = IMAP4_PORT):
         if last_error != 0:
             # FIXME
             raise socket.error(last_error)
-        self.file = self.sock.makefile('rb')
+        self.sslobj = ssl_wrap(self.sock, self.keyfile, self.certfile)
+        self.sslobj = sslwrapper(self.sslobj)
 
-def new_open_ssl(self, host = '', port = IMAP4_SSL_PORT):
-        """Setup connection to remote server on "host:port".
-            (default: localhost:standard IMAP4 SSL port).
+    def readline(self):
+        return self.sslobj.readline()
+
+
+class WrappedIMAP4(IMAP4):
+    """Improved version of imaplib.IMAP4 that can also connect to IPv6"""
+
+    def open(self, host = '', port = IMAP4_PORT):
+        """Setup connection to remote server on "host:port"
+            (default: localhost:standard IMAP4 port).
         This connection will be used by the routines:
             read, readline, send, shutdown.
         """
         self.host = host
         self.port = port
-        #This connects to the first ip found ipv4/ipv6
-        #Added by Adriaan Peeters <apeeters at lashout.net> based on a socket
-        #example from the python documentation:
-        #http://www.python.org/doc/lib/socket-example.html
         res = socket.getaddrinfo(host, port, socket.AF_UNSPEC,
                                  socket.SOCK_STREAM)
+
+        # Try each address returned by getaddrinfo in turn until we
+        # manage to connect to one.
         # Try all the addresses in turn until we connect()
         last_error = 0
         for remote in res:
@@ -171,8 +172,8 @@ def new_open_ssl(self, host = '', port = IMAP4_SSL_PORT):
         if last_error != 0:
             # FIXME
             raise socket.error(last_error)
-        self.sslobj = ssl_wrap(self.sock, self.keyfile, self.certfile)
-        self.sslobj = sslwrapper(self.sslobj)
+        self.file = self.sock.makefile('rb')
+
 
 mustquote = re.compile(r"[^\w!#$%&'+,.:;<=>?^`|~-]")
 
diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py
index d9dd6ba..cf1fdc9 100644
--- a/offlineimap/imapserver.py
+++ b/offlineimap/imapserver.py
@@ -59,15 +59,12 @@ class UsefulIMAPMixIn:
     def _mesg(self, s, secs=None):
         imaplibutil.new_mesg(self, s, secs)
 
-class UsefulIMAP4(UsefulIMAPMixIn, imaplib.IMAP4):
-    def open(self, host = '', port = imaplib.IMAP4_PORT):
-        imaplibutil.new_open(self, host, port)
+class UsefulIMAP4(UsefulIMAPMixIn, imaplibutil.WrappedIMAP4):
 
     # This is a hack around Darwin's implementation of realloc() (which
     # Python uses inside the socket code). On Darwin, we split the
     # message into 100k chunks, which should be small enough - smaller
     # might start seriously hurting performance ...
-
     def read(self, size):
         if (system() == 'Darwin') and (size>0) :
             read = 0
@@ -81,12 +78,8 @@ class UsefulIMAP4(UsefulIMAPMixIn, imaplib.IMAP4):
             return imaplib.IMAP4.read (self, size)
 
 class UsefulIMAP4_SSL(UsefulIMAPMixIn, imaplibutil.WrappedIMAP4_SSL):
-    def open(self, host = '', port = imaplib.IMAP4_SSL_PORT):
-        imaplibutil.new_open_ssl(self, host, port)
-
     # This is the same hack as above, to be used in the case of an SSL
     # connexion.
-
     def read(self, size):
         if (system() == 'Darwin') and (size>0) :
             read = 0
-- 
1.7.1





More information about the OfflineIMAP-project mailing list