Show
Ignore:
Timestamp:
11/07/07 09:23:09 (1 year ago)
Author:
alberto
Message:

Applied reindent.py an all .py files to switch from tabs to 4 spaces. Closes #66

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/tests/lib/smtp_mailsink.py

    r53 r67  
    11# -*- coding: UTF-8 -*- 
    2 """A library which implements a SMTP mail sink (dummy SMTP server) in order  
     2"""A library which implements a SMTP mail sink (dummy SMTP server) in order 
    33to test correct sending of emails.""" 
    44 
     
    1717# You should have received a copy of the GNU Lesser General Public 
    1818# License along with this library; if not, write to the Free Software 
    19 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301      USA  
     19# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301      USA 
    2020 
    2121 
    22 # This coded is based on code published on  
    23 # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440690/ but was  
     22# This coded is based on code published on 
     23# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/440690/ but was 
    2424# heavily modified for easier usage and Python 2.3 compatibility. 
    2525# 
     
    2727# Written by Adam Feuer, Matt Branthwaite, and Troy Frever 
    2828# Published under the Python License (http://www.python.org/license). 
    29   
     29 
    3030# Example usage: 
    31 #  
     31# 
    3232# #!/usr/bin/env python 
    3333# # -*- coding: UTF-8 -*- 
    3434# "Wait until a single message was received on localhost/port 10026" 
    35 #  
     35# 
    3636# from smtp_mailsink import SMTPMailsink 
    3737# sink = SMTPMailsink(host='localhost', port=10026) 
    3838# sink.start() 
    3939# while not sink.has_message(): 
    40 #         time.sleep(1) 
     40#         time.sleep(1) 
    4141# print "received message: " + str(sink.get_messages()) 
    4242# sink.stop() 
     
    5252 
    5353class SMTPMailsinkServer(smtpd.SMTPServer): 
    54         """This is the actual mailsink server which stores all received messages in 
    55         an internal queue. Do not access the queue directly. All accessor methods 
    56         in this object are sufficiently guarded against race conditions.""" 
    57          
    58         def __init__( self, *args, **kwargs): 
    59                 smtpd.SMTPServer.__init__( self, *args, **kwargs ) 
    60                 self.queued_mails = [] 
    61                 self.lock = threading.Lock() 
     54    """This is the actual mailsink server which stores all received messages in 
     55    an internal queue. Do not access the queue directly. All accessor methods 
     56    in this object are sufficiently guarded against race conditions.""" 
    6257 
    63         def has_message(self): 
    64                 """Return True if at least one message was received successfully. The  
    65                 access to the internal queue is synchronized so the caller will be  
    66                 blocked until the necessary lock was aquired.""" 
    67                 self.lock.acquire() 
    68                 number_messages = len(self.queued_mails) 
    69                 self.lock.release() 
    70                 return number_messages > 0 
     58    def __init__( self, *args, **kwargs): 
     59        smtpd.SMTPServer.__init__( self, *args, **kwargs ) 
     60        self.queued_mails = [] 
     61        self.lock = threading.Lock() 
    7162 
    72         def get_messages(self): 
    73                 """Return a copy of the internal queue with all received messages. The  
    74                 access to the internal queue is synchronized so the caller will be  
    75                 blocked until the necessary lock was aquired.""" 
    76                 self.lock.acquire() 
    77                 messages = copy.copy(self.queued_mails) 
    78                 self.lock.release() 
    79                 return messages 
     63    def has_message(self): 
     64        """Return True if at least one message was received successfully. The 
     65        access to the internal queue is synchronized so the caller will be 
     66        blocked until the necessary lock was aquired.""" 
     67        self.lock.acquire() 
     68        number_messages = len(self.queued_mails) 
     69        self.lock.release() 
     70        return number_messages > 0 
    8071 
    81         def pop(self, index=None): 
    82                 """Return the index'th message in the queue (default=last) which is 
    83                 removed from the queue afterwards. Throws IndexError if index is bigger  
    84                 than the number of messages in the queue.""" 
    85                 item = None 
    86                 self.lock.acquire() 
    87                 if index == None: 
    88                         index = len(self.queued_mails) - 1 
    89                 try: 
    90                         item = self.queued_mails.pop(index) 
    91                 except Exception: 
    92                         print 'pop: before lock release' 
    93                         self.lock.release() 
    94                         raise 
    95                 self.lock.release() 
    96                 return item 
     72    def get_messages(self): 
     73        """Return a copy of the internal queue with all received messages. The 
     74        access to the internal queue is synchronized so the caller will be 
     75        blocked until the necessary lock was aquired.""" 
     76        self.lock.acquire() 
     77        messages = copy.copy(self.queued_mails) 
     78        self.lock.release() 
     79        return messages 
     80 
     81    def pop(self, index=None): 
     82        """Return the index'th message in the queue (default=last) which is 
     83        removed from the queue afterwards. Throws IndexError if index is bigger 
     84        than the number of messages in the queue.""" 
     85        item = None 
     86        self.lock.acquire() 
     87        if index == None: 
     88            index = len(self.queued_mails) - 1 
     89        try: 
     90            item = self.queued_mails.pop(index) 
     91        except Exception: 
     92            print 'pop: before lock release' 
     93            self.lock.release() 
     94            raise 
     95        self.lock.release() 
     96        return item 
    9797 
    9898 
    99         def process_message(self, peer, mailfrom, rcpttos, data): 
    100                 "Store a received message in the internal queue. For internal use only!" 
    101                 msg = {'client': peer, 'from': mailfrom, 'recipients': rcpttos,  
    102                            'mail': data} 
    103                 self.lock.acquire() 
    104                 self.queued_mails.append(msg) 
    105                 self.lock.release() 
     99    def process_message(self, peer, mailfrom, rcpttos, data): 
     100        "Store a received message in the internal queue. For internal use only!" 
     101        msg = {'client': peer, 'from': mailfrom, 'recipients': rcpttos, 
     102                   'mail': data} 
     103        self.lock.acquire() 
     104        self.queued_mails.append(msg) 
     105        self.lock.release() 
    106106 
    107107 
    108108class SMTPMailsink(threading.Thread): 
    109         """This class is responsible for controlling the actual mailsink server  
    110         class."""        
     109    """This class is responsible for controlling the actual mailsink server 
     110    class.""" 
    111111 
    112         def __init__(self, host='localhost', port=25): 
    113                 threading.Thread.__init__(self) 
    114                 self.stop_event = threading.Event() 
    115                 self.server = SMTPMailsinkServer((host, port), None) 
     112    def __init__(self, host='localhost', port=25): 
     113        threading.Thread.__init__(self) 
     114        self.stop_event = threading.Event() 
     115        self.server = SMTPMailsinkServer((host, port), None) 
    116116 
    117         def run(self): 
    118                 "Just run in a loop until stop() is called." 
    119                 while not self.stop_event.isSet(): 
    120                         try: 
    121                                 asyncore.loop(timeout=0.1) 
    122                         except select.error, e: 
    123                                 if e.args[0] != errno.EBADF: 
    124                                         raise 
     117    def run(self): 
     118        "Just run in a loop until stop() is called." 
     119        while not self.stop_event.isSet(): 
     120            try: 
     121                asyncore.loop(timeout=0.1) 
     122            except select.error, e: 
     123                if e.args[0] != errno.EBADF: 
     124                    raise 
    125125 
    126         def stop(self, timeout_seconds=5.0): 
    127                 """Stop the mailsink and shut down this thread. timeout_seconds  
    128                 specifies how long the caller should wait for the mailsink server to  
    129                 close down (default: 5 seconds). If the server did not stop in time, a 
    130                 warning message is printed.""" 
    131                 self.stop_event.set() 
    132                 self.server.close() 
    133                 threading.Thread.join(self, timeout=timeout_seconds) 
    134                 if self.isAlive(): 
    135                         print "WARNING: Thread still alive. Timeout while waiting for " + \ 
    136                                   "termination!" 
    137                  
    138         def has_message(self): 
    139                 "Return True if at least one message was received successfully." 
    140                 return self.server.has_message() 
    141                  
    142         def get_messages(self): 
    143                 "Return a copy of the internal queue with all received messages." 
    144                 return self.server.get_messages() 
     126    def stop(self, timeout_seconds=5.0): 
     127        """Stop the mailsink and shut down this thread. timeout_seconds 
     128        specifies how long the caller should wait for the mailsink server to 
     129        close down (default: 5 seconds). If the server did not stop in time, a 
     130        warning message is printed.""" 
     131        self.stop_event.set() 
     132        self.server.close() 
     133        threading.Thread.join(self, timeout=timeout_seconds) 
     134        if self.isAlive(): 
     135            print "WARNING: Thread still alive. Timeout while waiting for " + \ 
     136                      "termination!" 
    145137 
    146         def pop(self, index=None): 
    147                 """Return the index'th message in the queue (default=last) which is 
    148                 removed from the queue afterwards. Throws IndexError if index is bigger  
    149                 than the number of messages in the queue.""" 
    150                 return self.server.pop(index) 
     138    def has_message(self): 
     139        "Return True if at least one message was received successfully." 
     140        return self.server.has_message() 
     141 
     142    def get_messages(self): 
     143        "Return a copy of the internal queue with all received messages." 
     144        return self.server.get_messages() 
     145 
     146    def pop(self, index=None): 
     147        """Return the index'th message in the queue (default=last) which is 
     148        removed from the queue afterwards. Throws IndexError if index is bigger 
     149        than the number of messages in the queue.""" 
     150        return self.server.pop(index)