-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmaillist_file_adapter.py
58 lines (42 loc) · 1.68 KB
/
maillist_file_adapter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from maillist import MailList
import os
class MailListFileAdapter():
"""docstring for MailListFileAdapter"""
def __init__(self, db_path, mail_list=None):
self.db_path = db_path
self.mail_list = mail_list
self._ensure_db_path()
def get_file_name(self):
return self.mail_list.get_name().replace(" ", "_")
def get_file_path(self):
return self.db_path + self.get_file_name()
# (name, email) -> "<name> - <email>"
def prepare_for_save(self):
subscribers = self.mail_list.get_subscribers()
subscribers = map(lambda t: "{} - {}".format(t[0], t[1]), subscribers)
return sorted(subscribers)
def save(self):
file_to_save = open(self.get_file_path(), "w")
contents = "{}\n".format(self.mail_list.get_id())
contents += "\n".join(self.prepare_for_save())
file_to_save.write(contents)
file_to_save.close()
def load(self, file_name):
maillist_name = file_name.replace("_", " ")
# create a Dummy mail list, so we can call the methods
if self.mail_list is None:
self.mail_list = MailList(-1, maillist_name)
file = open(self.get_file_path(), "r")
contents = file.read()
file.close()
lines = contents.split("\n")
maillist_id = int(lines[0])
lines.pop(0)
result = MailList(maillist_id, maillist_name)
for unparsed_subscriber in lines:
subscriber = unparsed_subscriber.split(" - ")
result.add_subscriber(subscriber[0], subscriber[1])
return result
def _ensure_db_path(self):
if not os.path.exists(self.db_path):
os.makedirs(self.db_path)