source: mailjam/postman/daemon.py@ 4:14b2a12d495e

Last change on this file since 4:14b2a12d495e was 2:108a82defd3e, checked in by Francisco de Borja Lopez Rio <borja@…>, 12 years ago

Fixed some bugs in the models and mta modules

Added tests for the mta module, including a dummy email that is used for
those tests.

Added an initial version of the Postman class (inside the daemon module). This
class will handle the main execution of the postman daemon, the manager
that will take care of mailing lists management.

Added the structure of folders needed to store json files associated with a given
postman instance.

File size: 4.4 KB
Line 
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()
44
45 def load(self):
46 """
47 Load all data from the storage files
48 """
49 if self.dbs['mailings'].exists():
50 # load the list of managed mailing lists
51 # FIXME: This is quite naive, we do not perform any check here after
52 # loading the data from the json file, which can be modified by
53 # untrustred users.
54 self.mailings_addresses = self.dbs['mailings'].read()
55
56 # now load all the mailing objects:
57 for address in self.mailings_addresses:
58 mailing = MailingList(address, address)
59 mailing.load()
60 self.mailings[address] = mailing
61
62
63 def add_mailing_list(self, list_info={}):
64 """
65 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
67 following keys:
68
69 - name: (string) the name we will give to the list
70 - address: (string) the email address of the list
71 - members: (list) a list of email adddress of the list members
72
73 """
74 if not isinstance(list_info, dict):
75 raise TypeError(list_info, ' is not a valid dictionary')
76
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')
81
82 if list_info['address'] in self.mailings_addresses:
83 raise IndexError(list_info['address'],
84 ' has been already added to postman')
85
86 mailing = MailingList(list_info['name'], list_info['address'],
87 list_info['members'])
88 self.mailings[mailing.address] = mailing
89 self.mailings_addresses.append(mailing.address)
90 # After adding new mailings, save them to disk
91 self.save()
92
93 def add_mailing_member(self, member_addr=None, list_addr=None):
94 """
95 Add a new member for the mailing list represented by list_addr (a string
96 containing the email address of any mailing list managed by this postman
97 instance). member_addr is a string representing the email address of the
98 new member
99 """
100
101 if not member_addr:
102 raise ValueError(member_addr, 'missing member address')
103
104 if not list_addr:
105 raise ValueError(list_addr, 'missing list address')
106
107 if list_addr not in self.mailings_addresses:
108 # FIXME: Perhaps we should add it, perhaps not (mispelled address?)
109 raise IndexError(list_addr, ' is not a valid mailing list')
110
111 member = Member(member_addr)
112
113
114
115
116
117
118
119
Note: See TracBrowser for help on using the repository browser.