コード例 #1
0
def main():
    _list = MailList("Otbor Pochivka.txt")
    _list.add_subscriber("Tsveta", "tsveta@tsveta")
    _list.add_subscriber("Pepa", "pepa@pepa")

    m = MailListFileAdapter(_list)

    print (m.get_file_name())
    m.save()
コード例 #2
0
class MailListFileAdapterTest(unittest.TestCase):
    """docstring for MailListFileAdapterTest"""

    def setUp(self):
        self.obj = MailList("Otbor Pochivka")
        self.obj.add_subscriber("Tsveta", "tsveta@tsveta")
        self.obj.add_subscriber("Pepa", "pepa@pepa")

        self.m = MailListFileAdapter(self.obj)

    def test_get_file_name(self):
        _str = self.m.get_file_name()
        self.assertEqual("Otbor_Pochivka", _str)

    def test_prepare_for_save(self):
        expected = sorted(["Tsveta - tsveta@tsveta", "Pepa - pepa@pepa"])

        self.assertEqual(expected, self.m.prepare_for_save())
コード例 #3
0
ファイル: test_mailList.py プロジェクト: pepi09/MailList
class TestMailList(unittest.TestCase):
    def setUp(self):
        self.obj = MailList("someName")
        self.obj.add_subscriber("Pepa", "pepa@pepa")

    def test_atributes(self):
        self.assertEqual("someName", self.obj.getName())

    def test_add_subscriber(self):
        self.obj.add_subscriber("Tsveta", "tsveta@tsveta")
        expected = [("Pepa", "pepa@pepa"), ("Tsveta", "tsveta@tsveta")]
        self.assertEqual(expected, self.obj.get_subscribers())

    def test_get_subscribers(self):
        self.obj.add_subscriber("Tsveta", "tsveta@tsveta")
        expected = [("Pepa", "pepa@pepa"), ("Tsveta", "tsveta@tsveta")]
        self.assertEqual(expected, self.obj.get_subscribers())
コード例 #4
0
class FileMailListAdapter():
    """docstring for FileMailListAdapter"""
    def __init__(self, filename):
        self.__mail = MailList(filename)

    def make_subscribers(self):
        file = open(self.__mail.getName(), "r")
        contents = file.read()
        file.close()
        if not len(contents) == 0:
            contents = list(map(lambda x: x.split(), contents.split('\n')))
        # contents = list(map(lambda x: (x[0], x[2]), contents))
        # self.__mail.subscribers = contents
        # return contents
            dict_ = {}
            for item in contents:
                dict_[item[0]] = item[2]
            return dict_
        return {}

    def getMail(self):
        self.__mail.subscribers = self.make_subscribers()
        return self.__mail
コード例 #5
0
 def __init__(self, filename):
     self.__mail = MailList(filename)
