CVS: tmda/TMDA Auth.py, 1.19, 1.20 ChangeLog, 1.293, 1.294 Errors.py, 1.10, 1.11

Timothy Legant <[email protected]>
Newsgroups gmane.mail.spam.tmda.cvs
Message-ID <[email protected]>
Update of /cvsroot/tmda/tmda/TMDA
In directory sc8-pr-cvs1:/tmp/cvs-serv11682/TMDA

Modified Files:
	Auth.py ChangeLog Errors.py 
Log Message:
Cleaned up exception handling in Auth.init_auth_method (Auth.py) so
that error messages could actually be read.

Fixed IMAP4_SSL class definition in Auth.init_remote method (Auth.py).
Previously, it was defined as a local variable that just disappeared
after init_remote() finished.  Also, fixed various code issues to
bring it in line with the IMAP4_SSL distributed with Python 2.3.

Cleaned up Errors.py.  Removed __repr__ method in AddressError and
QueueError.  It is of no value in exception classes.  Renamed __repr__
to __str__ in AuthError, since it needs a customized string
representation.  In all three classes, made __init__ call the ancestor
class's __init__.



Index: Auth.py
===================================================================
RCS file: /cvsroot/tmda/tmda/TMDA/Auth.py,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- Auth.py	21 Jun 2003 02:30:17 -0000	1.19
+++ Auth.py	16 Nov 2003 21:46:46 -0000	1.20
@@ -29,6 +29,7 @@
 import md5
 import popen2
 import time
+import imaplib
 
 # TMDA imports
 import Version
@@ -152,16 +153,15 @@
         """Initializes the authentication mechanism.
         See init_file, init_checkpw, and init_remote for more details.
         """
-        try:
-            cmd = "self.init_%s(arg)" % type
-            eval( cmd )
-        except AttributeError, err:
-            self.debug( "Attribute Error: %s" % err )
-            raise ValueError, \
-                "Authentication type '%s' not recognised.\n " % type + \
-                "Must be one of %s" % repr(self.allowed_authtypes)
-        except ValueError, err:
-            raise err
+        mname = 'init_%s' % type
+        meth = getattr(self, mname, None)
+        if meth is None:
+            self.debug("Attribute Error: " \
+                       "Auth instance has no attribute '%s'" % mname)
+            raise ValueError(
+                "Authentication type '%s' not recognised.\n " \
+                "Must be one of %s" % (type, repr(self.allowed_authtypes)))
+        meth(arg)
 
     def init_file(self, file):
         """Initializes the authentication scheme with a flat file.
@@ -253,7 +253,6 @@
 
         if self.__authremote['proto'] == 'imaps':
             try:
-                import imaplib
                 self.IMAP4_SSL = imaplib.IMAP4_SSL
             except AttributeError:
                 class IMAP4_SSL(imaplib.IMAP4):
@@ -263,38 +262,44 @@
                         This connection will be used by the routines:
                         read, readline, send, shutdown.
                         """
