Changeset 5:573fdae8b1f6 in mailjam


Ignore:
Timestamp:
May 16, 2012, 11:41:47 AM (12 years ago)
Author:
Francisco de Borja Lopez Rio <borja@…>
Branch:
default
Phase:
public
Message:

Added tests for postman.daemon.Postman

Added a method to postman.daemon.Postman to "clear" all the storage associated
to a given postman instance (removing all the files from disk)

Fixed some bugs, mostly caused by typos through the sources, detected while
writing more tests

Removed the storage and archive attributes from postman.models.MailingList,
now they are methods "marked" as properties using the @property decorator.
This keeps the storage objects from being serialized into json objects by
the storage backend (de-serializing them caused some trouble and it is not
necessary at all)

Added create() and delete() methods to postman.storage.JsonStorage. the first
one ensures the path to the storage file exists (creating all subdirs if
necessary) and the second one deletes the storage file from disk

Location:
postman
Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • postman/daemon.py

    r2 r5  
    4242            for m in self.mailings.keys():           
    4343                self.mailings[m].save()
     44            return True
     45        return False
    4446
    4547    def load(self):
     
    5961                mailing.load()
    6062                self.mailings[address] = mailing               
    61            
     63            return True
     64        return False
    6265
    63     def add_mailing_list(self, list_info={}):
     66    def clear(self):
     67        """
     68        Delete all stored data from disk (useful for testing).
     69        DANGER: Calling this method will remove all data from disk, leaving the
     70        postman instance with no persistence data, if the postman process die,
     71        before another .save() call is made, all data will be lost.
     72        """
     73        if self.dbs['mailings'].exists():
     74            # We do not delete each mailing list file, but only the file
     75            # containing the list of existing mailing lists
     76            self.dbs['mailings'].delete()
     77            return True
     78        return False
     79
     80    def add_mailing_list(self, info={}):
    6481        """
    6582        Add a new mailing list to this postman instance. expects one parameter,
    66         list_info, which is a dictionary that should contain, at least, the
     83        info, which is a dictionary that should contain, at least, the
    6784        following keys:
    6885
     
    7289         
    7390        """
    74         if not isinstance(list_info, dict):
    75             raise TypeError(list_info, ' is not a valid dictionary')
     91        if not isinstance(info, dict):
     92            raise TypeError(info, ' is not a valid dictionary')
    7693
    77         if 'name' not in list_info.keys() or \
    78            'address' not in list_info.keys() or \
    79            'members' not in list_info.keys():
    80             raise ValueError(list_info, ' does not seem to be a valid configuration')
     94        if 'name' not in info.keys() or \
     95           'address' not in info.keys() or \
     96           'members' not in info.keys() or \
     97           'config' not in info.keys():
     98            raise ValueError(info, ' does not seem to be a valid configuration')
    8199       
    82         if list_info['address'] in self.mailings_addresses:
    83             raise IndexError(list_info['address'],
     100        if info['address'] in self.mailings_addresses:
     101            raise IndexError(info['address'],
    84102                             ' has been already added to postman')
    85103
    86         mailing = MailingList(list_info['name'], list_info['address'],
    87                               list_info['members'])       
     104        mailing = MailingList(info['name'], info['address'],
     105                              info['members'], info['config'])       
    88106        self.mailings[mailing.address] = mailing
    89107        self.mailings_addresses.append(mailing.address)
    90108        # After adding new mailings, save them to disk
    91109        self.save()
     110        return True
    92111       
    93112    def add_mailing_member(self, member_addr=None, list_addr=None):
     
    109128            raise IndexError(list_addr, ' is not a valid mailing list')
    110129
    111         member = Member(member_addr)
    112        
     130        added = self.mailings[list_addr].add_member_by_address(member_addr)
     131        if added:
     132            self.save()
     133        return added
    113134       
    114135
  • postman/models.py

    r4 r5  
    3333    """
    3434
    35     def __init__(self, name, address, members={}, config={}, storage=None):
     35    def __init__(self, name, address, members={}, config={}):
    3636        self.name = name
    3737        self.address = address
     
    3939        self.config = config
    4040        self._validate_config() # validate the config parameters
    41         self.storage = Storage(os.path.join(self.config['storage'],
    42                                             self.address))
    43         self.archive = Storage(os.path.join(self.config['archive'],
    44                                             self.address))       
    4541       
    4642    def __repr__(self):
     
    7268            raise ValueError(address, ' is not a valid email address')
    7369        return address in self.members_addresses()
    74        
     70
     71    @property
     72    def storage(self):
     73        return Storage(os.path.join(self.config['storage'], self.address))
     74
     75    @property
     76    def archive(self):
     77        return Storage(os.path.join(self.config['archive'], self.address))
     78   
    7579    def members_addresses(self):
    7680        return self.members.keys()
     
    97101        return True
    98102
     103    def info(self):
     104        """
     105        Returns a dict we can use to add this mailing list to a postman
     106        instance
     107        """
     108        # FIXME: This code could be replaced with something that
     109        # automagically generates the dict from the instance attributes
     110        return {'name': self.name, 'address': self.address,
     111                'members': self.members, 'config': self.config}
     112
    99113    def load(self):
    100114        if self.storage.exists():
  • postman/storage.py

    r4 r5  
    11# -*- coding: utf-8 -*-
    22
    3 import os, json
     3import os, errno, json
     4
    45
    56class JsonStorage():
     
    1718        return os.path.exists(self.path)   
    1819
     20    def create(self):
     21        """
     22        Ensure all the subdirectories in the path to the storage file
     23        exist
     24        """
     25        try:
     26            os.makedirs(os.path.dirname(self.path))
     27        except OSError, e:
     28            # If the dir already exists do not complain, if it is
     29            # any other error, raise the exception
     30            if e.errno != errno.EEXIST:
     31                raise
     32   
    1933    def jsonize(self, obj):
    2034        """
     
    4559       
    4660    def write(self, data):
     61        if not self.exists():
     62            # ensure the path to the storage file exists
     63            self.create()
    4764        with open(self.path, 'w') as storage:
    4865            json.dump(data, storage, sort_keys=True, indent=4,
     
    6481        return data
    6582
     83    def delete(self):
     84        """
     85        Remove the storage file
     86        """
     87        if self.exists():
     88            os.remove(self.path)
     89            return True
     90        return False
  • postman/tests/__init__.py

    r2 r5  
    33from models import *
    44from mta import *
    5 
     5from daemon import *
  • postman/tests/mta.py

    r2 r5  
    33import os, sys
    44from unittest import TestCase
    5 
     5from postman.mta import Sendmail
    66from postman.models import Member, MailingList
    7 from postman.mta import Sendmail
    87
    98
Note: See TracChangeset for help on using the changeset viewer.