コード例 #6
0
ファイル: mail.py プロジェクト: pepi09/MailList
def main():
    print("Hello Stranger! This is a cutting-edge, console-based mail-list! Type help, to see a list of commands.")
    while True:
        command = input(">")
        command = command.split()

        if command[0] == "help":
            call("python help.py", shell=True)

        if command[0] == "show_lists":
            if len(command) == 1:
                lists = get_lists()
                if not len(lists) == 0:
                    for i in range(len(lists)):
                        lists[i] = lists[i][0:len(lists[i]) - 4]
                        print("[" + str(i + 1) + "]" + " - " + lists[i])
                else:
                    print ("There are no email lists!")

        if command[0] == "show_list":
            lists = get_lists()
            i = int(command[1])
            try:
                l = open("%s" % (lists[i - 1]), "r")
            except IndexError:
                print("List with unique identifier %s was not found!" %(i))
            else:
                list = l.read()
                list = list.split('\n')
                if not list == ['']:
                    for i in range(len(list)):
                        print("[" + str(i + 1) + "]" + " " + list[i])
                else:
                    print ("The mail list is empty!")

        if command[0] == "add":
            lists = get_lists()
            i = int(command[1]) - 1

            name = input("Name: ")
            email = input("Email ")

            f = FileMailListAdapter(lists[i])
            mail = f.getMail()
            emails = mail.get_emails()
            if emails.count(email) == 0:
                mail.add_subscriber(name, email)
                adapter = MailListFileAdapter(mail)
                adapter.save()
            else:
                print("A person with the given email <%s> is already in the list!" %(email))

        if command[0] == "update_subscriber":
            lists = get_lists()
            i = int(command[1])
            j = int(command[2])
            if i <= len(lists):
                f = FileMailListAdapter(lists[i - 1])
                mail = f.getMail()
                subscribers = mail.get_subscribers()
                if j <= len(subscribers):
                    print("Updating: %s - %s \n Pres enter if you want the field to remain the same" %(subscribers[j - 1][0],subscribers[j - 1][1]))
                    old_name = subscribers[j - 1][0]
                    old_email = subscribers[j - 1][1]

                    name = input("enter new name> ")
                    email = input("enter new email> ")

                    if name != "" and email != "":
                        mail.update_subscriber(old_name, name, email)
                    elif name == "" and email != "":
                        mail.update_subscriber(old_name, old_name, email)
                    elif name != "" and email == "":
                        mail.update_subscriber(old_name, name, old_email)
                    else:
                        mail.update_subscriber(old_name,old_name,old_email)
                    adapter = MailListFileAdapter(mail)
                    adapter.save()
                else:
                    print ("Subscriber with identifider <" + str(j) + "> not found in the list." )
            else:
                print("List with unique identifier <" + str(i) + "> was not found.")

        if command[0] == "remove_subscriber":
            lists = get_lists()
            i = int(command[1])
            j = int(command[2])

            if i <= len(lists):
                f = FileMailListAdapter(lists[i - 1])
                mail = f.getMail()
                subscribers = mail.get_subscribers()
                if j <= len(subscribers):
                    mail.remove_subscriber(subscribers[j - 1][0])

                    adapter = MailListFileAdapter(mail)
                    adapter.save()
                else:
                    print ("Subscriber with identifider <" + str(j) + "> not found in the list.")
            else:
                print("List with unique identifier <"+ str(i) + "> was not found")

        if command[0] == "exit":
            break

        if command[0] == "create":
            lists = get_lists()
            if command[1] + ".txt" in lists:
                print ("A list with name <" + command[1] + "> already exists!")
            else:
                mail = MailList(command[1] + ".txt")
                f = MailListFileAdapter(mail)
                f.save()
                print ("New list <" + command[1] + "> was created")

        if command[0] == "update":
            lists = get_lists()
            i = int(command[1])
            if i > len(lists):
                print("List with unique identifier <" + str(i) + "> \
was not found")
            else:
                f = FileMailListAdapter(lists[i - 1])
                mail = f.getMail()
                mail.set_name(command[2])

                adapter = MailListFileAdapter(mail)
                adapter.save()
                print("Updated " + lists[i - 1] + " to " + command[2])

        if command[0] == "delete":
            lists = get_lists()
            i = int(command[1]) - 1
            if i <= len(lists):
                print ("Are you sure you want to delete " + lists[i])
                choice = input("(Y/N)>")
                if choice == 'Y':
                    if i <= len(lists):
                        call("rm " + lists[i], shell=True)
                else:
                    print("You crazy bastard. Stop playing with fire!")
            else:
                print("List with unique identifier <" + str(i) + "> \
was not found.")

        if command[0] == "search_email":
            lists = get_lists()
            result = []
            for i in range(len(lists)):
                f = FileMailListAdapter(lists[i])
                mail = f.getMail()
                subscribers = mail.get_subscribers()
                for j in range(len(subscribers)):
                    if command[1] == subscribers[j][1]:
                        result.append(lists[i][:len(lists[i]) - 4:])
            if len(result) == 0:
                print("<" + command[1] + "> was not found in the \
current mailing lists.")
            else:
                for k in range(len(result)):
                    print("[" + str(k + 1) + "] " + result[k])


        if command[0] == "merge_lists":
            lists = get_lists()
            i = int(command[1]) - 1
            j = int(command[2]) - 1
            new_list = command[3]
            try:
                list1 = lists[i]
                list2 = lists[j]
            except IndexError:
                print("Can`t find lists with the given number")
            else:
                print("Merged lists <%s> and <%s> into <%s>" %(lists[i][:len(lists[i]) - 4:],lists[j][:len(lists[j]) - 4:],new_list))
                new_mail = MailList(new_list + ".txt")
                f = MailListFileAdapter(new_mail)
                f.save()
                
                m1 = FileMailListAdapter(lists[i])
                mail1 = m1.getMail()
                subs1 = mail1.get_subscribers()
                emails1 = mail1.get_emails()

                for name in subs1:
                    new_mail.add_subscriber(name[0],name[1])

                m2 = FileMailListAdapter(lists[j])
                mail2 = m2.getMail()
                subs2 = mail2.get_subscribers()

                for name in subs2:
                    if emails1.count(name[1]) == 0:
                        new_mail.add_subscriber(name[0],name[1])

                adapter = MailListFileAdapter(new_mail)
                adapter.save()

        if command[0] == "export":
            lists = get_lists()
            i = int(command[1]) - 1
            try:
                f = FileMailListAdapter(lists[i])
            except IndexError:
                print("List with unique identifier <" + str(i) + "> was not found.")
            else:
                print("Exported <%s> to <%s.json>" %(lists[i][:len(lists[i]) - 4:],lists[i][:len(lists[i]) - 4:]))
                mail = f.getMail()
                subscribers = mail.return_subscribers()

                json_file = json.dumps(subscribers,sort_keys=True,indent=4,ensure_ascii=False)
                print(json_file)
                json_list = open("%s.json" %(lists[i][:len(lists[i]) - 4:]),"w")
                json_list.write("%s" %(json.loads(json_file)))
