Changeset 5:573fdae8b1f6 in mailjam
- Timestamp:
- May 16, 2012, 11:41:47 AM (13 years ago)
- Branch:
- default
- Phase:
- public
- Location:
- postman
- Files:
-
- 1 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
postman/daemon.py
r2 r5 42 42 for m in self.mailings.keys(): 43 43 self.mailings[m].save() 44 return True 45 return False 44 46 45 47 def load(self): … … 59 61 mailing.load() 60 62 self.mailings[address] = mailing 61 63 return True 64 return False 62 65 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={}): 64 81 """ 65 82 Add a new mailing list to this postman instance. expects one parameter, 66 list_info, which is a dictionary that should contain, at least, the83 info, which is a dictionary that should contain, at least, the 67 84 following keys: 68 85 … … 72 89 73 90 """ 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') 76 93 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') 81 99 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'], 84 102 ' has been already added to postman') 85 103 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']) 88 106 self.mailings[mailing.address] = mailing 89 107 self.mailings_addresses.append(mailing.address) 90 108 # After adding new mailings, save them to disk 91 109 self.save() 110 return True 92 111 93 112 def add_mailing_member(self, member_addr=None, list_addr=None): … … 109 128 raise IndexError(list_addr, ' is not a valid mailing list') 110 129 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 113 134 114 135 -
postman/models.py
r4 r5 33 33 """ 34 34 35 def __init__(self, name, address, members={}, config={} , storage=None):35 def __init__(self, name, address, members={}, config={}): 36 36 self.name = name 37 37 self.address = address … … 39 39 self.config = config 40 40 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))45 41 46 42 def __repr__(self): … … 72 68 raise ValueError(address, ' is not a valid email address') 73 69 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 75 79 def members_addresses(self): 76 80 return self.members.keys() … … 97 101 return True 98 102 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 99 113 def load(self): 100 114 if self.storage.exists(): -
postman/storage.py
r4 r5 1 1 # -*- coding: utf-8 -*- 2 2 3 import os, json 3 import os, errno, json 4 4 5 5 6 class JsonStorage(): … … 17 18 return os.path.exists(self.path) 18 19 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 19 33 def jsonize(self, obj): 20 34 """ … … 45 59 46 60 def write(self, data): 61 if not self.exists(): 62 # ensure the path to the storage file exists 63 self.create() 47 64 with open(self.path, 'w') as storage: 48 65 json.dump(data, storage, sort_keys=True, indent=4, … … 64 81 return data 65 82 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 3 3 from models import * 4 4 from mta import * 5 5 from daemon import * -
postman/tests/mta.py
r2 r5 3 3 import os, sys 4 4 from unittest import TestCase 5 5 from postman.mta import Sendmail 6 6 from postman.models import Member, MailingList 7 from postman.mta import Sendmail8 7 9 8
Note:
See TracChangeset
for help on using the changeset viewer.