def setUp(self):
        self.factory = MailListFactory()
        self.m = self.factory.create("Hack Bulgaria")
        self.m.add_subscriber("Rado", "rado@rado")
        self.m.add_subscriber("Ivan", "ivan@ivan")
        self.db_path = "list_tests/"

        self.maillist_adapter = MailListFileAdapter(self.db_path, self.m)
Beispiel #2
0
    def create_list_callback(self, arguments):
        name = " ".join(arguments)

        maillist = self.factory.create(name)
        maillist_adapter = MailListFileAdapter(self.db_path, maillist)
        maillist_adapter.save()

        self.lists[maillist.get_id()] = (maillist, maillist_adapter)
Beispiel #3
0
    def create_list_callback(self, arguments):
        name = " ".join(arguments)

        maillist = self.factory.create(name)
        maillist_adapter = MailListFileAdapter(self.db_path, maillist)
        maillist_adapter.save()

        self.lists[maillist.get_id()] = (maillist, maillist_adapter)
Beispiel #4
0
    def create_list_callback(self, arguments):
        name = " ".join(arguments)

        maillist = self.factory.create(name)
        maillist_adapter = MailListFileAdapter(self.db_path, maillist)
        maillist_adapter.save()

        self.lists.append(maillist.get_name())
        print(self.lists)
Beispiel #5
0
    def _load_initial_state(self):
        dir_lists = map(basename, glob(self.db_path + "*"))

        for list_file in dir_lists:
            adapter = MailListFileAdapter(self.db_path)
            parsed_list = adapter.load(list_file)

            maillist_adapter = MailListFileAdapter(self.db_path, parsed_list)

            self.lists[parsed_list.get_id()] = (parsed_list, maillist_adapter)
Beispiel #6
0
    def _load_initial_state(self):
        dir_lists = map(basename, glob(self.db_path + "*"))

        for list_file in dir_lists:
            adapter = MailListFileAdapter(self.db_path)
            parsed_list = adapter.load(list_file)

            maillist_adapter = MailListFileAdapter(self.db_path, parsed_list)

            self.lists[parsed_list.get_id()] = (parsed_list, maillist_adapter)
def main():
    f = FileMailListAdapter("Otbor_Pochivka.txt")
    mail = f.getMail()
    print (mail.get_subscribers())
    mail.add_subscriber("bla", "bla@bla")
    mail.add_subscriber("lqlq", "lqlq@lqlq")

    print (mail.get_subscribers())

    a = MailListFileAdapter(mail)
    a.save()
    def test_load_from_file(self):
        m = self.factory.create("Hack Bulgaria")
        m.add_subscriber("Ivo", "*****@*****.**")
        m.add_subscriber("Maria", "*****@*****.**")
        file_adapter = MailListFileAdapter(self.db_path, m)
        file_name = file_adapter.get_file_name()

        file_adapter.save()

        loaded_mail_list = file_adapter.load(file_name)

        self.assertEqual(m.get_id(), loaded_mail_list.get_id())
        self.assertEqual(m.get_name(), loaded_mail_list.get_name())
        self.assertEqual(m.get_subscribers(),
                         loaded_mail_list.get_subscribers())
    def setUp(self):
        self.factory = MailListFactory()
        self.m = self.factory.create("Hack Bulgaria")
        self.m.add_subscriber("Rado", "rado@rado")
        self.m.add_subscriber("Ivan", "ivan@ivan")
        self.db_path = "list_tests/"

        self.maillist_adapter = MailListFileAdapter(self.db_path, self.m)
    def test_load_from_empty_list(self):
        m = self.factory.create("Hack Bulgaria")
        file_adapter = MailListFileAdapter(self.db_path, m)
        file_adapter.save()

        load_adapter = MailListFileAdapter(self.db_path)
        loaded_mail_list = load_adapter.load(file_adapter.get_file_name())

        self.assertEqual(0, loaded_mail_list.count())
        self.assertEqual([], loaded_mail_list.get_subscribers())
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())
    def test_load_from_file_without_giving_maillist(self):
        m = self.factory.create("Hack Bulgaria")
        m.add_subscriber("Ivo", "*****@*****.**")
        m.add_subscriber("Maria", "*****@*****.**")
        file_adapter = MailListFileAdapter(self.db_path, m)
        file_adapter.save()

        load_adapter = MailListFileAdapter(self.db_path)
        loaded_mail_list = load_adapter.load(file_adapter.get_file_name())

        self.assertEqual(m.get_id(), loaded_mail_list.get_id())
        self.assertEqual(m.get_name(), loaded_mail_list.get_name())
        self.assertEqual(m.get_subscribers(),
                         loaded_mail_list.get_subscribers())
    def test_load_from_empty_list(self):
        m = self.factory.create("Hack Bulgaria")
        file_adapter = MailListFileAdapter(self.db_path, m)
        file_adapter.save()

        load_adapter = MailListFileAdapter(self.db_path)
        loaded_mail_list = load_adapter.load(file_adapter.get_file_name())

        self.assertEqual(0, loaded_mail_list.count())
        self.assertEqual([], loaded_mail_list.get_subscribers())
