source: mailjam/postman/daemon.py@ 5:573fdae8b1f6

Last change on this file since 5:573fdae8b1f6 was 5:573fdae8b1f6, checked in by Francisco de Borja Lopez Rio <borja@…>, 13 years ago

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

File size: 5.2 KB
RevLine 
[2]1# -*- coding: utf-8 -*-
2
3import os
4
5from postman import config
6from postman.models import Member, MailingList
7from postman.storage import JsonStorage as Storage
8
9
10class Postman():
11
12 def __init__(self, configfile=None):
13 if not configfile:
14 # FIXME: This is not used right now, we are getting the stuff
15 # from postman.config, but we will move configurations to a
16 # external ini-style config file soon
17 configfile = os.path.join(os.path.dirname(__file__),
18 '../conf/postman.conf')
19 self.configfile = configfile
20
21 # lists were the currently managed mailing lists information is going
22 # to be saved
23 self.mailings = {}
24 self.mailings_addresses = []
25
26 # the files were internal information (like active mailing lists,
27 # members, etc) is saved
28 self.dbs = {'mailings': Storage(os.path.join(config.storage_path,
29 'mailings.json')),
30 'members': Storage(os.path.join(config.storage_path,
31 'members.json'))}
32
33 def save(self):
34 """
35 Save all the current managed data to disk
36 """
37 if self.mailings:
38 # Save the config file from where we can reload information about the
39 # mailing lists managed by this postman instance
40 self.dbs['mailings'].write(self.mailings_addresses)
41 # Save each mailing list data into its separated persistence file
42 for m in self.mailings.keys():
43 self.mailings[m].save()
[5]44 return True
45 return False
[2]46
47 def load(self):
48 """
49 Load all data from the storage files
50 """
51 if self.dbs['mailings'].exists():
52 # load the list of managed mailing lists
53 # FIXME: This is quite naive, we do not perform any check here after
54 # loading the data from the json file, which can be modified by
55 # untrustred users.
56 self.mailings_addresses = self.dbs['mailings'].read()
57
58 # now load all the mailing objects:
59 for address in self.mailings_addresses:
60 mailing = MailingList(address, address)
61 mailing.load()
62 self.mailings[address] = mailing
[5]63 return True
64 return False
[2]65
[5]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={}):
[2]81 """
82 Add a new mailing list to this postman instance. expects one parameter,
[5]83 info, which is a dictionary that should contain, at least, the
[2]84 following keys:
85
86 - name: (string) the name we will give to the list
87 - address: (string) the email address of the list
88 - members: (list) a list of email adddress of the list members
89
90 """
[5]91 if not isinstance(info, dict):
92 raise TypeError(info, ' is not a valid dictionary')
[2]93
[5]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')
[2]99
[5]100 if info['address'] in self.mailings_addresses:
101 raise IndexError(info['address'],
[2]102 ' has been already added to postman')
103
[5]104 mailing = MailingList(info['name'], info['address'],
105 info['members'], info['config'])
[2]106 self.mailings[mailing.address] = mailing
107 self.mailings_addresses.append(mailing.address)
108 # After adding new mailings, save them to disk
109 self.save()
[5]110 return True
[2]111
112 def add_mailing_member(self, member_addr=None, list_addr=None):
113 """
114 Add a new member for the mailing list represented by list_addr (a string
115 containing the email address of any mailing list managed by this postman
116 instance). member_addr is a string representing the email address of the
117 new member
118 """
119
120 if not member_addr:
121 raise ValueError(member_addr, 'missing member address')
122
123 if not list_addr:
124 raise ValueError(list_addr, 'missing list address')
125
126 if list_addr not in self.mailings_addresses:
127 # FIXME: Perhaps we should add it, perhaps not (mispelled address?)
128 raise IndexError(list_addr, ' is not a valid mailing list')
129
[5]130 added = self.mailings[list_addr].add_member_by_address(member_addr)
131 if added:
132 self.save()
133 return added
[2]134
135
136
137
138
139
140
Note: See TracBrowser for help on using the repository browser.