コード例 #7
0
ファイル: test_mailList.py プロジェクト: pepi09/MailList
 def setUp(self):
     self.obj = MailList("someName")
     self.obj.add_subscriber("Pepa", "pepa@pepa")
コード例 #8
0
ファイル: maillist_test.py プロジェクト: h3lgi/MailList
 def setUp(self):
     self.maillist = MailList(1, 'new list')
コード例 #9
0
ファイル: maillist_test.py プロジェクト: h3lgi/MailList
class TestMaillist(unittest.TestCase):

    def setUp(self):
        self.maillist = MailList(1, 'new list')

    def test_get_name(self):
        self.assertEqual('new list', self.maillist.get_name())

    def test_add_user(self):
        self.maillist.add_user("helgi", "*****@*****.**")
        self.assertEqual(1, len(self.maillist.users))
        self.assertEqual("helgi", self.maillist.users[0].name)
        self.assertEqual("*****@*****.**", self.maillist.users[0].email)

    def test_print_user(self):
        self.maillist.add_user("helgi", "*****@*****.**")
        expected = "[1] helgi - [email protected]"
        self.assertEqual(expected, self.maillist.print_user())

    def test_search_email(self):
        self.maillist.add_user("helgi", "*****@*****.**")
        self.assertFalse(self.maillist.search_email("*****@*****.**"))
        self.assertTrue(self.maillist.search_email("*****@*****.**"))

    def test_search_name(self):
        self.maillist.add_user("helgi", "*****@*****.**")
        self.assertFalse(self.maillist.search_name("juja"))
        self.assertTrue(self.maillist.search_name("helgi"))
コード例 #10
0
    def setUp(self):
        self.obj = MailList("Otbor Pochivka")
        self.obj.add_subscriber("Tsveta", "tsveta@tsveta")
        self.obj.add_subscriber("Pepa", "pepa@pepa")

        self.m = MailListFileAdapter(self.obj)