Don't call open_new functions outside of any class

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@SSpaeth.de>
Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
Sebastian 2010-12-16 12:43:46 +00:00 committed by Nicolas Sebrecht
parent 800138d8a4
commit 219eb8c47f
2 changed files with 37 additions and 43 deletions

View File

@ -110,41 +110,6 @@ 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).
This connection will be used by the routines:
read, readline, send, shutdown.
"""
self.host = host
self.port = port
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:
af, socktype, proto, canonname, sa = remote
self.sock = socket.socket(af, socktype, proto)
last_error = self.sock.connect_ex(sa)
if last_error == 0:
break
else:
self.sock.close()
if last_error != 0:
# FIXME
raise socket.error(last_error)
self.file = self.sock.makefile('rb')
def new_open_ssl(self, host = '', port = IMAP4_SSL_PORT):
"""Setup connection to remote server on "host:port".
(default: localhost:standard IMAP4 SSL port).
This connection will be used by the routines:
@ -174,6 +139,42 @@ def new_open_ssl(self, host = '', port = IMAP4_SSL_PORT):
self.sslobj = ssl_wrap(self.sock, self.keyfile, self.certfile)
self.sslobj = sslwrapper(self.sslobj)
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
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:
af, socktype, proto, canonname, sa = remote
self.sock = socket.socket(af, socktype, proto)
last_error = self.sock.connect_ex(sa)
if last_error == 0:
break
else:
self.sock.close()
if last_error != 0:
# FIXME
raise socket.error(last_error)
self.file = self.sock.makefile('rb')
mustquote = re.compile(r"[^\w!#$%&'+,.:;<=>?^`|~-]")
def Internaldate2epoch(resp):

View File

@ -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