class MailListFileAdapterTest(unittest.TestCase):
    """docstring for MailListFileAdapterTest"""
    def setUp(self):
        self.factory = MailListFactory()
        self.m = self.factory.create("Hack Bulgaria")
        self.m.add_subscriber("Rado", "rado@rado")
        self.m.add_subscriber("Ivan", "ivan@ivan")
        self.db_path = "list_tests/"

        self.maillist_adapter = MailListFileAdapter(self.db_path, self.m)

    def test_get_file_name(self):
        self.assertEqual("Hack_Bulgaria",
                         self.maillist_adapter.get_file_name())

    def test_get_file_path(self):
        self.assertEqual(self.db_path + "Hack_Bulgaria",
                         self.maillist_adapter.get_file_path())

    def test_prepare_for_save(self):
        expected = sorted(["Rado - rado@rado", "Ivan - ivan@ivan"])
        self.assertEqual(expected, self.maillist_adapter.prepare_for_save())

    def test_save_id_on_first_line(self):
        file_name = self.maillist_adapter.get_file_name()
        self.maillist_adapter.save()

        file = open(self.db_path + file_name, "r")
        contents = file.read()
        file.close()

        lines = contents.split("\n")
        self.assertEqual("1", lines[0])

    def test_save_contents_format(self):
        file_name = self.maillist_adapter.get_file_name()
        self.maillist_adapter.save()
        file = open(self.db_path + file_name, "r")
        contents = file.read()
        lines = contents.split("\n")
        file.close()

        expected = sorted(["Rado - rado@rado", "Ivan - ivan@ivan"])
        expected = "\n".join(expected)
        lines.pop(0)  # remove the id

        self.assertEqual(expected, "\n".join(lines))

        os.remove(self.maillist_adapter.get_file_path())

    def test_load_from_file(self):
        m = self.factory.create("Hack Bulgaria")
        m.add_subscriber("Ivo", "*****@*****.**")
        m.add_subscriber("Maria", "*****@*****.**")
        file_adapter = MailListFileAdapter(self.db_path, m)
        file_name = file_adapter.get_file_name()

        file_adapter.save()

        loaded_mail_list = file_adapter.load(file_name)

        self.assertEqual(m.get_id(), loaded_mail_list.get_id())
        self.assertEqual(m.get_name(), loaded_mail_list.get_name())
        self.assertEqual(m.get_subscribers(),
                         loaded_mail_list.get_subscribers())

    def test_load_from_file_without_giving_maillist(self):
        m = self.factory.create("Hack Bulgaria")
        m.add_subscriber("Ivo", "*****@*****.**")
        m.add_subscriber("Maria", "*****@*****.**")
        file_adapter = MailListFileAdapter(self.db_path, m)
        file_adapter.save()

        load_adapter = MailListFileAdapter(self.db_path)
        loaded_mail_list = load_adapter.load(file_adapter.get_file_name())

        self.assertEqual(m.get_id(), loaded_mail_list.get_id())
        self.assertEqual(m.get_name(), loaded_mail_list.get_name())
        self.assertEqual(m.get_subscribers(),
                         loaded_mail_list.get_subscribers())

    def test_load_from_empty_list(self):
        m = self.factory.create("Hack Bulgaria")
        file_adapter = MailListFileAdapter(self.db_path, m)
        file_adapter.save()

        load_adapter = MailListFileAdapter(self.db_path)
        loaded_mail_list = load_adapter.load(file_adapter.get_file_name())

        self.assertEqual(0, loaded_mail_list.count())
        self.assertEqual([], loaded_mail_list.get_subscribers())

    def tearDown(self):
        call("rm -rf {}".format(self.db_path), shell=True)