-                        self.sock = socket.socket(socket.AF_INET, \
+                        self.host = host
+                        self.port = port
+                        self.sock = socket.socket(socket.AF_INET,
                                                   socket.SOCK_STREAM)
-                        self.sock.connect((self.host, self.port))
-                        self.sslsock = socket.ssl(self.sock)
-                        self.file = self.sock.makefile('rb')
+                        self.sock.connect((host, port))
+                        self.sslobj = socket.ssl(self.sock, None, None)
 
                     def read(self, size):
                         """Read 'size' bytes from remote."""
-                        buf = self.sslsock.read(size)
-                        return buf
+                        # sslobj.read() sometimes returns < size bytes
+                        data = self.sslobj.read(size)
+                        while len(data) < size:
+                            data += self.sslobj.read(size-len(data))
+                        return data
 
                     def readline(self):
                         """Read line from remote."""
-                        line = [ ]
-                        c = self.sslsock.read(1)
-                        while c:
-                            line.append(c)
-                            if c == '\n':
-                                break
-                            c = self.sslsock.read(1)
-                        buf = ''.join(line)
-                        return buf
+                        line = ""
+                        while 1:
+                            char = self.sslobj.read(1)
+                            line += char
+                            if char == "\n": return line
 
                     def send(self, data):
                         """Send data to remote."""
                         bytes = len(data)
                         while bytes > 0:
-                            sent = self.sslsock.write(data)
+                            sent = self.sslobj.write(data)
                             if sent == bytes:
-                                break   # avoid copy
+                                break    # avoid copy
                             data = data[sent:]
                             bytes = bytes - sent
+
+                    def shutdown(self):
+                        """Close I/O established in "open"."""
+                        self.sock.close()
+
+                self.IMAP4_SSL = locals()['IMAP4_SSL']
         elif self.__authremote['proto'] == 'ldap':
             try:
                 import ldap
@@ -470,7 +475,6 @@
             self.__authremote['port'] = authport
         port = self.__defaultauthports[self.__authremote['proto']]
         if self.__authremote['proto'] == 'imap':
-            import imaplib
             if self.__authremote['port']:
                 port = int(self.__authremote['port'])
             M = imaplib.IMAP4(self.__authremote['host'], port)
@@ -488,7 +492,6 @@
                 self.debug( "Uncaught %s: %s" % (err.__class__, err) )
                 return 0
         elif self.__authremote['proto'] == 'imaps':
-            import imaplib
             if self.__authremote['port']:
                 port = int(self.__authremote['port'])
             M = self.IMAP4_SSL(self.__authremote['host'], port)

Index: ChangeLog
===================================================================
RCS file: /cvsroot/tmda/tmda/TMDA/ChangeLog,v
retrieving revision 1.293
retrieving revision 1.294
diff -u -r1.293 -r1.294
--- ChangeLog	12 Nov 2003 03:20:08 -0000	1.293
+++ ChangeLog	16 Nov 2003 21:46:46 -0000	1.294
@@ -1,3 +1,21 @@
+2003-11-16  Tim Legant  <[email protected]>
+
+	* Auth.py (Auth.init_auth_method): Cleaned up exception handling
+	so that error messages could actually be read!
+
+	* (Auth.init_remote): Fixed IMAP4_SSL class definition.
+	Previously, it was defined as a local variable which just
+	disappeared after init_remote() finished.  Now, we store it in an
+	instance variable named IMAP4_SSL.  Also, fixed various code
+	issues to bring it in line with the version distributed with
+	Python 2.3.
+
+	* Errors.py (AddressError, QueueError): Removed __repr__ method.
+	Called ancestor's __init__.
+
+	* (AuthError): Renamed __repr__ to __str__; cleaned it up.  Called
+	ancestor's __init__.
+
 2003-11-11  Tim Legant  <[email protected]>
 
 	* AutoResponse.py (AutoResponse.__init__): Recreate incoming

Index: Errors.py
===================================================================
RCS file: /cvsroot/tmda/tmda/TMDA/Errors.py,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- Errors.py	27 Apr 2003 07:58:17 -0000	1.10
+++ Errors.py	16 Nov 2003 21:46:46 -0000	1.11
@@ -45,10 +45,7 @@
 class AddressError(TMDAError):
     """Address errors."""
     def __init__(self, errmsg = ''):
-        self.args = errmsg
-
-    def __repr__(self):
-        return self.args
+        TMDAError.__init__(errmsg)
 
 class BadCryptoError(AddressError):
     """Bad (or no) cryptographic information in address."""
@@ -60,22 +57,20 @@
 
 class QueueError(TMDAError):
     def __init__(self, errmsg = 'Unknown error'):
-        self.args = errmsg
-
-    def __repr__(self):
-        return '%s:\n%s' % (self.__class__, self.args)
+        TMDAError.__init__(errmsg)
 
 class MessageError(QueueError):
     pass
 
 class AuthError(TMDAError):
     """Authentication Errors""" 
-    def __init__(self, errmsg = 'Authentication Error', helpmsg = ''):
+    def __init__(self, errmsg='Authentication Error', helpmsg=''):
+        TMDAError.__init__(self, errmsg)
         self.msg = errmsg
         self.help = helpmsg
 
-    def __repr__(self):
-        if self.help == '':
-          return '%s: %s' % (self.__class__, self.msg)
-        else:
-          return '%s: %s\n(%s)' % (self.__class__, self.msg, self.help)
+    def __str__(self):
+        s = '%s: %s' % (self.__class__, self.msg)
+        if self.help:
+            s += '\n(%s)' % (self.help,)
+        return s
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.