Beispiel #15
0
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)))
class MailListFileAdapterTest(unittest.TestCase):
    """docstring for MailListFileAdapterTest"""
    def setUp(self):
        self.factory = MailListFactory()
        self.m = self.factory.create("Hack Bulgaria")
        self.m.add_subscriber("Rado", "rado@rado")
        self.m.add_subscriber("Ivan", "ivan@ivan")
        self.db_path = "list_tests/"

        self.maillist_adapter = MailListFileAdapter(self.db_path, self.m)

    def test_get_file_name(self):
        self.assertEqual("Hack_Bulgaria",
                         self.maillist_adapter.get_file_name())

    def test_get_file_path(self):
        self.assertEqual(self.db_path + "Hack_Bulgaria",
                         self.maillist_adapter.get_file_path())

    def test_prepare_for_save(self):
        expected = sorted(["Rado - rado@rado", "Ivan - ivan@ivan"])
        self.assertEqual(expected, self.maillist_adapter.prepare_for_save())

    def test_save_id_on_first_line(self):
        file_name = self.maillist_adapter.get_file_name()
        self.maillist_adapter.save()

        file = open(self.db_path + file_name, "r")
        contents = file.read()
        file.close()

        lines = contents.split("\n")
        self.assertEqual("1", lines[0])

    def test_save_contents_format(self):
        file_name = self.maillist_adapter.get_file_name()
        self.maillist_adapter.save()
        file = open(self.db_path + file_name, "r")
        contents = file.read()
        lines = contents.split("\n")
        file.close()

        expected = sorted(["Rado - rado@rado", "Ivan - ivan@ivan"])
        expected = "\n".join(expected)
        lines.pop(0)  # remove the id

        self.assertEqual(expected, "\n".join(lines))

        os.remove(self.maillist_adapter.get_file_path())

    def test_load_from_file(self):
        m = self.factory.create("Hack Bulgaria")
        m.add_subscriber("Ivo", "*****@*****.**")
        m.add_subscriber("Maria", "*****@*****.**")
        file_adapter = MailListFileAdapter(self.db_path, m)
        file_name = file_adapter.get_file_name()

        file_adapter.save()

        loaded_mail_list = file_adapter.load(file_name)

        self.assertEqual(m.get_id(), loaded_mail_list.get_id())
        self.assertEqual(m.get_name(), loaded_mail_list.get_name())
        self.assertEqual(m.get_subscribers(),
                         loaded_mail_list.get_subscribers())

    def test_load_from_file_without_giving_maillist(self):
        m = self.factory.create("Hack Bulgaria")
        m.add_subscriber("Ivo", "*****@*****.**")
        m.add_subscriber("Maria", "*****@*****.**")
        file_adapter = MailListFileAdapter(self.db_path, m)
        file_adapter.save()

        load_adapter = MailListFileAdapter(self.db_path)
        loaded_mail_list = load_adapter.load(file_adapter.get_file_name())

        self.assertEqual(m.get_id(), loaded_mail_list.get_id())
        self.assertEqual(m.get_name(), loaded_mail_list.get_name())
        self.assertEqual(m.get_subscribers(),
                         loaded_mail_list.get_subscribers())

    def test_load_from_empty_list(self):
        #pass
        m = self.factory.create("Hack Bulgaria")
        file_adapter = MailListFileAdapter(self.db_path, m)
        file_adapter.save()

        load_adapter = MailListFileAdapter(self.db_path)
        self.assertEqual(1, 1)
        loaded_mail_list = load_adapter.load(file_adapter.get_file_name())

        self.assertEqual(0, loaded_mail_list.count())
        self.assertEqual([], loaded_mail_list.get_subscribers())

    def tearDown(self):
        call("rm -rf {}".format(self.db_path), shell=True)
    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)